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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
//// ssp-reader, in Gleam.
////
//// Reads a .ssp, the object-centric document abstraction written as text,
//// and says what it found and what it did not understand.
////
//// Written from the specification alone:
//// ssp-format-specification.md, of the sisudoc-abstraction-format spec
//// ocda-model.md, of the same
import argv
import gleam/dict
import gleam/int
import gleam/io
import gleam/list
import gleam/result
import gleam/string
import simplifile
import ssp/parse
import ssp/types.{type Document, type Object}
pub fn main() -> Nil {
case argv.load().arguments {
[] -> usage()
args -> {
let any_version = list.contains(args, "--any-version")
let outline = list.contains(args, "--outline")
let report = list.contains(args, "--report-unknown")
case list.filter(args, fn(a) { !string.starts_with(a, "--") }) {
[path, ..] -> run(path, any_version, outline, report)
[] -> usage()
}
}
}
}
fn usage() -> Nil {
io.println("ssp-reader - reads a .ssp and says what is in it")
io.println("")
io.println(" ssp_reader [options] <file.ssp>")
io.println("")
io.println(" --outline the headings, as a tree")
io.println(" --report-unknown what was read and not understood")
io.println(" --any-version read a format major this reader does not know")
}
fn run(path: String, any_version: Bool, outline: Bool, report: Bool) -> Nil {
case simplifile.read(from: path) {
Error(_) -> io.println("error: cannot read " <> path)
Ok(content) -> {
let document = parse.parse(content)
case version_ok(document, any_version) {
False -> Nil
True -> {
summary(document, path)
case outline {
True -> print_outline(document)
False -> Nil
}
case report {
True -> print_unknown(document)
False -> Nil
}
}
}
}
}
}
/// Specification section 2.1: check the major part and refuse what is not
/// known, rather than loading an unfamiliar artefact half populated.
fn version_ok(document: Document, any_version: Bool) -> Bool {
let major = types.version_major(document)
case document.version, major == types.format_major_read, any_version {
"", _, _ -> {
io.println("warning: no version banner, reading anyway")
True
}
_, True, _ -> True
_, False, True -> {
io.println(
"warning: format v"
<> document.version
<> ", this reader knows v"
<> types.format_major_read
<> ".x; reading anyway",
)
True
}
_, False, False -> {
io.println(
"error: format v"
<> document.version
<> ", this reader knows v"
<> types.format_major_read
<> ".x",
)
io.println(
" a major version differs: a property may have changed meaning",
)
io.println(" --any-version reads it regardless.")
False
}
}
}
fn summary(document: Document, path: String) -> Nil {
io.println("Parsed: " <> path)
io.println(" Format: v" <> document.version)
io.println(" Source: " <> document.source_filename)
case dict.get(document.source, "language") {
Ok(language) -> io.println(" Language: " <> language)
Error(_) -> Nil
}
io.println(" Title: " <> meta(document, "title.main"))
io.println(" Author: " <> meta(document, "creator.author"))
let counts =
document.sections
|> list.map(fn(s) { s.name <> "=" <> int.to_string(list.length(s.objects)) })
|> string.join(" ")
io.println(" Sections: " <> counts)
}
fn meta(document: Document, key: String) -> String {
document.meta |> dict.get(key) |> result.unwrap("")
}
/// The headings, indented by level. Model section 4: the eight levels are
/// A to D for the title matter and 1 to 4 for the body.
fn print_outline(document: Document) -> Nil {
io.println("")
document.sections
|> list.each(fn(section) {
section.objects
|> list.filter(fn(o) { o.kind == "heading" && types.renders(o) })
|> list.each(fn(o) { io.println(heading_line(o)) })
})
}
fn heading_line(object: Object) -> String {
let depth = case object.level {
"A" -> 0
"B" -> 1
"C" -> 2
"D" -> 3
"1" -> 4
"2" -> 5
"3" -> 6
"4" -> 7
_ -> 8
}
string.repeat(" ", depth)
<> object.level
<> "~ ["
<> int.to_string(object.ocn)
<> "] "
<> first_line(object.text)
}
fn first_line(text: List(String)) -> String {
case text {
[line, ..] -> line
[] -> ""
}
}
/// A reader that drops what it does not know is conforming; one that
/// cannot say what it dropped cannot show a specification to be complete.
fn print_unknown(document: Document) -> Nil {
io.println("")
case dict.size(document.unknown_properties) {
0 -> io.println("read and understood: every property was used")
n -> {
io.println("read and not used (" <> int.to_string(n) <> " distinct):")
document.unknown_properties
|> dict.to_list
|> list.sort(fn(a, b) { string.compare(a.0, b.0) })
|> list.each(fn(pair) {
io.println(
" ." <> pair.0 <> " " <> int.to_string(pair.1),
)
})
}
}
case dict.size(document.unknown_blocks) {
0 -> Nil
_ -> {
io.println("blocks not understood:")
document.unknown_blocks
|> dict.to_list
|> list.each(fn(pair) {
io.println(" @" <> pair.0 <> " " <> int.to_string(pair.1))
})
}
}
case document.malformed_lines {
0 -> Nil
n -> io.println("lines of no known shape: " <> int.to_string(n))
}
}
|