~samwhited/blogsync

ref: c7d923e270765e9f5973f8ff39e10d4e218d3cb0 blogsync/tmpl.go -rw-r--r-- 2.0 KiB
c7d923e2Sam Whited blogsync: fix name of convert command flagset 1 year, 10 months ago
                                                                                
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
// Copyright 2019 The Blog Sync Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"strings"

	"github.com/BurntSushi/toml"
	"mellium.im/blogsync/internal/blog"
	"mellium.im/cli"
)

const helpIndent = "\t"

func tmplArticle() *cli.Command {
	defTmplData := tmplData{
		Meta: make(blog.Metadata),
		Config: Config{
			Params: make(map[string]interface{}),
		},
	}
	var encodedTmplData strings.Builder
	e := toml.NewEncoder(&encodedTmplData)
	e.Indent = ""
	err := e.Encode(defTmplData)
	if err != nil {
		panic(fmt.Errorf(`Error marshaling TOML for help output.  This should never
happen.  If you see this message, please report it and include the following
error message in your report:

%w`, err))
	}

	// This is all very inefficient, but it's a tiny string in a help article so
	// I'm not sure that we care, let's go for simple debugging and readability.
	lines := strings.Split(encodedTmplData.String(), "\n")
	for i, line := range lines {
		lines[i] = "\t" + line
	}
	indentedTmplData := strings.Join(lines, "\n")

	return &cli.Command{
		Usage: "templates",
		Description: fmt.Sprintf(`Writing templates that can be applied to pages.

The publish command supports specifying a template that can be applied to a post
before it is published.  These templates use Go's text/template package, a
description of which can be found here:

%shttps://golang.org/pkg/text/template/

Templates are passed the following data (represented in TOML format):

%s

The body field contains the markdown from the page being published, that is,
everything after the frontmatter.  The Meta table contains the fields from the
TOML frontmatter.  The Config field contains values loaded from the site config
file.  If you want to add arbitrary values to the config file they must be in
the Params section.

If no template is specified when publishing, the body is published as-is using
the template:

%[1]s%[3]s`, helpIndent, indentedTmplData, defTmpl),
	}
}