Pārlūkot izejas kodu

Extract content options into their own struct

All options that affect the content now live in a separate struct.

We do this so that we can parse these options from within the blog yaml
file itself.
Lucas Stadler 9 gadi atpakaļ
vecāks
revīzija
8f8b9cb08f
1 mainītis faili ar 29 papildinājumiem un 24 dzēšanām
  1. 29 24
      go/blog/blog.go

+ 29 - 24
go/blog/blog.go

@ -30,16 +30,21 @@ type Post struct {
30 30
	Type    string   `yaml:"type"`
31 31
}
32 32
33
type Options struct {
34
	WriteBack      bool
35
	HashIds        bool
36
	Reverse        bool
37
	Css            string
38
	NoDefaultStyle bool
39
	Title          string
40
	After          string
41
}
42
33 43
var flags struct {
34
	writeBack         bool
35
	hashIds           bool
36
	reverse           bool
37
	css               string
38
	noDefaultStyle    bool
39
	printDefaultStyle bool
40
	title             string
41
	after             string
44
	Options
45
	PrintDefaultStyle bool
42 46
}
47
43 48
var dataPath string = "blog.yaml"
44 49
45 50
var defaultStyle = `
@ -130,14 +135,14 @@ article.does-not-match {
130 135
`
131 136
132 137
func init() {
133
	flag.BoolVar(&flags.writeBack, "write-back", false, "Rewrite the YAML file with the generated ids")
134
	flag.BoolVar(&flags.hashIds, "hash-ids", false, "Use hash-based post ids")
135
	flag.BoolVar(&flags.reverse, "reverse", false, "Reverse the order of the articles in the file")
136
	flag.StringVar(&flags.css, "css", "", "Use custom `css` styles")
137
	flag.BoolVar(&flags.noDefaultStyle, "no-default-style", false, "Don't use the default styles")
138
	flag.BoolVar(&flags.printDefaultStyle, "print-default-style", false, "Print the default styles")
139
	flag.StringVar(&flags.title, "title", "A blog", "Custom `title` to use")
140
	flag.StringVar(&flags.after, "after", "", "Insert additional `html` at the end of the generated page")
138
	flag.BoolVar(&flags.WriteBack, "write-back", false, "Rewrite the YAML file with the generated ids")
139
	flag.BoolVar(&flags.HashIds, "hash-ids", false, "Use hash-based post ids")
140
	flag.BoolVar(&flags.Reverse, "reverse", false, "Reverse the order of the articles in the file")
141
	flag.StringVar(&flags.Css, "css", "", "Use custom `css` styles")
142
	flag.BoolVar(&flags.NoDefaultStyle, "no-default-style", false, "Don't use the default styles")
143
	flag.BoolVar(&flags.PrintDefaultStyle, "print-default-style", false, "Print the default styles")
144
	flag.StringVar(&flags.Title, "title", "A blog", "Custom `title` to use")
145
	flag.StringVar(&flags.After, "after", "", "Insert additional `html` at the end of the generated page")
141 146
142 147
	flag.Usage = func() {
143 148
		fmt.Fprintf(os.Stderr, "Usage: %s [flags] [<blog.yaml> [<blog.html>]]\n\n", os.Args[0])
@ -148,7 +153,7 @@ func init() {
148 153
func main() {
149 154
	flag.Parse()
150 155
151
	if flags.printDefaultStyle {
156
	if flags.PrintDefaultStyle {
152 157
		fmt.Print(defaultStyle)
153 158
		os.Exit(0)
154 159
	}
@ -181,7 +186,7 @@ func main() {
181 186
		exit(err)
182 187
	}
183 188
184
	if flags.noDefaultStyle {
189
	if flags.NoDefaultStyle {
185 190
		defaultStyle = ""
186 191
	}
187 192
	fmt.Fprintf(out, `<!doctype html>
@ -195,9 +200,9 @@ func main() {
195 200
</head>
196 201
197 202
<body>
198
`, template.HTMLEscapeString(flags.title), defaultStyle, flags.css)
203
`, template.HTMLEscapeString(flags.Title), defaultStyle, flags.Css)
199 204
200
	if flags.reverse {
205
	if flags.Reverse {
201 206
		l := len(posts)
202 207
		reversePosts := make([]Post, l)
203 208
		for i := 0; i < l; i++ {
@ -415,14 +420,14 @@ func main() {
415 420
	}
416 421
	</script>`)
417 422
418
	if flags.after != "" {
419
		fmt.Fprintf(out, "\n%s\n", flags.after)
423
	if flags.After != "" {
424
		fmt.Fprintf(out, "\n%s\n", flags.After)
420 425
	}
421 426
422 427
	fmt.Fprintf(out, "\n</body>\n</html>\n")
423 428
	out.Close()
424 429
425
	if flags.writeBack {
430
	if flags.WriteBack {
426 431
		dataOut, err := yaml.Marshal(posts)
427 432
		if err != nil {
428 433
			exit(err)
@ -586,7 +591,7 @@ func exit(err error) {
586 591
}
587 592
588 593
func generateId(p Post) string {
589
	if flags.hashIds {
594
	if flags.HashIds {
590 595
		return hashId(p)
591 596
	}
592 597
	return slugId(p)