lib/markdown: factorize clones `code_from_block` into `BlockCode::raw_content`
[nit.git] / lib / markdown / markdown.nit
index 854eed5..9124ad7 100644 (file)
@@ -169,7 +169,7 @@ class MarkdownProcessor
                                if c == '\n' then
                                        eol = true
                                else if c == '\t' then
-                                       var np = pos + (4 - (pos.bin_and(3)))
+                                       var np = pos + (4 - (pos & 3))
                                        while pos < np do
                                                value.add ' '
                                                pos += 1
@@ -646,6 +646,11 @@ interface Decorator
        # Kind of emitter used for decoration.
        type EMITTER: MarkdownEmitter
 
+       # Render a single plain char.
+       #
+       # Redefine this method to add special escaping for plain text.
+       fun add_char(v: EMITTER, c: Char) do v.addc c
+
        # Render a ruler block.
        fun add_ruler(v: EMITTER, block: BlockRuler) is abstract
 
@@ -1168,6 +1173,26 @@ abstract class Block
                        block = block.next
                end
        end
+
+       # The raw content of the block as a multi-line string.
+       fun raw_content: String do
+               var infence = self isa BlockFence
+               var text = new FlatBuffer
+               var line = self.block.first_line
+               while line != null do
+                       if not line.is_empty then
+                               var str = line.value
+                               if not infence and str.has_prefix("    ") then
+                                       text.append str.substring(4, str.length - line.trailing)
+                               else
+                                       text.append str
+                               end
+                       end
+                       text.append "\n"
+                       line = line.next
+               end
+               return text.write_to_string
+       end
 end
 
 # A block without any markdown specificities.
@@ -1838,7 +1863,7 @@ end
 
 # A markdown list line.
 # Mainly used to factorize code between ordered and unordered lists.
-class LineList
+abstract class LineList
        super Line
 
        redef fun process(v) do
@@ -1918,7 +1943,7 @@ abstract class Token
        var char: Char
 
        # Output that token using `MarkdownEmitter::decorator`.
-       fun emit(v: MarkdownEmitter) do v.addc char
+       fun emit(v: MarkdownEmitter) do v.decorator.add_char(v, char)
 end
 
 # A token without a specific meaning.
@@ -2102,6 +2127,7 @@ abstract class TokenLinkOrImage
                                        if pos == -1 then return -1
                                end
                        end
+                       if pos < start then return -1
                        if md[pos] != ')' then return -1
                else if md[pos] == '[' then
                        pos += 1