-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
169 lines (138 loc) · 3.58 KB
/
parser.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
package zmdocs
import (
"fmt"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
)
// A parser loads files from the provided config
type Parser struct {
Config *ParserConfig
Files []*File
}
// Returns a new Parser instance from the provided config
func NewParser(config *ParserConfig) *Parser {
log.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
EnvironmentOverrideColors: true,
FullTimestamp: true,
QuoteEmptyFields: true,
})
if os.Getenv("ZMDOCS_DEBUG") == "true" {
log.SetLevel(logrus.DebugLevel)
} else {
log.SetLevel(logrus.InfoLevel)
}
p := Parser{
Config: config,
Files: make([]*File, 0),
}
log.WithFields(logrus.Fields{
"rootDir": config.RootDir,
"outDir": config.OutDir,
"baseUrl": config.BaseURL,
"repo": config.Repo,
"siteTitle": config.SiteTitle,
}).Debug("creating new parser")
return &p
}
// Load configuration from a file and creates a new parser with it
func NewParserFromConfigFile(configPath string) (*Parser, error) {
if config, err := NewConfigFromFile(configPath); err != nil {
return nil, err
} else {
return NewParser(config), nil
}
}
// Returns a Renderer instance from the parsed files
// This must be ran after a successful LoadSourceFiles so there are files to render
func (p *Parser) Renderer() (*Renderer, error) {
if p.Config.BaseURL != "" {
for _, it := range p.Config.MenuItems {
p.handleHomePage(it)
}
}
rndCtxs := make([]*RenderContext, 0)
for _, f := range p.Files {
if ctx, err := f.RenderContext(p); err != nil {
return nil, err
} else {
rndCtxs = append(rndCtxs, ctx)
}
if f.AddToMenu {
f.AppendToMenu(p.Config.MenuItems)
}
}
rnd := &Renderer{
MenuItems: p.Config.MenuItems,
Contexts: rndCtxs,
Templates: p.Config.Templates,
}
return rnd, nil
}
// Load all source files
func (p *Parser) LoadSourceFiles() error {
log.Info("loading source files")
log.Debug("Loading static files")
if err := p.loadStaticFiles(); err != nil {
return err
}
log.Debug("Done loading static files")
log.Debug("Loading glob files")
if err := p.loadGlobFiles(); err != nil {
return err
}
log.Debug("Done loading glob files")
log.Infof("loaded %d files", len(p.Files))
return nil
}
func (p *Parser) loadStaticFiles() error {
for _, pg := range p.Config.Pages {
g := filepath.Join(p.Config.RootDir, pg.SourceFile)
file := File{
BasePage: *&pg.BasePage,
Title: pg.Title,
}
file.SourceFile = g
p.Files = append(p.Files, &file)
}
return nil
}
func (p *Parser) loadGlobFiles() error {
for i, ap := range p.Config.AutoPages {
g := filepath.Join(p.Config.RootDir, ap.SourceGlob)
log.WithFields(logrus.Fields{
"pattern": ap.Pattern,
"absPath": g,
}).Debug("processing page pattern")
if patternMatches, err := GetPatternMatches(g, ap.Pattern); err != nil {
return fmt.Errorf("unable to process page pattern #%d: %s", i, err.Error())
} else {
log.WithFields(logrus.Fields{
"pattern": ap.Pattern,
}).Debugf("found %d matches", len(patternMatches))
for _, pm := range patternMatches {
if file, err := GetFileForPatternMatch(ap, pm); err != nil {
return fmt.Errorf("unable to process file %s: \n\t%s", pm.Path, err.Error())
} else {
p.Files = append(p.Files, file)
}
}
}
}
return nil
}
func (p *Parser) handleHomePage(it *MenuItem) {
if it.Group {
for _, iit := range it.Items {
p.handleHomePage(iit)
}
return
}
if it.Link == "" || it.Link == "/" {
log.WithFields(logrus.Fields{
"title": it.Title,
}).Debug("setting page link to baseURL")
it.Link = p.Config.BaseURL
}
}