/
pandoc.go
103 lines (84 loc) · 2.37 KB
/
pandoc.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
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
)
const dateFormat = "%d.%02d.%02d~-~%02d:%02d:%02d"
const metadataFileName = `metadata.yaml`
const optionsFileName = `pandoc.options`
type metadata struct {
Target string `yaml:"target"`
Tag string
Date string
Day string
Month string
Year string
Time string
Hour string
Minute string
Second string
}
// read options and run pandoc on 99-filename.md files
func runPandoc() {
pandocOptions := readOptionsFile(optionsFileName)
options := strings.Fields(fillMeta(pandocOptions))
inputFiles := fs.getMarkdownFileList()
inputFiles = append(inputFiles, metadataFileName)
options = append(options, inputFiles...)
executeProcess("pandoc", options...)
}
// fill pandoc options template with document metadata
// Target: obtained from 'metadata.yaml'
// Tag: 'git describe --tags'
// Date: current date
// Time: current time
func fillMeta(template string) string {
meta := readFileMetadata(metadataFileName)
// make sure target path exists
path := filepath.Dir(meta.Target)
if path != "." {
if _, err := os.Stat(path); os.IsNotExist(err) && !noop {
fmt.Println("Creating target path:", path)
checkFatal(os.MkdirAll(path, os.ModePerm))
}
}
meta.Tag = getGitTag()
t := time.Now()
meta.Date = formatDate(t)
meta.Time = formatTime(t)
meta.Day = fmt.Sprintf("%02d", t.Day())
meta.Month = fmt.Sprintf("%02d", t.Month())
meta.Year = fmt.Sprintf("%02d", t.Year())
meta.Hour = fmt.Sprintf("%02d", t.Hour())
meta.Minute = fmt.Sprintf("%02d", t.Minute())
meta.Second = fmt.Sprintf("%02d", t.Second())
return fillTemplate(template, meta)
}
// return git tag from project directory
func getGitTag() string {
cmdOut, _ := exec.Command("git", "describe", "--tags").Output()
return strings.TrimSpace(string(cmdOut))
}
// return formatted date
func formatDate(t time.Time) string {
return fmt.Sprintf("%02d.%02d.%d", t.Day(), t.Month(), t.Year())
}
// return formatted time
func formatTime(t time.Time) string {
return fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
}
// fill text template with data
func fillTemplate(text string, data interface{}) string {
template, err := template.New("test").Parse(text)
checkFatal(err)
buff := bytes.NewBufferString("")
err = template.Execute(buff, data)
checkFatal(err)
return buff.String()
}