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.

440 lines
13 KiB

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