Skip to content

Commit 8a4afac

Browse files
committed
Got config.json working, albeit without any validation.
1 parent 63b34e6 commit 8a4afac

File tree

5 files changed

+67
-43
lines changed

5 files changed

+67
-43
lines changed

README.md

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
# NERC
1+
# nerc
22

33
Web anti-framework in Nim.
44

5-
6-
75
## Rationale:
86

97
Too many web frameworks are written in slow, heavy, bloated interpreted languages, so this is an *anti*-framework written in a fast, lightweight, and *nim*ble compiled language.
108

11-
12-
139
## License:
1410

1511
This code is dedicated to the public domain, but is also made available under the terms of the 0-clause BSD license, as some jurisdictions do not recognize the public domain.
1612

17-
18-
1913
The terms of the 0-clause BSD license are thus:
20-
```
2114

15+
```
2216
Copyright (C) 2025 Christopher DeBoy <chrisxdeboy@gmail.com>
2317
2418
@@ -33,3 +27,4 @@ FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
3327
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
3428
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
3529
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30+
```

src/nerc.nim

+51-19
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ type
2525
case kind: ItemKind
2626
of itemDir:
2727
contents: seq[DirTreeNode]
28+
html: string
2829
style: string
2930
config: JsonNode
3031
of itemFile:
3132
fileKind: FileKind
33+
label: string
3234

3335

3436
proc convertMarkdownToNercPage(tree: DirTreeNode)
@@ -66,6 +68,20 @@ var
6668
fsTree: DirTreeNode = DirTreeNode(depth: 0, name: "Main", path: ".", kind: itemDir)
6769

6870

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+
6985

7086
proc removeSuffixInsensitive(s, suffix: string): string =
7187
if s.toLowerAscii().endsWith(suffix.toLowerAscii()):
@@ -74,7 +90,7 @@ proc removeSuffixInsensitive(s, suffix: string): string =
7490

7591

7692
proc convertMarkdownToNercPage(tree: DirTreeNode) =
77-
echo tree.path[2..^1] & " : " & tree.name & "\n\n\n"
93+
echo tree.path[2..^1] & " : " & tree.name & "\n"
7894
if tree.kind == itemDir: return
7995

8096
var outPath: string = tree.path[2..^1]
@@ -85,9 +101,15 @@ proc convertMarkdownToNercPage(tree: DirTreeNode) =
85101

86102
let mdFile = readFile(tree.path[2..^1])
87103

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+
91113
writefile(outPath, htmlTxt)
92114

93115

@@ -97,24 +119,32 @@ proc buildDirTree(node: DirTreeNode, depth: uint) =
97119
for kind, name in walkDir(path, relative = true):
98120
if name[0] == '.': continue # Skip hidden files and directories (such as .git)
99121
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)
101123

102124
if name.toLowerAscii().endsWith(".md"):
103125
new_node.fileKind = fileMarkdown
104126
#convertMarkdownToNercPage(path)
105127
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
107133
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"):
110140
new_node.fileKind = fileHTML
111141
else: continue
112142

113143
#echo "\t", path & "/" & name
114144
node.contents.add(new_node)
115145

116146
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)
118148
#echo path & "/" & name
119149
buildDirTree(new_node, depth+1)
120150
node.contents.add(new_node)
@@ -126,7 +156,8 @@ proc printTree(tree: DirTreeNode) =
126156
case tree.fileKind
127157
of fileMarkdown: echo tree.depth, repeat('\t', tree.depth), tree.name, " : Markdown"
128158
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"
130161

131162
elif tree.kind == itemDir:
132163
echo repeat('\t', tree.depth), tree.path, " : ", tree.name, " : ", tree.contents.len()
@@ -140,17 +171,19 @@ proc genSidebar(tree: DirTreeNode, currentItem: DirTreeNode): string =
140171
var sidebar: string
141172

142173
if tree.kind == itemFile:
143-
if tree.fileKind != fileMarkdown: return ""
144-
145174
var
146175
name = tree.name
147176
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 ""
148185

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 ""
154187
if tree == currentItem: name = ">> " & name & " <<"
155188

156189
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 =
176209

177210

178211
proc populateDirs(treeRoot: DirTreeNode) =
179-
180212
for node in treeRoot.contents:
181213
if node.kind == itemFile:
182214
if node.fileKind != fileMarkdown: continue
183215
convertMarkdownToNercPage(node)
184216
elif node.kind == itemDir:
185217
populateDirs(node)
186-
218+
187219

188220
proc main() =
189221
let args = commandLineParams()

src/res/config.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"page title": "Default nerc site",
3+
"links left": {
4+
"Main": "/"
5+
},
36
"site title": "Default nerc site",
47
"subtitle": "These settings can be overridden by putting a config.json file in the root of this nerc site.",
5-
"footer left": "This page was generated by<a href=\"\">nerc</a>.",
8+
"footer left": "This page was generated by&nbsp;<a href=\"https://github.com/8bitprodigy/nerc\">nerc</a>.",
69
"footer right": "nerc is public domain/0BSD software."
710
}

src/res/styles.css

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
* {
2-
font-family: 'Liver';
32
margin: 0px;
43
padding: 0px;
54
}
@@ -10,7 +9,6 @@ body {
109
display: flex;
1110
flex-direction: column;
1211
position: absolute;
13-
background: #737573;
1412
}
1513

1614
.spacer {
@@ -19,8 +17,6 @@ body {
1917

2018
.nerc {
2119
margin:5px;
22-
box-shadow: 5px 5px 0 0 rgba(0, 0, 0, 0.125);
23-
border: 4px solid;
2420
display: flex;
2521
}
2622

@@ -34,17 +30,21 @@ body {
3430
}
3531

3632
#links {
37-
background: #efffef;
38-
border-color: #8ccf8c;
3933
padding-left: 8px;
4034
padding-right: 8px;
4135
}
4236

4337
#header {
44-
background: #efffff;
45-
border-color: #9cefef;
4638
padding-left: 8px;
4739
}
40+
#header h1 {
41+
display: block;
42+
margin-top: auto;
43+
}
44+
#header h2 {
45+
display: block;
46+
margin-top: auto;
47+
}
4848

4949
#body {
5050
width: 100%;
@@ -54,9 +54,6 @@ body {
5454
}
5555

5656
#sidebar {
57-
font-family: 'Liver';
58-
background-color: #ffffef;
59-
border-color: #428a42;
6057
display: block;
6158
left: 0px;
6259
padding-top: 20px;
@@ -72,8 +69,6 @@ body {
7269
}
7370

7471
#content {
75-
background: #ffffff;
76-
border-color: #52aaad;
7772
display: block;
7873
flex-grow: 1;
7974
padding-left: 80px;
@@ -101,8 +96,6 @@ body {
10196
}
10297

10398
#footer {
104-
background-color: #8c8ace;
105-
border-color: #00009c;
10699
padding-left:8px;
107100
padding-right: 8px;
108101
}

src/res/template.htm

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<h1>
2222
<!--site title-->
2323
</h1>
24+
&nbsp;&nbsp;
2425
<h2>
2526
<!--subtitle-->
2627
</h2>

0 commit comments

Comments
 (0)