Skip to content

Commit a3fcc75

Browse files
committed
fix deep html parser block interrupts paragraph
1 parent 66cff5c commit a3fcc75

File tree

6 files changed

+252
-2
lines changed

6 files changed

+252
-2
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.vladsch.flexmark.profiles.pegdown;
2+
3+
import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension;
4+
import com.vladsch.flexmark.html.HtmlRenderer;
5+
import com.vladsch.flexmark.parser.Parser;
6+
import com.vladsch.flexmark.spec.SpecExample;
7+
import com.vladsch.flexmark.spec.SpecReader;
8+
import com.vladsch.flexmark.test.ComboSpecTestCase;
9+
import com.vladsch.flexmark.util.builder.BuilderBase;
10+
import com.vladsch.flexmark.util.options.DataHolder;
11+
import com.vladsch.flexmark.util.options.MutableDataSet;
12+
import org.junit.runners.Parameterized;
13+
14+
import java.util.*;
15+
16+
import static com.vladsch.flexmark.profiles.pegdown.Extensions.*;
17+
18+
public class ComboPegdownDoxiaCompatibilitySpecTest extends ComboSpecTestCase {
19+
private static final String SPEC_RESOURCE = "/pegdown_doxia_compatibility_spec.md";
20+
static final DataHolder OPTIONS = PegdownOptionsAdapter.flexmarkOptions(ALL & ~( HARDWRAPS | ANCHORLINKS )).toMutable()
21+
.set(HtmlRenderer.INDENT_SIZE, 2)
22+
.set(HtmlRenderer.FENCED_CODE_LANGUAGE_CLASS_PREFIX, "")
23+
.set(HtmlRenderer.OBFUSCATE_EMAIL_RANDOM, false)
24+
.set(HtmlRenderer.PERCENT_ENCODE_URLS, true)
25+
;
26+
27+
private static final Map<String, DataHolder> optionsMap = new HashMap<String, DataHolder>();
28+
static {
29+
optionsMap.put("no-deep-parser", new MutableDataSet().set(Parser.HTML_BLOCK_DEEP_PARSER,false ));
30+
}
31+
32+
private static final Parser PARSER = Parser.builder(OPTIONS).build();
33+
// The spec says URL-escaping is optional, but the examples assume that it's enabled.
34+
private static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
35+
36+
private static DataHolder optionsSet(String optionSet) {
37+
return optionsMap.get(optionSet);
38+
}
39+
40+
public ComboPegdownDoxiaCompatibilitySpecTest(SpecExample example) {
41+
super(example);
42+
}
43+
44+
@Parameterized.Parameters(name = "{0}")
45+
public static List<Object[]> data() {
46+
List<SpecExample> examples = SpecReader.readExamples(SPEC_RESOURCE);
47+
List<Object[]> data = new ArrayList<Object[]>();
48+
49+
// NULL example runs full spec test
50+
data.add(new Object[] { SpecExample.NULL });
51+
52+
for (SpecExample example : examples) {
53+
data.add(new Object[] { example });
54+
}
55+
return data;
56+
}
57+
58+
@Override
59+
public DataHolder options(String optionSet) {
60+
return optionsSet(optionSet);
61+
}
62+
63+
@Override
64+
public String getSpecResourceName() {
65+
return SPEC_RESOURCE;
66+
}
67+
68+
@Override
69+
public Parser parser() {
70+
return PARSER;
71+
}
72+
73+
@Override
74+
public HtmlRenderer renderer() {
75+
return RENDERER;
76+
}
77+
}

