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
|
/////+
////- Name: SisuDoc Spine, Doc Reform [a part of]
//// - Description: documents, structuring, processing, publishing, search
//// - static content generator
////
//// - Author: Ralph Amissah
//// [ralph.amissah@gmail.com]
////
//// - Copyright: (C) 2015 (continuously updated, current 2026) Ralph Amissah, All Rights Reserved.
////
//// - License: AGPL 3 or later:
////
//// Spine (SiSU), a framework for document structuring, publishing and
//// search
////
//// Copyright (C) Ralph Amissah
////
//// This program is free software: you can redistribute it and/or modify it
//// under the terms of the GNU AFERO General Public License as published by the
//// Free Software Foundation, either version 3 of the License, or (at your
//// option) any later version.
////
//// This program is distributed in the hope that it will be useful, but WITHOUT
//// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
//// more details.
////
//// You should have received a copy of the GNU General Public License along with
//// this program. If not, see [https://www.gnu.org/licenses/].
////
//// If you have Internet connection, the latest version of the AGPL should be
//// available at these locations:
//// [https://www.fsf.org/licensing/licenses/agpl.html]
//// [https://www.gnu.org/licenses/agpl.html]
////
//// - Spine (by Doc Reform, related to SiSU) uses standard:
//// - docReform markup syntax
//// - standard SiSU markup syntax with modified headers and minor modifications
//// - docReform object numbering
//// - standard SiSU object citation numbering & system
////
//// - Homepages:
//// [https://www.sisudoc.org]
//// [https://www.doc-reform.org]
////
//// - Git
//// [https://git.sisudoc.org/]
////
////+/
//// ocda-lib, in Gleam: the abstraction as a library, put to the question.
////
//// A library has no binary, so this is the programs over it: the reader,
//// and the two that test its claims.
////
//// ocda-lib dump [--unknown] <file>.ssp|<file>.ocda.db [lang]
//// ocda-lib agree <doc>.ssp <doc>.ocda.db [lang]
//// ocda-lib read [options] <file> [lang] (also installed as ocda-read)
////
//// `dump` writes the whole loaded model out canonically, byte for byte as
//// the D library's `ocda-dump` writes it, so that the two implementations
//// can be diffed. `agree` asks the same question the D library's
//// `ocda-agree` asks, within this language: does loading a document's
//// `.ssp` give what loading its `.ocda.db` gives?
import argv
import gleam/int
import gleam/io
import gleam/list
import gleam/string
import ocda/dump
import ocda/load
import ocda/model.{type Document}
import read/main as reader
pub fn main() {
case argv.load().arguments {
["dump", "--unknown", path] -> dump_file(path, "", True)
["dump", "--unknown", path, lang] -> dump_file(path, lang, True)
["dump", path] -> dump_file(path, "", False)
["dump", path, lang] -> dump_file(path, lang, False)
["agree", ssp, db] -> agree(ssp, db, "")
["agree", ssp, db, lang] -> agree(ssp, db, lang)
["read", ..rest] ->
case reader.run(rest) {
0 -> Nil
code -> halt(code)
}
_ -> usage()
}
}
fn usage() -> Nil {
io.println_error(
"usage: ocda-lib dump [--unknown] <file>.ssp|<file>.ocda.db [lang]
ocda-lib agree <doc>.ssp <doc>.ocda.db [lang]
ocda-lib read [--help] [options] <file> [lang]",
)
halt(2)
}
fn dump_file(path: String, lang: String, unknown: Bool) -> Nil {
case load.load(path, lang, False) {
Error(message) -> fail(message)
Ok(doc) ->
case unknown {
True -> io.print(dump.unknown(doc))
False -> io.print(dump.document(doc))
}
}
}
/// The library's whole purpose is that a writer cannot tell which
/// serialisation it was handed. That is only true if the two loaders give
/// the same model, so this asks, and says where they part company rather
/// than only that they did.
fn agree(ssp_path: String, db_path: String, lang: String) -> Nil {
case load.load(ssp_path, "", False), load.load(db_path, lang, False) {
Error(message), _ -> fail(message)
_, Error(message) -> fail(message)
Ok(from_ssp), Ok(from_db) -> {
let differences = compare(from_ssp, from_db)
case differences {
[] -> io.println("agree: the two loaders give the same model")
_ -> {
list.each(list.take(differences, 12), io.println)
case list.length(differences) > 12 {
True -> io.println(" ... stopping after 12")
False -> Nil
}
io.println(int.to_string(list.length(differences)) <> " difference(s)")
halt(1)
}
}
}
}
}
/// Compared through the dump rather than field by field, which is both
/// shorter and stricter: the dump carries every field, so nothing can
/// differ without showing here.
fn compare(from_ssp: Document, from_db: Document) -> List(String) {
let a = dump.document(from_ssp)
let b = dump.document(from_db)
case a == b {
True -> []
False -> diff_lines(a, b)
}
}
fn diff_lines(a: String, b: String) -> List(String) {
let la = split_lines(a)
let lb = split_lines(b)
case list.length(la) == list.length(lb) {
False -> [
" differ: line count",
" .ssp: " <> int.to_string(list.length(la)),
" .ocda.db: " <> int.to_string(list.length(lb)),
..pairwise(la, lb)
]
True -> pairwise(la, lb)
}
}
fn pairwise(la: List(String), lb: List(String)) -> List(String) {
list.zip(la, lb)
|> list.index_map(fn(pair, i) { #(i, pair.0, pair.1) })
|> list.filter(fn(t) { t.1 != t.2 })
|> list.map(fn(t) {
" differ: line "
<> int.to_string(t.0 + 1)
<> "\n .ssp: "
<> t.1
<> "\n .ocda.db: "
<> t.2
})
}
fn split_lines(text: String) -> List(String) {
case text {
"" -> []
_ -> string.split(text, on: "\n")
}
}
fn fail(message: String) -> Nil {
io.println_error("error: " <> message)
halt(2)
}
/// Gleam has no standard exit, and the exit status is what a sweep script
/// reads.
@external(erlang, "erlang", "halt")
fn halt(code: Int) -> Nil
|