Source code for website arcusiridis.com
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.

157 lines
7.0 KiB

5 years ago
  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. module Jekyll
  46. class ImageTag < Liquid::Tag
  47. @url = nil
  48. @caption = nil
  49. @class = nil
  50. @link = nil
  51. // Patterns
  52. IMAGE_URL_WITH_CLASS_AND_CAPTION =
  53. IMAGE_URL_WITH_CLASS_AND_CAPTION_AND_LINK = /(\w+)(\s+)((https?:\/\/|\/)(\S+))(\s+)"(.*?)"(\s+)->((https?:\/\/|\/)(\S+))(\s*)/i
  54. IMAGE_URL_WITH_CAPTION = /((https?:\/\/|\/)(\S+))(\s+)"(.*?)"/i
  55. IMAGE_URL_WITH_CLASS = /(\w+)(\s+)((https?:\/\/|\/)(\S+))/i
  56. IMAGE_URL = /((https?:\/\/|\/)(\S+))/i
  57. def initialize(tag_name, markup, tokens)
  58. super
  59. if markup =~ IMAGE_URL_WITH_CLASS_AND_CAPTION_AND_LINK
  60. @class = $1
  61. @url = $3
  62. @caption = $7
  63. @link = $9
  64. elsif markup =~ IMAGE_URL_WITH_CLASS_AND_CAPTION
  65. @class = $1
  66. @url = $3
  67. @caption = $7
  68. elsif markup =~ IMAGE_URL_WITH_CAPTION
  69. @url = $1
  70. @caption = $5
  71. elsif markup =~ IMAGE_URL_WITH_CLASS
  72. @class = $1
  73. @url = $3
  74. elsif markup =~ IMAGE_URL
  75. @url = $1
  76. end
  77. end
  78. def render(context)
  79. if @class
  80. source = "<figure class='#{@class}'>"
  81. else
  82. source = "<figure>"
  83. end
  84. if @link
  85. source += "<a href=\"#{@link}\">"
  86. end
  87. source += "<img src=\"#{@url}\">"
  88. if @link
  89. source += "</a>"
  90. end
  91. source += "<figcaption>#{@caption}</figcaption>" if @caption
  92. source += "</figure>"
  93. source
  94. end
  95. end
  96. end
  97. Liquid::Template.register_tag('image', Jekyll::ImageTag)
  98. is written as this Hugo shortcode:
  99. <!-- image -->
  100. <figure {{ with .Get "class" }}class="{{.}}"{{ end }}>
  101. {{ with .Get "link"}}<a href="{{.}}">{{ end }}
  102. <img src="{{ .Get "src" }}" {{ if or (.Get "alt") (.Get "caption") }}alt="{{ with .Get "alt"}}{{.}}{{else}}{{ .Get "caption" }}{{ end }}"{{ end }} />
  103. {{ if .Get "link"}}</a>{{ end }}
  104. {{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
  105. <figcaption>{{ if isset .Params "title" }}
  106. {{ .Get "title" }}{{ end }}
  107. {{ if or (.Get "caption") (.Get "attr")}}<p>
  108. {{ .Get "caption" }}
  109. {{ with .Get "attrlink"}}<a href="{{.}}"> {{ end }}
  110. {{ .Get "attr" }}
  111. {{ if .Get "attrlink"}}</a> {{ end }}
  112. </p> {{ end }}
  113. </figcaption>
  114. {{ end }}
  115. </figure>
  116. <!-- image -->
  117. ### Usage
  118. I simply changed:
  119. {% 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/ %}
  120. to this (this example uses a slightly extended version named `fig`, different than the built-in `figure`):
  121. {{%/* 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/" */%}}
  122. As a bonus, the shortcode named parameters are, arguably, more readable.
  123. ## Finishing touches
  124. ### Fix content
  125. 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.
  126. ### Clean up
  127. You'll want to remove the Jekyll configuration at this point. If you have anything else that isn't used, delete it.
  128. ## A practical example in a diff
  129. [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).