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.

454 lines
14 KiB

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta name="description" content="How has this blog been made? In this post we will see how to develop a minimalistic static blog template engine with Go." />
  5. <meta charset="utf-8">
  6. <title>Static blog template engine implementation in Go - arnaucube</title>
  7. <meta name="title" content="Static blog template engine implementation in Go - arnaucube">
  8. <meta name="description" content="How has this blog been made? In this post we will see how to develop a minimalistic static blog template engine with Go.">
  9. <meta property="og:title" content="Static blog template engine implementation in Go - arnaucube" />
  10. <meta property="og:description" content="How has this blog been made? In this post we will see how to develop a minimalistic static blog template engine with Go." />
  11. <meta property="og:url" content="https://arnaucube.com/blog/blogo.html" />
  12. <meta property="og:type" content="article" />
  13. <meta property="og:image" content="https://arnaucube.com/blog/" />
  14. <meta name="twitter:title" content="Static blog template engine implementation in Go - arnaucube">
  15. <meta name="twitter:description" content="How has this blog been made? In this post we will see how to develop a minimalistic static blog template engine with Go.">
  16. <meta name="twitter:image" content="https://arnaucube.com/blog/">
  17. <meta name="twitter:card" content="summary_large_image">
  18. <meta name="author" content="arnaucube">
  19. <meta name="viewport" content="width=device-width, initial-scale=1">
  20. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  21. <link rel="stylesheet" href="css/style.css">
  22. <!-- highlightjs -->
  23. <!-- <link rel="stylesheet" href="js/highlightjs/atom-one-dark.css"> -->
  24. <link rel="stylesheet" href="js/highlightjs/gruvbox-dark.css">
  25. <script src="js/highlightjs/highlight.pack.js"></script>
  26. <!-- katex -->
  27. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.11/dist/katex.min.css" integrity="sha384-Um5gpz1odJg5Z4HAmzPtgZKdTBHZdw8S29IecapCSB31ligYPhHQZMIlWLYQGVoc" crossorigin="anonymous">
  28. </head>
  29. <body>
  30. <!-- o_gradient_background" -->
  31. <nav id="mainNav" class="navbar navbar-default navbar-fixed-top"
  32. style="height:50px;font-size:130%;">
  33. <div class="container">
  34. <a href="/blog" style="color:#000;">Blog index</a>
  35. <a href="/" style="color:#000;float:right;">arnaucube.com</a>
  36. </div>
  37. <img style="height:5px; width:100%; margin-top:8px;" src="img/gradient-line.jpg" />
  38. </nav>
  39. <div class="container" style="margin-top:40px;max-width:800px;">
  40. <h1>Static blog template engine implementation in Go</h1>
  41. <p><em>2017-12-26</em></p>
  42. <p>Some days ago, I decided to start this blog, to put there all the thoughts and ideas that goes through my mind. After some research, I&rsquo;ve found some interesting projects, but with a lot of features that I don&rsquo;t need to use. So I decided to write my own minimalistic static blog template engine with Go lang.</p>
  43. <p>This is how I made <a href="https://github.com/arnaucube/blogo">blogo</a> the blog static template engine to do this blog.</p>
  44. <h3>Static blog template engine?</h3>
  45. <p>The main idea is to be able to write the blog posts in Markdown files, and with the template engine, output the HTML files ready to upload in the web hosting server.</p>
  46. <h2>Structure</h2>
  47. <p>What we wan to have in the input is:</p>
  48. <pre><code>/blogo
  49. /input
  50. /css
  51. mycss.css
  52. /img
  53. post01_image.png
  54. index.html
  55. postThumbTemplate.html
  56. post01.md
  57. post01thumb.md
  58. post02.md
  59. post02thumb.md
  60. </code></pre>
  61. <h4>blogo.json</h4>
  62. <p>This is the file that have the configuration that will be read by the Go script.</p>
  63. <pre><code class="language-json">{
  64. &quot;title&quot;: &quot;my blog&quot;,
  65. &quot;indexTemplate&quot;: &quot;index.html&quot;,
  66. &quot;postThumbTemplate&quot;: &quot;postThumbTemplate.html&quot;,
  67. &quot;posts&quot;: [
  68. {
  69. &quot;thumb&quot;: &quot;post01thumb.md&quot;,
  70. &quot;md&quot;: &quot;post01.md&quot;
  71. },
  72. {
  73. &quot;thumb&quot;: &quot;post02thumb.md&quot;,
  74. &quot;md&quot;: &quot;post02.md&quot;
  75. }
  76. ],
  77. &quot;copyRaw&quot;: [
  78. &quot;css&quot;,
  79. &quot;js&quot;
  80. ]
  81. }
  82. </code></pre>
  83. <p>The <em>copyRaw</em> element, will be all the directories to copy raw to the output.</p>
  84. <h4>index.html</h4>
  85. <p>This is the file that will be used as the template for the main page and also for the posts pages.</p>
  86. <pre><code class="language-html">&lt;!DOCTYPE html&gt;
  87. &lt;html&gt;
  88. &lt;head&gt;
  89. &lt;title&gt;[blogo-title]&lt;/title&gt;
  90. &lt;/head&gt;
  91. &lt;body&gt;
  92. &lt;div&gt;
  93. [blogo-content]
  94. &lt;/div&gt;
  95. &lt;/body&gt;
  96. &lt;/html&gt;
  97. </code></pre>
  98. <p>As we can see, we just need to define the html file, and in the title define the <em>[blogo-title]</em>, and in the content place the <em>[blogo-content]</em>.</p>
  99. <h4>postThumbTemplate.html</h4>
  100. <p>This is the file where is placed the html box for each post that will be displayed in the main page.</p>
  101. <pre><code class="language-html">&lt;div class=&quot;well&quot;&gt;
  102. [blogo-index-post-template]
  103. &lt;/div&gt;
  104. </code></pre>
  105. <h4>post01thumb.md</h4>
  106. <pre><code class="language-markdown"># Post 01 thumb
  107. This is the description of the Post 01
  108. </code></pre>
  109. <h4>post01.md</h4>
  110. <pre><code class="language-markdown"># Title of the Post 01
  111. Hi, this is the content of the post
  112. '''js
  113. console.log(&quot;hello world&quot;);
  114. '''
  115. </code></pre>
  116. <h2>Let&rsquo;s start to code</h2>
  117. <p>As we have exposed, we want to develop a Go lang script that from some HTML template and the Markdown text files, generates the complete blog with the main page and all the posts.</p>
  118. <h4>readConfig.go</h4>
  119. <p>This is the file that reads the <em>blogo.json</em> file to get the blog configuration.</p>
  120. <pre><code class="language-go">package main
  121. import (
  122. &quot;encoding/json&quot;
  123. &quot;io/ioutil&quot;
  124. )
  125. //Post is the struct for each post of the blog
  126. type Post struct {
  127. Thumb string `json:&quot;thumb&quot;`
  128. Md string `json:&quot;md&quot;`
  129. }
  130. //Config gets the config.json file into struct
  131. type Config struct {
  132. Title string `json:&quot;title&quot;`
  133. IndexTemplate string `json:&quot;indexTemplate&quot;`
  134. PostThumbTemplate string `json:&quot;postThumbTemplate&quot;`
  135. Posts []Post `json:&quot;posts&quot;`
  136. CopyRaw []string `json:&quot;copyRaw&quot;`
  137. }
  138. var config Config
  139. func readConfig(path string) {
  140. file, err := ioutil.ReadFile(path)
  141. check(err)
  142. content := string(file)
  143. json.Unmarshal([]byte(content), &amp;config)
  144. }
  145. </code></pre>
  146. <h4>files.go, the operations with files</h4>
  147. <p>We will need some file operation functions, so we have placed all in this file.</p>
  148. <pre><code class="language-go">package main
  149. import (
  150. &quot;io/ioutil&quot;
  151. &quot;os/exec&quot;
  152. &quot;strings&quot;
  153. &quot;github.com/fatih/color&quot;
  154. )
  155. func readFile(path string) string {
  156. dat, err := ioutil.ReadFile(path)
  157. if err != nil {
  158. color.Red(path)
  159. }
  160. check(err)
  161. return string(dat)
  162. }
  163. func writeFile(path string, newContent string) {
  164. err := ioutil.WriteFile(path, []byte(newContent), 0644)
  165. check(err)
  166. color.Green(path)
  167. }
  168. func getLines(text string) []string {
  169. lines := strings.Split(text, &quot;\n&quot;)
  170. return lines
  171. }
  172. func concatStringsWithJumps(lines []string) string {
  173. var r string
  174. for _, l := range lines {
  175. r = r + l + &quot;\n&quot;
  176. }
  177. return r
  178. }
  179. func copyRaw(original string, destination string) {
  180. color.Green(original + &quot; --&gt; to --&gt; &quot; + destination)
  181. _, err := exec.Command(&quot;cp&quot;, &quot;-rf&quot;, original, destination).Output()
  182. check(err)
  183. }
  184. </code></pre>
  185. <h4>main.go</h4>
  186. <p>To convert the HTML content to Markdown content, we will use <a href="https://github.com/russross/blackfriday">https://github.com/russross/blackfriday</a></p>
  187. <pre><code class="language-go">package main
  188. import (
  189. &quot;fmt&quot;
  190. &quot;strings&quot;
  191. blackfriday &quot;gopkg.in/russross/blackfriday.v2&quot;
  192. )
  193. const directory = &quot;blogo-input&quot;
  194. func main() {
  195. readConfig(directory + &quot;/blogo.json&quot;)
  196. fmt.Println(config)
  197. // generate index page
  198. indexTemplate := readFile(directory + &quot;/&quot; + config.IndexTemplate)
  199. indexPostTemplate := readFile(directory + &quot;/&quot; + config.PostThumbTemplate)
  200. var blogoIndex string
  201. blogoIndex = &quot;&quot;
  202. for _, post := range config.Posts {
  203. mdpostthumb := readFile(directory + &quot;/&quot; + post.Thumb)
  204. htmlpostthumb := string(blackfriday.Run([]byte(mdpostthumb)))
  205. //put the htmlpostthumb in the blogo-index-post-template
  206. m := make(map[string]string)
  207. m[&quot;[blogo-index-post-template]&quot;] = htmlpostthumb
  208. r := putHTMLToTemplate(indexPostTemplate, m)
  209. filename := strings.Split(post.Md, &quot;.&quot;)[0]
  210. r = &quot;&lt;a href='&quot; + filename + &quot;.html'&gt;&quot; + r + &quot;&lt;/a&gt;&quot;
  211. blogoIndex = blogoIndex + r
  212. }
  213. //put the blogoIndex in the index.html
  214. m := make(map[string]string)
  215. m[&quot;[blogo-title]&quot;] = config.Title
  216. m[&quot;[blogo-content]&quot;] = blogoIndex
  217. r := putHTMLToTemplate(indexTemplate, m)
  218. writeFile(&quot;index.html&quot;, r)
  219. // generate posts pages
  220. for _, post := range config.Posts {
  221. mdcontent := readFile(directory + &quot;/&quot; + post.Md)
  222. htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
  223. m := make(map[string]string)
  224. m[&quot;[blogo-title]&quot;] = config.Title
  225. m[&quot;[blogo-content]&quot;] = htmlcontent
  226. r := putHTMLToTemplate(indexTemplate, m)
  227. //fmt.Println(r)
  228. filename := strings.Split(post.Md, &quot;.&quot;)[0]
  229. writeFile(filename+&quot;.html&quot;, r)
  230. }
  231. //copy raw
  232. fmt.Println(&quot;copying raw:&quot;)
  233. for _, dir := range config.CopyRaw {
  234. copyRaw(directory+&quot;/&quot;+dir, &quot;.&quot;)
  235. }
  236. }
  237. func putHTMLToTemplate(template string, m map[string]string) string {
  238. lines := getLines(template)
  239. var resultL []string
  240. for _, line := range lines {
  241. inserted := false
  242. for k, v := range m {
  243. if strings.Contains(line, k) {
  244. //in the line, change [tag] with the content
  245. lineReplaced := strings.Replace(line, k, v, -1)
  246. resultL = append(resultL, lineReplaced)
  247. inserted = true
  248. }
  249. }
  250. if inserted == false {
  251. resultL = append(resultL, line)
  252. }
  253. }
  254. result := concatStringsWithJumps(resultL)
  255. return result
  256. }
  257. </code></pre>
  258. <h2>Try it</h2>
  259. <p>To try it, we need to compile the Go code:</p>
  260. <pre><code>&gt; go build
  261. </code></pre>
  262. <p>And run it:</p>
  263. <pre><code>&gt; ./blogo
  264. </code></pre>
  265. <p>Or we can just build and run to test:</p>
  266. <pre><code>&gt; go run *.go
  267. </code></pre>
  268. <p>As the output, we will obtain the html pages with the content:</p>
  269. <ul>
  270. <li>index.html</li>
  271. </ul>
  272. <pre><code class="language-html">&lt;!DOCTYPE html&gt;
  273. &lt;html&gt;
  274. &lt;head&gt;
  275. &lt;title&gt;my blog&lt;/title&gt;
  276. &lt;/head&gt;
  277. &lt;body&gt;
  278. &lt;div class=&quot;row&quot;&gt;
  279. &lt;a href='post01.html'&gt;
  280. &lt;div class=&quot;col-md-3&quot;&gt;
  281. &lt;h1&gt;Post 01 thumb&lt;/h1&gt;
  282. &lt;p&gt;This is the description of the Post 01&lt;/p&gt;
  283. &lt;/div&gt;
  284. &lt;/a&gt;
  285. &lt;a href='post02.html'&gt;
  286. &lt;div class=&quot;col-md-3&quot;&gt;
  287. &lt;p&gt;Post 02 thumb&lt;/p&gt;
  288. &lt;/div&gt;
  289. &lt;/a&gt;
  290. &lt;/div&gt;
  291. &lt;/body&gt;
  292. &lt;/html&gt;
  293. </code></pre>
  294. <ul>
  295. <li>post01.html</li>
  296. </ul>
  297. <pre><code class="language-html">&lt;!DOCTYPE html&gt;
  298. &lt;html&gt;
  299. &lt;head&gt;
  300. &lt;title&gt;my blog&lt;/title&gt;
  301. &lt;/head&gt;
  302. &lt;body&gt;
  303. &lt;div&gt;
  304. &lt;h1&gt;Title of the Post 01&lt;/h1&gt;
  305. &lt;p&gt;Hi, this is the content of the post&lt;/p&gt;
  306. &lt;pre&gt;
  307. &lt;code class=&quot;language-js&quot;&gt; console.log(&amp;quot;hello world&amp;quot;);
  308. &lt;/code&gt;
  309. &lt;/pre&gt;
  310. &lt;/div&gt;
  311. &lt;/body&gt;
  312. &lt;/html&gt;
  313. </code></pre>
  314. <h2>Conclusion</h2>
  315. <p>In this post, we have seen how to develop a very minimalistic static blog template engine with Go. In fact, is the blog engine that I&rsquo;m using for this blog.</p>
  316. <p>There are lots of blog template engines nowadays, but maybe we don&rsquo;t need sophisticated engine, and we just need a minimalistic one. In that case, we have seen how to develop one.</p>
  317. <p>The complete project code is able in <a href="https://github.com/arnaucube/blogo">https://github.com/arnaucube/blogo</a></p>
  318. </div>
  319. <footer style="text-align:center; margin-top:100px;margin-bottom:50px;">
  320. <div class="container">
  321. <div class="row">
  322. <ul class="list-inline">
  323. <li><a href="https://twitter.com/arnaucube"
  324. style="color:gray;text-decoration:none;"
  325. target="_blank">twitter.com/arnaucube</a>
  326. </li>
  327. <li><a href="https://github.com/arnaucube"
  328. style="color:gray;text-decoration:none;"
  329. target="_blank">github.com/arnaucube</a>
  330. </li>
  331. </ul>
  332. </div>
  333. <div class="row" style="display:inline-block;">
  334. Blog made with <a href="http://github.com/arnaucube/blogo/"
  335. target="_blank" style="color: gray;text-decoration:none;">Blogo</a>
  336. </div>
  337. </div>
  338. </footer>
  339. <script>
  340. </script>
  341. <script src="js/external-links.js"></script>
  342. <script>hljs.initHighlightingOnLoad();</script>
  343. <script defer src="https://cdn.jsdelivr.net/npm/katex@0.13.11/dist/katex.min.js" integrity="sha384-YNHdsYkH6gMx9y3mRkmcJ2mFUjTd0qNQQvY9VYZgQd7DcN7env35GzlmFaZ23JGp" crossorigin="anonymous"></script>
  344. <script defer src="https://cdn.jsdelivr.net/npm/katex@0.13.11/dist/contrib/auto-render.min.js" integrity="sha384-vZTG03m+2yp6N6BNi5iM4rW4oIwk5DfcNdFfxkk9ZWpDriOkXX8voJBFrAO7MpVl" crossorigin="anonymous"></script>
  345. <script>
  346. document.addEventListener("DOMContentLoaded", function() {
  347. renderMathInElement(document.body, {
  348. displayMode: false,
  349. // customised options
  350. // • auto-render specific keys, e.g.:
  351. delimiters: [
  352. {left: '$$', right: '$$', display: true},
  353. {left: '$', right: '$', display: false},
  354. ],
  355. // • rendering keys, e.g.:
  356. throwOnError : true
  357. });
  358. });
  359. </script>
  360. <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
  361. </body>
  362. </html>