summaryrefslogtreecommitdiff
path: root/valadoc/doclets/gtkdoc/gcomment.vala
blob: 0cedacc8cb7f783d08c6fbbdc166344b234b3806 (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
/* gcomment.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>
 */

public class Gtkdoc.Header {
	public string name;
	public string[]? annotations;
	public string? value;
	public double pos;
	public bool block;

	public Header (string name, string? value = null, double pos = double.MAX, bool block = true) {
		this.name = name;
		this.value = value;
		this.pos = pos;
		this.block = block;
	}

	public int cmp (Header header) {
		if (pos > header.pos) {
			return 1;
		} else if (pos < header.pos) {
			return -1;
		}
		return 0;
	}
}

public class Gtkdoc.GComment {
	public string symbol;
	public string[] symbol_annotations;
	public Vala.List<Header> headers = new Vala.ArrayList<Header> ();
	public bool short_description;
	public string brief_comment;
	public string long_comment;
	public string returns;
	public string[] returns_annotations;
	public Vala.List<Header> versioning = new Vala.ArrayList<Header> ();
	public string[] see_also;
	public bool is_section;

	public string to_string () {
		var builder = new StringBuilder ();

		builder.append_printf ("/**\n * %s", (is_section ? "SECTION:%s" : "%s:").printf (symbol));
		if (symbol_annotations != null && symbol_annotations.length > 0) {
			foreach (var annotation in symbol_annotations) {
				builder.append_printf (" (%s)", annotation);
			}
		}

		if (short_description && brief_comment != null) {
			builder.append_printf ("\n * @short_description: %s", commentize (brief_comment));
		}

		headers.sort ((CompareDataFunc) Header.cmp);
		foreach (var header in headers) {
			builder.append_printf ("\n * @%s:", header.name);
			if (header.annotations != null && header.annotations.length > 0) {
				foreach (var annotation in header.annotations) {
					builder.append_printf (" (%s)", annotation);
				}
				builder.append_c (':');
			}

			if (header.value != null) {
				builder.append_c (' ');
				builder.append (commentize (header.value));
			}
		}

		if (!short_description && brief_comment != null) {
			builder.append_printf  ("\n * \n * %s", commentize (brief_comment));
		}
		if (long_comment != null) {
			builder.append_printf  ("\n * \n * %s", commentize (long_comment));
		}

		if (see_also.length > 0) {
			builder.append_printf ("\n * \n * <emphasis>See also</emphasis>: %s", string.joinv (", ", see_also));
		}

		if (returns != null || returns_annotations.length > 0) {
			builder.append ("\n * \n * Returns:");
			if (returns_annotations != null) {
				foreach (var annotation in returns_annotations) {
					builder.append_printf (" (%s)", annotation);
				}

				if (returns_annotations.length > 0) {
					builder.append_c (':');
				}
			}
			builder.append_c (' ');

			if (returns != null) {
				builder.append (commentize (returns));
			}
		}

		if (versioning.size > 0) {
			builder.append ("\n *");
			foreach (var version in versioning) {
				builder.append_printf ("\n * %s:", version.name);
				if (version.value != null) {
					builder.append_printf (" %s", commentize (version.value));
				}
			}
		}

		builder.append ("\n */");
		return builder.str;
	}

	public string to_docbook (Valadoc.ErrorReporter reporter) {
		/*
		 * FIXME: this is not how it should be.
		 * The real solution is to create a comment like gtkdoc-mkdb does.
		 * This implies replacing the work of gtkdoc-mkdb and have a more accurate management of headers,
		 * (i.e. differentiate between parameters, short_description, see_also, etc.).
		 *
		 * For now we'll assume all headers are parameters.
		 * This is enough for a manually generated xml file only for D-Bus API.
		 *
		 * In other words, we are converting C/gtkdoc comment to a docbook comment.
		 */

		string? deprecated = null;
		string? since = null;
		foreach (var header in versioning) {
			if (header.name == "Deprecated") {
				deprecated = header.value;
			} else if (header.name == "Since") {
				since = header.value;
			} else {
				reporter.simple_warning ("GtkDoc", "Unknown versioning tag '%s'", header.name);
			}
		}

		var builder = new StringBuilder ();
		if (deprecated != null) {
			builder.append_printf ("""<warning><para><literal>%s</literal> is deprecated and should not be used in newly-written code. %s</para></warning>""", symbol, deprecated);
		}

		if (brief_comment != null) {
			builder.append_printf ("<para>%s</para>", brief_comment);
		}
		if (long_comment != null) {
			builder.append (long_comment);
		}

		headers.sort ((CompareDataFunc) Header.cmp);
		if (headers.size > 0 || returns != null) {
			builder.append ("""<variablelist role="params">""");
			foreach (var header in headers) {
				builder.append_printf ("""<varlistentry><term><parameter>%s</parameter>&#160;:</term>
<listitem><simpara> %s </simpara></listitem></varlistentry>""",
									   header.name, header.value);
			}
			if (returns != null) {
				builder.append_printf ("""<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term>
<listitem><simpara> %s </simpara></listitem></varlistentry>""", returns);
			}
			builder.append ("</variablelist>");
		}

		if (since != null) {
			builder.append_printf ("""<para role="since">Since %s</para>""", since);
		}

		return builder.str;
	}
}