-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assign line numbers to all block tokens during parsing. Added unit te…
…sts.
- Loading branch information
1 parent
21cb864
commit 639e1fd
Showing
6 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Test document for line numbers | ||
|
||
See test_line_numbers.py. | ||
Every number written as inline code should match the line number of its nearest | ||
parent block token. Same with titles of link reference definitions. | ||
|
||
## Heading `7` | ||
|
||
Basic paragraph. | ||
|
||
> Block quote (with a paragraph inside) | ||
> It's the start line that counts! `11` | ||
> * a list inside the quote | ||
> * with two items `14` | ||
> | ||
> Still the same block quote, but another paragraph `16` | ||
1. List item `18` | ||
2. Nested list | ||
* item | ||
* another item `21` | ||
3. List item with a nested `22` | ||
> block quote `23` | ||
| Table `25` | Columns | | ||
| ---------- | ------- | | ||
| ? | ! `27` | | ||
|
||
Paragraph with <p>inline HTML</p> `29` | ||
|
||
Setext | ||
heading `31` | ||
------------ | ||
|
||
Paragraph with [ref] to a link reference definition. | ||
|
||
[ref]: /url (37) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import unittest | ||
|
||
import mistletoe.block_token as block_token | ||
import mistletoe.span_token as span_token | ||
from mistletoe.markdown_renderer import ( | ||
LinkReferenceDefinition, | ||
LinkReferenceDefinitionBlock, | ||
) | ||
|
||
|
||
class TestLineNumbers(unittest.TestCase): | ||
def setUp(self) -> None: | ||
block_token.add_token(block_token.HTMLBlock) | ||
span_token.add_token(span_token.HTMLSpan) | ||
block_token.remove_token(block_token.Footnote) | ||
block_token.add_token(LinkReferenceDefinitionBlock) | ||
return super().setUp() | ||
|
||
def tearDown(self) -> None: | ||
span_token.reset_tokens() | ||
block_token.reset_tokens() | ||
return super().tearDown() | ||
|
||
def test_main(self): | ||
# see line_numbers.md for a description of how the test works. | ||
NUMBER_OF_LINE_NUMBERS_TO_BE_CHECKED = 13 | ||
with open("test/samples/line_numbers.md", "r") as fin: | ||
document = block_token.Document(fin) | ||
count = self.check_line_numbers(document) | ||
self.assertEqual(count, NUMBER_OF_LINE_NUMBERS_TO_BE_CHECKED) | ||
|
||
def check_line_numbers(self, token: block_token.BlockToken): | ||
"""Check the line number on the given block token and its children, if possible.""" | ||
count = 0 | ||
line_number = self.get_expected_line_number(token) | ||
if line_number: | ||
self.assertEqual(token.line_number, line_number) | ||
count += 1 | ||
|
||
if isinstance(token, block_token.Table): | ||
count += self.check_line_numbers(token.header) | ||
|
||
for child in token.children: | ||
if isinstance(child, block_token.BlockToken): | ||
count += self.check_line_numbers(child) | ||
|
||
return count | ||
|
||
def get_expected_line_number(self, token: block_token.BlockToken): | ||
# the expected line number, if it exists, should be wrapped in an inline | ||
# code token and be an immediate child of the token. | ||
# or it could be the title of a link reference definition. | ||
for child in token.children: | ||
if isinstance(child, span_token.InlineCode): | ||
return int(child.children[0].content) | ||
if isinstance(child, LinkReferenceDefinition): | ||
return int(child.title) |