25
25
case kind: ItemKind
26
26
of itemDir:
27
27
contents: seq [DirTreeNode ]
28
+ html: string
28
29
style: string
29
30
config: JsonNode
30
31
of itemFile:
31
32
fileKind: FileKind
33
+ label: string
32
34
33
35
34
36
proc convertMarkdownToNercPage (tree: DirTreeNode )
66
68
fsTree: DirTreeNode = DirTreeNode (depth: 0 , name: " Main" , path: " ." , kind: itemDir)
67
69
68
70
71
+ fsTree.html = htmlTemplate
72
+ fsTree.style = defaultStyle
73
+ fsTree.config = parseJson (defaultJson)
74
+
75
+
76
+ proc getConfig (parentDir: DirTreeNode , key: string ): JsonNode {.inline .} =
77
+ if not parentDir.config.isNil and parentDir.config.hasKey (key):
78
+ return parentDir.config[key]
79
+
80
+ if parentDir.parent != nil :
81
+ return getConfig (parentDir.parent, key)
82
+
83
+ return %* " "
84
+
69
85
70
86
proc removeSuffixInsensitive (s, suffix: string ): string =
71
87
if s.toLowerAscii ().endsWith (suffix.toLowerAscii ()):
@@ -74,7 +90,7 @@ proc removeSuffixInsensitive(s, suffix: string): string =
74
90
75
91
76
92
proc convertMarkdownToNercPage (tree: DirTreeNode ) =
77
- echo tree.path[2 ..^ 1 ] & " : " & tree.name & " \n\n\n "
93
+ echo tree.path[2 ..^ 1 ] & " : " & tree.name & " \n "
78
94
if tree.kind == itemDir: return
79
95
80
96
var outPath: string = tree.path[2 ..^ 1 ]
@@ -85,9 +101,15 @@ proc convertMarkdownToNercPage(tree: DirTreeNode) =
85
101
86
102
let mdFile = readFile (tree.path[2 ..^ 1 ])
87
103
88
- var htmlTxt = htmlTemplate.replace (ContentTag ,markdown (mdFile))
89
- htmlTxt = htmlTxt.replace (SidebarTag ,genSidebar (fsTree,tree))
90
-
104
+ var htmlTxt = htmlTemplate
105
+ if htmlTxt.contains (PageTitleTag ): htmlTxt = htmlTxt.replace (PageTitleTag , tree.parent.getConfig (" page title" ).getStr () & " - " & tree.name)
106
+ if htmlTxt.contains (SiteTitleTag ): htmlTxt = htmlTxt.replace (SiteTitleTag , tree.parent.getConfig (" site title" ).getStr ())
107
+ if htmlTxt.contains (SubtitleTag ): htmlTxt = htmlTxt.replace (SubtitleTag , tree.parent.getConfig (" subtitle" ).getStr ())
108
+ if htmlTxt.contains (SidebarTag ): htmlTxt = htmlTxt.replace (SidebarTag , genSidebar (fsTree, tree))
109
+ if htmlTxt.contains (ContentTag ): htmlTxt = htmlTxt.replace (ContentTag , markdown (mdFile))
110
+ if htmlTxt.contains (FooterLeftTag ): htmlTxt = htmlTxt.replace (FooterLeftTag , tree.parent.getConfig (" footer left" ).getStr ())
111
+ if htmlTxt.contains (FooterRightTag ): htmlTxt = htmlTxt.replace (FooterRightTag , tree.parent.getConfig (" footer right" ).getStr ())
112
+
91
113
writefile (outPath, htmlTxt)
92
114
93
115
@@ -97,24 +119,32 @@ proc buildDirTree(node: DirTreeNode, depth: uint) =
97
119
for kind, name in walkDir (path, relative = true ):
98
120
if name[0 ] == '.' : continue # Skip hidden files and directories (such as .git)
99
121
if kind == pcFile:
100
- var new_node: DirTreeNode = DirTreeNode (depth: depth, kind: itemFile, name: name.split ('.' )[0 ].replace ('_' , ' ' ), path: path & '/' & name)
122
+ var new_node: DirTreeNode = DirTreeNode (depth: depth, kind: itemFile, name: name.split ('.' )[0 ].replace ('_' , ' ' ), path: path & '/' & name, parent: node )
101
123
102
124
if name.toLowerAscii ().endsWith (" .md" ):
103
125
new_node.fileKind = fileMarkdown
104
126
# convertMarkdownToNercPage(path)
105
127
elif name.toLowerAscii () == " config.json" :
106
- new_node.fileKind = fileJSON
128
+ echo new_node.path[2 ..^ 1 ]
129
+ var file: string = readFile (new_node.path[2 ..^ 1 ])
130
+ echo file
131
+ node.config = parseJson (file)
132
+ continue
107
133
elif name.toLowerAscii () == " template.htm" :
108
- new_node.fileKind = fileTemplate
109
- elif name.toLowerAscii ().endsWith (" .htm" ) || name.toLowerAscii ().endsWith (" .html" ):
134
+ node.html = readFile (new_node.path)
135
+ continue
136
+ elif name.toLowerAscii () == " styles.css" :
137
+
138
+ continue
139
+ elif name.toLowerAscii ().endsWith (" .html" ):
110
140
new_node.fileKind = fileHTML
111
141
else : continue
112
142
113
143
# echo "\t", path & "/" & name
114
144
node.contents.add (new_node)
115
145
116
146
elif kind == pcDir:
117
- var new_node: DirTreeNode = DirTreeNode (depth: depth, kind: itemDir, name: name, path: path & '/' & name)
147
+ var new_node: DirTreeNode = DirTreeNode (depth: depth, kind: itemDir, name: name, path: path & '/' & name, parent: node )
118
148
# echo path & "/" & name
119
149
buildDirTree (new_node, depth+ 1 )
120
150
node.contents.add (new_node)
@@ -126,7 +156,8 @@ proc printTree(tree: DirTreeNode) =
126
156
case tree.fileKind
127
157
of fileMarkdown: echo tree.depth, repeat ('\t ' , tree.depth), tree.name, " : Markdown"
128
158
of fileJSON: echo tree.depth, repeat ('\t ' , tree.depth), tree.name, " : JSON"
129
- of fileTemplate: echo tree.depth, repeat ('\t ' , tree.depth), tree.name, " : HTML"
159
+ of fileTemplate: echo tree.depth, repeat ('\t ' , tree.depth), tree.name, " : Template"
160
+ of fileHTML: echo tree.depth, repeat ('\t ' , tree.depth), tree.name, " : HTML"
130
161
131
162
elif tree.kind == itemDir:
132
163
echo repeat ('\t ' , tree.depth), tree.path, " : " , tree.name, " : " , tree.contents.len ()
@@ -140,17 +171,19 @@ proc genSidebar(tree: DirTreeNode, currentItem: DirTreeNode): string =
140
171
var sidebar: string
141
172
142
173
if tree.kind == itemFile:
143
- if tree.fileKind != fileMarkdown: return " "
144
-
145
174
var
146
175
name = tree.name
147
176
path = tree.path
177
+
178
+ if tree.fileKind != fileMarkdown and tree.fileKind != fileHTML: return " "
179
+
180
+ if tree.fileKind == fileMarkdown:
181
+ path.removeSuffix (" md" )
182
+ path = path & " htm"
183
+
184
+ if " readme" == toLowerAscii (name): return " "
148
185
149
- path.removeSuffix (" md" )
150
- path = path & " htm"
151
-
152
- if " readme" == toLowerAscii (name): return " "
153
- if " index" == name: return " "
186
+ if " index" == toLowerAscii (name): return " "
154
187
if tree == currentItem: name = " >> " & name & " <<"
155
188
156
189
return repeat ('\t ' , tree.depth) & " <li class=\" page\" ><a href=\" " & path & " \" >" & name & " </a></li>\n "
@@ -176,14 +209,13 @@ proc genSidebar(tree: DirTreeNode, currentItem: DirTreeNode): string =
176
209
177
210
178
211
proc populateDirs (treeRoot: DirTreeNode ) =
179
-
180
212
for node in treeRoot.contents:
181
213
if node.kind == itemFile:
182
214
if node.fileKind != fileMarkdown: continue
183
215
convertMarkdownToNercPage (node)
184
216
elif node.kind == itemDir:
185
217
populateDirs (node)
186
-
218
+
187
219
188
220
proc main () =
189
221
let args = commandLineParams ()
0 commit comments