Skip to content

Commit cb73590

Browse files
committed
Merge branch 'develop'
2 parents 59417ab + 22180a1 commit cb73590

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

README.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ Options may be specified as kwargs to the ``markdownify`` function, or as a
100100
nested ``Options`` class in ``MarkdownConverter`` subclasses.
101101

102102

103+
Creating Custom Converters
104+
=========================
105+
106+
If you have a special usecase that calls for a special conversion, you can
107+
always inherit from ``MarkdownConverter`` and override the method you want to
108+
change:
109+
110+
.. code:: python
111+
112+
from markdownify import MarkdownConverter
113+
114+
class ImageBlockConverter(MarkdownConverter):
115+
"""
116+
Create a custom MarkdownConverter that adds two newlines after an image
117+
"""
118+
def convert_img(self, el, text, convert_as_inline):
119+
return super().convert_img(el, text, convert_as_inline) + '\n\n'
120+
121+
# Create shorthand method for conversion
122+
def md(html, **options):
123+
return ImageBlockConverter(**options).convert(html)
124+
125+
103126
Development
104127
===========
105128

markdownify/__init__.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,26 @@ def is_nested_node(el):
142142
return text
143143

144144
def process_text(self, el):
145-
text = six.text_type(el)
145+
text = six.text_type(el) or ''
146146

147147
# dont remove any whitespace when handling pre or code in pre
148-
if (el.parent.name == 'pre'
149-
or (el.parent.name == 'code' and el.parent.parent.name == 'pre')):
150-
return escape(text or '')
148+
if not (el.parent.name == 'pre'
149+
or (el.parent.name == 'code'
150+
and el.parent.parent.name == 'pre')):
151+
text = whitespace_re.sub(' ', text)
151152

152-
cleaned_text = escape(whitespace_re.sub(' ', text or ''))
153+
if el.parent.name != 'code':
154+
text = escape(text)
153155

154156
# remove trailing whitespaces if any of the following condition is true:
155157
# - current text node is the last node in li
156158
# - current text node is followed by an embedded list
157-
if el.parent.name == 'li' and (not el.next_sibling or el.next_sibling.name in ['ul', 'ol']):
158-
return cleaned_text.rstrip()
159+
if (el.parent.name == 'li'
160+
and (not el.next_sibling
161+
or el.next_sibling.name in ['ul', 'ol'])):
162+
text = text.rstrip()
159163

160-
return cleaned_text
164+
return text
161165

162166
def __getattr__(self, attr):
163167
# Handle headings

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pkgmeta = {
1111
'__title__': 'markdownify',
1212
'__author__': 'Matthew Tretter',
13-
'__version__': '0.9.0',
13+
'__version__': '0.9.1',
1414
}
1515

1616

tests/test_conversions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def test_br():
7070

7171
def test_code():
7272
inline_tests('code', '`')
73+
assert md('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
7374

7475

7576
def test_del():
@@ -131,8 +132,6 @@ def test_hn_nested_simple_tag():
131132

132133

133134
def test_hn_nested_img():
134-
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '![Alt text](/path/to/img.jpg "Optional title")'
135-
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)'
136135
image_attributes_to_markdown = [
137136
("", ""),
138137
("alt='Alt Text'", "Alt Text"),

tests/test_custom_converter.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from markdownify import MarkdownConverter
2+
3+
4+
class ImageBlockConverter(MarkdownConverter):
5+
"""
6+
Create a custom MarkdownConverter that adds two newlines after an image
7+
"""
8+
def convert_img(self, el, text, convert_as_inline):
9+
return super().convert_img(el, text, convert_as_inline) + '\n\n'
10+
11+
12+
def test_img():
13+
# Create shorthand method for conversion
14+
def md(html, **options):
15+
return ImageBlockConverter(**options).convert(html)
16+
17+
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '![Alt text](/path/to/img.jpg "Optional title")\n\n'
18+
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)\n\n'

0 commit comments

Comments
 (0)