Skip to content

👌 Centralise indented code block test #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions markdown_it/rules_block/blockquote.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def blockquote(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
pos = state.bMarks[startLine] + state.tShift[startLine]
max = state.eMarks[startLine]

# if it's indented more than 3 spaces, it should be a code block
if (state.sCount[startLine] - state.blkIndent) >= 4:
if state.is_code_block(startLine):
return False

# check the block quote marker
Expand Down
4 changes: 2 additions & 2 deletions markdown_it/rules_block/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def code(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
LOGGER.debug("entering code: %s, %s, %s, %s", state, startLine, endLine, silent)

if state.sCount[startLine] - state.blkIndent < 4:
if not state.is_code_block(startLine):
return False

last = nextLine = startLine + 1
Expand All @@ -19,7 +19,7 @@ def code(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
nextLine += 1
continue

if state.sCount[nextLine] - state.blkIndent >= 4:
if state.is_code_block(nextLine):
nextLine += 1
last = nextLine
continue
Expand Down
6 changes: 2 additions & 4 deletions markdown_it/rules_block/fence.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def fence(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

if pos + 3 > maximum:
Expand Down Expand Up @@ -72,8 +71,7 @@ def fence(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
except IndexError:
break

if state.sCount[nextLine] - state.blkIndent >= 4:
# closing fence should be indented less than 4 spaces
if state.is_code_block(nextLine):
continue

pos = state.skipChars(pos, marker)
Expand Down
3 changes: 1 addition & 2 deletions markdown_it/rules_block/heading.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def heading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bo
pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

ch: int | None = state.srcCharCode[pos]
Expand Down
3 changes: 1 addition & 2 deletions markdown_it/rules_block/hr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def hr(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

try:
Expand Down
3 changes: 1 addition & 2 deletions markdown_it/rules_block/html_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def html_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

if not state.md.options.get("html", None):
Expand Down
3 changes: 1 addition & 2 deletions markdown_it/rules_block/lheading.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def lheading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> b
ruler: Ruler = state.md.block.ruler
terminatorRules = ruler.getRules("paragraph")

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

oldParentType = state.parentType
Expand Down
6 changes: 2 additions & 4 deletions markdown_it/rules_block/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
isTerminatingParagraph = False
tight = True

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

# Special case:
Expand Down Expand Up @@ -295,8 +294,7 @@ def list_block(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
if state.sCount[nextLine] < state.blkIndent:
break

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
break

# fail if terminating block found
Expand Down
3 changes: 1 addition & 2 deletions markdown_it/rules_block/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def reference(state: StateBlock, startLine: int, _endLine: int, silent: bool) ->
maximum = state.eMarks[startLine]
nextLine = startLine + 1

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False

if state.srcCharCode[pos] != 0x5B: # /* [ */
Expand Down
9 changes: 9 additions & 0 deletions markdown_it/rules_block/state_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ def __init__(

self.lineMax = len(self.bMarks) - 1 # don't count last fake line

# pre-check if code blocks are enabled, to speed up is_code_block method
self._code_enabled = "code" in self.md["block"].ruler.get_active_rules()

def __repr__(self) -> str:
return (
f"{self.__class__.__name__}"
Expand Down Expand Up @@ -228,3 +231,9 @@ def getLines(self, begin: int, end: int, indent: int, keepLastLF: bool) -> str:
i += 1

return "".join(queue)

def is_code_block(self, line: int) -> bool:
"""Check if line is a code block,
i.e. the code block rule is enabled and text is indented by more than 3 spaces.
"""
return self._code_enabled and (self.sCount[line] - self.blkIndent) >= 4
7 changes: 3 additions & 4 deletions markdown_it/rules_block/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
if state.sCount[nextLine] < state.blkIndent:
return False

# if it's indented more than 3 spaces, it should be a code block
if state.sCount[nextLine] - state.blkIndent >= 4:
if state.is_code_block(nextLine):
return False

# first character of the second line should be '|', '-', ':',
Expand Down Expand Up @@ -126,7 +125,7 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
lineText = getLine(state, startLine).strip()
if "|" not in lineText:
return False
if state.sCount[startLine] - state.blkIndent >= 4:
if state.is_code_block(startLine):
return False
columns = escapedSplit(lineText)
if columns and columns[0] == "":
Expand Down Expand Up @@ -192,7 +191,7 @@ def table(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool
lineText = getLine(state, nextLine).strip()
if not lineText:
break
if state.sCount[nextLine] - state.blkIndent >= 4:
if state.is_code_block(nextLine):
break
columns = escapedSplit(lineText)
if columns and columns[0] == "":
Expand Down
69 changes: 69 additions & 0 deletions tests/test_port/fixtures/disable_code_block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
indent paragraph
.
This is a paragraph,
with multiple lines.

This paragraph
has variable indents,
like this.
.
<p>This is a paragraph,
with multiple lines.</p>
<p>This paragraph
has variable indents,
like this.</p>
.

indent in HTML
.
<div>

Paragraph

</div>
.
<div>
<p>Paragraph</p>
</div>
.

indent fence
.
```python
def foo():
pass
```
.
<pre><code class="language-python">def foo():
pass
</code></pre>
.

indent heading
.
# Heading
.
<h1>Heading</h1>
.

indent table
.
| foo | bar |
| --- | --- |
| baz | bim |
.
<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>baz</td>
<td>bim</td>
</tr>
</tbody>
</table>
.
11 changes: 11 additions & 0 deletions tests/test_port/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ def test_strikethrough(line, title, input, expected):
assert text.rstrip() == expected.rstrip()


@pytest.mark.parametrize(
"line,title,input,expected",
read_fixture_file(FIXTURE_PATH.joinpath("disable_code_block.md")),
)
def test_disable_code_block(line, title, input, expected):
md = MarkdownIt().enable("table").disable("code")
text = md.render(input)
print(text.rstrip())
assert text.rstrip() == expected.rstrip()


@pytest.mark.parametrize(
"line,title,input,expected",
read_fixture_file(FIXTURE_PATH.joinpath("issue-fixes.md")),
Expand Down