summaryrefslogtreecommitdiff
path: root/libvaladoc/content/sourcecode.vala
blob: e2031ad6272d21a96ac8c83488a5e73f9cb9ac9d (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
/* sourcecode.vala
 *
 * Copyright (C) 2008-2009 Didier Villevalois
 * Copyright (C) 2008-2012 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:
 * 	Didier 'Ptitjes Villevalois <ptitjes@free.fr>
 */


public class Valadoc.Content.SourceCode : ContentElement, Inline {
	public enum Language {
		UNKNOWN,
		GENIE,
		VALA,
		XML,
		C;

		public static Language from_path (string path) {
			int pos = path.last_index_of (".");
			if (pos < 0) {
				return Language.UNKNOWN;
			}

			string ext = path.substring (pos + 1);
			return from_string (ext, true);
		}

		public static Language from_string (string str, bool is_extension = false) {
			switch (str) {
			case "genie":
				if (is_extension) {
					return Language.UNKNOWN;
				}
				return Language.GENIE;
			case "gs":
				return Language.GENIE;
			case "xml":
				return Language.XML;
			case "vala":
				return Language.VALA;
			case "c":
			case "h":
				return Language.C;
			}

			return Language.UNKNOWN;
		}

		public unowned string to_string () {
			switch (this) {
			case Language.GENIE:
				return "genie";
			case Language.VALA:
				return "vala";
			case Language.XML:
				return "xml";
			case Language.C:
				return "c";
			}

			assert_not_reached ();
		}
	}


	public string code {
		get;
		set;
	}

	public Run? highlighted_code {
		get;
		private set;
	}

	public Language language {
		get;
		set;
	}

	internal SourceCode () {
		base ();
		_language = Language.VALA;
	}

	private string? get_path (string path, Api.Node container, string source_file_path,
							  ErrorReporter reporter)
	{
		// search relative to our file
		if (!Path.is_absolute (path)) {
			string relative_to_file = Path.build_path (Path.DIR_SEPARATOR_S,
													   Path.get_dirname (source_file_path),
													   path);
			if (FileUtils.test (relative_to_file, FileTest.EXISTS | FileTest.IS_REGULAR)) {
				return (owned) relative_to_file;
			}
		}

		// search relative to the current directory / absolute path
		if (!FileUtils.test (path, FileTest.EXISTS | FileTest.IS_REGULAR)) {
			string node_segment = (container is Api.Package)? "" : container.get_full_name () + ": ";
			code = "File '%s' does not exist".printf (path);
			reporter.simple_warning ("%s: %s{{{".printf (source_file_path, node_segment),
									 "%s", code);
			return null;
		}

		return path;
	}

	private void load_source_code (string _path, Api.Node container, string source_file_path,
								   ErrorReporter reporter)
	{
		string? path = get_path (_path, container, source_file_path, reporter);
		if (path == null) {
			return ;
		}

		try {
			string content = null;
			FileUtils.get_contents (path, out content);
			_language = Language.from_path (path);
			code = (owned) content;
		} catch (FileError err) {
			string node_segment = (container is Api.Package)? "" : container.get_full_name () + ": ";
			reporter.simple_error ("%s: %s{{{".printf (source_file_path, node_segment),
								   "Can't read file '%s': %s", path, err.message);
		}
	}

	private inline bool is_empty_string (string line) {
		for (int i = 0; line[i] != '\0'; i++) {
			if (line[i].isspace () == false) {
				return false;
			}
		}

		return true;
	}

	private string strip_code (string code) {
		string[] lines = code.split ("\n");
		for (int i = lines.length - 1; i >= 0 && is_empty_string (lines[i]); i--) {
			lines[i] = null;
		}

		string** _lines = lines;
		for (int i = 0; lines[i] != null && is_empty_string (lines[i]); i++) {
			_lines = &lines[i + 1];
		}

		return string.joinv ("\n", (string[]) _lines);
	}

	public override void check (Api.Tree api_root, Api.Node container, string file_path,
								ErrorReporter reporter, Settings settings)
	{
		string[] splitted = code.split ("\n", 2);
		if (splitted[0].strip () == "") {
			code = splitted[1] ?? "";
		} else if (splitted[0].has_prefix ("#!")) {
			unowned string start = (string) (((char*) splitted[0]) + 2);
			if (start.has_prefix ("include:")) {
				start = (string) (((char*) start) + 8);
				string path = start.strip ();
				load_source_code (path, container, file_path, reporter);
			} else {
				string name = start._strip ().ascii_down ();
				_language = Language.from_string (name);
				code = splitted[1] ?? "";
				if (_language == Language.UNKNOWN && name != "none") {
					string node_segment = (container is Api.Package)? "" : container.get_full_name () + ": ";
					reporter.simple_warning ("%s: %s{{{".printf (file_path, node_segment),
						"Unsupported programming language '%s'", name);
				}
			}
		}

		code = strip_code (code);

		if (_language == Language.VALA) {
			highlighted_code = api_root.highlighter.highlight_vala (code);
		} else if (_language == Language.XML) {
			highlighted_code = api_root.highlighter.highlight_xml (code);
		} else if (_language == Language.C) {
			highlighted_code = api_root.highlighter.highlight_c (code);
		} else {
			highlighted_code = new Run (Run.Style.MONOSPACED);
			highlighted_code.content.add (new Text (code));
		}
	}

	public override void accept (ContentVisitor visitor) {
		visitor.visit_source_code (this);
	}

	public override void accept_children (ContentVisitor visitor) {
		if (highlighted_code != null) {
			highlighted_code.accept (visitor);
		}
	}

	public override bool is_empty () {
		// empty source blocks are visible as well
		return false;
	}

	public override ContentElement copy (ContentElement? new_parent = null) {
		SourceCode source_code = new SourceCode ();
		source_code.parent = new_parent;

		source_code.language = language;
		source_code.code = code;

		return source_code;
	}
}