src/doc/commands: move cmd processing to `commands_html`
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 12 Jun 2018 16:07:28 +0000 (12:07 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 22 Jun 2018 03:42:27 +0000 (23:42 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/commands/commands_docdown.nit
src/doc/commands/commands_html.nit

index 596f549..95bdee9 100644 (file)
 # Doc down related queries
 module commands_docdown
 
-import commands::commands_parser
 import commands::commands_html
 import commands::commands_md
 
-intrude import markdown::wikilinks
-
 # Retrieve the MDoc summary
 #
 # List all MarkdownHeading found and their ids.
@@ -61,103 +58,3 @@ class CmdSummary
                return res
        end
 end
-
-# Custom Markdown processor able to process doc commands
-class CmdDecorator
-       super NitdocDecorator
-
-       redef type PROCESSOR: CmdMarkdownProcessor
-
-       # Model used by wikilink commands to find entities
-       var model: Model
-
-       # Filter to apply if any
-       var filter: nullable ModelFilter
-
-       redef fun add_span_code(v, buffer, from, to) do
-               var text = new FlatBuffer
-               buffer.read(text, from, to)
-               var name = text.write_to_string
-               name = name.replace("nullable ", "")
-               var mentity = try_find_mentity(name)
-               if mentity == null then
-                       super
-               else
-                       v.add "<code>"
-                       v.emit_text mentity.html_link.write_to_string
-                       v.add "</code>"
-               end
-       end
-
-       private fun try_find_mentity(text: String): nullable MEntity do
-               var mentity = model.mentity_by_full_name(text, filter)
-               if mentity != null then return mentity
-
-               var mentities = model.mentities_by_name(text, filter)
-               if mentities.is_empty then
-                       return null
-               else if mentities.length > 1 then
-                       # TODO smart resolve conflicts
-               end
-               return mentities.first
-       end
-
-       redef fun add_wikilink(v, token) do
-               v.render_wikilink(token, model)
-       end
-end
-
-# Same as `InlineDecorator` but with wikilink commands handling
-class CmdInlineDecorator
-       super InlineDecorator
-
-       redef type PROCESSOR: CmdMarkdownProcessor
-
-       # Model used by wikilink commands to find entities
-       var model: Model
-
-       redef fun add_wikilink(v, token) do
-               v.render_wikilink(token, model)
-       end
-end
-
-# Custom MarkdownEmitter for commands
-class CmdMarkdownProcessor
-       super MarkdownProcessor
-
-       # Parser used to process doc commands
-       var parser: CommandParser
-
-       # Render a wikilink
-       fun render_wikilink(token: TokenWikiLink, model: Model) do
-               var link = token.link
-               if link == null then return
-               var name = token.name
-               if name != null then link = "{name} | {link}"
-
-               var command = parser.parse(link.write_to_string)
-               var error = parser.error
-
-               if error isa CmdError then
-                       emit_text error.to_html.write_to_string
-                       return
-               end
-               if error isa CmdWarning then
-                       emit_text error.to_html.write_to_string
-               end
-               add command.as(not null).to_html
-       end
-end
-
-redef class Text
-       # Read `self` between `nstart` and `nend` (excluded) and writte chars to `out`.
-       private fun read(out: FlatBuffer, nstart, nend: Int): Int do
-               var pos = nstart
-               while pos < length and pos < nend do
-                       out.add self[pos]
-                       pos += 1
-               end
-               if pos == length then return -1
-               return pos
-       end
-end
index df8b43a..e00a024 100644 (file)
@@ -19,9 +19,11 @@ import commands_catalog
 import commands_graph
 import commands_ini
 import commands_main
+import commands_parser
 import commands_usage
 
 import templates::templates_html
+intrude import markdown::wikilinks
 
 redef class DocCommand
 
@@ -358,3 +360,105 @@ redef class CmdTesting
                return "<pre>{command}</pre>"
        end
 end
+
+# MDoc
+
+# Custom Markdown processor able to process doc commands
+class CmdDecorator
+       super NitdocDecorator
+
+       redef type PROCESSOR: CmdMarkdownProcessor
+
+       # Model used by wikilink commands to find entities
+       var model: Model
+
+       # Filter to apply if any
+       var filter: nullable ModelFilter
+
+       redef fun add_span_code(v, buffer, from, to) do
+               var text = new FlatBuffer
+               buffer.read(text, from, to)
+               var name = text.write_to_string
+               name = name.replace("nullable ", "")
+               var mentity = try_find_mentity(name)
+               if mentity == null then
+                       super
+               else
+                       v.add "<code>"
+                       v.emit_text mentity.html_link.write_to_string
+                       v.add "</code>"
+               end
+       end
+
+       private fun try_find_mentity(text: String): nullable MEntity do
+               var mentity = model.mentity_by_full_name(text, filter)
+               if mentity != null then return mentity
+
+               var mentities = model.mentities_by_name(text, filter)
+               if mentities.is_empty then
+                       return null
+               else if mentities.length > 1 then
+                       # TODO smart resolve conflicts
+               end
+               return mentities.first
+       end
+
+       redef fun add_wikilink(v, token) do
+               v.render_wikilink(token, model)
+       end
+end
+
+# Same as `InlineDecorator` but with wikilink commands handling
+class CmdInlineDecorator
+       super InlineDecorator
+
+       redef type PROCESSOR: CmdMarkdownProcessor
+
+       # Model used by wikilink commands to find entities
+       var model: Model
+
+       redef fun add_wikilink(v, token) do
+               v.render_wikilink(token, model)
+       end
+end
+
+# Custom MarkdownEmitter for commands
+class CmdMarkdownProcessor
+       super MarkdownProcessor
+
+       # Parser used to process doc commands
+       var parser: CommandParser
+
+       # Render a wikilink
+       fun render_wikilink(token: TokenWikiLink, model: Model) do
+               var link = token.link
+               if link == null then return
+               var name = token.name
+               if name != null then link = "{name} | {link}"
+
+               var command = parser.parse(link.write_to_string)
+               var error = parser.error
+
+               if error isa CmdError then
+                       emit_text error.to_html.write_to_string
+                       return
+               end
+               if error isa CmdWarning then
+                       emit_text error.to_html.write_to_string
+               end
+               add command.as(not null).to_html
+       end
+end
+
+redef class Text
+       # Read `self` between `nstart` and `nend` (excluded) and writte chars to `out`.
+       private fun read(out: FlatBuffer, nstart, nend: Int): Int do
+               var pos = nstart
+               while pos < length and pos < nend do
+                       out.add self[pos]
+                       pos += 1
+               end
+               if pos == length then return -1
+               return pos
+       end
+end