flexmark-profile-pegdown/src/test/java/com/vladsch/flexmark/profiles/pegdown/PegdownProfileTestSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ComboPegdownSpecTest.class,
88
ComboPegdownCompatibilitySpecTest.class,
99
ComboPegdownExtensionCompatibilitySpecTest.class,
10+
ComboPegdownDoxiaCompatibilitySpecTest.class,
1011
ComboStackOverflowSpecTest.class,
1112
ComboIssueMn236ExceptionSpecTest.class,
1213
})
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Pegdown with Doxia Compatibility Spec
3+
author: Vladimir Schneider
4+
version: 0.2
5+
date: '2017-01-07'
6+
license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
7+
...
8+
9+
---
10+
11+
## Issue 384
12+
13+
Issue #384,
14+
15+
```````````````````````````````` example Issue 384: 1
16+
<a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a>
17+
<div class="bd-callout bd-callout-danger">
18+
→It should be run under same user as OM
19+
</div>
20+
.
21+
<p><a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a></p>
22+
<div class="bd-callout bd-callout-danger">
23+
→It should be run under same user as OM
24+
</div>
25+
.
26+
Document[0, 201]
27+
Paragraph[0, 111]
28+
HtmlInline[0, 78] chars:[0, 78, "<a hr … tml\">"]
29+
Text[78, 106] chars:[78, 106, "Insta … erver"]
30+
HtmlInline[106, 110] chars:[106, 110, "</a>"]
31+
HtmlBlock[111, 201]
32+
````````````````````````````````
33+
34+
35+
```````````````````````````````` example(Issue 384: 2) options(no-deep-parser)
36+
<a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a>
37+
<div class="bd-callout bd-callout-danger">
38+
→It should be run under same user as OM
39+
</div>
40+
.
41+
<p><a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a></p>
42+
<div class="bd-callout bd-callout-danger">
43+
→It should be run under same user as OM
44+
</div>
45+
.
46+
Document[0, 200]
47+
Paragraph[0, 111]
48+
HtmlInline[0, 78] chars:[0, 78, "<a hr … tml\">"]
49+
Text[78, 106] chars:[78, 106, "Insta … erver"]
50+
HtmlInline[106, 110] chars:[106, 110, "</a>"]
51+
HtmlBlock[111, 200]
52+
````````````````````````````````
53+
54+
55+
```````````````````````````````` example Issue 384: 3
56+
<!--
57+
# Licensed under the Apache License, Version 2.0 (the "License") http://www.apache.org/licenses/LICENSE-2.0 -->
58+
59+
# Media Server Installation
60+
61+
## Install Kurento Media server
62+
63+
<a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a>
64+
<div class="bd-callout bd-callout-danger">
65+
→It should be run under same user as OM
66+
</div>
67+
68+
## Specify/Install Turn server
69+
70+
<div class="bd-callout bd-callout-info">Optional step</div>
71+
.
72+
<!--
73+
# Licensed under the Apache License, Version 2.0 (the "License") http://www.apache.org/licenses/LICENSE-2.0 -->
74+
<h1>Media Server Installation</h1>
75+
<h2>Install Kurento Media server</h2>
76+
<p><a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a></p>
77+
<div class="bd-callout bd-callout-danger">
78+
→It should be run under same user as OM
79+
</div>
80+
<h2>Specify/Install Turn server</h2>
81+
<div class="bd-callout bd-callout-info">Optional step</div>
82+
.
83+
Document[0, 475]
84+
HtmlCommentBlock[0, 118]
85+
Heading[119, 146] textOpen:[119, 120, "#"] text:[121, 146, "Media Server Installation"]
86+
Text[121, 146] chars:[121, 146, "Media … ation"]
87+
Heading[148, 179] textOpen:[148, 150, "##"] text:[151, 179, "Install Kurento Media server"]
88+
Text[151, 179] chars:[151, 179, "Insta … erver"]
89+
Paragraph[181, 292]
90+
HtmlInline[181, 259] chars:[181, 259, "<a hr … tml\">"]
91+
Text[259, 287] chars:[259, 287, "Insta … erver"]
92+
HtmlInline[287, 291] chars:[287, 291, "</a>"]
93+
HtmlBlock[292, 382]
94+
Heading[383, 413] textOpen:[383, 385, "##"] text:[386, 413, "Specify/Install Turn server"]
95+
Text[386, 413] chars:[386, 413, "Speci … erver"]
96+
HtmlBlock[415, 475]
97+
````````````````````````````````
98+
99+

