Skip to content

Commit 2d0cd97

Browse files
committed
Merge branch 'develop'
2 parents ec185e2 + d4882b8 commit 2d0cd97

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ bullets
7575
lists are nested. Otherwise, the bullet will alternate based on nesting
7676
level. Defaults to ``'*+-'``.
7777

78+
strong_em_symbol
79+
In markdown, both ``*`` and ``_`` are used to encode **strong** or
80+
*emphasized* texts. Either of these symbols can be chosen by the options
81+
``ASTERISK`` (default) or ``UNDERSCORE`` respectively.
82+
83+
newline_style
84+
Defines the style of marking linebreaks (``<br>``) in markdown. The default
85+
value ``SPACES`` of this option will adopt the usual two spaces and a newline,
86+
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash an a
87+
newline). While the latter convention is non-standard, it is commonly
88+
preferred and supported by a lot of interpreters.
89+
7890
Options may be specified as kwargs to the ``markdownify`` function, or as a
7991
nested ``Options`` class in ``MarkdownConverter`` subclasses.
8092

markdownify/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
UNDERLINED = 'underlined'
1616
SETEXT = UNDERLINED
1717

18+
# Newline style
19+
SPACES = 'spaces'
20+
BACKSLASH = 'backslash'
21+
22+
# Strong and emphasis style
23+
ASTERISK = '*'
24+
UNDERSCORE = '_'
25+
1826

1927
def escape(text):
2028
if not text:
@@ -46,6 +54,8 @@ class DefaultOptions:
4654
autolinks = True
4755
heading_style = UNDERLINED
4856
bullets = '*+-' # An iterable of bullet types.
57+
strong_em_symbol = ASTERISK
58+
newline_style = SPACES
4959

5060
class Options(DefaultOptions):
5161
pass
@@ -154,19 +164,23 @@ def convert_br(self, el, text, convert_as_inline):
154164
if convert_as_inline:
155165
return ""
156166

157-
return ' \n'
167+
if self.options['newline_style'].lower() == BACKSLASH:
168+
return '\\\n'
169+
else:
170+
return ' \n'
158171

159172
def convert_em(self, el, text, convert_as_inline):
173+
em_tag = self.options['strong_em_symbol']
160174
prefix, suffix, text = chomp(text)
161175
if not text:
162176
return ''
163-
return '%s*%s*%s' % (prefix, text, suffix)
177+
return '%s%s%s%s%s' % (prefix, em_tag, text, em_tag, suffix)
164178

165179
def convert_hn(self, n, el, text, convert_as_inline):
166180
if convert_as_inline:
167181
return text
168182

169-
style = self.options['heading_style']
183+
style = self.options['heading_style'].lower()
170184
text = text.rstrip()
171185
if style == UNDERLINED and n <= 2:
172186
line = '=' if n == 1 else '-'
@@ -222,10 +236,11 @@ def convert_p(self, el, text, convert_as_inline):
222236
return '%s\n\n' % text if text else ''
223237

224238
def convert_strong(self, el, text, convert_as_inline):
239+
strong_tag = 2 * self.options['strong_em_symbol']
225240
prefix, suffix, text = chomp(text)
226241
if not text:
227242
return ''
228-
return '%s**%s**%s' % (prefix, text, suffix)
243+
return '%s%s%s%s%s' % (prefix, strong_tag, text, strong_tag, suffix)
229244

230245
def convert_img(self, el, text, convert_as_inline):
231246
alt = el.attrs.get('alt', None) or ''

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.6.5',
13+
'__version__': '0.6.6',
1414
}
1515

1616

tests/test_conversions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from markdownify import markdownify as md, ATX, ATX_CLOSED
1+
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
22
import re
33

44

@@ -220,3 +220,14 @@ def test_img():
220220

221221
def test_div():
222222
assert md('Hello</div> World') == 'Hello World'
223+
224+
225+
def test_strong_em_symbol():
226+
assert md('<strong>Hello</strong>', strong_em_symbol=UNDERSCORE) == '__Hello__'
227+
assert md('<b>Hello</b>', strong_em_symbol=UNDERSCORE) == '__Hello__'
228+
assert md('<em>Hello</em>', strong_em_symbol=UNDERSCORE) == '_Hello_'
229+
assert md('<i>Hello</i>', strong_em_symbol=UNDERSCORE) == '_Hello_'
230+
231+
232+
def test_newline_style():
233+
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'

0 commit comments

Comments
 (0)