-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
80 lines (64 loc) · 1.75 KB
/
file.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
package zmdocs
import (
"github.com/russross/blackfriday"
"html/template"
"io/ioutil"
)
var blackFridayExtensions = blackfriday.WithExtensions(blackfriday.CommonExtensions | blackfriday.AutoHeadingIDs | blackfriday.Autolink | blackfriday.Footnotes)
var blackFridayRndOpts = blackfriday.HTMLRendererParameters{
Flags: blackfriday.CommonHTMLFlags | blackfriday.FootnoteReturnLinks,
}
type File struct {
BasePage
Title string
}
func (f *File) RenderContext(p *Parser) (*RenderContext, error) {
var fc []byte
var err error
if fc, err = ioutil.ReadFile(f.SourceFile); err != nil {
return nil, err
}
rnd := blackfriday.NewHTMLRenderer(blackFridayRndOpts)
o := blackfriday.Run(fc, blackFridayExtensions, blackfriday.WithRenderer(rnd))
if f.Title == "" {
mp := blackfriday.New(blackFridayExtensions, blackfriday.WithRenderer(rnd))
a := mp.Parse(fc)
it := a.FirstChild
for it != nil && f.Title == "" {
if it.Type == blackfriday.Heading && it.Level == 1 && it.FirstChild != nil && it.FirstChild.Type == blackfriday.Text {
f.Title = string(it.FirstChild.Literal)
break
}
it = it.Next
}
}
ctx := NewRenderContext(f, p.Config, template.HTML(o))
if f.Path == "" || f.Path == "/" {
if p.Config.BaseURL != "" {
ctx.Link = p.Config.BaseURL
}
}
return ctx, nil
}
func (f *File) MenuItem() *MenuItem {
return &MenuItem{
Name: f.Name,
Title: f.Title,
Link: f.Path,
}
}
func (f *File) AppendToMenu(menuItems []*MenuItem) {
menuItem := f.MenuItem()
if f.MenuGroup != "" {
for _, mit := range menuItems {
if mit.Group && mit.Name == f.MenuGroup {
if mit.Items == nil {
mit.Items = make([]*MenuItem, 0)
}
mit.Items = append(mit.Items, menuItem)
}
}
} else {
menuItems = append(menuItems, menuItem)
}
}