-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (143 loc) · 4.13 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"asciidoc2md/parser"
"asciidoc2md/settings"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"context"
"github.com/alecthomas/kong"
"github.com/fatih/color"
"io/ioutil"
stdLog "log"
"os"
"path/filepath"
)
var log slog.Logger //global logger
func initLog(verbose bool) {
//os.Setenv("FORCE_COLOR", "TRUE")
if verbose {
log = sloghuman.Make(color.Output).Leveled(slog.LevelDebug)
return
}
log = sloghuman.Make(color.Output)
stdLog.SetOutput(slog.Stdlib(context.Background(), log).Writer())
}
type CLI struct {
Debug bool `help:"Debug mode."`
Config string `help:"Configuration file." short:"c" type:"existingfile"`
Slug string `optional help:"A template for split file name. Output files would have names like <slug>_[1...N].md." default:"part"`
SplitLevel int `optional help:"A level of the headers to split a file at." default:2`
Dump string `help:"Write parsed document to file."`
ArtifactsDir string `optional name:"art" type:"existingdir" default:"." help:"Artifacts folder where asciidoc2md looks for .idmap files."`
GenMap struct {
Input string `arg help:"*.adoc file to process." type:"existingfile" name:"file.adoc"`
WriteNav string `optional help:"Path to mkdocs.yml file to write navigation index." type:"existingfile"`
} `cmd:"" help:"Generate <file.adoc.idmap> file."`
Convert struct {
Input string `arg help:"*.adoc file to process." type:"existingfile" name:"file.adoc"`
Out string `help:"Output directory." short:"o" type:"existingdir"`
ImagePath string `help:"A relative path to the images folder." short:"im" default:"images/" `
} `cmd:"" help:"Convert <file.adoc> into markdown."`
}
var cli CLI
//asciidoc2md input_file output_path output_file_slug image_path
// go run asciidoc2md data/adm.adoc /mnt/c/personal/mkdocs/my-project/docs/ adm/adm images/
func main() {
ctx := kong.Parse(&cli,
kong.Name("asciidoc2md"),
kong.Description("Asciidoc to markdown file converter."),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
Summary: true,
}))
if cli.Debug {
initLog(true)
} else {
initLog(false)
}
switch ctx.Command() {
case "gen-map <file.adoc>":
genIdMap()
case "convert <file.adoc>":
convert()
}
}
func initConfigCLI(configFile string, opts *CLI) *settings.Config {
var config *settings.Config
if configFile != "" {
str, err := ioutil.ReadFile(configFile)
if err != nil {
panic(err)
}
config, err = settings.Parse(str)
if err != nil {
panic(err)
}
} else {
config = &settings.Config{}
}
if opts != nil {
config.ArtifactsDir = opts.ArtifactsDir
config.InputFile = opts.Convert.Input
if opts.GenMap.Input != "" {
config.InputFile = opts.GenMap.Input
}
config.NavFile = opts.GenMap.WriteNav
}
return config
}
func genIdMap() {
log.Debug(context.Background(), "genIdMap")
splitter := initSplitter(cli.GenMap.Input,
"",
"",
cli.Slug,
cli.SplitLevel,
cli.Dump,
initConfigCLI(cli.Config, &cli),
log)
err := splitter.GenerateIdMap()
if err != nil {
panic(err)
}
}
func initSplitter(inputFile string, imagePath string, outPath string, slug string, splitLvl int, dumpFile string, conf *settings.Config, log slog.Logger) *FileSplitter {
ctx := context.Background()
log.Debug(ctx, "convert")
log.Info(ctx, "input file", slog.F("name", inputFile))
log.Info(ctx, "image path", slog.F("path", imagePath))
input, err := ioutil.ReadFile(inputFile)
if err != nil {
panic(err)
}
dir, name := filepath.Split(inputFile)
p := parser.New(string(input), func(name string) ([]byte, error) {
return ioutil.ReadFile(filepath.Join(dir, name))
}, log)
doc, err := p.Parse(name)
if err != nil {
panic(err)
}
if dumpFile != "" {
err = ioutil.WriteFile(dumpFile, []byte(doc.String()), os.ModePerm)
if err != nil {
panic(err)
}
}
return NewFileSplitter(doc, slug, conf, outPath, splitLvl, log)
}
func convert() {
splitter := initSplitter(cli.Convert.Input,
cli.Convert.ImagePath,
cli.Convert.Out,
cli.Slug,
cli.SplitLevel,
cli.Dump,
initConfigCLI(cli.Config, &cli),
log)
err := splitter.RenderMarkdown(cli.Convert.ImagePath)
if err != nil {
panic(err)
}
}