1 module hunt.markdown.internal.ParagraphParser;
2 
3 import hunt.markdown.internal.ReferenceParser;
4 import hunt.markdown.internal.util.Parsing;
5 import hunt.markdown.internal.BlockContent;
6 import hunt.markdown.node.Block;
7 import hunt.markdown.node.Paragraph;
8 import hunt.markdown.parser.block.AbstractBlockParser;
9 import hunt.markdown.parser.block.BlockContinue;
10 import hunt.markdown.parser.InlineParser;
11 import hunt.markdown.parser.block.ParserState;
12 import hunt.markdown.parser.block.BlockParser;
13 import hunt.util.Comparator;
14 import hunt.text.Common;
15 
16 class ParagraphParser : AbstractBlockParser {
17 
18     private Paragraph block;
19     private BlockContent content;
20 
21     this()
22     {
23         block = new Paragraph();
24         content = new BlockContent();
25     }
26 
27     override public Block getBlock() {
28         return block;
29     }
30 
31     public BlockContinue tryContinue(ParserState state) {
32         if (!state.isBlank()) {
33             return BlockContinue.atIndex(state.getIndex());
34         } else {
35             return BlockContinue.none();
36         }
37     }
38 
39     override public void addLine(string line) {
40         content.add(line);
41     }
42 
43     override public void closeBlock() {
44     }
45 
46     override int opCmp(BlockParser o)
47     {
48         auto cmp = compare(getBlock(),o.getBlock());
49         import hunt.logging;
50         logDebug("------223-2--");
51         return cmp;
52     }
53 
54     public void closeBlock(ReferenceParser inlineParser) {
55         string contentString = content.getString();
56         bool hasReferenceDefs = false;
57 
58         int pos;
59         // try parsing the beginning as link reference definitions:
60         while (contentString.length > 3 && contentString[0] == '[' &&
61                 (pos = inlineParser.parseReference(contentString)) != 0) {
62             contentString = contentString.substring(pos);
63             hasReferenceDefs = true;
64         }
65         if (hasReferenceDefs && Parsing.isBlank(contentString)) {
66             block.unlink();
67             content = null;
68         } else {
69             content = new BlockContent(contentString);
70         }
71     }
72 
73     override public void parseInlines(InlineParser inlineParser) {
74         if (content !is null) {
75             inlineParser.parse(content.getString(), block);
76         }
77     }
78 
79     public string getContentString() {
80         return content.getString();
81     }
82 }