<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<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." />
|
|
<meta charset="utf-8">
|
|
<title> Static blog template engine implementation in Go - arnaucube - blog</title>
|
|
<meta name="title" content=" Static blog template engine implementation in Go - arnaucube - blog">
|
|
<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.">
|
|
|
|
<meta property="og:title" content=" Static blog template engine implementation in Go - arnaucube - blog" />
|
|
<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." />
|
|
<meta property="og:url" content="https://arnaucube.com/blog/blogo.html" />
|
|
<meta property="og:type" content="article" />
|
|
<meta property="og:image" content="https://arnaucube.com/blog/" />
|
|
<meta name="twitter:title" content=" Static blog template engine implementation in Go - arnaucube - blog">
|
|
<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.">
|
|
<meta name="twitter:image" content="https://arnaucube.com/blog/">
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
<meta name="author" content="arnaucube">
|
|
|
|
<link rel="icon" type="image/png" href="img/logoArnauCubeFavicon.png">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
<link href="css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="css/style.css">
|
|
|
|
<!-- highlightjs -->
|
|
<!-- <link rel="stylesheet" href="js/highlightjs/atom-one-dark.css"> -->
|
|
<link rel="stylesheet" href="js/highlightjs/atom-one-light.css">
|
|
<!-- <link rel="stylesheet" href="js/highlightjs/gruvbox-dark.css"> -->
|
|
<script src="js/highlightjs/highlight.pack.js"></script>
|
|
|
|
<!-- katex -->
|
|
<link rel="stylesheet" href="js/katex/katex.min.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<!-- o_gradient_background" -->
|
|
<nav id="mainNav" class="navbar navbar-default navbar-fixed-top"
|
|
style="height:50px;font-size:130%;">
|
|
<div class="container">
|
|
<div style="float:left;">
|
|
<a href="/blog" style="color:#000;display:inline-block;">Blog index</a>
|
|
<span style="margin-right:20px; margin-left:20px;">|</span>
|
|
<a href="/blog/notes.html" style="font-size:90%;color:#000;display:inline-block;">other-notes</a>
|
|
</div>
|
|
<div style="float:right;">
|
|
<a href="/" style="color:#000;display:inline-block;">arnaucube.com</a>
|
|
<div class="onoffswitch" style="margin:10px;display:inline-block;" title="change theme">
|
|
<input onclick="switchTheme()" type="checkbox" name="onoffswitch" class="onoffswitch-checkbox"
|
|
id="themeSwitcher">
|
|
<label class="onoffswitch-label" for="themeSwitcher"></label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<img style="height:5px; width:100%; margin-top:8px;" src="img/gradient-line.jpg" />
|
|
</nav>
|
|
|
|
|
|
|
|
<div class="container" style="margin-top:40px;max-width:800px;">
|
|
<h1>Static blog template engine implementation in Go</h1>
|
|
|
|
<p><em>2017-12-26</em></p>
|
|
|
|
<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’ve found some interesting projects, but with a lot of features that I don’t need to use. So I decided to write my own minimalistic static blog template engine with Go lang.</p>
|
|
|
|
<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>
|
|
|
|
<h3>Static blog template engine?</h3>
|
|
|
|
<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>
|
|
|
|
<h2>Structure</h2>
|
|
|
|
<p>What we wan to have in the input is:</p>
|
|
|
|
<pre><code>/blogo
|
|
/input
|
|
/css
|
|
mycss.css
|
|
/img
|
|
post01_image.png
|
|
index.html
|
|
postThumbTemplate.html
|
|
post01.md
|
|
post01thumb.md
|
|
post02.md
|
|
post02thumb.md
|
|
</code></pre>
|
|
|
|
<h4>blogo.json</h4>
|
|
|
|
<p>This is the file that have the configuration that will be read by the Go script.</p>
|
|
|
|
<pre><code class="language-json">{
|
|
"title": "my blog",
|
|
"indexTemplate": "index.html",
|
|
"postThumbTemplate": "postThumbTemplate.html",
|
|
"posts": [
|
|
{
|
|
"thumb": "post01thumb.md",
|
|
"md": "post01.md"
|
|
},
|
|
{
|
|
"thumb": "post02thumb.md",
|
|
"md": "post02.md"
|
|
}
|
|
],
|
|
"copyRaw": [
|
|
"css",
|
|
"js"
|
|
]
|
|
}
|
|
</code></pre>
|
|
|
|
<p>The <em>copyRaw</em> element, will be all the directories to copy raw to the output.</p>
|
|
|
|
<h4>index.html</h4>
|
|
|
|
<p>This is the file that will be used as the template for the main page and also for the posts pages.</p>
|
|
|
|
<pre><code class="language-html"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>[blogo-title]</title>
|
|
</head>
|
|
<body>
|
|
<div>
|
|
[blogo-content]
|
|
</div>
|
|
</body>
|
|
</html>
|
|
</code></pre>
|
|
|
|
<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>
|
|
|
|
<h4>postThumbTemplate.html</h4>
|
|
|
|
<p>This is the file where is placed the html box for each post that will be displayed in the main page.</p>
|
|
|
|
<pre><code class="language-html"><div class="well">
|
|
[blogo-index-post-template]
|
|
</div>
|
|
</code></pre>
|
|
|
|
<h4>post01thumb.md</h4>
|
|
|
|
<pre><code class="language-markdown"># Post 01 thumb
|
|
This is the description of the Post 01
|
|
</code></pre>
|
|
|
|
<h4>post01.md</h4>
|
|
|
|
<pre><code class="language-markdown"># Title of the Post 01
|
|
Hi, this is the content of the post
|
|
|
|
'''js
|
|
console.log("hello world");
|
|
'''
|
|
</code></pre>
|
|
|
|
<h2>Let’s start to code</h2>
|
|
|
|
<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>
|
|
|
|
<h4>readConfig.go</h4>
|
|
|
|
<p>This is the file that reads the <em>blogo.json</em> file to get the blog configuration.</p>
|
|
|
|
<pre><code class="language-go">package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
)
|
|
|
|
//Post is the struct for each post of the blog
|
|
type Post struct {
|
|
Thumb string `json:"thumb"`
|
|
Md string `json:"md"`
|
|
}
|
|
|
|
//Config gets the config.json file into struct
|
|
type Config struct {
|
|
Title string `json:"title"`
|
|
IndexTemplate string `json:"indexTemplate"`
|
|
PostThumbTemplate string `json:"postThumbTemplate"`
|
|
Posts []Post `json:"posts"`
|
|
CopyRaw []string `json:"copyRaw"`
|
|
}
|
|
|
|
var config Config
|
|
|
|
func readConfig(path string) {
|
|
file, err := ioutil.ReadFile(path)
|
|
check(err)
|
|
content := string(file)
|
|
json.Unmarshal([]byte(content), &config)
|
|
}
|
|
</code></pre>
|
|
|
|
<h4>files.go, the operations with files</h4>
|
|
|
|
<p>We will need some file operation functions, so we have placed all in this file.</p>
|
|
|
|
<pre><code class="language-go">package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/fatih/color"
|
|
)
|
|
|
|
func readFile(path string) string {
|
|
dat, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
color.Red(path)
|
|
}
|
|
check(err)
|
|
return string(dat)
|
|
}
|
|
|
|
func writeFile(path string, newContent string) {
|
|
err := ioutil.WriteFile(path, []byte(newContent), 0644)
|
|
check(err)
|
|
color.Green(path)
|
|
}
|
|
|
|
func getLines(text string) []string {
|
|
lines := strings.Split(text, "\n")
|
|
return lines
|
|
}
|
|
|
|
func concatStringsWithJumps(lines []string) string {
|
|
var r string
|
|
for _, l := range lines {
|
|
r = r + l + "\n"
|
|
}
|
|
return r
|
|
}
|
|
|
|
func copyRaw(original string, destination string) {
|
|
color.Green(original + " --> to --> " + destination)
|
|
_, err := exec.Command("cp", "-rf", original, destination).Output()
|
|
check(err)
|
|
}
|
|
</code></pre>
|
|
|
|
<h4>main.go</h4>
|
|
|
|
<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>
|
|
|
|
<pre><code class="language-go">package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
blackfriday "gopkg.in/russross/blackfriday.v2"
|
|
)
|
|
|
|
const directory = "blogo-input"
|
|
|
|
func main() {
|
|
readConfig(directory + "/blogo.json")
|
|
fmt.Println(config)
|
|
|
|
// generate index page
|
|
indexTemplate := readFile(directory + "/" + config.IndexTemplate)
|
|
indexPostTemplate := readFile(directory + "/" + config.PostThumbTemplate)
|
|
var blogoIndex string
|
|
blogoIndex = ""
|
|
for _, post := range config.Posts {
|
|
mdpostthumb := readFile(directory + "/" + post.Thumb)
|
|
htmlpostthumb := string(blackfriday.Run([]byte(mdpostthumb)))
|
|
|
|
//put the htmlpostthumb in the blogo-index-post-template
|
|
m := make(map[string]string)
|
|
m["[blogo-index-post-template]"] = htmlpostthumb
|
|
r := putHTMLToTemplate(indexPostTemplate, m)
|
|
filename := strings.Split(post.Md, ".")[0]
|
|
r = "<a href='" + filename + ".html'>" + r + "</a>"
|
|
blogoIndex = blogoIndex + r
|
|
}
|
|
//put the blogoIndex in the index.html
|
|
m := make(map[string]string)
|
|
m["[blogo-title]"] = config.Title
|
|
m["[blogo-content]"] = blogoIndex
|
|
r := putHTMLToTemplate(indexTemplate, m)
|
|
writeFile("index.html", r)
|
|
|
|
// generate posts pages
|
|
for _, post := range config.Posts {
|
|
mdcontent := readFile(directory + "/" + post.Md)
|
|
htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
|
|
|
|
m := make(map[string]string)
|
|
m["[blogo-title]"] = config.Title
|
|
m["[blogo-content]"] = htmlcontent
|
|
|
|
r := putHTMLToTemplate(indexTemplate, m)
|
|
//fmt.Println(r)
|
|
|
|
filename := strings.Split(post.Md, ".")[0]
|
|
writeFile(filename+".html", r)
|
|
}
|
|
|
|
//copy raw
|
|
fmt.Println("copying raw:")
|
|
for _, dir := range config.CopyRaw {
|
|
copyRaw(directory+"/"+dir, ".")
|
|
}
|
|
}
|
|
|
|
func putHTMLToTemplate(template string, m map[string]string) string {
|
|
lines := getLines(template)
|
|
var resultL []string
|
|
for _, line := range lines {
|
|
inserted := false
|
|
for k, v := range m {
|
|
if strings.Contains(line, k) {
|
|
//in the line, change [tag] with the content
|
|
lineReplaced := strings.Replace(line, k, v, -1)
|
|
resultL = append(resultL, lineReplaced)
|
|
inserted = true
|
|
}
|
|
}
|
|
if inserted == false {
|
|
resultL = append(resultL, line)
|
|
}
|
|
}
|
|
result := concatStringsWithJumps(resultL)
|
|
return result
|
|
}
|
|
</code></pre>
|
|
|
|
<h2>Try it</h2>
|
|
|
|
<p>To try it, we need to compile the Go code:</p>
|
|
|
|
<pre><code>> go build
|
|
</code></pre>
|
|
|
|
<p>And run it:</p>
|
|
|
|
<pre><code>> ./blogo
|
|
</code></pre>
|
|
|
|
<p>Or we can just build and run to test:</p>
|
|
|
|
<pre><code>> go run *.go
|
|
</code></pre>
|
|
|
|
<p>As the output, we will obtain the html pages with the content:</p>
|
|
|
|
<ul>
|
|
<li>index.html</li>
|
|
</ul>
|
|
|
|
<pre><code class="language-html"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>my blog</title>
|
|
</head>
|
|
<body>
|
|
<div class="row">
|
|
<a href='post01.html'>
|
|
<div class="col-md-3">
|
|
<h1>Post 01 thumb</h1>
|
|
<p>This is the description of the Post 01</p>
|
|
</div>
|
|
</a>
|
|
<a href='post02.html'>
|
|
<div class="col-md-3">
|
|
<p>Post 02 thumb</p>
|
|
</div>
|
|
</a>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
</code></pre>
|
|
|
|
<ul>
|
|
<li>post01.html</li>
|
|
</ul>
|
|
|
|
<pre><code class="language-html"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>my blog</title>
|
|
</head>
|
|
<body>
|
|
<div>
|
|
<h1>Title of the Post 01</h1>
|
|
<p>Hi, this is the content of the post</p>
|
|
<pre>
|
|
<code class="language-js"> console.log(&quot;hello world&quot;);
|
|
</code>
|
|
</pre>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
</code></pre>
|
|
|
|
<h2>Conclusion</h2>
|
|
|
|
<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’m using for this blog.</p>
|
|
|
|
<p>There are lots of blog template engines nowadays, but maybe we don’t need sophisticated engine, and we just need a minimalistic one. In that case, we have seen how to develop one.</p>
|
|
|
|
<p>The complete project code is able in <a href="https://github.com/arnaucube/blogo">https://github.com/arnaucube/blogo</a></p>
|
|
|
|
</div>
|
|
|
|
<footer style="text-align:center; margin-top:100px;margin-bottom:50px;">
|
|
<div class="container">
|
|
<br>
|
|
<a href="/blog">Go to main</a>
|
|
<br><br>
|
|
<div class="row">
|
|
<ul class="list-inline">
|
|
<li><a href="https://twitter.com/arnaucube"
|
|
style="color:gray;text-decoration:none;"
|
|
target="_blank">twitter.com/arnaucube</a>
|
|
</li>
|
|
<li><a href="https://github.com/arnaucube"
|
|
style="color:gray;text-decoration:none;"
|
|
target="_blank">github.com/arnaucube</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<div class="row" style="display:inline-block;">
|
|
Blog made with <a href="http://github.com/arnaucube/blogo/"
|
|
target="_blank" style="color: gray;text-decoration:none;">Blogo</a>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
|
|
<script>
|
|
</script>
|
|
<script src="js/external-links.js"></script>
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
<script defer src="js/katex/katex.min.js"></script>
|
|
<script defer src="js/katex/auto-render.min.js"></script>
|
|
<script>
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
renderMathInElement(document.body, {
|
|
displayMode: false,
|
|
// customised options
|
|
// • auto-render specific keys, e.g.:
|
|
delimiters: [
|
|
{left: '$$', right: '$$', display: true},
|
|
{left: '$', right: '$', display: false},
|
|
{left: "\\[", right: "\\]", display: true},
|
|
{left: "\\(", right: "\\)", display: false},
|
|
],
|
|
// • rendering keys, e.g.:
|
|
throwOnError : true
|
|
});
|
|
});
|
|
|
|
///
|
|
let theme = localStorage.getItem("theme");
|
|
if ((theme === "light-theme")||(theme==null)) {
|
|
theme = "light-theme";
|
|
document.getElementById("themeSwitcher").checked = false;
|
|
} else if (theme === "dark-theme") {
|
|
theme = "dark-theme";
|
|
document.getElementById("themeSwitcher").checked = true;
|
|
}
|
|
document.body.className = theme;
|
|
localStorage.setItem("theme", theme);
|
|
|
|
function switchTheme() {
|
|
theme = localStorage.getItem("theme");
|
|
if (theme === "light-theme") {
|
|
theme = "dark-theme";
|
|
document.getElementById("themeSwitcher").checked = true;
|
|
} else {
|
|
theme = "light-theme";
|
|
document.getElementById("themeSwitcher").checked = false;
|
|
}
|
|
document.body.className = theme;
|
|
localStorage.setItem("theme", theme);
|
|
|
|
console.log(theme);
|
|
}
|
|
</script>
|
|
<script>
|
|
function tagLinks(tagName) {
|
|
var tags = document.getElementsByTagName(tagName);
|
|
for (var i=0, hElem; hElem = tags[i]; i++) {
|
|
if (hElem.parentNode.className=="row postThumb") {
|
|
continue;
|
|
}
|
|
hElem.id = hElem.innerHTML.toLowerCase().replace(" ", "-");
|
|
hElem.innerHTML = "<a style='text-decoration:none;color:black;' href='#"+hElem.id+"'>"+hElem.innerHTML+"</a>";
|
|
}
|
|
}
|
|
tagLinks("h2");
|
|
tagLinks("h3");
|
|
tagLinks("h4");
|
|
tagLinks("h5");
|
|
</script>
|
|
<script src="js/mermaid.min.js"></script>
|
|
|
|
|
|
</body>
|
|
</html>
|
|
|