/
main.go
231 lines (202 loc) · 6.32 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/gSpera/sMark/output/html"
"github.com/gSpera/sMark/output/prettify"
"github.com/gSpera/sMark/output/telegraph"
"github.com/gSpera/sMark/parser"
"github.com/gSpera/sMark/token"
sMark "github.com/gSpera/sMark/utils"
tgraph "github.com/toby3d/telegraph"
fsnotify "gopkg.in/fsnotify.v1"
)
//ProgramName is the name of the program.
const ProgramName = "sMark"
func main() {
//Logger
log.SetPrefix(ProgramName + ": ")
log.SetFlags(log.Ltime | log.Lshortfile)
//Options from Command Line
inputFile := flag.String("i", "-", "Input file, - for stdin")
outputFile := flag.String("o", "out.html", "Output file")
newLine := flag.Bool("newline", true, "Include newlines as in source")
customCSS := flag.String("css", "", "A custom css file")
inlineCSS := flag.String("inline-css", "", "Inline CSS")
enableFont := flag.Bool("font", true, "Enable a default font")
onlyBody := flag.Bool("only-body", false, "Output only the html boy and not the whole page")
title := flag.String("title", "", "The title of the output document")
tabWidth := flag.Uint("tabs-width", 4, "The width (in spaces) of one tab")
verbose := flag.Bool("verbose", false, "Output logging")
htmlOut := flag.Bool("html", true, "Output HTML")
telegraphOut := flag.Bool("telegraph", false, "Output to Telegra.ph")
prettify := flag.String("prettify", "", "Prettify")
watch := flag.Bool("watch", false, "Watches the file for changes (CTRL + c to exit)")
enableJs := flag.Bool("enable-js", true, "Enable JavaScript in HTML")
flag.Parse()
options := sMark.Options{
String: map[string]string{
"InputFile": *inputFile,
"OutputFile": *outputFile,
"CustomCSS": *customCSS,
"InlineCSS": *inlineCSS,
"Title": *title,
"Prettify": *prettify,
},
Bool: map[string]bool{
"NewLine": *newLine,
"EnableFont": *enableFont,
"OnlyBody": *onlyBody,
"Verbose": *verbose,
"HTMLOut": *htmlOut,
"TelegraphOut": *telegraphOut,
"Watch": *watch,
"EnableJS": *enableJs,
},
Generic: map[string]interface{}{
"TabWidth": *tabWidth, //Uint
},
}
if !options.Bool["Verbose"] {
log.SetOutput(ioutil.Discard)
}
if options.Bool["Watch"] {
if options.String["InputFile"] == "-" {
fmt.Fprintln(os.Stderr, "Could not watch stdin")
return
}
fmt.Println("Watching File, use CTRL + c to stop")
options, _ = compile(options)
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create watcher: %v", err)
}
defer watcher.Close()
err = watcher.Add(options.String["InputFile"])
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot watch %s: %v\n", options.String["InputFile"], err)
}
if tmpl, ok := options.String["TemplateFile"]; ok {
if err := watcher.Add(options.String["TemplateFile"]); err != nil {
fmt.Fprintf(os.Stderr, "Could not Watch TemplateFile: %s: %v\n", tmpl, err)
} else {
fmt.Fprintf(os.Stderr, "Watching TemplateFile: %s\n", tmpl)
}
}
for {
select {
case event := <-watcher.Events:
if event.Op == fsnotify.Remove || event.Op == fsnotify.Rename {
fmt.Fprintln(os.Stderr, "Quitting, file is being removed or renamed")
return
}
if event.Op != fsnotify.Write {
continue
}
compile(options)
case err := <-watcher.Errors:
fmt.Fprintln(os.Stderr, "Error in watcher:", err)
}
}
}
compile(options)
}
func compile(options sMark.Options) (sMark.Options, []token.ParagraphToken) {
log.Println("Start Compilation")
input, err := streamFromFilename(options.String["InputFile"])
if os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "Error: File doesn't exist")
os.Exit(1)
}
if err != nil {
log.Fatalf("Error: Could not open file: %v", err)
}
log.Println("Filename: ", options.String["InputFile"])
reader := bufio.NewReader(input)
log.Println("Parsing")
tokenList, err := parser.ParseReader(reader)
if err != nil {
log.Fatalf("Error: Could not parse: %v", err)
}
log.Println("Parsing DONE")
log.Println("Updating Options")
log.Println("\t- From Paragraphs")
newOptions, tokenList := parser.OptionsFromParagraphs(tokenList)
flOptions := newOptions
options.Update(newOptions)
log.Println("\t- From Paragraphs DONE")
log.Println("\t- From Command Line")
for _, arg := range flag.Args() {
k, v := sMark.ParseOption(arg)
options.Insert(k, v)
}
log.Println("\t- From Command Line DONE")
log.Println("Updating Options DONE")
log.Println("Selecting Title")
if options.String["Title"] == "" {
log.Println("Changing Title")
title := parser.TitleFromParagraph(tokenList)
options.String["Title"] = title
}
log.Println("Selecting Title DONE")
//HTML Output Engine
if options.Bool["HTMLOut"] {
log.Println("Outputting HTML")
ioutil.WriteFile(options.String["OutputFile"], htmlout.ToString(tokenList, options), os.ModePerm)
log.Println("Outputting HTML DONE")
}
//Telegraph Output Engine
if options.Bool["TelegraphOut"] {
log.Println("Outputting Telegraph")
page := telegraphout.ToString(tokenList, options)
log.Println("Title:", page.Title)
var accessToken string
fmt.Print("Insert Access Token: ")
fmt.Scan(&accessToken)
acc := tgraph.Account{
AccessToken: accessToken,
}
pagePubblished, err := acc.CreatePage(&page, false)
if err != nil {
fmt.Fprintln(os.Stderr, "Cannot create page:", err)
log.Fatalf("Error: Could not create page: %v", err)
}
log.Println("Outputting Telegraph DONE")
fmt.Println("Title:", pagePubblished.Title)
fmt.Println("URL:", pagePubblished.URL)
}
//sMark Output Engine
if options.String["Prettify"] != "" {
log.Println("Outputting sMark")
//Prettifier doesn't need all the Options but only from the file
data, err := prettify.Output(tokenList, flOptions)
if err != nil {
log.Fatalf("Could not compile to sMark: %v\n", err)
}
f, err := os.Create(options.String["Prettify"])
if err != nil {
log.Fatalf("Could not create file %s: %v", options.String["Prettify"], err)
}
defer f.Close()
_, err = f.Write(data)
if err != nil {
log.Fatalf("Could not output to file: %v\n", err)
}
log.Println("Outputting sMark DONE")
}
return options, tokenList
}
func streamFromFilename(filename string) (*os.File, error) {
if filename == "-" {
return os.Stdin, nil
}
fl, err := os.Open(filename)
if err != nil {
return nil, err
}
return fl, nil
}