-
Notifications
You must be signed in to change notification settings - Fork 1
/
site.go
142 lines (121 loc) · 3.31 KB
/
site.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/atmoz/ply/fileutil"
)
const defaultTarget string = "ply.build"
const defaultIgnore string = `/\.`
const defaultFileMode os.FileMode = 0644
const defaultDirMode os.FileMode = 0755
type Site struct {
Pages []*Page
SourcePath string
TargetPath string
Tags map[string][]*Page
plyPath string
includeMarkdown bool
includeTemplate bool
prettyUrls bool
keepLinks bool
allowExec bool
copyOptions *fileutil.CopyOptions
templates map[string]*PlyTemplate
}
func (site *Site) Init() (err error) {
if site.SourcePath == "" {
if site.SourcePath, err = os.Getwd(); err != nil {
fail(err)
}
}
if site.SourcePath, err = filepath.Abs(site.SourcePath); err != nil {
return err
}
if site.TargetPath == "" {
site.TargetPath = filepath.Join(site.SourcePath, defaultTarget)
}
if site.TargetPath, err = filepath.Abs(site.TargetPath); err != nil {
return err
}
if site.SourcePath == site.TargetPath {
return errors.New("Target path can't be the same as source path")
}
if site.copyOptions == nil {
site.copyOptions = new(fileutil.CopyOptions)
site.copyOptions.IgnoreRegex = append(
site.copyOptions.IgnoreRegex, regexp.MustCompile(defaultIgnore))
}
site.copyOptions.IgnoreRegex = append(
site.copyOptions.IgnoreRegex, regexp.MustCompile("^"+regexp.QuoteMeta(site.TargetPath)))
if site.plyPath == "" {
site.plyPath = filepath.Join(site.SourcePath, ".ply")
}
site.Tags = make(map[string][]*Page)
site.templates = make(map[string]*PlyTemplate)
return nil
}
func (site *Site) Build() error {
if site.SourcePath != site.TargetPath {
if err := fileutil.CopyDirectory(site.SourcePath, site.TargetPath, site.copyOptions); err != nil {
return err
}
if info, err := os.Stat(site.plyPath); !os.IsNotExist(err) && info.IsDir() {
if err := fileutil.CopyDirectory(site.plyPath, site.TargetPath, nil); err != nil {
return err
}
}
}
if err := filepath.Walk(site.TargetPath, site.buildWalk); err != nil {
return err
}
for _, p := range site.Pages {
if content, err := p.parse(); err == nil {
if site.prettyUrls {
if err = os.MkdirAll(p.Path.AbsDir, defaultDirMode); err != nil {
return err
}
}
if err := ioutil.WriteFile(p.Path.Abs, content, defaultFileMode); err != nil {
return err
}
fmt.Println("Page:", p.Path.Abs)
} else {
fmt.Println(err)
os.Exit(1)
}
}
if err := filepath.Walk(site.TargetPath, site.cleanWalk); err != nil {
return err
}
return nil
}
func (site *Site) buildWalk(path string, f os.FileInfo, err error) error {
basename := filepath.Base(path)
if strings.HasSuffix(basename, ".md") {
if page, err := NewPage(site, path); err == nil {
site.Pages = append(site.Pages, page)
} else {
return err
}
} else if basename == "ply.template" {
if template, err := NewPlyTemplate(site, path); err != nil {
return err
} else {
site.templates[filepath.Dir(path)] = template
}
}
return nil
}
func (site *Site) cleanWalk(path string, f os.FileInfo, err error) error {
cleanMarkdown := !site.includeMarkdown && strings.HasSuffix(path, ".md")
cleanTemplate := !site.includeTemplate && filepath.Base(path) == "ply.template"
if cleanMarkdown || cleanTemplate {
os.Remove(path)
}
return nil
}