diff options
Diffstat (limited to 'gleam/src/ssp_reader.gleam')
| -rw-r--r-- | gleam/src/ssp_reader.gleam | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/gleam/src/ssp_reader.gleam b/gleam/src/ssp_reader.gleam new file mode 100644 index 0000000..d8c4c78 --- /dev/null +++ b/gleam/src/ssp_reader.gleam @@ -0,0 +1,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 doc-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)) + } +} |
