summaryrefslogtreecommitdiff
path: root/libvaladoc/markupwriter.vala
blob: b9bd3c3b9368d41ed0b5907503629ddf98961712 (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
/* markupwriter.vala
 *
 * Copyright (C) 2008-2009 Florian Brosch, Didier Villevalois
 *
 * 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:
 * 	Didier 'Ptitjes Villevalois <ptitjes@free.fr>
 */


/**
 * Writes markups and text to a file.
 */
public class Valadoc.MarkupWriter {
	protected WriteFunc write;
	protected int indent;

	protected long current_column = 0;
	protected bool last_was_tag;
	private bool wrap = true;

	public static string escape (string txt) {
		StringBuilder builder = new StringBuilder ();
		unowned string start = txt;
		unowned string pos;
		unichar c;

		for (pos = txt; (c = pos.get_char ()) != '\0'; pos = pos.next_char ()) {
			switch (c) {
			case '"':
				builder.append_len (start, (ssize_t) ((char*) pos - (char*) start));
				builder.append ("&quot;");
				start = pos.next_char ();
				break;

			case '<':
				builder.append_len (start, (ssize_t) ((char*) pos - (char*) start));
				builder.append ("&lt;");
				start = pos.next_char ();
				break;

			case '>':
				builder.append_len (start, (ssize_t) ((char*) pos - (char*) start));
				builder.append ("&gt;");
				start = pos.next_char ();
				break;

			case '&':
				builder.append_len (start, (ssize_t) ((char*) pos - (char*) start));
				builder.append ("&amp;");
				start = pos.next_char ();
				break;

			case '\'':
				builder.append_len (start, (ssize_t) ((char*) pos - (char*) start));
				builder.append ("&apos;");
				start = pos.next_char ();
				break;
			}
		}

		if (&txt == &start) {
			return txt;
		} else {
			builder.append_len (start, (ssize_t) ((char*) pos - (char*) start));
			return (owned) builder.str;
		}
	}

	/**
	 * Writes text to a destination like a {@link GLib.StringBuilder} or a {@link GLib.FileStream}
	 */
	public delegate void WriteFunc (string text);

	private const int MAX_COLUMN = 150;

	/**
	 * Initializes a new instance of the MarkupWriter
	 *
	 * @param write stream a WriteFunc
	 * @param xml_declaration specifies whether this file starts with an xml-declaration
	 */
	public MarkupWriter (owned WriteFunc write, bool xml_declaration = true) {
		this.write = (owned) write;
		if (xml_declaration) {
			do_write ("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		}
		indent = -1;
		last_was_tag = true;
	}

	/**
	 * Writes an start tag of a markup element to the file
	 *
	 * @param name the name of the markup
	 * @param attributes a list of name/value pairs
	 * @return this
	 */
	public unowned MarkupWriter start_tag (string name, string[]? attributes=null) {
		indent++;
		check_column (name);

		if (attributes.length % 2 != 0) {
			warning ("Given attributes array is not a list of pairs (name and value)");
			// This only effects array length of this in-parameter in this scope
			attributes.length -= 1;
		}

		var content = new StringBuilder ("<");
		content.append (name);
		for (int i = 0; i < attributes.length; i=i+2) {
			if (attributes[i+1] != null) {
				content.append_printf (" %s=\"%s\"", attributes[i], attributes[i+1]);
			}
		}
		content.append (">");

		do_write (content.str);
		last_was_tag = true;
		return this;
	}

	/**
	 * Writes a simple tag (<name />) to the file
	 *
	 * @param name the name of the markup
	 * @param attributes a list of name/value pairs
	 * @return this
	 */
	public unowned MarkupWriter simple_tag (string name, string[]? attributes=null) {
		indent++;
		check_column (name);

		if (attributes.length % 2 != 0) {
			warning ("Given attributes array is not a list of pairs (name and value)");
			// This only effects array length of this in-parameter in this scope
			attributes.length -= 1;
		}

		var content = new StringBuilder ("<");
		content.append (name);
		for (int i = 0; i < attributes.length; i=i+2) {
			if (attributes[i+1] != null) {
				content.append_printf (" %s=\"%s\"", attributes[i], attributes[i+1]);
			}
		}
		content.append ("/>");

		do_write (content.str);
		indent--;
		last_was_tag = true;
		return this;
	}

	/**
	 * Writes an end tag of a markup element to the file
	 *
	 * @param name the name of the markup
	 * @return this
	 */
	public unowned MarkupWriter end_tag (string name) {
		check_column (name, true);
		do_write ("</%s>".printf (name));
		indent--;
		last_was_tag = true;
		return this;
	}

	/**
	 * Writes the specified string to the output stream
	 *
	 * @see raw_text
	 * @return this
	 */
	public unowned MarkupWriter text (string text) {
		if (wrap && text.length + current_column > MAX_COLUMN) {
			long wrote = 0;
			while (wrote < text.length) {
				long space_pos = -1;
				for (long i = wrote + 1; i < text.length; i++) {
					if (text[i] == ' ') {
						if (i - wrote + current_column > MAX_COLUMN) {
							break;
						}
						space_pos = i;
					}
				}
				if (text.length - wrote + current_column <= MAX_COLUMN) {
					do_write (text.substring (wrote));
					wrote = text.length + 1;
				} else if (space_pos == -1) {
					// Force line break
				} else {
					do_write (text.substring (wrote, space_pos - wrote));
					wrote = space_pos + 1;
				}
				if (wrote < text.length) {
					break_line ();
					do_write ("  ");
				}
			}
		} else {
			do_write (text);
		}
		last_was_tag = false;
		return this;
	}

	/**
	 * Writes the specified string to the output stream
	 *
	 * @see text
	 * @return this
	 */
	public unowned MarkupWriter raw_text (string text) {
		do_write (text);
		last_was_tag = false;
		return this;
	}

	public void set_wrap (bool wrap) {
		this.wrap = wrap;
	}

	private void break_line () {
		write ("\n");
		write (string.nfill (indent * 2, ' '));
		current_column = indent * 2;
	}

	protected void do_write (string text) {
		if (wrap && current_column + text.length > MAX_COLUMN) {
			break_line ();
		}
		write (text);
		current_column += text.length;
	}

	private void check_column (string name, bool end_tag = false) {
		if (!wrap) {
			return;
		} else if (!end_tag && inline_element (name) /*&& !last_was_tag*/) {
			return;
		} else if (end_tag && content_inline_element (name)) {
			return;
		} else if (end_tag && !last_was_tag) {
			return;
		}
		break_line ();
	}

	protected virtual bool inline_element (string name) {
		return false;
	}

	protected virtual bool content_inline_element (string name) {
		return true;
	}
}