Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdewinter authored Dec 18, 2023
1 parent f0f5866 commit ffe19aa
Show file tree
Hide file tree
Showing 9 changed files with 653 additions and 15 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 821](https://github.com/jackdewinter/pymarkdown/issues/821)
- Rule MD037 - Added fix options
- [Issue 822](https://github.com/jackdewinter/pymarkdown/issues/822)
- Rule MD038 - Added fix options
- [Issue 823](https://github.com/jackdewinter/pymarkdown/issues/823)
Expand Down
8 changes: 8 additions & 0 deletions docs/rules/rule_md037.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
| `md037` |
| `no-space-in-emphasis` |

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

## Summary

Spaces inside emphasis markers.
Expand Down Expand Up @@ -66,3 +70,7 @@ and emphasis sequences. Therefore, text such as `this * is not * emphasis`
raised triggered on both the first and the second emphasis characters.
This rule looks for scenarios where there are a matched pair of emphasis
characters, instead of just looking for those individual characters.

## Fix Description

Within the block of emphasized text, any leading and trailing whitespace is removed.
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": 4451,
"totalCovered": 4451
"totalMeasured": 4467,
"totalCovered": 4467
},
"lineLevel": {
"totalMeasured": 18452,
"totalCovered": 18452
"totalMeasured": 18480,
"totalCovered": 18480
}
}

6 changes: 4 additions & 2 deletions publish/pylint_suppression.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@
},
"pymarkdown/plugins/rule_md_035.py": {},
"pymarkdown/plugins/rule_md_036.py": {},
"pymarkdown/plugins/rule_md_037.py": {},
"pymarkdown/plugins/rule_md_037.py": {
"too-many-arguments": 1
},
"pymarkdown/plugins/rule_md_038.py": {},
"pymarkdown/plugins/rule_md_039.py": {},
"pymarkdown/plugins/rule_md_040.py": {},
Expand Down Expand Up @@ -485,7 +487,7 @@
"too-many-instance-attributes": 23,
"too-many-public-methods": 4,
"too-few-public-methods": 39,
"too-many-arguments": 216,
"too-many-arguments": 217,
"too-many-locals": 33,
"chained-comparison": 1,
"too-many-boolean-expressions": 2,
Expand Down
10 changes: 9 additions & 1 deletion publish/test-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@
},
{
"name": "test.rules.test_md037",
"totalTests": 10,
"totalTests": 21,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
Expand Down Expand Up @@ -1697,6 +1697,14 @@
"errorTests": 0,
"skippedTests": 0,
"elapsedTimeInMilliseconds": 0
},
{
"name": "test.tokens.test_text_markdown_token",
"totalTests": 1,
"failedTests": 0,
"errorTests": 0,
"skippedTests": 0,
"elapsedTimeInMilliseconds": 0
}
]
}
Expand Down
79 changes: 72 additions & 7 deletions pymarkdown/plugins/rule_md_037.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Module to implement a plugin that looks for spaces within emphasis sections.
"""
from typing import List, Optional, cast
from typing import List, Optional, Tuple, cast

from pymarkdown.plugin_manager.plugin_details import PluginDetails
from pymarkdown.plugin_manager.plugin_scan_context import PluginScanContext
Expand Down Expand Up @@ -43,29 +43,94 @@ def starting_new_file(self) -> None:
self.__start_emphasis_token = None
self.__emphasis_token_list = []

# pylint: disable=too-many-arguments
def __fix(
self,
context: PluginScanContext,
start_token: Optional[TextMarkdownToken],
end_token: Optional[TextMarkdownToken],
did_first_start_with_space: bool,
did_last_end_with_space: bool,
) -> None:
if start_token == end_token:
assert start_token is not None
adjusted_token_text = start_token.token_text
if did_first_start_with_space:
adjusted_token_text = adjusted_token_text.lstrip()
if did_last_end_with_space:
adjusted_token_text = adjusted_token_text.rstrip()
self.register_fix_token_request(
context,
start_token,
"next_token",
"token_text",
adjusted_token_text,
)
else:
if did_first_start_with_space:
assert start_token is not None
adjusted_token_text = start_token.token_text.lstrip()
self.register_fix_token_request(
context,
start_token,
"next_token",
"token_text",
adjusted_token_text,
)
if did_last_end_with_space:
assert end_token is not None
adjusted_token_text = end_token.token_text.rstrip()
self.register_fix_token_request(
context,
end_token,
"next_token",
"token_text",
adjusted_token_text,
)

# pylint: enable=too-many-arguments

def __handle_emphasis_text(
self, context: PluginScanContext, token: MarkdownToken
) -> None: # sourcery skip: extract-method
text_token = cast(TextMarkdownToken, token)
assert self.__start_emphasis_token is not None
if text_token.token_text == self.__start_emphasis_token.token_text:
assert self.__emphasis_token_list
did_first_start_with_space = self.__handle_emphasis_text_space_check(0)
did_last_end_with_space = self.__handle_emphasis_text_space_check(-1)
(
start_token,
did_first_start_with_space,
) = self.__handle_emphasis_text_space_check(0)
(
end_token,
did_last_end_with_space,
) = self.__handle_emphasis_text_space_check(-1)
if did_first_start_with_space or did_last_end_with_space:
assert self.__start_emphasis_token is not None
self.report_next_token_error(context, self.__start_emphasis_token)
if context.in_fix_mode:
self.__fix(
context,
start_token,
end_token,
did_first_start_with_space,
did_last_end_with_space,
)
else:
self.report_next_token_error(context, self.__start_emphasis_token)

self.__start_emphasis_token = None
self.__emphasis_token_list = []
else:
self.__emphasis_token_list.append(token)

def __handle_emphasis_text_space_check(self, token_text_index: int) -> bool:
def __handle_emphasis_text_space_check(
self, token_text_index: int
) -> Tuple[Optional[TextMarkdownToken], bool]:
first_capture_token = self.__emphasis_token_list[token_text_index]
assert first_capture_token.is_text
if not first_capture_token.is_text:
return None, False
other_text_token = cast(TextMarkdownToken, first_capture_token)
return other_text_token.token_text[token_text_index] == " "
return other_text_token, other_text_token.token_text[token_text_index] == " "

def __handle_start_emphasis(
self, context: PluginScanContext, token: MarkdownToken
Expand Down
13 changes: 12 additions & 1 deletion pymarkdown/tokens/text_markdown_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""

import logging
from typing import List, Optional, Tuple, cast
from typing import List, Optional, Tuple, Union, cast

from typing_extensions import override

from pymarkdown.general.constants import Constants
from pymarkdown.general.parser_helper import ParserHelper
Expand Down Expand Up @@ -114,6 +116,15 @@ def create_copy(self) -> "TextMarkdownToken":
column_number=self.column_number,
)

@override
def _modify_token(self, field_name: str, field_value: Union[str, int]) -> bool:
if field_name == "token_text" and isinstance(field_value, str):
self.__token_text = field_value
self.__compose_extra_data_field()

return True
return False

def __compose_extra_data_field(self) -> None:
"""
Compose the object's self.extra_data field from the local object's variables.
Expand Down
Loading

0 comments on commit ffe19aa

Please sign in to comment.