Quellcode durchsuchen

Support generating individual files per post

Might do years or tags later...
Luna Stadler vor 4 Jahren
Ursprung
Commit
2973c030fd
1 geänderte Dateien mit 60 neuen und 17 gelöschten Zeilen
  1. 60 17
      go/blog/blog.go

+ 60 - 17
go/blog/blog.go

@ -50,10 +50,15 @@ type Options struct {
50 50
	NoDefaultStyle bool   `yaml:"no_default_style"`
51 51
	Title          string `yaml:"title"`
52 52
	After          string `yaml:"after"`
53
	PostsDir       string `yaml:"posts_dir"`
53 54
54 55
	// options only in the YAML prefix:
55 56
56 57
	Output string `yaml:"output"`
58
59
	// options only for file output
60
	title    string
61
	showTags bool
57 62
}
58 63
59 64
var flags struct {
@ -159,6 +164,7 @@ func init() {
159 164
	flag.BoolVar(&flags.PrintDefaultStyle, "print-default-style", false, "Print the default styles")
160 165
	flag.StringVar(&flags.Title, "title", "A blog", "Custom `title` to use")
161 166
	flag.StringVar(&flags.After, "after", "", "Insert additional `html` at the end of the generated page")
167
	flag.StringVar(&flags.PostsDir, "posts-dir", "", "Directory to write per-post html files to")
162 168
163 169
	flag.Usage = func() {
164 170
		fmt.Fprintf(os.Stderr, "Usage: %s [flags] [<blog.yaml> [<blog.html>]]\n\n", os.Args[0])
@ -233,6 +239,10 @@ func main() {
233 239
				}
234 240
			case "after":
235 241
				flags.After = opts.After
242
			case "posts-dir":
243
				if opts.PostsDir != "" {
244
					flags.PostsDir = opts.PostsDir
245
				}
236 246
			}
237 247
		})
238 248
@ -257,6 +267,34 @@ func main() {
257 267
		exit(err)
258 268
	}
259 269
270
	for i, post := range posts {
271
		if post.ID == "" {
272
			posts[i].ID = generateID(post)
273
		}
274
	}
275
276
	flags.showTags = true
277
	writePosts(posts, out, flags.Options)
278
279
	if flags.PostsDir != "" {
280
		for _, post := range posts {
281
			postPath := path.Join(path.Dir(dataPath), flags.PostsDir, post.ID+".html")
282
283
			f, err := os.Create(postPath)
284
			if err != nil {
285
				exit(err)
286
			}
287
288
			flags.Title = post.Title
289
			flags.showTags = false
290
			writePosts([]Post{post}, f, flags.Options)
291
292
			fmt.Println("wrote", postPath)
293
		}
294
	}
295
}
296
297
func writePosts(posts []Post, out io.WriteCloser, opts Options) {
260 298
	if flags.NoDefaultStyle {
261 299
		defaultStyle = ""
262 300
	}
@ -283,12 +321,7 @@ func main() {
283 321
	}
284 322
285 323
	tags := map[string]bool{}
286
	for i, post := range posts {
287
		if post.ID == "" {
288
			posts[i].ID = generateID(post)
289
			post = posts[i]
290
		}
291
324
	for _, post := range posts {
292 325
		if post.Tags != nil {
293 326
			for _, tag := range post.Tags {
294 327
				tags[tag] = true
@ -340,14 +373,16 @@ func main() {
340 373
		}
341 374
	}
342 375
343
	sortedTags := []string{}
344
	for tag := range tags {
345
		sortedTags = append(sortedTags, tag)
346
	}
347
	sort.Strings(sortedTags)
348
	err = tagsTmpl.Execute(out, sortedTags)
349
	if err != nil {
350
		exit(err)
376
	if opts.showTags {
377
		sortedTags := []string{}
378
		for tag := range tags {
379
			sortedTags = append(sortedTags, tag)
380
		}
381
		sort.Strings(sortedTags)
382
		err := tagsTmpl.Execute(out, sortedTags)
383
		if err != nil {
384
			exit(err)
385
		}
351 386
	}
352 387
353 388
	fmt.Fprintf(out, `
@ -508,6 +543,13 @@ func main() {
508 543
}
509 544
510 545
var funcs = template.FuncMap{
546
	"permalink": func(post Post) string {
547
		if flags.PostsDir == "" {
548
			return "#" + post.ID
549
		}
550
551
		return path.Join(flags.PostsDir, post.ID+".html")
552
	},
511 553
	"markdown": func(markdown string) template.HTML {
512 554
		return template.HTML(blackfriday.MarkdownCommon([]byte(markdown)))
513 555
	},
@ -522,11 +564,12 @@ var funcs = template.FuncMap{
522 564
523 565
var baseTmpl = template.Must(template.New("base").
524 566
	Funcs(funcs).Parse(`
567
{{ $options := .Options }}
525 568
{{ define "title" }}
526 569
	<header>
527 570
		{{- if .Title }}
528 571
		<h1>{{ .Title }}</h1>{{ end }}
529
		<a class="permalink" href="#{{ .ID }}">∞</a>
572
		<a class="permalink" href="{{ permalink . }}">∞</a>
530 573
		{{- if .Date }}
531 574
		<time>{{ .Date }}</time>{{ end }}
532 575
	</header>
@ -546,7 +589,7 @@ var shellTmpl = template.Must(baseTmpl.New("shell").
546 589
<article id="{{ .ID }}" class="{{ .Type }}" {{- if .Tags }} data-tags="{{ json .Tags }}"{{ end }}>
547 590
	<header>
548 591
		<h1><code class="language-shell">{{ .Title }}</code></h1>
549
		<a class="permalink" href="#{{ .ID }}">∞</a>
592
		<a class="permalink" href="{{ permalink . }}">∞</a>
550 593
		{{- if .Date }}
551 594
		<time>{{ .Date }}</time>{{ end }}
552 595
	</header>
@ -566,7 +609,7 @@ var linkTmpl = template.Must(baseTmpl.New("link").
566 609
<article id="{{ .ID }}" class="{{ .Type }}" {{- if .Tags }} data-tags="{{ json .Tags }}"{{ end }}>
567 610
	<header>
568 611
		<h1><a href="{{ .URL }}">{{ .Title }}</a></h1>
569
		<a class="permalink" href="#{{ .ID }}">∞</a>
612
		<a class="permalink" href="{{ permalink . }}">∞</a>
570 613
		{{- if .Date }}
571 614
		<time>{{ .Date }}</time>{{ end }}
572 615
	</header>