summaryrefslogtreecommitdiff
path: root/valadoc/doclets/gtkdoc/commentconverter.vala
blob: a8b960e1be058cc9f136f3b7cccecad89655e840 (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
/* commentconverter.vala
 *
 * Copyright (C) 2010 Luca Bruno
 *
 * 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:
 * 	Luca Bruno <lethalman88@gmail.com>
 */

using Valadoc;
using Valadoc.Api;
using Valadoc.Content;

public class Gtkdoc.CommentConverter : ContentVisitor {
	public Api.Node node_reference;

	public bool is_dbus;
	public string brief_comment;
	public string long_comment;
	public string returns;
	public Vala.List<Header> parameters = new Vala.ArrayList<Header> ();
	public Vala.List<Header> versioning = new Vala.ArrayList<Header> ();
	public string[] see_also = new string[]{};

	private StringBuilder current_builder = new StringBuilder ();
	private bool in_brief_comment = true;
	private ErrorReporter reporter;

	public CommentConverter (ErrorReporter reporter, Api.Node? node_reference = null) {
		this.node_reference = node_reference;
		this.reporter = reporter;
	}

	public void convert (Content.Comment comment, bool is_dbus = false) {
		this.is_dbus = is_dbus;
		comment.accept (this);

		long_comment = current_builder.str.strip ();
		if (long_comment == "") {
			long_comment = null;
		}
	}

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

	public override void visit_embedded (Embedded em) {
		current_builder.append ("<figure>");
		if (em.caption != null) {
			current_builder.append_printf ("<title>%s</title>", em.caption);
		}

		current_builder.append_printf ("<mediaobject><imageobject><imagedata fileref=\"%s\"/></imageobject>",
									   em.url);

		if (em.caption != null) {
			current_builder.append_printf ("<textobject><phrase>%s</phrase></textobject>", em.caption);
		}

		em.accept_children (this);
		current_builder.append ("</mediaobject>");
		current_builder.append ("</figure>");
	}

	public override void visit_headline (Headline hl) {
		// what to do here?
		reporter.simple_warning ("GtkDoc", "Headline elements not supported");
		current_builder.append ("\n");
		hl.accept_children (this);
		current_builder.append ("\n");
	}

	public override void visit_wiki_link (WikiLink link) {
		// wiki pages are not supported right now
		if (link.content.size > 0) {
			link.accept_children (this);
		} else {
			current_builder.append (link.name);
		}
	}

	public override void visit_link (Link link) {
		current_builder.append_printf ("<ulink url=\"%s\">", link.url);
		link.accept_children (this);
		current_builder.append ("</ulink>");
	}

	public override void visit_symbol_link (SymbolLink sl) {
		if (sl.symbol != null) {
			// If the symbol is a method and it doesn't have a constructor, fall back to linking to the class
			if (sl.symbol is Method && ((Method) sl.symbol).is_constructor &&
			    ((Method) sl.symbol).parent is Class && ((Class) ((Method) sl.symbol).parent).is_abstract) {
				current_builder.append (get_docbook_link (((Method) sl.symbol).parent, is_dbus) ?? sl.given_symbol_name);
			} else {
				current_builder.append (get_docbook_link (sl.symbol, is_dbus) ?? sl.given_symbol_name);
			}
		} else {
			current_builder.append (sl.given_symbol_name);
		}
	}

	public override void visit_list (Content.List list) {
		string tag = "orderedlist";
		switch (list.bullet) {
		case Content.List.Bullet.NONE:
			current_builder.append ("<itemizedlist mark=\"none\">");
			tag = "itemizedlist";
			break;

		case Content.List.Bullet.UNORDERED:
			current_builder.append ("<itemizedlist>");
			tag = "itemizedlist";
			break;

		case Content.List.Bullet.ORDERED:
			current_builder.append ("<orderedlist>");
			break;

		case Content.List.Bullet.ORDERED_NUMBER:
			current_builder.append ("<orderedlist numeration=\"arabic\">");
			break;

		case Content.List.Bullet.ORDERED_LOWER_CASE_ALPHA:
			current_builder.append ("<orderedlist numeration=\"loweralpha\">");
			break;

		case Content.List.Bullet.ORDERED_UPPER_CASE_ALPHA:
			current_builder.append ("<orderedlist numeration=\"upperalpha\">");
			break;

		case Content.List.Bullet.ORDERED_LOWER_CASE_ROMAN:
			current_builder.append ("<orderedlist numeration=\"lowerroman\">");
			break;

		case Content.List.Bullet.ORDERED_UPPER_CASE_ROMAN:
			current_builder.append ("<orderedlist numeration=\"upperroman\">");
			break;

		default:
			reporter.simple_warning ("GtkDoc",
									 "unsupported list type: '%s'", list.bullet.to_string ());
			break;
		}

		list.accept_children (this);
		current_builder.append_printf ("</%s>", tag);
	}

	public override void visit_list_item (ListItem item) {
		current_builder.append ("<listitem>");
		item.accept_children (this);
		current_builder.append ("</listitem>");
	}

	public override void visit_paragraph (Paragraph para) {
		if (!in_brief_comment) {
			current_builder.append ("<para>");
		}
		para.accept_children (this);

		if (in_brief_comment) {
			brief_comment = current_builder.str;
			current_builder = new StringBuilder ();
			in_brief_comment = false;
		} else {
			current_builder.append ("</para>");
		}
	}

	public override void visit_warning (Warning element) {
		current_builder.append ("<warning>");
		element.accept_children (this);
		current_builder.append ("</warning>");
	}

	public override void visit_note (Note element) {
		current_builder.append ("<note>");
		element.accept_children (this);
		current_builder.append ("</note>");
	}

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

	public override void visit_run (Run run) {
		string? tag = null;
		switch (run.style) {
		case Run.Style.BOLD:
			current_builder.append ("<emphasis role=\"bold\">");
			tag = "emphasis";
			break;

		case Run.Style.ITALIC:
			current_builder.append ("<emphasis>");
			tag = "emphasis";
			break;

		case Run.Style.UNDERLINED:
			current_builder.append ("<emphasis role=\"underline\">");
			tag = "emphasis";
			break;

		case Run.Style.MONOSPACED:
			current_builder.append ("<code>");
			tag = "code";
			break;
		default:
			break;
		}
		run.accept_children (this);

		if (tag != null) {
			current_builder.append_printf ("</%s>", tag);
		}
	}

	public override void visit_source_code (SourceCode code) {
		current_builder.append ("\n|[\n");
		current_builder.append (Markup.escape_text (code.code));
		current_builder.append ("\n]|\n");
	}

	public override void visit_table (Table t) {
		current_builder.append ("<table>");
		t.accept_children (this);
		current_builder.append ("</table>");
	}

	public override void visit_table_row (TableRow row) {
		current_builder.append ("<tr>");
		row.accept_children (this);
		current_builder.append ("</tr>");
	}

	public override void visit_table_cell (TableCell cell) {
		current_builder.append ("<td>");
		cell.accept_children (this);
		current_builder.append ("</td>");
	}

	public override void visit_taglet (Taglet t) {
		var old_builder = (owned)current_builder;
		current_builder = new StringBuilder ();

		t.accept_children (this);
		if (t is Taglets.Param) {
			double pos = double.MAX;
			var param_name = ((Taglets.Param)t).parameter_name;
			if (node_reference != null) {
				pos = get_parameter_pos (node_reference, param_name);
			}
			var header = new Header (param_name, current_builder.str, pos);
			parameters.add (header);
		} else if (t is Taglets.InheritDoc) {
			((Taglets.InheritDoc)t).produce_content().accept (this);
		} else if (t is Taglets.Return) {
			returns = current_builder.str;
		} else if (t is Taglets.Since) {
			var header = new Header ("Since", ((Taglets.Since)t).version);
			versioning.add (header);
		} else if (t is Taglets.Deprecated) {
			var header = new Header ("Deprecated", current_builder.str);
			versioning.add (header);
		} else if (t is Taglets.See) {
			var see = (Taglets.See)t;
			var see_also = this.see_also; // vala bug
			if (see.symbol != null) {
				see_also += get_docbook_link (see.symbol, is_dbus) ?? see.symbol_name;
			} else {
				see_also += see.symbol_name;
			}
			this.see_also = see_also;
		} else if (t is Taglets.Link) {
			((Taglets.Link)t).produce_content().accept (this);
		} else if (t is Taglets.Throws) {
			var taglet = (Taglets.Throws) t;
			var link = get_docbook_link (taglet.error_domain) ?? taglet.error_domain_name;
			old_builder.append_printf ("\n<para>%s will be returned in @error %s</para>",
									   link,
									   current_builder.str);
		} else {
			reporter.simple_warning ("GtkDoc", "Taglet not supported"); // TODO
		}
		current_builder = (owned)old_builder;
	}

	public override void visit_text (Text t) {
		current_builder.append (Markup.escape_text (t.content));
		t.accept_children (this);
	}
}