Просмотр исходного кода

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
Родитель
Сommit
8f8b9cb08f
1 измененных файлов с 29 добавлено и 24 удалено
  1. 29 24
      go/blog/blog.go

+ 29 - 24
go/blog/blog.go

30
	Type    string   `yaml:"type"`
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
var flags struct {
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
var dataPath string = "blog.yaml"
48
var dataPath string = "blog.yaml"
44
49
45
var defaultStyle = `
50
var defaultStyle = `
130
`
135
`
131
136
132
func init() {
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
	flag.Usage = func() {
147
	flag.Usage = func() {
143
		fmt.Fprintf(os.Stderr, "Usage: %s [flags] [<blog.yaml> [<blog.html>]]\n\n", os.Args[0])
148
		fmt.Fprintf(os.Stderr, "Usage: %s [flags] [<blog.yaml> [<blog.html>]]\n\n", os.Args[0])
148
func main() {
153
func main() {
149
	flag.Parse()
154
	flag.Parse()
150
155
151
	if flags.printDefaultStyle {
156
	if flags.PrintDefaultStyle {
152
		fmt.Print(defaultStyle)
157
		fmt.Print(defaultStyle)
153
		os.Exit(0)
158
		os.Exit(0)
154
	}
159
	}
181
		exit(err)
186
		exit(err)
182
	}
187
	}
183
188
184
	if flags.noDefaultStyle {
189
	if flags.NoDefaultStyle {
185
		defaultStyle = ""
190
		defaultStyle = ""
186
	}
191
	}
187
	fmt.Fprintf(out, `<!doctype html>
192
	fmt.Fprintf(out, `<!doctype html>
195
</head>
200
</head>
196
201
197
<body>
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
		l := len(posts)
206
		l := len(posts)
202
		reversePosts := make([]Post, l)
207
		reversePosts := make([]Post, l)
203
		for i := 0; i < l; i++ {
208
		for i := 0; i < l; i++ {
415
	}
420
	}
416
	</script>`)
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
	fmt.Fprintf(out, "\n</body>\n</html>\n")
427
	fmt.Fprintf(out, "\n</body>\n</html>\n")
423
	out.Close()
428
	out.Close()
424
429
425
	if flags.writeBack {
430
	if flags.WriteBack {
426
		dataOut, err := yaml.Marshal(posts)
431
		dataOut, err := yaml.Marshal(posts)
427
		if err != nil {
432
		if err != nil {
428
			exit(err)
433
			exit(err)
586
}
591
}
587
592
588
func generateId(p Post) string {
593
func generateId(p Post) string {
589
	if flags.hashIds {
594
	if flags.HashIds {
590
		return hashId(p)
595
		return hashId(p)
591
	}
596
	}
592
	return slugId(p)
597
	return slugId(p)