-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
83 lines (73 loc) · 1.61 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/ngyewch/mdbook-asciidoc/renderer"
"github.com/ngyewch/mdbook-plugin"
"github.com/urfave/cli/v2"
"log"
"os"
"strconv"
"strings"
"time"
)
var (
version string
commit string
commitTimestamp string
)
func main() {
app := &cli.App{
Name: "mdbook-asciidoc",
Usage: "mdbook-asciidoc",
Version: version,
Action: doMain,
}
cli.VersionPrinter = func(cCtx *cli.Context) {
var parts []string
if version != "" {
parts = append(parts, fmt.Sprintf("version=%s", version))
}
if commit != "" {
parts = append(parts, fmt.Sprintf("commit=%s", commit))
}
if commitTimestamp != "" {
formattedCommitTimestamp := func(commitTimestamp string) string {
epochSeconds, err := strconv.ParseInt(commitTimestamp, 10, 64)
if err != nil {
return ""
}
t := time.Unix(epochSeconds, 0)
return t.Format(time.RFC3339)
}(commitTimestamp)
if formattedCommitTimestamp != "" {
parts = append(parts, fmt.Sprintf("commitTimestamp=%s", formattedCommitTimestamp))
}
}
fmt.Println(strings.Join(parts, " "))
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func doMain(cCtx *cli.Context) error {
renderContext, err := mdbook.ParseRenderContextFromReader(os.Stdin)
if err != nil {
return err
}
jsonBytes, err := json.Marshal(renderContext.Config.Output["asciidoc"])
if err != nil {
return err
}
var config renderer.Config
err = json.Unmarshal(jsonBytes, &config)
if err != nil {
return err
}
err = renderer.Render(renderContext, config)
if err != nil {
return err
}
return nil
}