Selaa lähdekoodia

Add a tiny (link)blog-generating thing

It does basically nothing, it just generates one file, and so on...

But it's fun.  :)
Lucas Stadler 9 vuotta sitten
vanhempi
commit
39bb34e29a
2 muutettua tiedostoa jossa 137 lisäystä ja 0 poistoa
  1. 126 0
      go/blog/blog.go
  2. 11 0
      go/blog/blog.yaml

+ 126 - 0
go/blog/blog.go

@ -0,0 +1,126 @@
1
package main
2
3
import (
4
	"crypto/md5"
5
	"crypto/rand"
6
	"encoding/hex"
7
	"fmt"
8
	"html/template"
9
	"io"
10
	"io/ioutil"
11
	"os"
12
13
	"github.com/russross/blackfriday"
14
	"gopkg.in/yaml.v2"
15
)
16
17
type Post struct {
18
	Id      string `yaml:"id"`
19
	Title   string `yaml:"title"`
20
	URL     string `yaml:"url"`
21
	Content string `yaml:"content"`
22
	Type    string `yaml:"type"`
23
}
24
25
func main() {
26
	f, err := os.Open("blog.yaml")
27
	if err != nil {
28
		exit(err)
29
	}
30
	defer f.Close()
31
32
	data, err := ioutil.ReadAll(f)
33
	if err != nil {
34
		exit(err)
35
	}
36
37
	var posts []Post
38
	err = yaml.Unmarshal(data, &posts)
39
	if err != nil {
40
		exit(err)
41
	}
42
43
	fmt.Printf(`<doctype html>
44
<html>
45
<head>
46
	<meta charset="utf-8" />
47
	<title>A blog</title>
48
</head>
49
50
<body>
51
`)
52
53
	for i, post := range posts {
54
		if post.Id == "" {
55
			posts[i].Id = generateId(post)
56
			post = posts[i]
57
		}
58
59
		var err error
60
		switch post.Type {
61
		case "shell":
62
			err = shellTmpl.Execute(os.Stdout, post)
63
		case "link":
64
			err = linkTmpl.Execute(os.Stdout, post)
65
		default:
66
			fmt.Fprintf(os.Stderr, "Error: no output for type '%s'\n", post.Type)
67
			os.Exit(1)
68
		}
69
		if err != nil {
70
			exit(err)
71
		}
72
	}
73
74
	fmt.Printf("\n</body>\n</html>\n")
75
}
76
77
var funcs = template.FuncMap{
78
	"markdown": func(markdown string) template.HTML {
79
		return template.HTML(blackfriday.MarkdownCommon([]byte(markdown)))
80
	},
81
}
82
83
var shellTmpl = template.Must(template.New("shell").
84
	Funcs(funcs).Parse(`
85
<article id="{{ .Id }}" class="shell">
86
	<h1><code class="language-shell">{{ .Title }}</code></h1>
87
	{{- if .Content }}
88
89
	{{ markdown .Content }}
90
	{{- end -}}
91
</article>
92
`))
93
94
var linkTmpl = template.Must(template.New("link").
95
	Funcs(funcs).Parse(`
96
<article id="{{ .Id }}" class="link">
97
	<h1><a href="{{ .URL }}">{{ .Title }}</a></h1>
98
	{{- if .Content }}
99
100
	{{ markdown .Content }}
101
	{{- end -}}
102
</article>
103
`))
104
105
func exit(err error) {
106
	fmt.Fprintf(os.Stderr, "Error: %s\n", err)
107
	os.Exit(1)
108
}
109
110
func generateId(p Post) string {
111
	h := md5.New()
112
	io.WriteString(h, p.Title)
113
	io.WriteString(h, p.Content)
114
	io.WriteString(h, p.Type)
115
	return hex.EncodeToString(h.Sum(nil))
116
}
117
118
func randomId() string {
119
	buf := make([]byte, 16)
120
	_, err := rand.Read(buf)
121
	if err != nil {
122
		panic(err)
123
	}
124
125
	return hex.EncodeToString(buf)
126
}

+ 11 - 0
go/blog/blog.yaml

@ -0,0 +1,11 @@
1
---
2
- title: pacman -Qo $(ls -1t --time=atime /usr/bin | tail -n30)
3
  content:
4
    Find infrequently used binaries on your system.  `ls --time=atime`
5
    is the key here, it uses the access time instead of the modification
6
    time, which is the default.
7
  type: shell
8
- title: XOXO Festival videos (2012-2015)
9
  url: https://www.youtube.com/user/xoxofest/videos
10
  content: Unfortunately, the ones for 2016 are missing.  I'm not sure if there will be video, I haven't found anything official so far.
11
  type: link