[valadoc] Add a metadata-format for gir files



commit 27b237422064ba6c910ae73fe4adeb79e04e5166
Author: Florian Brosch <flo brosch gmail com>
Date:   Sat Jan 28 04:54:48 2012 +0100

    Add a metadata-format for gir files

 src/libvaladoc/Makefile.am                         |    1 +
 src/libvaladoc/documentation/girmetadata.vala      |  132 ++++++++++++++++++++
 .../documentation/gtkdoccommentparser.vala         |   22 +++-
 3 files changed, 153 insertions(+), 2 deletions(-)
---
diff --git a/src/libvaladoc/Makefile.am b/src/libvaladoc/Makefile.am
index b2f07a6..b54bbde 100755
--- a/src/libvaladoc/Makefile.am
+++ b/src/libvaladoc/Makefile.am
@@ -44,6 +44,7 @@ libvaladoc_la_VALASOURCES = \
 	documentation/wikiscanner.vala \
 	documentation/gtkdoccommentparser.vala \
 	documentation/gtkdoccommentscanner.vala \
+	documentation/girmetadata.vala \
 	importer/documentationimporter.vala \
 	importer/valadocdocumentationimporter.vala \
 	importer/valadocdocumentationimporterscanner.vala \
diff --git a/src/libvaladoc/documentation/girmetadata.vala b/src/libvaladoc/documentation/girmetadata.vala
new file mode 100644
index 0000000..010ecd9
--- /dev/null
+++ b/src/libvaladoc/documentation/girmetadata.vala
@@ -0,0 +1,132 @@
+/* girmetadata.vala
+ *
+ * Copyright (C) 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:
+ * 	Brosch Florian <flo brosch gmail com>
+ */
+
+/**
+ * Metadata reader for GIR files
+ */
+public class GirMetaData : Object {
+	private string? metadata_path = null;
+	private string? resource_dir = null;
+
+	/**
+	 * Used to manipulate paths to resources inside gir-files
+	 */
+	public string get_resource_path (string resource) {
+		if (resource_dir == null || metadata_path == null) {
+			return resource;
+		}
+
+		if (Path.is_absolute (resource_dir)) {
+			return Path.build_filename (resource_dir, resource);
+		}
+
+		return Path.build_filename (Path.get_dirname (metadata_path), resource_dir, resource);
+	}
+
+	private string? get_metadata_file_name (string gir_file_path) {
+		string metadata_file_name = Path.get_basename (gir_file_path);
+		int last_dot_pos = metadata_file_name.last_index_of (".");
+		if (last_dot_pos < 0) {
+			return null;
+		}
+
+		metadata_file_name = metadata_file_name.substring (0, last_dot_pos);
+		return metadata_file_name + ".valadoc.metadata";
+	}
+
+	private string? get_metadata_path (string gir_file_path, string[] metadata_dirs) {
+		string? metadata_file_name = get_metadata_file_name (gir_file_path);
+		if (metadata_file_name == null) {
+			return null;
+		}
+
+		// search for metatada at the same location as the gir file
+		string metadata_path = Path.build_filename (Path.get_dirname (gir_file_path), metadata_file_name);
+		if (FileUtils.test (metadata_path, FileTest.IS_REGULAR)) {
+			return metadata_path;
+		}
+
+		foreach (string metadata_dir in metadata_dirs) {
+			metadata_path = Path.build_filename (metadata_dir, metadata_file_name);
+			if (FileUtils.test (metadata_path, FileTest.IS_REGULAR)) {
+				return metadata_path;
+			}
+		}
+
+		return null;
+	}
+
+	private void load_general_metadata (KeyFile key_file) throws KeyFileError {
+		foreach (string key in key_file.get_keys ("General")) {
+			switch (key) {
+			case "resources":
+				this.resource_dir = key_file.get_string ("General", "resources");
+				break;
+
+			default:
+				stderr.printf ("Unknown key 'General.%s' in '%s'", key, metadata_path);
+				break;
+			}
+		}
+	}
+
+	public GirMetaData (string gir_file_path, string[] metadata_dirs) {
+		if (!FileUtils.test (gir_file_path, FileTest.IS_REGULAR)) {
+			return ;
+		}
+
+		metadata_path = get_metadata_path (gir_file_path, metadata_dirs);
+		if (metadata_path == null) {
+			return ;
+		}
+
+		KeyFile key_file;
+
+		try {
+			key_file = new KeyFile ();
+			key_file.load_from_file (metadata_path, KeyFileFlags.NONE);
+		} catch (KeyFileError e) {
+			stdout.printf ("Key file error: '%s': in %s\n", metadata_path, e.message);
+			return ;
+		} catch (FileError e) {
+			stdout.printf ("File error: '%s': in %s\n", metadata_path, e.message);
+			return ;
+		}
+
+		try {
+			foreach (string group in key_file.get_groups ()) {
+				switch (group) {
+				case "General":
+					load_general_metadata (key_file);
+					break;
+
+				default:
+					stdout.printf ("Unknown group '%s' in %s\n", group, metadata_path);
+					break;
+				}
+			}
+		} catch (KeyFileError e) {
+			stderr.printf ("%s: %s", metadata_path, e.message);
+		}
+	}
+}
+
diff --git a/src/libvaladoc/documentation/gtkdoccommentparser.vala b/src/libvaladoc/documentation/gtkdoccommentparser.vala
index b148606..d63774b 100644
--- a/src/libvaladoc/documentation/gtkdoccommentparser.vala
+++ b/src/libvaladoc/documentation/gtkdoccommentparser.vala
@@ -46,6 +46,23 @@ public class Valadoc.Gtkdoc.Parser : Object, ResourceLocator {
 
 	private Regex? normalize_regex = null;
 
+	private HashMap<Api.SourceFile, GirMetaData> metadata = new HashMap<Api.SourceFile, GirMetaData> ();
+	private GirMetaData? current_metadata = null;
+
+	private GirMetaData get_metadata_for_comment (Api.GirSourceComment gir_comment) {
+		GirMetaData metadata = metadata.get (gir_comment.file);
+		if (metadata != null) {
+			return metadata;
+		}
+
+		metadata = new GirMetaData (gir_comment.file.relative_path, settings.metadata_directories);
+		this.metadata.set (gir_comment.file, metadata);
+		return metadata;
+	}
+
+	private inline string fix_resource_path (string path) {
+		return this.current_metadata.get_resource_path (path);
+	}
 
 	private void reset (Api.SourceComment comment) {
 		this.scanner.reset (comment.content);
@@ -218,6 +235,7 @@ public class Valadoc.Gtkdoc.Parser : Object, ResourceLocator {
 	private Api.Node? element;
 
 	public Comment? parse (Api.Node element, Api.GirSourceComment gir_comment) {
+		this.current_metadata = get_metadata_for_comment (gir_comment);
 		this.element = element;
 
 		Comment? comment = this.parse_main_content (gir_comment);
@@ -756,7 +774,7 @@ public class Valadoc.Gtkdoc.Parser : Object, ResourceLocator {
 		}
 
 		Embedded e = factory.create_embedded ();
-		e.url = current.attributes.get ("fileref");
+		e.url = fix_resource_path (current.attributes.get ("fileref"));
 
 		next ();
 		parse_docbook_spaces ();
@@ -821,7 +839,7 @@ public class Valadoc.Gtkdoc.Parser : Object, ResourceLocator {
 
 		if (current.type == TokenType.XML_OPEN && current.content == "programlisting") {
 			append_block_content_not_null (content, parse_docbook_programlisting ());
-		} else if (current.type == TokenType.XML_OPEN && current.content == "programlisting") {
+		} else if (current.type == TokenType.XML_OPEN && current.content == "inlinegraphic") {
 			Embedded? img = parse_docbook_inlinegraphic ();
 			Paragraph p = factory.create_paragraph ();
 			append_block_content_not_null (content, p);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]