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.

506 lines
16 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  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 - blog</title>
  7. <meta name="title" content=" Static blog template engine implementation in Go - arnaucube - blog">
  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 - blog" />
  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 - blog">
  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="css/bootstrap.min.css" rel="stylesheet">
  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="js/katex/katex.min.css">
  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. <div style="float:right;">
  36. <a href="/" style="color:#000;display:inline-block;">arnaucube.com</a>
  37. <div class="onoffswitch" style="margin:10px;display:inline-block;" title="change theme">
  38. <input onclick="switchTheme()" type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
  39. id="themeSwitcher">
  40. <label class="onoffswitch-label" for="themeSwitcher"></label>
  41. </div>
  42. </div>
  43. </div>
  44. <img style="height:5px; width:100%; margin-top:8px;" src="img/gradient-line.jpg" />
  45. </nav>
  46. <div class="container" style="margin-top:40px;max-width:800px;">
  47. <h1>Static blog template engine implementation in Go</h1>
  48. <p><em>2017-12-26</em></p>
  49. <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>
  50. <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>
  51. <h3>Static blog template engine?</h3>
  52. <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>
  53. <h2>Structure</h2>
  54. <p>What we wan to have in the input is:</p>
  55. <pre><code>/blogo
  56. /input
  57. /css
  58. mycss.css
  59. /img
  60. post01_image.png
  61. index.html
  62. postThumbTemplate.html
  63. post01.md
  64. post01thumb.md
  65. post02.md
  66. post02thumb.md
  67. </code></pre>
  68. <h4>blogo.json</h4>
  69. <p>This is the file that have the configuration that will be read by the Go script.</p>
  70. <pre><code class="language-json">{
  71. &quot;title&quot;: &quot;my blog&quot;,
  72. &quot;indexTemplate&quot;: &quot;index.html&quot;,
  73. &quot;postThumbTemplate&quot;: &quot;postThumbTemplate.html&quot;,
  74. &quot;posts&quot;: [
  75. {
  76. &quot;thumb&quot;: &quot;post01thumb.md&quot;,
  77. &quot;md&quot;: &quot;post01.md&quot;
  78. },
  79. {
  80. &quot;thumb&quot;: &quot;post02thumb.md&quot;,
  81. &quot;md&quot;: &quot;post02.md&quot;
  82. }
  83. ],
  84. &quot;copyRaw&quot;: [
  85. &quot;css&quot;,
  86. &quot;js&quot;
  87. ]
  88. }
  89. </code></pre>
  90. <p>The <em>copyRaw</em> element, will be all the directories to copy raw to the output.</p>
  91. <h4>index.html</h4>
  92. <p>This is the file that will be used as the template for the main page and also for the posts pages.</p>
  93. <pre><code class="language-html">&lt;!DOCTYPE html&gt;
  94. &lt;html&gt;
  95. &lt;head&gt;
  96. &lt;title&gt;[blogo-title]&lt;/title&gt;
  97. &lt;/head&gt;
  98. &lt;body&gt;
  99. &lt;div&gt;
  100. [blogo-content]
  101. &lt;/div&gt;
  102. &lt;/body&gt;
  103. &lt;/html&gt;
  104. </code></pre>
  105. <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>
  106. <h4>postThumbTemplate.html</h4>
  107. <p>This is the file where is placed the html box for each post that will be displayed in the main page.</p>
  108. <pre><code class="language-html">&lt;div class=&quot;well&quot;&gt;
  109. [blogo-index-post-template]
  110. &lt;/div&gt;
  111. </code></pre>
  112. <h4>post01thumb.md</h4>
  113. <pre><code class="language-markdown"># Post 01 thumb
  114. This is the description of the Post 01
  115. </code></pre>
  116. <h4>post01.md</h4>
  117. <pre><code class="language-markdown"># Title of the Post 01
  118. Hi, this is the content of the post
  119. '''js
  120. console.log(&quot;hello world&quot;);
  121. '''
  122. </code></pre>
  123. <h2>Let&rsquo;s start to code</h2>
  124. <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>
  125. <h4>readConfig.go</h4>
  126. <p>This is the file that reads the <em>blogo.json</em> file to get the blog configuration.</p>
  127. <pre><code class="language-go">package main
  128. import (
  129. &quot;encoding/json&quot;
  130. &quot;io/ioutil&quot;
  131. )
  132. //Post is the struct for each post of the blog
  133. type Post struct {
  134. Thumb string `json:&quot;thumb&quot;`
  135. Md string `json:&quot;md&quot;`
  136. }
  137. //Config gets the config.json file into struct
  138. type Config struct {
  139. Title string `json:&quot;title&quot;`
  140. IndexTemplate string `json:&quot;indexTemplate&quot;`
  141. PostThumbTemplate string `json:&quot;postThumbTemplate&quot;`
  142. Posts []Post `json:&quot;posts&quot;`
  143. CopyRaw []string `json:&quot;copyRaw&quot;`
  144. }
  145. var config Config
  146. func readConfig(path string) {
  147. file, err := ioutil.ReadFile(path)
  148. check(err)
  149. content := string(file)
  150. json.Unmarshal([]byte(content), &amp;config)
  151. }
  152. </code></pre>
  153. <h4>files.go, the operations with files</h4>
  154. <p>We will need some file operation functions, so we have placed all in this file.</p>
  155. <pre><code class="language-go">package main
  156. import (
  157. &quot;io/ioutil&quot;
  158. &quot;os/exec&quot;
  159. &quot;strings&quot;
  160. &quot;github.com/fatih/color&quot;
  161. )
  162. func readFile(path string) string {
  163. dat, err := ioutil.ReadFile(path)
  164. if err != nil {
  165. color.Red(path)
  166. }
  167. check(err)
  168. return string(dat)
  169. }
  170. func writeFile(path string, newContent string) {
  171. err := ioutil.WriteFile(path, []byte(newContent), 0644)
  172. check(err)
  173. color.Green(path)
  174. }
  175. func getLines(text string) []string {
  176. lines := strings.Split(text, &quot;\n&quot;)
  177. return lines
  178. }
  179. func concatStringsWithJumps(lines []string) string {
  180. var r string
  181. for _, l := range lines {
  182. r = r + l + &quot;\n&quot;
  183. }
  184. return r
  185. }
  186. func copyRaw(original string, destination string) {
  187. color.Green(original + &quot; --&gt; to --&gt; &quot; + destination)
  188. _, err := exec.Command(&quot;cp&quot;, &quot;-rf&quot;, original, destination).Output()
  189. check(err)
  190. }
  191. </code></pre>
  192. <h4>main.go</h4>
  193. <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>
  194. <pre><code class="language-go">package main
  195. import (
  196. &quot;fmt&quot;
  197. &quot;strings&quot;
  198. blackfriday &quot;gopkg.in/russross/blackfriday.v2&quot;
  199. )
  200. const directory = &quot;blogo-input&quot;
  201. func main() {
  202. readConfig(directory + &quot;/blogo.json&quot;)
  203. fmt.Println(config)
  204. // generate index page
  205. indexTemplate := readFile(directory + &quot;/&quot; + config.IndexTemplate)
  206. indexPostTemplate := readFile(directory + &quot;/&quot; + config.PostThumbTemplate)
  207. var blogoIndex string
  208. blogoIndex = &quot;&quot;
  209. for _, post := range config.Posts {
  210. mdpostthumb := readFile(directory + &quot;/&quot; + post.Thumb)
  211. htmlpostthumb := string(blackfriday.Run([]byte(mdpostthumb)))
  212. //put the htmlpostthumb in the blogo-index-post-template
  213. m := make(map[string]string)
  214. m[&quot;[blogo-index-post-template]&quot;] = htmlpostthumb
  215. r := putHTMLToTemplate(indexPostTemplate, m)
  216. filename := strings.Split(post.Md, &quot;.&quot;)[0]
  217. r = &quot;&lt;a href='&quot; + filename + &quot;.html'&gt;&quot; + r + &quot;&lt;/a&gt;&quot;
  218. blogoIndex = blogoIndex + r
  219. }
  220. //put the blogoIndex in the index.html
  221. m := make(map[string]string)
  222. m[&quot;[blogo-title]&quot;] = config.Title
  223. m[&quot;[blogo-content]&quot;] = blogoIndex
  224. r := putHTMLToTemplate(indexTemplate, m)
  225. writeFile(&quot;index.html&quot;, r)
  226. // generate posts pages
  227. for _, post := range config.Posts {
  228. mdcontent := readFile(directory + &quot;/&quot; + post.Md)
  229. htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
  230. m := make(map[string]string)
  231. m[&quot;[blogo-title]&quot;] = config.Title
  232. m[&quot;[blogo-content]&quot;] = htmlcontent
  233. r := putHTMLToTemplate(indexTemplate, m)
  234. //fmt.Println(r)
  235. filename := strings.Split(post.Md, &quot;.&quot;)[0]
  236. writeFile(filename+&quot;.html&quot;, r)
  237. }
  238. //copy raw
  239. fmt.Println(&quot;copying raw:&quot;)
  240. for _, dir := range config.CopyRaw {
  241. copyRaw(directory+&quot;/&quot;+dir, &quot;.&quot;)
  242. }
  243. }
  244. func putHTMLToTemplate(template string, m map[string]string) string {
  245. lines := getLines(template)
  246. var resultL []string
  247. for _, line := range lines {
  248. inserted := false
  249. for k, v := range m {
  250. if strings.Contains(line, k) {
  251. //in the line, change [tag] with the content
  252. lineReplaced := strings.Replace(line, k, v, -1)
  253. resultL = append(resultL, lineReplaced)
  254. inserted = true
  255. }
  256. }
  257. if inserted == false {
  258. resultL = append(resultL, line)
  259. }
  260. }
  261. result := concatStringsWithJumps(resultL)
  262. return result
  263. }
  264. </code></pre>
  265. <h2>Try it</h2>
  266. <p>To try it, we need to compile the Go code:</p>
  267. <pre><code>&gt; go build
  268. </code></pre>
  269. <p>And run it:</p>
  270. <pre><code>&gt; ./blogo
  271. </code></pre>
  272. <p>Or we can just build and run to test:</p>
  273. <pre><code>&gt; go run *.go
  274. </code></pre>
  275. <p>As the output, we will obtain the html pages with the content:</p>
  276. <ul>
  277. <li>index.html</li>
  278. </ul>
  279. <pre><code class="language-html">&lt;!DOCTYPE html&gt;
  280. &lt;html&gt;
  281. &lt;head&gt;
  282. &lt;title&gt;my blog&lt;/title&gt;
  283. &lt;/head&gt;
  284. &lt;body&gt;
  285. &lt;div class=&quot;row&quot;&gt;
  286. &lt;a href='post01.html'&gt;
  287. &lt;div class=&quot;col-md-3&quot;&gt;
  288. &lt;h1&gt;Post 01 thumb&lt;/h1&gt;
  289. &lt;p&gt;This is the description of the Post 01&lt;/p&gt;
  290. &lt;/div&gt;
  291. &lt;/a&gt;
  292. &lt;a href='post02.html'&gt;
  293. &lt;div class=&quot;col-md-3&quot;&gt;
  294. &lt;p&gt;Post 02 thumb&lt;/p&gt;
  295. &lt;/div&gt;
  296. &lt;/a&gt;
  297. &lt;/div&gt;
  298. &lt;/body&gt;
  299. &lt;/html&gt;
  300. </code></pre>
  301. <ul>
  302. <li>post01.html</li>
  303. </ul>
  304. <pre><code class="language-html">&lt;!DOCTYPE html&gt;
  305. &lt;html&gt;
  306. &lt;head&gt;
  307. &lt;title&gt;my blog&lt;/title&gt;
  308. &lt;/head&gt;
  309. &lt;body&gt;
  310. &lt;div&gt;
  311. &lt;h1&gt;Title of the Post 01&lt;/h1&gt;
  312. &lt;p&gt;Hi, this is the content of the post&lt;/p&gt;
  313. &lt;pre&gt;
  314. &lt;code class=&quot;language-js&quot;&gt; console.log(&amp;quot;hello world&amp;quot;);
  315. &lt;/code&gt;
  316. &lt;/pre&gt;
  317. &lt;/div&gt;
  318. &lt;/body&gt;
  319. &lt;/html&gt;
  320. </code></pre>
  321. <h2>Conclusion</h2>
  322. <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>
  323. <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>
  324. <p>The complete project code is able in <a href="https://github.com/arnaucube/blogo">https://github.com/arnaucube/blogo</a></p>
  325. </div>
  326. <footer style="text-align:center; margin-top:100px;margin-bottom:50px;">
  327. <div class="container">
  328. <div class="row">
  329. <ul class="list-inline">
  330. <li><a href="https://twitter.com/arnaucube"
  331. style="color:gray;text-decoration:none;"
  332. target="_blank">twitter.com/arnaucube</a>
  333. </li>
  334. <li><a href="https://github.com/arnaucube"
  335. style="color:gray;text-decoration:none;"
  336. target="_blank">github.com/arnaucube</a>
  337. </li>
  338. </ul>
  339. </div>
  340. <div class="row" style="display:inline-block;">
  341. Blog made with <a href="http://github.com/arnaucube/blogo/"
  342. target="_blank" style="color: gray;text-decoration:none;">Blogo</a>
  343. </div>
  344. </div>
  345. </footer>
  346. <script>
  347. </script>
  348. <script src="js/external-links.js"></script>
  349. <script>hljs.initHighlightingOnLoad();</script>
  350. <script defer src="js/katex/katex.min.js"></script>
  351. <script defer src="js/katex/auto-render.min.js"></script>
  352. <script>
  353. document.addEventListener("DOMContentLoaded", function() {
  354. renderMathInElement(document.body, {
  355. displayMode: false,
  356. // customised options
  357. // • auto-render specific keys, e.g.:
  358. delimiters: [
  359. {left: '$$', right: '$$', display: true},
  360. {left: '$', right: '$', display: false},
  361. {left: "\\[", right: "\\]", display: true},
  362. {left: "\\(", right: "\\)", display: false},
  363. ],
  364. // • rendering keys, e.g.:
  365. throwOnError : true
  366. });
  367. });
  368. ///
  369. let theme = localStorage.getItem("theme");
  370. if ((theme === "light-theme")||(theme==null)) {
  371. theme = "light-theme";
  372. document.getElementById("themeSwitcher").checked = false;
  373. } else if (theme === "dark-theme") {
  374. theme = "dark-theme";
  375. document.getElementById("themeSwitcher").checked = true;
  376. }
  377. document.body.className = theme;
  378. localStorage.setItem("theme", theme);
  379. function switchTheme() {
  380. theme = localStorage.getItem("theme");
  381. if (theme === "light-theme") {
  382. theme = "dark-theme";
  383. document.getElementById("themeSwitcher").checked = true;
  384. } else {
  385. theme = "light-theme";
  386. document.getElementById("themeSwitcher").checked = false;
  387. }
  388. document.body.className = theme;
  389. localStorage.setItem("theme", theme);
  390. console.log(theme);
  391. }
  392. </script>
  393. <script>
  394. function tagLinks(tagName) {
  395. var tags = document.getElementsByTagName(tagName);
  396. for (var i=0, hElem; hElem = tags[i]; i++) {
  397. if (hElem.parentNode.className=="row postThumb") {
  398. continue;
  399. }
  400. hElem.id = hElem.innerHTML.toLowerCase().replace(" ", "-");
  401. hElem.innerHTML = "<a style='text-decoration:none;color:black;' href='#"+hElem.id+"'>"+hElem.innerHTML+"</a>";
  402. }
  403. }
  404. tagLinks("h2");
  405. tagLinks("h3");
  406. tagLinks("h4");
  407. tagLinks("h5");
  408. </script>
  409. <script src="js/mermaid.min.js"></script>
  410. </body>
  411. </html>