flexmark/src/main/java/com/vladsch/flexmark/parser/core/HtmlBlockParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,16 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
306306
deepParser.parseHtmlChunk(line.subSequence(nextNonSpace, line.length()), myHtmlBlockStartOnlyOnBlockTags, myHtmlBlockDeepParseNonBlock, myHtmlBlockDeepParseFirstOpenTagOnOneLine);
307307
if (deepParser.hadHtml()) {
308308
// have our html block start
309-
if (((deepParser.getHtmlMatch() == OPEN_TAG || (!myHtmlCommentBlocksInterruptParagraph && deepParser.getHtmlMatch() == COMMENT)) && matchedBlockParser.getBlockParser().getBlock() instanceof Paragraph)) {
309+
if ((deepParser.getHtmlMatch() == OPEN_TAG || (!myHtmlCommentBlocksInterruptParagraph && deepParser.getHtmlMatch() == COMMENT))
310+
&& (!deepParser.isFirstBlockTag() && matchedBlockParser.getBlockParser().getBlock() instanceof Paragraph)) {
310311
} else {
311312
// not paragraph or can interrupt paragraph
312313
return BlockStart.of(new HtmlBlockParser(state.getProperties(), null, deepParser.getHtmlMatch() == COMMENT, deepParser)).atIndex(state.getIndex());
313314
}
314315
}
315316
} else {
316317
for (int blockType = 1; blockType <= 7; blockType++) {
317-
// Type 7 can not interrupt a paragraph or may not start a block altogether
318+
// Type 7 cannot interrupt a paragraph or may not start a block altogether
318319
if (blockType == 7 && (myHtmlBlockStartOnlyOnBlockTags || matchedBlockParser.getBlockParser().getBlock() instanceof Paragraph)) {
319320
continue;
320321
}

flexmark/src/main/java/com/vladsch/flexmark/parser/internal/HtmlDeepParser.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public enum HtmlMatch {
101101
private HtmlMatch myHtmlMatch;
102102
private int myHtmlCount;
103103
final private HashSet<String> myBlockTags;
104+
private boolean myFirstBlockTag;
104105

105106
public HtmlDeepParser() {
106107
this(Collections.<String>emptyList());
@@ -111,6 +112,7 @@ public HtmlDeepParser(List<String> customTags) {
111112
myClosingPattern = null;
112113
myHtmlMatch = null;
113114
myHtmlCount = 0;
115+
myFirstBlockTag = false;
114116

115117
myBlockTags = new HashSet<>(BLOCK_TAGS);
116118
myBlockTags.addAll(customTags);
@@ -132,6 +134,10 @@ public int getHtmlCount() {
132134
return myHtmlCount;
133135
}
134136

137+
public boolean isFirstBlockTag() {
138+
return myFirstBlockTag;
139+
}
140+
135141
public boolean isHtmlClosed() {
136142
return myClosingPattern == null && myOpenTags.isEmpty();
137143
}
@@ -144,6 +150,17 @@ public boolean haveOpenRawTag() {
144150
return myClosingPattern != null && myHtmlMatch != HtmlMatch.OPEN_TAG;
145151
}
146152

153+
public boolean haveOpenBlockTag() {
154+
if (!myOpenTags.isEmpty()) {
155+
for (String openTag : myOpenTags) {
156+
if (myBlockTags.contains(openTag)) {
157+
return true;
158+
}
159+
}
160+
}
161+
return false;
162+
}
163+
147164
public boolean hadHtml() {
148165
return myHtmlCount > 0 || !isHtmlClosed();
149166
}
@@ -161,6 +178,7 @@ private void openTag(final String tagName) {
161178
}
162179
}
163180
myOpenTags.add(tagName);
181+
myFirstBlockTag = myBlockTags.contains(tagName);
164182
}
165183

166184
public void parseHtmlChunk(CharSequence html, boolean blockTagsOnly, final boolean parseNonBlock, final boolean firstOpenTagOnOneLine) {

flexmark/src/test/resources/issues_ast_spec.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,60 @@ Document[0, 128]
13971397
````````````````````````````````
13981398

13991399

1400+
## Issue 384
1401+
1402+
Issue #384,
1403+
1404+
```````````````````````````````` example(Issue 384: 1) options(keep-blank-lines)
1405+
<!--
1406+
# Licensed under the Apache License, Version 2.0 (the "License") http://www.apache.org/licenses/LICENSE-2.0 -->
1407+
1408+
# Media Server Installation
1409+
1410+
## Install Kurento Media server
1411+
1412+
<a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a>
1413+
<div class="bd-callout bd-callout-danger">
1414+
→It should be run under same user as OM
1415+
</div>
1416+
1417+
## Specify/Install Turn server
1418+
1419+
<div class="bd-callout bd-callout-info">Optional step</div>
1420+
.
1421+
<!--
1422+
# Licensed under the Apache License, Version 2.0 (the "License") http://www.apache.org/licenses/LICENSE-2.0 -->
1423+
<h1>Media Server Installation</h1>
1424+
<h2>Install Kurento Media server</h2>
1425+
<p><a href="https://doc-kurento.readthedocs.io/en/stable/user/installation.html">Install Kurento Media server</a></p>
1426+
<div class="bd-callout bd-callout-danger">
1427+
→It should be run under same user as OM
1428+
</div>
1429+
<h2>Specify/Install Turn server</h2>
1430+
<div class="bd-callout bd-callout-info">Optional step</div>
1431+
.
1432+
Document[0, 474]
1433+
HtmlCommentBlock[0, 118]
1434+
BlankLine[118, 119]
1435+
Heading[119, 146] textOpen:[119, 120, "#"] text:[121, 146, "Media Server Installation"]
1436+
Text[121, 146] chars:[121, 146, "Media … ation"]
1437+
BlankLine[147, 148]
1438+
Heading[148, 179] textOpen:[148, 150, "##"] text:[151, 179, "Install Kurento Media server"]
1439+
Text[151, 179] chars:[151, 179, "Insta … erver"]
1440+
BlankLine[180, 181]
1441+
Paragraph[181, 292]
1442+
HtmlInline[181, 259] chars:[181, 259, "<a hr … tml\">"]
1443+
Text[259, 287] chars:[259, 287, "Insta … erver"]
1444+
HtmlInline[287, 291] chars:[287, 291, "</a>"]
1445+
HtmlBlock[292, 382]
1446+
BlankLine[382, 383]
1447+
Heading[383, 413] textOpen:[383, 385, "##"] text:[386, 413, "Specify/Install Turn server"]
1448+
Text[386, 413] chars:[386, 413, "Speci … erver"]
1449+
BlankLine[414, 415]
1450+
HtmlBlock[415, 474]
1451+
````````````````````````````````
1452+
1453+
14001454
## Issue xxx-2
14011455

14021456
Issue xxx-2, leading spaces tab not part of indented code, which is correct

0 commit comments

Comments
 (0)