-
Notifications
You must be signed in to change notification settings - Fork 16
/
mdoc.go
174 lines (145 loc) · 3.53 KB
/
mdoc.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
/* Documentor generator for magpie. */
package main
import (
"flag"
"fmt"
"io/ioutil"
"magpie/docs"
"magpie/lexer"
"magpie/parser"
"path/filepath"
"strings"
"os"
)
func genDocs(path string, cfg doc.Config, isDir bool) {
if !isDir { //single file
genDoc(path, cfg)
return
}
//processing directory
fd, err := os.Open(path)
if err != nil {
fmt.Errorf("Open directory '%s' failed, reason:%v\n", path, err)
return
}
defer fd.Close()
list, err := fd.Readdir(-1)
if err != nil {
fmt.Errorf("Read directory '%s' failed, reason:%v\n", path, err)
return
}
for _, d := range list {
if strings.HasSuffix(d.Name(), ".mp") {
filename := filepath.Join(path, d.Name())
genDoc(filename, cfg)
}
}
}
func genDoc(filename string, cfg doc.Config) {
wd, err := os.Getwd()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
f, err := ioutil.ReadFile(wd + "/" + filename)
if err != nil {
fmt.Println("magpie: ", err.Error())
os.Exit(1)
}
contents := string(f)
l := lexer.New(filename, contents)
parser.FileLines = strings.Split(contents, "\n")
p := parser.NewWithDoc(l, wd)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
for _, err := range p.Errors() {
fmt.Println(err)
}
os.Exit(1)
}
//generate markdown docs
file := doc.New(filename, program)
md := doc.MdDocGen(file)
//remove the '.mp' extension
genFilename := strings.TrimSuffix(filename, filepath.Ext(filename))
//create markdown file
mdFile := genFilename + ".md"
outMd, err := os.Create(mdFile)
if err != nil {
fmt.Printf("Error creating '%s' file, reason:%v\n", mdFile, err)
os.Exit(1)
}
//generate markdown file
fmt.Fprintln(outMd, md)
outMd.Close()
if cfg.GenHTML == 0 {
return
}
//create html file
htmlFile := genFilename + ".html"
outHtml, err := os.Create(htmlFile)
if err != nil {
fmt.Printf("Error creating '%s' file, reason:%v\n", htmlFile, err)
os.Exit(1)
}
defer outHtml.Close()
html := doc.HtmlDocGen(md, file)
fmt.Fprintln(outHtml, html)
err = os.Remove(mdFile)
if err != nil {
fmt.Printf("Error remove file '%s', reason : %v\n", mdFile, err)
}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [magpie file]\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
var htmlFlag bool
flag.BoolVar(&htmlFlag, "html", false, "Generate html file using github REST API.")
var showSrcFlag bool
flag.BoolVar(&showSrcFlag, "showsource", false, "Show class and function source code in Generated file.")
var cssStyle int
msg := fmt.Sprintf("Set css style(Avialable: 0-%d) to use for html output.", len(doc.BuiltinCssStyle))
flag.IntVar(&cssStyle, "css", 0, msg)
var cssFile string
flag.StringVar(&cssFile, "cssfile", "", "Css file to use for generating html file.")
//parse the command line options
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Fprintln(os.Stderr, "Invalid number of arguments!")
flag.Usage()
}
path := flag.Arg(0)
fi, err := os.Stat(path)
if err != nil {
fmt.Println(err)
return
}
if htmlFlag {
doc.Cfg.GenHTML = 1
if cssFile != "" {
cssContents, err := ioutil.ReadFile(cssFile)
if err != nil {
fmt.Println("Error reading css file: ", err.Error())
//do not exits
} else {
doc.Cfg.CssContents = string(cssContents)
}
}
if cssStyle > len(doc.BuiltinCssStyle)-1 || cssStyle < 0 {
cssStyle = 0 //default
}
doc.Cfg.CssStyle = cssStyle
}
if showSrcFlag {
doc.Cfg.ShowSrcComment = 1
}
switch mode := fi.Mode(); {
case mode.IsDir():
genDocs(path, doc.Cfg, true)
case mode.IsRegular():
genDocs(path, doc.Cfg, false)
}
}