stdlib/strings: Abstracted hash (breaks hash on Buffer temporarily)
[nit.git] / src / markdown.nit
index 9f21f32..d4b7a81 100644 (file)
@@ -17,9 +17,12 @@ module markdown
 
 import parser
 import html
+import highlight
 
 # The class that does the convertion from a `ADoc` to HTML
 private class Doc2Mdwn
+       var toolcontext: ToolContext
+
        # The lines of the current code block, empty is no current code block
        var curblock = new Array[String]
 
@@ -48,7 +51,7 @@ private class Doc2Mdwn
                        # Count the number of spaces
                        lastindent = indent
                        indent = 0
-                       while text.length > indent and text[indent] == ' ' do indent += 1
+                       while text.length > indent and text.chars[indent] == ' ' do indent += 1
 
                        # Is codeblock? Then just collect them
                        if indent > 4 then
@@ -149,8 +152,7 @@ private class Doc2Mdwn
                                # Code part
                                var n2 = new HTMLTag("code")
                                n.add(n2)
-
-                               n2.text part
+                               process_code(n2, part)
                        end
                        is_text = not is_text
                end
@@ -163,26 +165,43 @@ private class Doc2Mdwn
                        var n = new HTMLTag("pre")
                        root.add(n)
                        var btext = curblock.to_s
-
-                       n.append btext
-
+                       process_code(n, btext)
                        curblock.clear
                end
        end
+
+       fun process_code(n: HTMLTag, text: String)
+       do
+               # Try to parse it
+               var ast = toolcontext.parse_something(text)
+
+               if ast isa AError then
+                       n.append text
+                       # n.attrs["title"] = ast.message
+                       n.add_class("rawcode")
+               else
+                       var v = new HighlightVisitor
+                       v.enter_visit(ast)
+                       n.add(v.html)
+                       n.add_class("nitcode")
+               end
+       end
 end
 
 redef class ADoc
        # Build a `<div>` element that contains the full documentation in HTML
        fun full_markdown: HTMLTag
        do
-               var d2m = new Doc2Mdwn
+               var tc = new ToolContext
+               var d2m = new Doc2Mdwn(tc)
                return d2m.work(self)
        end
 
        # Build a `<span>` element that contains the synopsys in HTML
        fun short_markdown: HTMLTag
        do
-               var d2m = new Doc2Mdwn
+               var tc = new ToolContext
+               var d2m = new Doc2Mdwn(tc)
                return d2m.short_work(self)
        end
 end