File: text.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 (351 lines) | stat: -rw-r--r-- 5,677 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
/**
 * A piece of formatted text.
 *
 * The text is represented as a list of pieces, each with their own formatting.
 */
class FormattedText {
	// Text pieces.
	TextSpan[] spans;

	// Create an empty text span.
	init() {
		init {}
	}

	// Create a text span from a string. It will contain a single span.
	init(Str text) {
		init {}
		add(text);
	}

	// Create from a single span piece.
	init(TextSpan span) {
		init {}
		add(span);
	}

	// Add a span.
	void add(TextSpan span) {
		if (span as PackedText)
			spans.append(span.text.spans);
		else
			spans << span;
	}

	// Add a string as a plain span.
	void add(Str span) {
		add(PlainTextSpan(span));
	}

	// Add another FormattedText object.
	void add(FormattedText other) {
		spans.append(other.spans);
	}

	// Add another FormattedText object as a line of text.
	void addLine(FormattedText other) {
		if (spans.any)
			add("\n");
		add(other);
	}

	// Visit the entire structure.
	FormattedText visit(Visitor v) {
		TextSpan[] newSpans;
		for (x in spans) {
			var newSpan = x.visit(v);
			if (newSpan as PackedText) {
				newSpans.append(newSpan.text.spans);
			} else {
				newSpans << newSpan;
			}
		}

		spans = newSpans;

		return v.visit(this);
	}

	// To string.
	void toS(StrBuf to) : override {
		for (span in spans)
			span.toS(to);
	}

	// To HTML.
	void toHtml(Html to) {
		for (span in spans)
			span.toHtml(to);
	}
}

/**
 * A piece of formatted text.
 */
class TextSpan {
	// Create an HTML representation of the text span.
	void toHtml(Html to) : abstract;

	// Visit the element recursively. The function is expected to run `v.visit(this)` as the last
	// step. The returned value is the element that replaces this span after the visiting
	// operation. If nothing is changed, then the object itself is returned.
	TextSpan visit(Visitor v) : abstract;
}

/**
 * A plain piece of text.
 */
class PlainTextSpan extends TextSpan {
	// The text.
	Str text;

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

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

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

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		v.visit(this);
	}
}

/**
 * Italics.
 */
class ItalicText extends TextSpan {
	// Text.
	FormattedText text;

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

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

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

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		text.visit(v);
		v.visit(this);
	}
}

/**
 * Bold.
 */
class BoldText extends TextSpan {
	// Text.
	FormattedText text;

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

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

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

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		text.visit(v);
		v.visit(this);
	}
}

/**
 * Inline code.
 */
class InlineCode extends TextSpan {
	// Text.
	Str text;

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

	// Create from string buffer.
	init(StrBuf text) {
		init { text = text.toS; }
	}

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

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

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		v.visit(this);
	}
}

/**
 * Link.
 */
class Link extends TextSpan {
	// Link text.
	FormattedText text;

	// Link target (TODO: Make into a URL).
	Str target;

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

	// To string.
	void toS(StrBuf to) : override {
		to << "[" << text << "](" << target << ")";
	}

	// To HTML.
	void toHtml(Html to) : override {
		to.html("<a href=\"");
		to.text(target);
		to.html("\">");
		text.toHtml(to);
		to.html("</a>");
	}

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		text.visit(v);
		v.visit(this);
	}
}

/**
 * Custom text (text inside [], but no () afterwards).
 */
class CustomText extends TextSpan {
	// Text.
	Str text;

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

	// To string.
	void toS(StrBuf to) : override {
		to << "[" << text << "]";
	}

	// To HTML.
	void toHtml(Html to) {
		to.text(text);
	}

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		v.visit(this);
	}
}


/**
 * Custom inline HTML. Not generated by the parser, but useful by themes to generate custom elements.
 */
class CustomInlineHtml extends TextSpan {
	// Text.
	Str text;

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

	// To string.
	void toS(StrBuf to) : override {
		to << "!! custom html: " << text << " !!";
	}

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

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		v.visit(this);
	}
}


/**
 * Packed text. Useful to replace a single TextSpan with multiple ones in a transform.
 */
class PackedText extends TextSpan {
	// Formatted text.
	FormattedText text;

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

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

	// Add.
	void add(TextSpan span) {
		text.add(span);
	}

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

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

	// Visit recursively.
	TextSpan visit(Visitor v) : override {
		text = text.visit(v);
		v.visit(this);
	}
}