summaryrefslogtreecommitdiff
path: root/libvaladoc/gtkdocrenderer.vala
blob: e76704e851d881514f1b8bb441ee4cec5049c9f6 (plain)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* gtkdocrenderer.vala
 *
 * Copyright (C) 2011 Florian Brosch
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Florian Brosch <flo.brosch@gmail.com>
 */

using GLib;
using Valadoc.Content;

public class Valadoc.GtkdocRenderer : ContentRenderer {
	private GtkDocMarkupWriter writer = new GtkDocMarkupWriter ();
	protected Settings settings;
	private bool separated;

	private string? get_cname (Api.Item item) {
		if (item is Api.Method) {
			return ((Api.Method)item).get_cname ();
		} else if (item is Api.Parameter) {
			return ((Api.Parameter)item).name;
		} else if (item is Api.Constant) {
			return ((Api.Constant)item).get_cname ();
		} else if (item is Api.Property) {
			return ((Api.Property)item).get_cname ();
		} else if (item is Api.Signal) {
			var name = ((Api.Signal)item).get_cname ();
			return name.replace ("_", "-");
		} else if (item is Api.Class) {
			return ((Api.Class)item).get_cname ();
		} else if (item is Api.Struct) {
			return ((Api.Struct)item).get_cname ();
		} else if (item is Api.Interface) {
			return ((Api.Interface)item).get_cname ();
		} else if (item is Api.ErrorDomain) {
			return ((Api.ErrorDomain)item).get_cname ();
		} else if (item is Api.ErrorCode) {
			return ((Api.ErrorCode)item).get_cname ();
		} else if (item is Api.Delegate) {
			return ((Api.Delegate)item).get_cname ();
		} else if (item is Api.Enum) {
			return ((Api.Enum)item).get_cname ();
		} else if (item is Api.EnumValue) {
			return ((Api.EnumValue)item).get_cname ();
		}

		return null;
	}

	public void write_docbook_link (Api.Item item) {
		writer.set_wrap (false);

		if (item is Api.Method) {
			writer.start_tag ("function")
				.text (((Api.Method)item).get_cname ())
				.end_tag ("function");
		} else if (item is Api.Parameter) {
			writer.start_tag ("parameter").
				text (((Api.Parameter)item).name ?? "...")
				.end_tag ("parameter");
		} else if (item is Api.Constant) {
			writer.start_tag ("constant").text (((Api.Constant)item)
				.get_cname ())
				.end_tag ("constant");
		} else if (item is Api.Property) {
			// TODO: use docbook-tags instead
			writer.text ("#").text (get_cname(item.parent))
				.text (":")
				.text (((Api.Property)item)
				.get_cname ().replace ("_", "-"));
		} else if (item is Api.Signal) {
			// TODO: use docbook-tags instead
			writer.text ("#").text (get_cname(item.parent))
				.text ("::")
				.text (((Api.Signal)item).get_cname ().replace ("_", "-"));
		} else if (item is Api.Namespace) {
			writer.text (((Api.Namespace) item).get_full_name ());
		} else {
			writer.start_tag ("type")
				.text (get_cname (item))
				.end_tag ("type");
		}

		writer.set_wrap (true);
	}

	public GtkdocRenderer () {
	}

	public override void render (ContentElement element) {
		reset ();
		element.accept (this);
	}

	public void render_symbol (Content.Comment? documentation) {
		render (documentation);

		append_exceptions (documentation.find_taglets (null, typeof(Taglets.Throws)));
		append_see (documentation.find_taglets (null, typeof(Taglets.See)));
		append_since (documentation.find_taglets (null, typeof(Taglets.Since)));
	}

	public override void render_children (ContentElement element) {
		reset ();
		element.accept_children (this);
	}

	private void reset () {
		separated = false;
		writer.reset ();
	}

	public unowned string content {
		get {
			if (writer.content.has_prefix ("\n")) {
				return writer.content.next_char ();
			}

			return writer.content;
		}
	}

	public override void visit_comment (Comment element) {
		element.accept_children (this);
	}

