markdown: trim indenting spaces from code-blocks in MDoc
authorJean Privat <jean@pryen.org>
Tue, 3 Jun 2014 12:49:33 +0000 (08:49 -0400)
committerJean Privat <jean@pryen.org>
Tue, 3 Jun 2014 12:49:33 +0000 (08:49 -0400)
So there is no more useless margin if code block is indented with 6 or more
spaces.

Signed-off-by: Jean Privat <jean@pryen.org>

src/markdown.nit

index ed73ead..f0a507d 100644 (file)
@@ -64,15 +64,12 @@ private class Doc2Mdwn
                                end
                                # else fence content
                                curblock.add(text)
-                               curblock.add("\n")
                                continue
                        end
 
                        # Is codeblock? Then just collect them
                        if indent >= 4 then
-                               var part = text.substring_from(4)
-                               curblock.add(part)
-                               curblock.add("\n")
+                               curblock.add(text)
                                continue
                        end
 
@@ -187,10 +184,27 @@ private class Doc2Mdwn
        do
                # Is there a codeblock to manage?
                if not curblock.is_empty then
+                       # determine the smalest indent
+                       var minindent = -1
+                       for text in curblock do
+                               var indent = 0
+                               while indent < text.length and text.chars[indent] == ' ' do indent += 1
+                               if minindent == -1 or indent < minindent then
+                                       minindent = indent
+                               end
+                       end
+
+                       # Generate the text
+                       var btext = new FlatBuffer
+                       for text in curblock do
+                               btext.append text.substring_from(minindent)
+                               btext.add '\n'
+                       end
+
+                       # add the node
                        var n = new HTMLTag("pre")
                        root.add(n)
-                       var btext = curblock.to_s
-                       process_code(n, btext)
+                       process_code(n, btext.to_s)
                        curblock.clear
                end
        end