neo_doxygen: Add an API to process description markup.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Fri, 19 Dec 2014 16:25:32 +0000 (11:25 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Sun, 21 Dec 2014 03:44:51 +0000 (22:44 -0500)
Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

contrib/neo_doxygen/src/doxml/doc_listener.nit

index 2b9df04..05ecd35 100644 (file)
@@ -16,6 +16,7 @@
 module doxml::doc_listener
 
 import listener
+import html
 
 # Processes documentation.
 class DocListener
@@ -24,9 +25,70 @@ class DocListener
        # The read documentation.
        var doc = new Documentation is writable
 
+       # Mapping between the type of a Doxygen element and the corresponding
+       # factory.
+       private var factories = new HashMap[String, HtmlElementFactory]
+
+       private var element_stack = new Array[HTMLTag]
+
+       # Does the next block have to be added to the detailed description?
+       private var in_detailed_description = false
+
+       redef fun listen_until(uri, local_name) do
+               super
+               if local_name == "briefdescription" then
+                       in_detailed_description = false
+               else
+                       in_detailed_description = true
+               end
+       end
+
+       redef fun start_dox_element(local_name, atts) do
+               super
+               var factory = factories.get_or_null(local_name)
+               if factory == null then return
+               element_stack.push(factory.create_element(local_name, atts))
+       end
+
+       redef fun end_dox_element(local_name) do
+               super
+               if not factories.has_key(local_name) then return
+               if element_stack.is_empty then return
+               var current_element = element_stack.pop
+               current_element.append(flush_buffer.trim)
+               if element_stack.is_empty then add_block(current_element.write_to_string)
+       end
+
        redef fun end_listening do
                super
-               var line = flush_buffer.trim
-               if not line.is_empty then doc.add(line)
+               if not element_stack.is_empty then
+                       var current_element = element_stack.first.write_to_string
+                       add_block(current_element)
+               end
+               add_block(flush_buffer.trim)
+               element_stack.clear
        end
+
+       private fun add_block(block: String) do
+               if block.is_empty then return
+               if in_detailed_description then
+                       doc.add(block)
+               else
+                       doc.brief_description = block
+                       in_detailed_description = true
+               end
+       end
+end
+
+# Provides a mean to create a certain kind of HTML elements.
+private abstract class HtmlElementFactory
+       # Create a new empty HTML element.
+       #
+       # Parameters:
+       #
+       # * `local_name`: Type of the Doxygen element that will be represented by
+       # the HTML element.
+       # * `attributes`: Attributes of the Doxygen element that will be
+       # represented by the HTML element.
+       fun create_element(local_name: String, attributes: Attributes): HTMLTag is abstract
 end