ruby 在现有 Jekyll 安装中切换主题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31327045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch theme in an existing Jekyll installation
提问by majom
There are many themes for Jekyll, e.g. https://github.com/jekyll/jekyll/wiki/Themes.
Jekyll 有很多主题,例如https://github.com/jekyll/jekyll/wiki/Themes。
What is the easiest way to switch to a new theme in an EXISTING Jekyll installation?
在现有 Jekyll 安装中切换到新主题的最简单方法是什么?
采纳答案by matrixanomaly
While you couldmigrate to an existing installation by forking a new theme and then manually copy and pasting over resources like CSS, JS, HTML in the _includes, _layoutsand other files you may need, this probably isn't a great idea as you end up having a mash up of old and new resources, which may not be of the same name, but in the scenario that they are (for example you didn't overwrite an old stylesheet that your post references), it will cause mixed up CSS styles that you'll have to debug and slowly fix.
虽然你可以通过派生新的主题,然后手动复制和粘贴在资源,如CSS,JS,HTML在迁移到现有的安装_includes,_layouts您可能需要和其他文件,这可能不是因为你最终有一个伟大的想法旧资源和新资源的混搭,它们可能名称不同,但在它们相同的情况下(例如,您没有覆盖您的帖子引用的旧样式表),它会导致 CSS 样式混淆你必须调试并慢慢修复。
Since I'm assuming you have a Jekyll install with Git(if you don't you really should), you could create a branch called new-themeand switch to that branch from the masteras the working branch. (A simpleton way of having something like this is to just copy your entire Jekyll install and paste it elsewhere as old-Jekyll-installif you don't want to deal with Git branches (but really, you should. Here's a tutorialthat helped me learn)
由于我假设您使用 Git 安装了 Jekyll(如果您不这样做的话),您可以创建一个名为的分支new-theme并从master作为工作分支的分支切换到该分支。(一个简单的方法是复制你的整个 Jekyll 安装并将其粘贴到其他地方,就old-Jekyll-install好像你不想处理 Git 分支一样(但实际上,你应该这样做。这是一个帮助我学习的教程)
- Pull down the files for the new theme.
- Manually copy over
_postsand your customized changes. - Port over your
_config.ymlby manually comparing them and moving over what is necessary. - Build the site and see what you're missing, what might be messed up (for example in the past you might have added a few
<br \>tags for spacing and you don't want that in the new theme). - Merge with
master(or push it to production)
- 下拉新主题的文件。
- 手动复制
_posts和您的自定义更改。 - 港口在你
_config.yml通过手动比较它们和移动对什么是必要的。 - 构建站点并查看您缺少什么,可能会弄乱什么(例如,过去您可能添加了一些
<br \>间距标签,而您不希望在新主题中使用这些标签)。 - 合并
master(或将其推送到生产)
That being said all this is fairly manual and a pain, but at least you won't have to deal with conflicts in resources. The downsideof doing this though is that your repository won't be synced with the theme repo. So you won't get upstream updates. I would still suggest that you forkthe theme repo, port over your personal customizations for your Jekyll site, and then rename that repo for production. (this would of course no longer be using the 'existing' Jekyll installation)
话虽如此,所有这些都相当手动且很痛苦,但至少您不必处理资源冲突。这样做的缺点是您的存储库不会与主题存储库同步。所以你不会得到上游更新。我仍然建议你fork主题 repo,移植你的 Jekyll 站点的个人定制,然后重命名该 repo 以进行生产。(当然,这将不再使用“现有”的 Jekyll 安装)
回答by Daniel A.A. Pelsmaeker
This is what I did to change the theme of an existing Jekyll installation. Adjust these instructions to suit your needs.
这就是我为更改现有 Jekyll 安装的主题所做的工作。调整这些说明以满足您的需要。
Pull the new theme
拉取新主题
We create a new orphan branch newthemeand ensure it's empty.
我们创建一个新的孤立分支newtheme并确保它是空的。
git checkout --orphan newtheme
git rm -rf .
git clean -dfx
Then we pull the theme files into it by adding the theme as an upstream remote. In this example I pull John Otander's Pixyll theme's masterbranch.
然后我们通过将主题添加为上游远程来将主题文件拉入其中。在这个例子中,我拉取了 John Otander 的 Pixyll 主题的master分支。
git remote add upstream https://github.com/johnotander/pixyll.git
git fetch upstream
git pull upstream master
Build the theme and test it.
构建主题并进行测试。
bundler install
jekyll serve
Merge your changes
合并您的更改
Now we merge our posts, configuration, etc. You can use Git checkoutto copy a file or folder from your old Jekyll site. Note that this will overwrite the theme's file if it exists.
现在我们合并我们的帖子、配置等。您可以使用 Gitcheckout从旧的 Jekyll 站点复制文件或文件夹。请注意,这将覆盖主题文件(如果存在)。
git checkout master -- _posts
Alternatively, you can copy a file under a new name, for example to merge it manually.
或者,您可以以新名称复制文件,例如手动合并。
git show master:_config.yml > _config.yml.old
If you accidently overwrote a theme file, you can restore it.
如果您不小心覆盖了主题文件,您可以恢复它。
git checkout upstream/master -- about.md
These are the files I had to copy, merge, adjust or remove:
这些是我必须复制、合并、调整或删除的文件:
- Markdown files in the root folder.
- Posts in the
_postsfolder. - Drafts in the
_draftsfolder. - The
_config.ymlconfiguration file. - The
Gemfilegem file. - The
CNAMEfile (for GitHub pages). - The
Rakefile(if any). - The favicon files (if any).
- Manual theme changes such as Google Analytics, Disqus, SEO fields (if any).
- 根文件夹中的 Markdown 文件。
_posts文件夹中的帖子。_drafts文件夹中的草稿。- 该
_config.yml配置文件。 - 在
Gemfile宝石文件。 - 该
CNAME文件(GitHub的页面)。 - 的
Rakefile(如果有的话)。 - 图标文件(如果有)。
- 手动主题更改,例如 Google Analytics、Disqus、SEO 字段(如果有)。
Commit your changes, and don't forget to test the theme again.
提交您的更改,不要忘记再次测试主题。
Replace the master branch
替换主分支
Finally we replace our existing masterbranch with the new newthemebranch. Assuming we're on the newthemebranch:
最后,我们用master新newtheme分支替换我们现有的分支。假设我们在newtheme分支上:
git checkout newtheme
git merge -s ours master
git checkout master
git merge newtheme
Push the changes.
推动变化。
git push
And clean up the local newthemebranch.
并清理本地newtheme分支。
git branch -d newtheme
That's it! You've successfully replaced your theme. If there's anything I missed, or you have anything to add, please leave a comment.
就是这样!您已成功替换主题。如果我遗漏了什么,或者您有什么要补充的,请发表评论。
Updating the theme
更新主题
If at any later point you want to update the theme to include the latest upstream changes, simply:
如果以后您想更新主题以包含最新的上游更改,只需:
git pull upstream master
And fix any merge conflicts. Here I assume the upstreamremote is still set to the theme's repository (you can check this with git remote -v).
并修复任何合并冲突。在这里,我假设upstream遥控器仍然设置为主题的存储库(您可以使用 进行检查git remote -v)。
回答by Ohad Schneider
Jekyll v3.2introduced gem-based themes(for future plans see here):
Jekyll v3.2引入了基于 gem 的主题(有关未来计划,请参见此处):
Gem-based themes make it easy for theme developers to make updates available to anyone who has the theme gem. When there's an update, theme developers push the update to RubyGems
The goal of gem-based themes is to allow you to get all the benefits of a robust, continually updated theme without having all the theme's files getting in your way and over-complicating what might be your primary focus: creating content.
基于 Gem 的主题使主题开发人员可以轻松地向拥有主题 gem 的任何人提供更新。当有更新时,主题开发者会将更新推送到 RubyGems
基于 gem 的主题的目标是让您获得强大的、不断更新的主题的所有好处,而不会让所有主题文件妨碍您的工作,并使您的主要关注点:创建内容变得过于复杂。
Installing a gem-based theme is simple:
安装基于 gem 的主题很简单:
- Add the theme to your site's Gemfile:
gem "jekyll-theme-awesome" - Install the theme:
bundle install. - Add the following to your site's
_config.ymlto activate the theme:theme: jekyll-theme-awesome - Build your site:
bundle exec jekyll serve
- 将主题添加到您网站的 Gemfile 中:
gem "jekyll-theme-awesome" - 安装主题:
bundle install. - 将以下内容添加到您的站点
_config.yml以激活主题:theme: jekyll-theme-awesome - 建立您的网站:
bundle exec jekyll serve
To switch themes, I believe something like this should work:
要切换主题,我相信这样的事情应该有效:
- Change to the new theme in your site's Gemfile:
gem "jekyll-theme-new" - Install the theme:
bundle install - Change you site's
_config.ymlto reference the new theme:theme: jekyll-theme-new - Build your site:
bundle exec jekyll serve - (optional - uninstall the old theme from your machine) Note down the old theme's installation folder (
bundle show jekyll-theme-awesome) and uninstall it withgem uninstall jekyll-theme-awesome. To be on the safe side, make sure its folder was indeed deleted.
- 在您站点的 Gemfile 中更改为新主题:
gem "jekyll-theme-new" - 安装主题:
bundle install - 更改您的站点
_config.yml以引用新主题:theme: jekyll-theme-new - 建立您的网站:
bundle exec jekyll serve - (可选 - 从您的机器上卸载旧主题)记下旧主题的安装文件夹 (
bundle show jekyll-theme-awesome) 并使用gem uninstall jekyll-theme-awesome. 为了安全起见,请确保确实删除了其文件夹。
Updating gem-based themes is easy:
更新基于 gem 的主题很容易:
If you have the theme gem, you can (if you desire) run
bundle updateto update all gems in your project. Or you can runbundle update <THEME>, replacing with the theme name, such asminima, to just update the theme gem. Any new files or updates the theme developer has made (such as to stylesheets or includes) will be pulled into your project automatically.
如果您有主题 gem,则可以(如果需要)运行
bundle update以更新项目中的所有 gem。或者您可以运行bundle update <THEME>,替换为主题名称,例如minima,以更新主题 gem。主题开发人员所做的任何新文件或更新(例如样式表或包含)都将自动添加到您的项目中。
Important note: at the time of writing, GitHub pages only supports a specific set of gem-based themes: Architect, Cayman, Dinky, Hacker, Leap day, Merlot, Midnight, Minima, Minimal, Modernist, Slate, Tactile, and Time machine. Of those, it seems only Minima is blog-oriented (e.g. it's the only one with built-in Disqus support). However, you should be able to use any theme if you are willing to run the Jekyll build process yourself.
重要说明:在撰写本文时,GitHub 页面仅支持一组特定的基于 gem 的主题:Architect、Cayman、Dinky、Hacker、Leap day、Merlot、Midnight、Minima、Minimal、Modernist、Slate、Tactile和Time machine. 其中,似乎只有 Minima 是面向博客的(例如,它是唯一具有内置 Disqus 支持的)。但是,如果您愿意自己运行 Jekyll 构建过程,您应该能够使用任何主题。
Another alternative is GitLab pages(tutorial, sample site).
回答by raphael
With GH-Pages
使用 GH 页面
I tested this, but I did it with a project where I didn't have anything I wanted to save, and with fairly simple themes, so this might not work so well with the increased complexity.
我对此进行了测试,但是我在一个没有任何想要保存的项目的项目中进行了测试,并且使用了相当简单的主题,因此随着复杂性的增加,这可能无法很好地工作。
For safety, create a new branch
git checkout -b newthemeAnd then add the new theme as a remote
git remote add new-theme-upstream https://github.com:drjekyllthemes/jekyll-minimal-theme.git git pull new-theme-upstream HEAD- The messy part, you're going to have a bunch of merge conflicts. Check which files have merge conflicts with
git status, hopefully these conflicts should only be in style files that you want to overwrite. If there any files you want to keep you can either edit them with a text editor: git will have labelled the changes in the file Push to your origin
git push origin newtheme- Go to your project's page on github and you'll notice something like this:

- Create a pull-request and merge the new changes in.
- Your project is still going to show as being a fork of the first theme, and you won't be able to create pull-requests for the upstream of the new theme. But you can merge in new updates for your new theme by using
git pull new-theme-upstream
为了安全起见,创建一个新分支
git checkout -b newtheme然后将新主题添加为遥控器
git remote add new-theme-upstream https://github.com:drjekyllthemes/jekyll-minimal-theme.git git pull new-theme-upstream HEAD- 混乱的部分,你会遇到一堆合并冲突。检查哪些文件与 合并冲突
git status,希望这些冲突应该只出现在您想要覆盖的样式文件中。如果您想保留任何文件,您可以使用文本编辑器编辑它们:git 将标记文件中的更改 推到你的原点
git push origin newtheme- 转到您项目在 github 上的页面,您会注意到如下内容:

- 创建一个拉取请求并合并新的更改。
- 您的项目仍将显示为第一个主题的分支,您将无法为新主题的上游创建拉取请求。但是您可以使用以下命令合并新主题的新更新
git pull new-theme-upstream
If you are not using gh-pages or if you are building jekyll locally before pushing to github (I think)
如果你没有使用 gh-pages 或者你在推送到 github 之前在本地构建 jekyll(我认为)
You could keep your themes in git submodules, as separate folders and then symlink the key elements for jekyll. This won'twork in gh-pagescrude example
您可以将主题保存在 git 子模块中,作为单独的文件夹,然后符号链接 jekyll 的关键元素。这不会在工作GH-页面原油例子
blog
|
+-- theme_1/
|
+-- theme_2/
| |
| +-- _layouts/
|
+-- _layouts ln - theme_2/_layouts
That way when changing themes the themes don't collide.
这样在更改主题时主题不会发生冲突。
回答by epidrome
The easiest way to switch theme in an existing or new jekyll installation is to use the following plugin: jekyll-remote-theme, which is available since November 2017.
在现有或新 jekyll 安装中切换主题的最简单方法是使用以下插件:jekyll-remote-theme,自 2017 年 11 月起可用。
Although it is currently in beta, it works fine and most importantly it is already white-listed on Github Pages, so there is no need to build locally, unless the requested theme includes unsupported Gems.
虽然它目前处于测试阶段,但它运行良好,最重要的是它已经在 Github Pages 上被列入白名单,因此无需在本地构建,除非请求的主题包含不受支持的 Gems。
Therefore, in the case of a simple web site with pages and blog, you can host and edit your content directly on the Github infrastructure and switch your site theme by typing-in the address of the new remote theme. An extra benefit is that you can test your content with several existing themesbefore committing to one of them.
因此,对于具有页面和博客的简单网站,您可以直接在 Github 基础架构上托管和编辑您的内容,并通过键入新远程主题的地址来切换您的站点主题。一个额外的好处是,您可以在提交其中一个主题之前使用多个现有主题测试您的内容。
In addition to easier switching, the jekyll-remote-theme method should automatically bring-in a new version of the remote theme, as soon as you make a change and there is a new version by the maintainer of the theme. If the maintainer of the theme makes a radical change that you don't like then you are always a few keystrokes away from a new theme.
除了更容易切换之外,jekyll-remote-theme 方法应该会自动引入新版本的远程主题,只要您进行更改并且主题维护者有新版本。如果主题的维护者进行了您不喜欢的根本性更改,那么您总是离新主题只有几步之遥。
I have several jekyll installations and I am already employing it without intending to switch in the short term, since it is the most elegant and future proof solution, for the time being.
我有几个 jekyll 安装,我已经在使用它,短期内不打算切换,因为它是目前最优雅和面向未来的解决方案。
If your existing jekyll installation is pure (i.e., you have edited only pages, posts, configuration) then the switch is seamless.If your existing theme has special layouts (e.g., splash.html and the new one does not have it) then your pages that employ the respective layout become orphans (i.e., basic html with no special formatting). I have switched an existing installation that had been extensively edited, so I got several orphan pages, but I did not get any of the git merge conflicts that are possible with other methods discussed here.
如果您现有的 jekyll 安装是纯粹的(即,您只编辑了页面、帖子、配置),那么切换是无缝的。如果您现有的主题有特殊的布局(例如, splash.html 而新的没有它),那么使用相应布局的页面将成为孤儿(即,没有特殊格式的基本 html)。我切换了一个已经过广泛编辑的现有安装,所以我得到了几个孤立页面,但我没有得到任何可能与这里讨论的其他方法发生的 git merge 冲突。