	public override void visit_embedded (Embedded element) {
		writer.start_tag ("figure");
		if (element.caption != null) {
			writer.start_tag ("title")
				.text (element.caption)
				.end_tag ("title");
		}

		writer.start_tag ("mediaobject");

		writer.start_tag ("imageobject")
			.simple_tag ("imagedata", {"fileref", element.url})
			.end_tag ("imageobject");

		if (element.caption != null) {
			writer.start_tag ("textobject")
				.start_tag ("phrase")
				.text (element.caption)
				.end_tag ("phrase")
				.end_tag ("textobject");
		}

		writer.end_tag ("mediaobject");
		writer.end_tag ("figure");
	}

	public override void visit_headline (Headline element) {
		assert_not_reached ();
	}

	public override void visit_wiki_link (WikiLink element) {
		// wiki pages are not supported by gir
		if (element.content.size > 0) {
			element.accept_children (this);
		} else {
			write_string (element.name);
		}
	}

	public override void visit_link (Link element) {
		writer.start_tag ("ulink", {"url", element.url});
		element.accept_children (this);
		writer.end_tag ("ulink");
	}

	public override void visit_symbol_link (SymbolLink element) {
		if (element.content.size > 0) {
			writer.text ("\"");
			element.accept_children (this);
			writer.text ("\" (");
			write_symbol_link (element);
			writer.text (")");
		} else {
			write_symbol_link (element);
		}
	}

	public void write_symbol_link (SymbolLink element) {
		if (element.symbol == null) {
			writer.text (element.given_symbol_name);
		} else {
			write_docbook_link (element.symbol);
		}
	}

	public override void visit_list (Content.List element) {
		string tag = "orderedlist";
		switch (element.bullet) {
		case Content.List.Bullet.NONE:
			writer.start_tag ("itemizedlist", {"mark", "none"});
			tag = "itemizedlist";
			break;

		case Content.List.Bullet.UNORDERED:
			writer.start_tag ("itemizedlist");
			tag = "itemizedlist";
			break;

		case Content.List.Bullet.ORDERED:
			writer.start_tag ("orderedlist");
			break;

		case Content.List.Bullet.ORDERED_NUMBER:
			writer.start_tag ("orderedlist", {"numeration", "arabic"});
			break;

		case Content.List.Bullet.ORDERED_LOWER_CASE_ALPHA:
			writer.start_tag ("orderedlist", {"numeration", "loweralpha"});
			break;

		case Content.List.Bullet.ORDERED_UPPER_CASE_ALPHA:
			writer.start_tag ("orderedlist", {"numeration", "upperalpha"});
			break;

		case Content.List.Bullet.ORDERED_LOWER_CASE_ROMAN:
			writer.start_tag ("orderedlist", {"numeration", "lowerroman"});
			break;

		case Content.List.Bullet.ORDERED_UPPER_CASE_ROMAN:
			writer.start_tag ("orderedlist", {"numeration", "upperroman"});
			break;

		default:
			assert_not_reached ();
		}

		element.accept_children (this);

		writer.end_tag (tag);
	}

	public override void visit_list_item (ListItem element) {
		writer.start_tag ("listitem");
		element.accept_children (this);
		writer.end_tag ("listitem");
	}

	public override void visit_page (Page element) {
		element.accept_children (this);
	}

	public override void visit_paragraph (Paragraph element) {
		writer.start_tag ("para");
		element.accept_children (this);
		writer.end_tag ("para");
	}

	public override void visit_warning (Warning element) {
		writer.start_tag ("warning");
		element.accept_children (this);
		writer.end_tag ("warning");
	}

	public override void visit_note (Note element) {
		writer.start_tag ("note");
		element.accept_children (this);
		writer.end_tag ("note");
	}

	public override void visit_run (Run element) {
		string? tag = null;

		switch (element.style) {
		case Run.Style.BOLD:
			writer.start_tag ("emphasis", {"role", "bold"});
			tag = "emphasis";
			break;

		case Run.Style.ITALIC:
			writer.start_tag ("emphasis");
			tag = "emphasis";
			break;

		case Run.Style.UNDERLINED:
			writer.start_tag ("emphasis", {"role", "underline"});
			tag = "emphasis";
			break;

		case Run.Style.MONOSPACED:
			writer.start_tag ("blockquote");
			tag = "blockquote";
			break;
		default:
			break;
		}

		element.accept_children (this);

		if (tag != null) {
			writer.end_tag (tag);
		}
	}

