Skip to content

Commit

Permalink
820 (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdewinter authored Dec 19, 2023
1 parent ffe19aa commit 4d58dd0
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 26 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ for a version 1.0 release in early 2
- Extension: Strikethrough
- [Issue 805](https://github.com/jackdewinter/pymarkdown/issues/805)
- Extension: Task List Items
- [Issue 820](https://github.com/jackdewinter/pymarkdown/issues/820)
- Rule MD035 - Added fix options
- [Issue 821](https://github.com/jackdewinter/pymarkdown/issues/821)
- Rule MD037 - Added fix options
- [Issue 822](https://github.com/jackdewinter/pymarkdown/issues/822)
Expand Down
10 changes: 10 additions & 0 deletions docs/rules/rule_md035.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
| `md035` |
| `hr-style` |

| Autofix Available |
| --- |
| Yes |

## Summary

Horizontal rule style.
Expand Down Expand Up @@ -78,3 +82,9 @@ is made, so that the following example will not trigger this rule:

This rule is largely inspired by the MarkdownLint rule
[MD035](https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md#md035---horizontal-rule-style).

## Fix Description

All thematic breaks are replaced with the configured thematic break text. If the
configuration is the default `consistent`, then the first thematic break in the
document sets the thematic break text used throughout the document.
8 changes: 4 additions & 4 deletions publish/coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"projectName": "pymarkdown",
"reportSource": "pytest",
"branchLevel": {
"totalMeasured": 4467,
"totalCovered": 4467
"totalMeasured": 4479,
"totalCovered": 4479
},
"lineLevel": {
"totalMeasured": 18480,
"totalCovered": 18480
"totalMeasured": 18508,
"totalCovered": 18508
}
}

10 changes: 9 additions & 1 deletion publish/test-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@
},
{
"name": "test.rules.test_md035",
"totalTests": 19,
"totalTests": 27,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
Expand Down Expand Up @@ -1705,6 +1705,14 @@
"errorTests": 0,
"skippedTests": 0,
"elapsedTimeInMilliseconds": 0
},
{
"name": "test.tokens.test_thematic_break_markdown_token",
"totalTests": 1,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
"elapsedTimeInMilliseconds": 0
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions pymarkdown/plugins/rule_md_001.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import cast

from pymarkdown.extensions.front_matter_markdown_token import FrontMatterMarkdownToken
from pymarkdown.plugin_manager.plugin_details import PluginDetails, PluginDetailsV2
from pymarkdown.plugin_manager.plugin_details import PluginDetailsV2
from pymarkdown.plugin_manager.plugin_scan_context import PluginScanContext
from pymarkdown.plugin_manager.rule_plugin import RulePlugin
from pymarkdown.tokens.markdown_token import MarkdownToken
Expand All @@ -23,7 +23,7 @@ def __init__(self) -> None:
self.__last_heading_count: int = 0
self.__front_matter_title: str = ""

def get_details(self) -> PluginDetails:
def get_details(self) -> PluginDetailsV2:
"""
Get the details for the plugin.
"""
Expand Down
41 changes: 32 additions & 9 deletions pymarkdown/plugins/rule_md_035.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from typing import cast

from pymarkdown.plugin_manager.plugin_details import PluginDetails
from pymarkdown.plugin_manager.plugin_details import PluginDetailsV2
from pymarkdown.plugin_manager.plugin_scan_context import PluginScanContext
from pymarkdown.plugin_manager.rule_plugin import RulePlugin
from pymarkdown.tokens.markdown_token import MarkdownToken
Expand All @@ -22,19 +22,19 @@ def __init__(self) -> None:
self.__rule_style: str = ""
self.__actual_style: str = ""

def get_details(self) -> PluginDetails:
def get_details(self) -> PluginDetailsV2:
"""
Get the details for the plugin.
"""
return PluginDetails(
return PluginDetailsV2(
plugin_name="hr-style",
plugin_id="MD035",
plugin_enabled_by_default=True,
plugin_description="Horizontal rule style",
plugin_version="0.5.0",
plugin_interface_version=1,
plugin_url="https://github.com/jackdewinter/pymarkdown/blob/main/docs/rules/rule_md035.md",
plugin_configuration="style",
plugin_supports_fix=True,
)

@classmethod
Expand All @@ -47,14 +47,21 @@ def __validate_configuration_style(cls, found_value: str) -> None:
)
is_valid = bool(found_value)
if is_valid:
valid_character = None
for next_character in found_value:
if next_character not in " _-*":
is_valid = False
break
if next_character != " ":
if valid_character is None:
valid_character = next_character
elif next_character != valid_character:
is_valid = False
break
if not is_valid:
raise ValueError(
f"Allowable values are: {RuleMd035.__consistent_style}, "
+ "'---', '***', or any other horizontal rule text."
+ "'---', '***', `___`, or any other horizontal rule text."
)

def initialize_from_config(self) -> None:
Expand Down Expand Up @@ -84,9 +91,25 @@ def next_token(self, context: PluginScanContext, token: MarkdownToken) -> None:
break_token = cast(ThematicBreakMarkdownToken, token)
if self.__actual_style:
if self.__actual_style != break_token.rest_of_line:
extra_data = f"Expected: {self.__actual_style}, Actual: {break_token.rest_of_line}"
self.report_next_token_error(
context, token, extra_error_information=extra_data
)
if context.in_fix_mode:
self.register_fix_token_request(
context,
token,
"next_token",
"start_character",
self.__actual_style[0],
)
self.register_fix_token_request(
context,
token,
"next_token",
"rest_of_line",
self.__actual_style,
)
else:
extra_data = f"Expected: {self.__actual_style}, Actual: {break_token.rest_of_line}"
self.report_next_token_error(
context, token, extra_error_information=extra_data
)
else:
self.__actual_style = break_token.rest_of_line
8 changes: 4 additions & 4 deletions pymarkdown/plugins/rule_md_037.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from typing import List, Optional, Tuple, cast

from pymarkdown.plugin_manager.plugin_details import PluginDetails
from pymarkdown.plugin_manager.plugin_details import PluginDetailsV2
from pymarkdown.plugin_manager.plugin_scan_context import PluginScanContext
from pymarkdown.plugin_manager.rule_plugin import RulePlugin
from pymarkdown.tokens.markdown_token import MarkdownToken
Expand All @@ -21,18 +21,18 @@ def __init__(self) -> None:
self.__start_emphasis_token: Optional[TextMarkdownToken] = None
self.__emphasis_token_list: List[MarkdownToken] = []

def get_details(self) -> PluginDetails:
def get_details(self) -> PluginDetailsV2:
"""
Get the details for the plugin.
"""
return PluginDetails(
return PluginDetailsV2(
plugin_name="no-space-in-emphasis",
plugin_id="MD037",
plugin_enabled_by_default=True,
plugin_description="Spaces inside emphasis markers",
plugin_version="0.5.0",
plugin_interface_version=1,
plugin_url="https://github.com/jackdewinter/pymarkdown/blob/main/docs/rules/rule_md037.md",
plugin_supports_fix=True,
)

def starting_new_file(self) -> None:
Expand Down
32 changes: 28 additions & 4 deletions pymarkdown/tokens/thematic_break_markdown_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
Module to provide for an encapsulation of the thematic break element.
"""

from typing import Optional, cast
from typing import Optional, Union, cast

from typing_extensions import override

from pymarkdown.general.parser_helper import ParserHelper
from pymarkdown.general.position_marker import PositionMarker
Expand All @@ -29,13 +31,13 @@ def __init__(
position_marker: PositionMarker,
) -> None:
assert extracted_whitespace is not None
self.__start_character = start_character
self.__extracted_whitespace = extracted_whitespace
self.__rest_of_line = rest_of_line
LeafMarkdownToken.__init__(
self,
MarkdownToken._token_thematic_break,
MarkdownToken.extra_data_separator.join(
[start_character, extracted_whitespace, self.__rest_of_line]
),
self.__compose_extra_data_field(),
position_marker=position_marker,
extracted_whitespace=extracted_whitespace,
)
Expand All @@ -57,6 +59,28 @@ def rest_of_line(self) -> str:
"""
return self.__rest_of_line

def __compose_extra_data_field(self) -> str:
"""
Compose the object's self.extra_data field from the local object's variables.
"""
x = MarkdownToken.extra_data_separator.join(
[self.__start_character, self.__extracted_whitespace, self.__rest_of_line]
)
self._set_extra_data(x)
return x

@override
def _modify_token(self, field_name: str, field_value: Union[str, int]) -> bool:
if field_name == "start_character" and isinstance(field_value, str):
self.__start_character = field_value
self.__compose_extra_data_field()
return True
if field_name == "rest_of_line" and isinstance(field_value, str):
self.__rest_of_line = field_value
self.__compose_extra_data_field()
return True
return False

def register_for_markdown_transform(
self, registration_function: RegisterMarkdownTransformHandlersProtocol
) -> None:
Expand Down
Loading

0 comments on commit 4d58dd0

Please sign in to comment.