-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
128 lines (103 loc) · 3.11 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
//
// VimWiki-GoDown
//
// A Vimwiki Markdown to HTML converter written in Golang.
// See README.md for more details.
//
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
v "github.com/maqiv/vimwiki-godown/vimwiki"
"github.com/russross/blackfriday"
)
func main() {
var targetFilePath, mdOutput, docTitle string
var htmlFlags, mdExtensions int
var err error
var targetFile *os.File
var renderer blackfriday.Renderer
fl := parseArguments(os.Args)
// Check if file already exists and overwrite flag is not set
targetFilePath = v.BuildTargetFilepath(fl.InputFile, fl.OutputDirectory)
if _, err = os.Stat(targetFilePath); os.IsNotExist(err) && !fl.Force {
fmt.Println("Conversion of file %v aborted: File does exist and force flag is set to 0.", targetFilePath)
// Exit with error code different from 0
os.Exit(1)
}
htmlFlags = 0 |
blackfriday.HTML_COMPLETE_PAGE |
blackfriday.HTML_HREF_TARGET_BLANK
mdExtensions = 0 |
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS
// Read input file in markdown format
mdOutput = readFile(fl.InputFile)
// Process markdown content
mdOutput = v.ProcessRelativeLinks(mdOutput, fl.UrlBasePrefix)
mdOutput = v.ProcessHtmlCheckboxes(mdOutput)
// Set document title
docTitle = v.FindPageTitle(mdOutput)
if len(docTitle) == 0 {
docTitle = fl.InputFile
}
renderer = blackfriday.HtmlRenderer(htmlFlags, docTitle, fl.CssFile)
// Convert markdown content to html
htmlOutputRaw := blackfriday.Markdown([]byte(mdOutput), renderer, mdExtensions)
fmt.Println(string(htmlOutputRaw))
// Write processed and converted html content to html file
if targetFile, err = os.OpenFile(targetFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err != nil {
panic(err)
}
if _, err = targetFile.Write(htmlOutputRaw); err != nil {
panic(err)
}
// Whey, no errors occurred
os.Exit(0)
}
// Commandline arguments are parsed like defined by vimwiki
// documentation at:
// https://github.com/vimwiki/vimwiki/blob/dev/doc/vimwiki.txt#L2091
//
// Additionally a url prefix is parsed (UrlBasePrefix) that is prefixed to
// all relative urls later.
// Related feature pull request for additional parameters:
// https://github.com/vimwiki/vimwiki/pull/348
func parseArguments(args []string) *v.Flags {
f := new(v.Flags)
frc, err := strconv.ParseBool(args[1])
if err != nil {
panic(err)
}
f.Force = frc
f.Syntax = args[2]
f.Extension = args[3]
f.OutputDirectory = args[4]
f.InputFile = args[5]
f.CssFile = args[6]
f.TmplPath = args[7]
f.TmplDefault = args[8]
f.TmplExtension = args[9]
f.RootPath = args[10]
// The "-" (dash) in the parameters that are handed over by Vimwiki means
// that the configuration value is left empty.
if args[11] != "-" {
f.UrlBasePrefix = args[11]
}
return f
}
// readFile reads the Markdown content from the source file
func readFile(filename string) string {
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
content := string(data)
return content
}