	public override void visit_source_code (SourceCode element) {
		writer.start_tag ("example")
			.start_tag ("programlisting");
		writer.text (element.code);
		writer.end_tag ("programlisting")
			.end_tag ("example");
	}

	public override void visit_table (Table element) {
		writer.start_tag ("table", {"align", "center"});
		element.accept_children (this);
		writer.end_tag ("table");
	}

	public override void visit_table_cell (TableCell element) {
		writer.start_tag ("td", {"colspan", element.colspan.to_string (), "rowspan", element.rowspan.to_string ()});
		element.accept_children (this);
		writer.end_tag ("td");
	}

	public override void visit_table_row (TableRow element) {
		writer.start_tag ("tr");
		element.accept_children (this);
		writer.end_tag ("tr");
	}

	public override void visit_text (Text element) {
		write_string (element.content);
	}

	private void write_string (string content) {
		unichar chr = content[0];
		long lpos = 0;
		int i = 0;

		for (i = 0; chr != '\0' ; i++, chr = content[i]) {
			switch (chr) {
			case '<':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&lt;");
				lpos = i+1;
				break;

			case '>':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&gt;");
				lpos = i+1;
				break;

			case '"':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&quot;");
				lpos = i+1;
				break;

			case '\'':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&apos;");
				lpos = i+1;
				break;

			case '&':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&amp;");
				lpos = i+1;
				break;

			case '#':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&num;");
				lpos = i+1;
				break;

			case '%':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&percnt;");
				lpos = i+1;
				break;

			case '@':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&commat;");
				lpos = i+1;
				break;

			case '(':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&lpar;");
				lpos = i+1;
				break;

			case ')':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.raw_text ("&rpar;");
				lpos = i+1;
				break;

			case '\n':
				writer.raw_text (content.substring (lpos, i-lpos));
				writer.simple_tag ("br");
				lpos = i+1;
				break;
			}
		}

		writer.raw_text (content.substring (lpos, i-lpos));
	}

	public void append_since (Vala.List<Content.Taglet> taglets) {
		foreach (Content.Taglet _taglet in taglets) {
			Taglets.Since taglet = _taglet as Taglets.Since;
			if (taglet == null || taglet.version == null) {
				// ignore unexpected taglets
				continue ;
			}

			if (separated == false) {
				writer.text ("\n");
			}

			writer.set_wrap (false);
			writer.text ("\nSince: ")
				.text (taglet.version);
			writer.set_wrap (true);
			separated = true;

			// ignore multiple occurrences
			return ;
		}
	}

	public void append_see (Vala.List<Content.Taglet> taglets) {
		bool first = true;
		foreach (Content.Taglet _taglet in taglets) {
			Taglets.See taglet = _taglet as Taglets.See;
			if (taglet == null || taglet.symbol == null) {
				// ignore unexpected taglets
				continue ;
			}

			if (first) {
				writer.start_tag ("para").text ("See also: ");
			} else {
				writer.text (", ");
			}

			write_docbook_link (taglet.symbol);
			first = false;
		}

		if (first == false) {
			writer.end_tag ("para");
		}
	}

	public void append_exceptions (Vala.List<Content.Taglet> taglets) {
		bool first = true;
		foreach (Content.Taglet _taglet in taglets) {
			Taglets.Throws taglet = _taglet as Taglets.Throws;
			if (taglet == null || taglet.error_domain == null) {
				// ignore unexpected taglets
				continue ;
			}

			if (first) {
				writer.start_tag ("para")
					.text ("This function may throw:")
					.end_tag ("para");
				writer.start_tag ("table");
			}

			writer.start_tag ("tr");

			writer.start_tag ("td");
			write_docbook_link (taglet.error_domain);
			writer.end_tag ("td");

			writer.start_tag ("td");
			taglet.accept_children (this);
			writer.end_tag ("td");

			writer.end_tag ("tr");

			first = false;
		}

		if (first == false) {
			writer.end_tag ("table");
		}
	}
}