]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add a metadata-format for gir files
authorFlorian Brosch <flo.brosch@gmail.com>
Sat, 28 Jan 2012 03:54:48 +0000 (04:54 +0100)
committerFlorian Brosch <flo.brosch@gmail.com>
Sat, 28 Jan 2012 03:54:48 +0000 (04:54 +0100)
src/libvaladoc/Makefile.am
src/libvaladoc/documentation/girmetadata.vala [new file with mode: 0644]
src/libvaladoc/documentation/gtkdoccommentparser.vala

index b2f07a629d5c93edd66495494f558ec7a9b565cc..b54bbdef1888603f739dede91c0c049a2288d4ea 100755 (executable)
@@ -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 (file)
index 0000000..010ecd9
--- /dev/null
@@ -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);
+               }
+       }
+}
+
index b1486069d4f945d525733c637455ae388af17ef6..d63774bf4ac28c2c719417ad9faab6ed665d5da2 100644 (file)
@@ -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);