This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
wikipage.go
145 lines (124 loc) · 2.84 KB
/
wikipage.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
package main
import (
"github.com/OUCC/SyaroNote/syaro/markdown"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const (
FOLDER_MD = "_.md"
SIDEBAR_MD = "_Sidebar.md"
)
type WikiPage struct {
WikiFile
Name string
Meta map[string]string
Title string
Author string
Date string
Aliases []string
Tags []string
MathJax string // TODO
Contents template.HTML
Sidebar template.HTML
TOC template.HTML
IsRoot bool
Parent WikiFile
BreadCrumb []WikiFile
Folders []WikiFile
MdFiles []WikiFile
OtherFiles []WikiFile
}
func loadPage(wf WikiFile) (WikiPage, error) {
wp := WikiPage{WikiFile: wf}
wp.Sidebar = loadSidebar()
var b []byte
switch wf.fileType {
case WIKIFILE_FOLDER:
// load _.md
wf_, err := loadFile(filepath.Join(wf.WikiPath, FOLDER_MD))
if err == nil { // _.md found
b, err = wf_.read()
if err != nil {
return wp, err
}
}
// file list
fis := wf.files()
wp.Folders = make([]WikiFile, 0, len(fis))
wp.MdFiles = make([]WikiFile, 0, len(fis))
wp.OtherFiles = make([]WikiFile, 0, len(fis))
for _, fi := range fis {
if strings.HasPrefix(fi.Name(), ".") { // is hidden file/dir
continue
}
switch fi.fileType {
case WIKIFILE_FOLDER:
wp.Folders = append(wp.Folders, fi)
case WIKIFILE_MARKDOWN:
wp.MdFiles = append(wp.MdFiles, fi)
case WIKIFILE_OTHER:
wp.OtherFiles = append(wp.OtherFiles, fi)
}
}
case WIKIFILE_MARKDOWN:
var err error
b, err = wf.read()
if err != nil {
return wp, err
}
}
wp.Contents = template.HTML(markdown.Convert(b))
wp.TOC = template.HTML(markdown.TOC(b))
// meta datas
wp.Meta = markdown.Meta(b)
wp.Title = wp.Meta["title"]
wp.Author = wp.Meta["author"]
wp.Date = wp.Meta["date"]
wp.Aliases = splitCommma(wp.Meta["alias"] + "," + wp.Meta["aliases"])
wp.Tags = splitCommma(wp.Meta["tag"] + "," + wp.Meta["tags"])
var ok bool
wp.Parent, ok = wf.parent()
wp.IsRoot = !ok
if wp.IsRoot {
wp.Name = "/"
} else {
wp.Name = removeExt(wp.WikiFile.Name())
}
// breadcrumb list
var bc []WikiFile
for wf2, ok := wf.parent(); ok; wf2, ok = wf2.parent() {
bc = append(bc, wf2)
}
// remove wiki root
if len(bc) > 1 {
l := len(bc) - 1 // parent count
wp.BreadCrumb = make([]WikiFile, l+1)
// reverse list
for i := 0; i < l; i++ {
wp.BreadCrumb[i] = bc[l-i-1]
}
wp.BreadCrumb[l] = wp.WikiFile // self
} else if len(bc) == 1 { // file under wiki root
wp.BreadCrumb = []WikiFile{wp.WikiFile}
} else {
wp.BreadCrumb = nil
}
return wp, nil
}
func loadSidebar() (html template.HTML) {
path := filepath.Join(setting.wikiRoot, SIDEBAR_MD)
if _, err := os.Stat(path); err != nil {
return // not found
}
log.Debug("%s found", SIDEBAR_MD)
b, err := ioutil.ReadFile(path)
if err != nil {
log.Error(err.Error())
return
}
html = template.HTML(markdown.Convert(b))
return
}