From 8b0d16e50c0a376810c751400831d59c20193d1a Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Sun, 15 Mar 2020 18:36:42 +0000 Subject: [PATCH] Update tokens to use `mistletoe.Position` rather than old style tuples --- myst_parser/__init__.py | 2 +- myst_parser/block_tokens.py | 7 ++++--- myst_parser/span_tokens.py | 7 ++++--- .../test_ast/test_block_break_basic_strings0_.yml | 6 ++++-- ...ck_break_following_content_no_space_strings8_.yml | 6 ++++-- .../test_block_break_following_content_strings5_.yml | 6 ++++-- .../test_block_break_following_space_strings6_.yml | 6 ++++-- .../test_block_break_follows_list_strings7_.yml | 12 ++++++++---- .../test_ast/test_block_break_indent_2_strings1_.yml | 6 ++++-- .../test_ast/test_comment_basic_strings0_.yml | 6 ++++-- .../test_ast/test_comment_follows_list_strings5_.yml | 12 ++++++++---- .../test_ast/test_comment_indent_2_strings1_.yml | 6 ++++-- 12 files changed, 53 insertions(+), 29 deletions(-) diff --git a/myst_parser/__init__.py b/myst_parser/__init__.py index a8bf64b2..be838190 100644 --- a/myst_parser/__init__.py +++ b/myst_parser/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.6.0" +__version__ = "0.7.0" def text_to_tokens(text: str): diff --git a/myst_parser/block_tokens.py b/myst_parser/block_tokens.py index 954b9f18..3c6ce68a 100644 --- a/myst_parser/block_tokens.py +++ b/myst_parser/block_tokens.py @@ -4,6 +4,7 @@ import attr from mistletoe import block_tokens +from mistletoe.base_elements import Position from mistletoe.block_tokens import Heading, ThematicBreak, CodeFence from mistletoe.attr_doc import autodoc @@ -62,7 +63,7 @@ def read(cls, lines): return cls( raw=line.splitlines()[0], content=cls.content, - position=(lines.lineno, lines.lineno), + position=Position.from_source_lines(lines), ) @@ -106,7 +107,7 @@ def read(cls, lines): return cls( raw=line.splitlines()[0], content=cls.content, - position=(lines.lineno, lines.lineno), + position=Position.from_source_lines(lines), ) @@ -198,7 +199,7 @@ def read(cls, lines): children=children, loose=loose, start_at=start, - position=(start_line, lines.lineno), + position=Position.from_source_lines(lines, start_line=start_line), ) diff --git a/myst_parser/span_tokens.py b/myst_parser/span_tokens.py index abeb5721..9d85d6bd 100644 --- a/myst_parser/span_tokens.py +++ b/myst_parser/span_tokens.py @@ -1,10 +1,11 @@ import re -from typing import Pattern, Tuple +from typing import Pattern import attr from mistletoe import span_tokens from mistletoe.attr_doc import autodoc +from mistletoe.base_elements import Position __all__ = ("Role", "Target") @@ -24,7 +25,7 @@ class Role(span_tokens.SpanToken): role_name: str = attr.ib(metadata={"doc": "The name of the extension point"}) children = attr.ib(metadata={"doc": "a single RawText node for alternative text."}) - position: Tuple[int, int] = attr.ib( + position: Position = attr.ib( default=None, repr=False, metadata={"doc": "Line position in source text (start, end)"}, @@ -53,7 +54,7 @@ class Target(span_tokens.SpanToken): children = attr.ib( factory=list, metadata={"doc": "a single RawText node for alternative text."} ) - position: Tuple[int, int] = attr.ib( + position: Position = attr.ib( default=None, repr=False, metadata={"doc": "Line position in source text (start, end)"}, diff --git a/tests/test_syntax/test_ast/test_block_break_basic_strings0_.yml b/tests/test_syntax/test_ast/test_block_break_basic_strings0_.yml index 5be5f834..9ce89d33 100644 --- a/tests/test_syntax/test_ast/test_block_break_basic_strings0_.yml +++ b/tests/test_syntax/test_ast/test_block_break_basic_strings0_.yml @@ -1,8 +1,10 @@ children: - content: '' position: - - 1 - - 1 + data: {} + line_end: null + line_start: 1 + uri: null raw: +++ type: BlockBreak footnotes: {} diff --git a/tests/test_syntax/test_ast/test_block_break_following_content_no_space_strings8_.yml b/tests/test_syntax/test_ast/test_block_break_following_content_no_space_strings8_.yml index f4612227..3ee9c861 100644 --- a/tests/test_syntax/test_ast/test_block_break_following_content_no_space_strings8_.yml +++ b/tests/test_syntax/test_ast/test_block_break_following_content_no_space_strings8_.yml @@ -1,8 +1,10 @@ children: - content: a position: - - 1 - - 1 + data: {} + line_end: null + line_start: 1 + uri: null raw: +++a type: BlockBreak footnotes: {} diff --git a/tests/test_syntax/test_ast/test_block_break_following_content_strings5_.yml b/tests/test_syntax/test_ast/test_block_break_following_content_strings5_.yml index fccca83f..2ead80a2 100644 --- a/tests/test_syntax/test_ast/test_block_break_following_content_strings5_.yml +++ b/tests/test_syntax/test_ast/test_block_break_following_content_strings5_.yml @@ -1,8 +1,10 @@ children: - content: a position: - - 1 - - 1 + data: {} + line_end: null + line_start: 1 + uri: null raw: +++ a type: BlockBreak footnotes: {} diff --git a/tests/test_syntax/test_ast/test_block_break_following_space_strings6_.yml b/tests/test_syntax/test_ast/test_block_break_following_space_strings6_.yml index 2ab79154..d6149398 100644 --- a/tests/test_syntax/test_ast/test_block_break_following_space_strings6_.yml +++ b/tests/test_syntax/test_ast/test_block_break_following_space_strings6_.yml @@ -1,8 +1,10 @@ children: - content: '' position: - - 1 - - 1 + data: {} + line_end: null + line_start: 1 + uri: null raw: '+++ ' type: BlockBreak footnotes: {} diff --git a/tests/test_syntax/test_ast/test_block_break_follows_list_strings7_.yml b/tests/test_syntax/test_ast/test_block_break_follows_list_strings7_.yml index b0956887..7d91587d 100644 --- a/tests/test_syntax/test_ast/test_block_break_follows_list_strings7_.yml +++ b/tests/test_syntax/test_ast/test_block_break_follows_list_strings7_.yml @@ -27,14 +27,18 @@ children: type: ListItem loose: false position: - - 0 - - 1 + data: {} + line_end: 1 + line_start: 0 + uri: null start_at: null type: List - content: '' position: - - 2 - - 2 + data: {} + line_end: null + line_start: 2 + uri: null raw: +++ type: BlockBreak footnotes: {} diff --git a/tests/test_syntax/test_ast/test_block_break_indent_2_strings1_.yml b/tests/test_syntax/test_ast/test_block_break_indent_2_strings1_.yml index 43e9760b..7aee4511 100644 --- a/tests/test_syntax/test_ast/test_block_break_indent_2_strings1_.yml +++ b/tests/test_syntax/test_ast/test_block_break_indent_2_strings1_.yml @@ -1,8 +1,10 @@ children: - content: '' position: - - 1 - - 1 + data: {} + line_end: null + line_start: 1 + uri: null raw: ' +++' type: BlockBreak footnotes: {} diff --git a/tests/test_syntax/test_ast/test_comment_basic_strings0_.yml b/tests/test_syntax/test_ast/test_comment_basic_strings0_.yml index a06b0a0c..fe6014e9 100644 --- a/tests/test_syntax/test_ast/test_comment_basic_strings0_.yml +++ b/tests/test_syntax/test_ast/test_comment_basic_strings0_.yml @@ -1,8 +1,10 @@ children: - content: comment position: - - 1 - - 1 + data: {} + line_end: null + line_start: 1 + uri: null raw: '% comment' type: LineComment footnotes: {} diff --git a/tests/test_syntax/test_ast/test_comment_follows_list_strings5_.yml b/tests/test_syntax/test_ast/test_comment_follows_list_strings5_.yml index f685d7a4..afb89f8a 100644 --- a/tests/test_syntax/test_ast/test_comment_follows_list_strings5_.yml +++ b/tests/test_syntax/test_ast/test_comment_follows_list_strings5_.yml @@ -27,14 +27,18 @@ children: type: ListItem loose: false position: - - 0 - - 1 + data: {} + line_end: 1 + line_start: 0 + uri: null start_at: null type: List - content: comment position: - - 2 - - 2 + data: {} + line_end: null + line_start: 2 + uri: null raw: '% comment' type: LineComment footnotes: {} diff --git a/tests/test_syntax/test_ast/test_comment_indent_2_strings1_.yml b/tests/test_syntax/test_ast/test_comment_indent_2_strings1_.yml index 2634ada8..cc2c229c 100644 --- a/tests/test_syntax/test_ast/test_comment_indent_2_strings1_.yml +++ b/tests/test_syntax/test_ast/test_comment_indent_2_strings1_.yml @@ -1,8 +1,10 @@ children: - content: comment position: - - 1 - - 1 + data: {} + line_end: null + line_start: 1 + uri: null raw: ' % comment' type: LineComment footnotes: {}