You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
7.0 KiB

  1. ---
  2. date: 2014-03-10
  3. linktitle: Migrating from Jekyll
  4. menu:
  5. main:
  6. parent: tutorials
  7. prev: /tutorials/mathjax
  8. title: Migrate to Hugo from Jekyll
  9. weight: 10
  10. ---
  11. ## Move static content to `static`
  12. Jekyll has a rule that any directory not starting with `_` will be copied as-is to the `_site` output. Hugo keeps all static content under `static`. You should therefore move it all there.
  13. With Jekyll, something that looked like
  14. <root>/
  15. ▾ images/
  16. logo.png
  17. should become
  18. <root>/
  19. ▾ static/
  20. ▾ images/
  21. logo.png
  22. Additionally, you'll want any files that should reside at the root (such as `CNAME`) to be moved to `static`.
  23. ## Create your Hugo configuration file
  24. Hugo can read your configuration as JSON, YAML or TOML. Hugo supports parameters custom configuration too. Refer to the [Hugo configuration documentation](/overview/configuration/) for details.
  25. ## Set your configuration publish folder to `_site`
  26. The default is for Jekyll to publish to `_site` and for Hugo to publish to `public`. If, like me, you have [`_site` mapped to a git submodule on the `gh-pages` branch](http://blog.blindgaenger.net/generate_github_pages_in_a_submodule.html), you'll want to do one of two alternatives:
  27. 1. Change your submodule to point to map `gh-pages` to public instead of `_site` (recommended).
  28. git submodule deinit _site
  29. git rm _site
  30. git submodule add -b gh-pages git@github.com:your-username/your-repo.git public
  31. 2. Or, change the Hugo configuration to use `_site` instead of `public`.
  32. {
  33. ..
  34. "publishdir": "_site",
  35. ..
  36. }
  37. ## Convert Jekyll templates to Hugo templates
  38. That's the bulk of the work right here. The documentation is your friend. You should refer to [Jekyll's template documentation](http://jekyllrb.com/docs/templates/) if you need to refresh your memory on how you built your blog and [Hugo's template](/layout/templates/) to learn Hugo's way.
  39. As a single reference data point, converting my templates for [heyitsalex.net](http://heyitsalex.net/) took me no more than a few hours.
  40. ## Convert Jekyll plugins to Hugo shortcodes
  41. Jekyll has [plugins](http://jekyllrb.com/docs/plugins/); Hugo has [shortcodes](/doc/shortcodes/). It's fairly trivial to do a port.
  42. ### Implementation
  43. As an example, I was using a custom [`image_tag`](https://github.com/alexandre-normand/alexandre-normand/blob/74bb12036a71334fdb7dba84e073382fc06908ec/_plugins/image_tag.rb) plugin to generate figures with caption when running Jekyll. As I read about shortcodes, I found Hugo had a nice built-in shortcode that does exactly the same thing.
  44. Jekyll's plugin:
  45. ```ruby
  46. module Jekyll
  47. class ImageTag < Liquid::Tag
  48. @url = nil
  49. @caption = nil
  50. @class = nil
  51. @link = nil
  52. // Patterns
  53. IMAGE_URL_WITH_CLASS_AND_CAPTION =
  54. IMAGE_URL_WITH_CLASS_AND_CAPTION_AND_LINK = /(\w+)(\s+)((https?:\/\/|\/)(\S+))(\s+)"(.*?)"(\s+)->((https?:\/\/|\/)(\S+))(\s*)/i
  55. IMAGE_URL_WITH_CAPTION = /((https?:\/\/|\/)(\S+))(\s+)"(.*?)"/i
  56. IMAGE_URL_WITH_CLASS = /(\w+)(\s+)((https?:\/\/|\/)(\S+))/i
  57. IMAGE_URL = /((https?:\/\/|\/)(\S+))/i
  58. def initialize(tag_name, markup, tokens)
  59. super
  60. if markup =~ IMAGE_URL_WITH_CLASS_AND_CAPTION_AND_LINK
  61. @class = $1
  62. @url = $3
  63. @caption = $7
  64. @link = $9
  65. elsif markup =~ IMAGE_URL_WITH_CLASS_AND_CAPTION
  66. @class = $1
  67. @url = $3
  68. @caption = $7
  69. elsif markup =~ IMAGE_URL_WITH_CAPTION
  70. @url = $1
  71. @caption = $5
  72. elsif markup =~ IMAGE_URL_WITH_CLASS
  73. @class = $1
  74. @url = $3
  75. elsif markup =~ IMAGE_URL
  76. @url = $1
  77. end
  78. end
  79. def render(context)
  80. if @class
  81. source = "<figure class='#{@class}'>"
  82. else
  83. source = "<figure>"
  84. end
  85. if @link
  86. source += "<a href=\"#{@link}\">"
  87. end
  88. source += "<img src=\"#{@url}\">"
  89. if @link
  90. source += "</a>"
  91. end
  92. source += "<figcaption>#{@caption}</figcaption>" if @caption
  93. source += "</figure>"
  94. source
  95. end
  96. end
  97. end
  98. Liquid::Template.register_tag('image', Jekyll::ImageTag)
  99. ```
  100. is written as this Hugo shortcode:
  101. <!-- image -->
  102. <figure {{ with .Get "class" }}class="{{.}}"{{ end }}>
  103. {{ with .Get "link"}}<a href="{{.}}">{{ end }}
  104. <img src="{{ .Get "src" }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}"{{ end }} />
  105. {{ if .Get "link"}}</a>{{ end }}
  106. {{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
  107. <figcaption>{{ if isset .Params "title" }}
  108. {{ .Get "title" }}{{ end }}
  109. {{ if or (.Get "caption") (.Get "attr")}}<p>
  110. {{ .Get "caption" }}
  111. {{ with .Get "attrlink"}}<a href="{{.}}"> {{ end }}
  112. {{ .Get "attr" }}
  113. {{ if .Get "attrlink"}}</a> {{ end }}
  114. </p> {{ end }}
  115. </figcaption>
  116. {{ end }}
  117. </figure>
  118. <!-- image -->
  119. ### Usage
  120. I simply changed:
  121. {% image full http://farm5.staticflickr.com/4136/4829260124_57712e570a_o_d.jpg "One of my favorite touristy-type photos. I secretly waited for the good light while we were "having fun" and took this. Only regret: a stupid pole in the top-left corner of the frame I had to clumsily get rid of at post-processing." ->http://www.flickr.com/photos/alexnormand/4829260124/in/set-72157624547713078/ %}
  122. to this (this example uses a slightly extended version named `fig`, different than the built-in `figure`):
  123. {{%/* fig class="full" src="http://farm5.staticflickr.com/4136/4829260124_57712e570a_o_d.jpg" title="One of my favorite touristy-type photos. I secretly waited for the good light while we were having fun and took this. Only regret: a stupid pole in the top-left corner of the frame I had to clumsily get rid of at post-processing." link="http://www.flickr.com/photos/alexnormand/4829260124/in/set-72157624547713078/" */%}}
  124. As a bonus, the shortcode named parameters are, arguably, more readable.
  125. ## Finishing touches
  126. ### Fix content
  127. Depending on the amount of customization that was done with each post with Jekyll, this step will require more or less effort. There are no hard and fast rules here except that `hugo server --watch` is your friend. Test your changes and fix errors as needed.
  128. ### Clean up
  129. You'll want to remove the Jekyll configuration at this point. If you have anything else that isn't used, delete it.
  130. ## A practical example in a diff
  131. [Hey, it's Alex](http://heyitsalex.net/) was migrated in less than a _father-with-kids day_ from Jekyll to Hugo. You can see all the changes (and screw-ups) by looking at this [diff](https://github.com/alexandre-normand/alexandre-normand/compare/869d69435bd2665c3fbf5b5c78d4c22759d7613a...b7f6605b1265e83b4b81495423294208cc74d610).