Skip to content

Commit 9e84cd0

Browse files
committed
Mustang can now render templates from YAML!
1 parent 6a616d0 commit 9e84cd0

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

mustang/Node.ooc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ SectionNode: class extends TNode {
6565
}
6666
else {
6767
itemContext = Context new()
68-
itemContext addValue("item", item)
68+
itemContext setValue("item", item)
6969
}
7070

7171
Renderer new(this firstChild) render(itemContext, out)

mustang/Value.ooc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ HashValue: class extends Value {
7373
}
7474

7575
setString: func(name: String, value: String) {
76-
addValue(name, StringValue new(value))
76+
setValue(name, StringValue new(value))
7777
}
7878

7979
setBool: func(name: String, value: Bool) {
80-
addValue(name, BoolValue new(value))
80+
setValue(name, BoolValue new(value))
8181
}
8282

8383
setList: func(name: String, list: List<Value>) {
84-
addValue(name, ListValue new(list))
84+
setValue(name, ListValue new(list))
8585
}
8686

8787
setHashMap: func(name: String, hash: HashMap<Value>) {
88-
addValue(name, HashValue new(hash))
88+
setValue(name, HashValue new(hash))
8989
}
9090

9191
getValue: func(name: String) -> Value {

mustang/YAMLContext.ooc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use yaml
2+
import yaml/[Parser, Document]
3+
4+
import io/File
5+
import mustang/[Context, Value]
6+
7+
8+
YAMLContext: class extends Context {
9+
loadFromString: static func(text: String) -> This {
10+
parser := YAMLParser new()
11+
parser setInputString(text)
12+
This new(parser)
13+
}
14+
15+
loadFromFile: static func(file: File) -> This {
16+
parser := YAMLParser new()
17+
parser setInputFile(file)
18+
This new(parser)
19+
}
20+
21+
loadFromFile: static func ~path(path: String) -> This {
22+
This loadFromFile(File new(path))
23+
}
24+
25+
init: func(parser: YAMLParser) {
26+
document := parser parseDocument()
27+
rootValue := nodeToValue(document getRootNode())
28+
if(rootValue class != HashValue) {
29+
Exception new("Root value must be a hash!") throw()
30+
}
31+
32+
super(rootValue as HashValue)
33+
}
34+
35+
nodeToValue: static func(node: DocumentNode) -> Value {
36+
match node class {
37+
case ScalarNode => StringValue new((node as ScalarNode) value)
38+
case SequenceNode => sequenceToListValue(node as SequenceNode)
39+
case MappingNode => mappingToHashValue(node as MappingNode)
40+
case => Exception new("Unknown node type!") throw(); null
41+
}
42+
}
43+
44+
sequenceToListValue: static func(seq: SequenceNode) -> ListValue {
45+
list := ListValue new()
46+
47+
for(node: DocumentNode in seq toList()) {
48+
list appendValue(nodeToValue(node))
49+
}
50+
51+
return list
52+
}
53+
54+
mappingToHashValue: static func(map: MappingNode) -> HashValue {
55+
hashes := map toHashMap()
56+
values := HashValue new()
57+
58+
for(key: String in hashes getKeys()) {
59+
value := hashes get(key)
60+
values setValue(key, nodeToValue(value))
61+
}
62+
63+
return values
64+
}
65+
}

mustang/mustang.ooc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import io/File
22
import structs/ArrayList
3-
import mustang/[TemplateParser, TemplateReader, Walker]
3+
import mustang/[Template, YAMLContext]
44

55
main: func(args: ArrayList<String>) -> Int {
6-
if(args size() < 2) {
7-
"Usage: mustang <TEMPLATE>" println()
6+
if(args size() < 3) {
7+
"Usage: mustang <TEMPLATE> <YAML>" println()
88
return 1
99
}
1010

11-
template := TemplateReader getReaderFromFile(File new(args[1]))
12-
parser := TemplateParser new(template, "{{", "}}")
13-
14-
root := parser parse()
15-
printer := NodePrinter new(root)
16-
printer print()
11+
context := YAMLContext loadFromFile(args[2])
12+
template := Template loadFromFile(args[1])
13+
template render(context) println()
1714

1815
return 0
1916
}

0 commit comments

Comments
 (0)