File: document.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (406 lines) | stat: -rw-r--r-- 6,844 bytes parent folder | download | duplicates (2)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
 * Root representation of a Markdown document.
 */
class Document {
	// Create an empty document.
	init(ElementSeq elements) {
		init { elements = elements; }
	}

	// Elements in the document.
	ElementSeq elements;

	// Visit the entire structure using a `Visitor`. The visitor allows modifying the elements as
	// well.
	void visit(Visitor v) {
		elements = elements.visit(v);
	}

	// Create a string representation of the markdown document.
	void toS(StrBuf to) : override {
		elements.toS(to);
	}

	// Create a HTML representation of the document.
	Html toHtml() {
		Html out;
		elements.toHtml(out);
		out;
	}
}

/**
 * Collection of elements.
 */
class ElementSeq {
	// Elements.
	Element[] elements;

	// Create.
	init() {
		init {}
	}

	// Add an element.
	void add(Element e) {
		if (e as PackedElements)
			add(e.elements);
		else
			elements << e;
	}

	// Add many elements.
	void add(ElementSeq e) {
		elements.append(e.elements);
	}

	// Remove an element.
	void remove(Nat id) {
		elements.remove(id);
	}

	// Visit the entire structure.
	ElementSeq visit(Visitor v) {
		Element[] newElems;
		for (x in elements) {
			var newElem = x.visit(v);
			if (newElem as PackedElements) {
				newElems.append(newElem.elements.elements);
			} else {
				newElems << newElem;
			}
		}

		elements = newElems;

		return v.visit(this);
	}

	// To string.
	void toS(StrBuf to) : override {
		to << join(elements, "\n\n");
	}

	// To HTML.
	void toHtml(Html to) {
		for (i, element in elements) {
			if (i > 0)
				to.html("\n");
			element.toHtml(to);
		}
	}

	// To HTML, ignore first paragraph.
	void toHtmlInline(Html to) {
		for (i, element in elements) {
			if (i == 0) {
				if (element as Paragraph) {
					element.text.toHtml(to);
					continue;
				}
			} else {
				to.html("\n");
			}

			element.toHtml(to);
		}
	}
}


/**
 * Element in a document.
 */
class Element {
	// Visit the element. The function is expected to run `v.visit(this)` as the last step. The
	// returned value is the element that replaces this element after the visiting operation. If
	// nothing is changed, then the object itself is returned.
	Element visit(Visitor v) : abstract;

	// Create an HTML representation of this object. The `Html` object is essentially a string
	// stream, but with logic for escaping text in the context of HTML.
	void toHtml(Html to) : abstract;
}


/**
 * Heading.
 */
class Heading extends Element {
	// Heading level.
	Nat level;

	// Text.
	FormattedText text;

	// Create.
	init(Nat level, FormattedText text) {
		init { level = level; text = text; }
	}

	// Visit.
	Element visit(Visitor v) {
		text = text.visit(v);
		return v.visit(this);
	}

	// Output.
	void toS(StrBuf to) : override {
		to << "#"*level << " " << text;
	}

	// To HTML.
	void toHtml(Html to) : override {
		to.html("<h${level}>");
		text.toHtml(to);
		to.html("</h${level}>");
	}
}


/**
 * A paragraph of text.
 */
class Paragraph extends Element {
	// The text in the paragraph.
	FormattedText text;

	// Create.
	init(FormattedText text) {
		init { text = text; }
	}

	// Visit.
	Element visit(Visitor v) {
		text = text.visit(v);
		return v.visit(this);
	}

	// Output.
	void toS(StrBuf to) : override {
		to << text;
	}

	// To HTML.
	void toHtml(Html to) : override {
		to.html("<p>\n");
		to.indent();
		text.toHtml(to);
		to.dedent();
		to.html("\n</p>");
	}
}


/**
 * A generic list.
 */
class List extends Element {
	// Ordered?
	Bool ordered;

	// Contents of the list.
	ElementSeq[] items;

