forked from niklasfasching/go-org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
86 lines (80 loc) · 1.76 KB
/
page.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
package blorg
import (
"fmt"
"html/template"
"os"
"time"
"github.com/niklasfasching/go-org/org"
)
type Page struct {
*Config
Document *org.Document
Info os.FileInfo
PermaLink string
Date time.Time
Content template.HTML
BufferSettings map[string]string
}
func NewPage(c *Config, path string, info os.FileInfo) (*Page, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
d := c.OrgConfig.Parse(f, path)
content, err := d.Write(getWriter())
if err != nil {
return nil, err
}
date, err := time.Parse("2006-01-02", d.Get("DATE"))
if err != nil {
date, _ = time.Parse("2006-01-02", "1970-01-01")
}
return &Page{
Config: c,
Document: d,
Info: info,
Date: date,
Content: template.HTML(content),
BufferSettings: d.BufferSettings,
}, nil
}
func (p *Page) Render(path string) error {
if p.BufferSettings["DRAFT"] != "" {
return nil
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
templateName := "item"
if v, ok := p.BufferSettings["TEMPLATE"]; ok {
templateName = v
}
t := p.Template.Lookup(templateName)
if t == nil {
return fmt.Errorf("cannot render page %s: unknown template %s", p.Info.Name(), templateName)
}
return t.Execute(f, toMap(p.BufferSettings, p))
}
func (p *Page) Summary() template.HTML {
for _, n := range p.Document.Nodes {
switch n := n.(type) {
case org.Block:
if n.Name == "SUMMARY" {
w := getWriter()
org.WriteNodes(w, n.Children...)
return template.HTML(w.String())
}
}
}
for i, n := range p.Document.Nodes {
switch n.(type) {
case org.Headline:
w := getWriter()
org.WriteNodes(w, p.Document.Nodes[:i]...)
return template.HTML(w.String())
}
}
return ""
}