Skip to content

make convert_hn() public instead of internal #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ def get_conv_fn(self, tag_name):
if not self.should_convert_tag(tag_name):
return None

# Handle headings with _convert_hn() function
# Handle headings with convert_hN() function
match = re_html_heading.match(tag_name)
if match:
n = int(match.group(1))
return lambda el, text, parent_tags: self._convert_hn(n, el, text, parent_tags)
return lambda el, text, parent_tags: self.convert_hN(n, el, text, parent_tags)

# For other tags, look up their conversion function by tag name
convert_fn_name = "convert_%s" % re_make_convert_fn_name.sub('_', tag_name)
Expand Down Expand Up @@ -509,12 +509,12 @@ def convert_dt(self, el, text, parent_tags):

return '\n\n%s\n' % text

def _convert_hn(self, n, el, text, parent_tags):
""" Method name prefixed with _ to prevent <hn> to call this """
def convert_hN(self, n, el, text, parent_tags):
# convert_hN() converts <hN> tags, where N is any integer
if '_inline' in parent_tags:
return text

# prevent MemoryErrors in case of very large n
# Markdown does not support heading depths of n > 6
n = max(1, min(6, n))

style = self.options['heading_style'].lower()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def test_hn():
assert md('<h5>Hello</h5>') == '\n\n##### Hello\n\n'
assert md('<h6>Hello</h6>') == '\n\n###### Hello\n\n'
assert md('<h10>Hello</h10>') == md('<h6>Hello</h6>')
assert md('<hn>Hello</hn>') == md('Hello')
assert md('<h0>Hello</h0>') == md('<h1>Hello</h1>')
assert md('<hx>Hello</hx>') == md('Hello')


def test_hn_chained():
Expand Down
10 changes: 8 additions & 2 deletions tests/test_custom_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def convert_img(self, el, text, parent_tags):

def convert_custom_tag(self, el, text, parent_tags):
"""Ensure conversion function is found for tags with special characters in name"""
return "FUNCTION USED: %s" % text
return "convert_custom_tag(): %s" % text

def convert_hN(self, n, el, text, parent_tags):
"""Ensure conversion function is found for headings"""
return "convert_hN(%d): %s" % (n, text)


def test_custom_conversion_functions():
Expand All @@ -23,7 +27,9 @@ def md(html, **options):
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />text') == '![Alt text](/path/to/img.jpg "Optional title")\n\ntext'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />text') == '![Alt text](/path/to/img.jpg)\n\ntext'

assert md("<custom-tag>text</custom-tag>") == "FUNCTION USED: text"
assert md("<custom-tag>text</custom-tag>") == "convert_custom_tag(): text"

assert md("<h3>text</h3>") == "convert_hN(3): text"


def test_soup():
Expand Down