	// Create.
	init(Bool ordered) {
		init { ordered = ordered; }
	}

	// Add an element. Content can be added later.
	void add(ElementSeq item) {
		items << item;
	}

	// Visit.
	Element visit(Visitor v) {
		for (i, x in items)
			items[i] = x.visit(v);
		return v.visit(this);
	}

	// To string.
	void toS(StrBuf to) : override {
		if (ordered) {
			Nat width = items.count.toS.count;

			to.indentBy(" "*(width + 2));
			for (i, e in items) {
				if (i > 0)
					to << "\n\n";
				to << width(width) << fill('0') << (i + 1) << ". ";
				{
					Indent z(to);
					e.toS(to);
				}
			}
		} else {
			to.indentBy("  ");
			for (i, e in items) {
				if (i > 0)
					to << "\n\n";
				to << "- ";
				{
					Indent z(to);
					e.toS(to);
				}
			}
		}
	}

	// To HTML.
	void toHtml(Html to) : override {
		if (ordered)
			to.html("<ol>\n");
		else
			to.html("<ul>\n");
		to.indent();
		for (e in items) {
			to.html("<li>\n");
			to.indent();
			e.toHtmlInline(to);
			to.html("\n");
			to.dedent();
			to.html("</li>\n");
		}
		to.dedent();
		if (ordered)
			to.html("</ol>");
		else
			to.html("</ul>");
	}
}


/**
 * Code.
 */
class CodeBlock extends Element {
	// Language of the code, if specified.
	Str language;

	// Lines of code in the code block.
	Str[] code;

	// Create.
	init(Str language) {
		init { language = language; }
	}

	// Add a line of code.
	void addLine(Str line) {
		code << line;
	}

	// Visit.
	Element visit(Visitor v) {
		return v.visit(this);
	}

	// To string.
	void toS(StrBuf to) : override {
		to << "```" << language << "\n";
		to << join(code, "\n") << "\n";
		to << "```";
	}

	// To HTML.
	void toHtml(Html to) : override {
		to.html("<pre>");
		to.pauseIndent();
		for (i, l in code) {
			if (i > 0)
				to.text("\n");
			to.text(l);
		}
		to.restoreIndent();
		to.html("</pre>");
	}
}


/**
 * Element that contains a sequence of other elements. Useful when replacing a single element with
 * multiple elements in a visitor.
 */
class PackedElements extends Element {
	// Elements.
	ElementSeq elements;

	// Create.
	init() {
		init {}
	}

	// Add an element.
	void add(Element elem) {
		elements.add(elem);
	}

	// Add a sequence of elements.
	void add(ElementSeq seq) {
		elements.add(seq);
	}

	// Visit elements.
	Element visit(Visitor v) : override {
		elements = elements.visit(v);
		return v.visit(this);
	}

	// To string.
	void toS(StrBuf to) : override {
		to << elements;
	}

	// To HTML.
	void toHtml(Html to) : override {
		elements.toHtml(to);
	}
}


/**
 * Custom HTML element.
 *
 * Used to make it convenient for other tools to generate custom HTML in the document. Not currently
 * created by the parser when parsing markdown.
 */
class CustomHtml extends Element {
	// HTML code.
	Str code;

	// Skip indentation?
	Bool skipIndent;

	// Create.
	init(Str code) {
		init { code = code; }
	}

	// Create, specify skip indent.
	init(Str code, Bool skipIndent) {
		init { code = code; skipIndent = skipIndent; }
	}

	// Visit.
	Element visit(Visitor v) : override {
		return v.visit(this);
	}

	// To string.
	void toS(StrBuf to) : override {
		to << "!! begin custom html !!\n";
		to << code;
		to << "\n!! end custom html !!";
	}

	// Output HTML.
	void toHtml(Html to) : override {
		if (skipIndent)
			to.pauseIndent();
		to.html(code);
		if (skipIndent)
			to.restoreIndent();
	}
}