-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
122 lines (108 loc) · 2.54 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package blogmngr
import (
"fmt"
"os"
"bytes"
"text/template"
"io/ioutil"
"strings"
)
type P map[string]interface{}
const indexTemplate = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Blog</title>
</head>
<body>
<h1>Blog</h1>
<hr>
<ul>
{{.entries}}
</ul>
<hr>
<small>- <a href="rss.xml">RSS</a> -</small>
</body>
</html>
`
const postTemplate = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{{.title}}</title>
</head>
<body>
<h1>{{.title}}</h1>
<hr>
<p>
<small>{{.date}}</small>
</p>
{{.content}}
<hr>
<small>- <a href="index.html">home</a> -</small>
</body>
</html>
`
const rssTemplate = `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Blog</title>
<link>{{.link}}</link>
<description>Generated by nob</description>
{{.entries}}
</channel>
</rss>
`
const RssArticle = ` <item>
<title>{{.title}}</title>
<link>{{.link}}</link>
<description>{{.desc}}</description>
</item>
`
const EntryLi = `<li><a href="%s">%s</a> <small>%s</small>
`
func CreateTemplates(templatesdir string) {
indexFile := fmt.Sprintf("%s/index.html", templatesdir)
postFile := fmt.Sprintf("%s/post.html", templatesdir)
rssFile := fmt.Sprintf("%s/rss.xml", templatesdir)
linkFile := fmt.Sprintf("%s/my_blog_link.txt", templatesdir)
var templates = map[string]string {
indexFile: indexTemplate,
postFile: postTemplate,
rssFile: rssTemplate,
}
for tpath, content := range templates {
f, _ := os.OpenFile(tpath, os.O_CREATE | os.O_WRONLY | os.O_APPEND, 0600)
f.WriteString(content)
f.Close()
}
// TODO refactor this
f, _ := os.OpenFile(linkFile, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0600)
f.WriteString("https://my-website.neocities.org/blog")
f.Close()
}
// TODO refactor this
func GetWebsiteURL() string {
cwd, _ := os.Getwd()
linkPath := fmt.Sprintf("%s/nob/templates/my_blog_link.txt", cwd)
f, _ := ioutil.ReadFile(linkPath)
return strings.Replace(string(f), "\n", "", -1)
}
func ReadTemplateFile(fname string) string {
cwd, _ := os.Getwd()
templatePath := fmt.Sprintf("%s/nob/templates/%s", cwd, fname)
f, _ := ioutil.ReadFile(templatePath)
return string(f)
}
func WriteToStatic(fname string, content string) {
cwd, _ := os.Getwd()
staticPath := fmt.Sprintf("%s/%s", cwd, fname)
f, _ := os.OpenFile(staticPath, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0600)
f.WriteString(content)
f.Close()
}
func FmtTemplate(format string, p P) string {
b := &bytes.Buffer{}
template.Must(template.New("").Parse(format)).Execute(b, p)
return b.String()
}