jekyll-to-hugo/app/converter/regex_heuristics.py

35 lines
901 B
Python
Raw Normal View History

2023-06-02 06:58:20 +00:00
import re
from app import utils
class RegexHeuristics:
"""
2023-06-02 06:58:20 +00:00
Regex heuristics class for applying modifying a line using regex lines.
"""
2023-06-02 06:58:20 +00:00
def __init__(self, configurator):
utils.guard_against_none(configurator, "configurator")
self.configurator = configurator
self._rules = {
2023-06-02 07:54:53 +00:00
"^(</*pre.*?>)`{0,3}(?P<content>.*?)(<\/pre>)?$": self._remove_pre_tag,
2023-06-02 06:58:20 +00:00
}
def _remove_pre_tag(self, match) -> str:
"""
Removes the pre tag from the match.
"""
return match.group("content")
def handle_regex_heuristics(self, line: str) -> str:
"""
Manipulates a line by using regex heuristics.
"""
for regex, callback in self._rules.items():
match = re.match(regex, line)
if match:
return callback(match)
else:
return line