Compare commits
37 Commits
tdgroot-co
...
0.11.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c533339cf | ||
|
|
5adda130b8 | ||
|
|
5f1b98e25d | ||
|
|
16acd2b763 | ||
|
|
2b8cf444f1 | ||
|
|
207d0f4ec6 | ||
|
|
ebb9ea713d | ||
|
|
d375116807 | ||
|
|
87b9f6c88e | ||
|
|
bda367dad9 | ||
|
|
eb0330bfc6 | ||
|
|
28793ac0b3 | ||
|
|
9231704988 | ||
|
|
1613c302bc | ||
|
|
55c9e84f38 | ||
|
|
99875683ac | ||
|
|
eaeb0603eb | ||
|
|
cb73590623 | ||
|
|
59417ab115 | ||
|
|
917b01e548 | ||
|
|
652714859d | ||
|
|
ea5b22824b | ||
|
|
ec5858e42f | ||
|
|
02bb914ef3 | ||
|
|
21c0d034d0 | ||
|
|
e3ddc789a2 | ||
|
|
2d0cd97323 | ||
|
|
ec185e2e9c | ||
|
|
079d1721aa | ||
|
|
bf24df3e2e | ||
|
|
15329588b1 | ||
|
|
34ad8485fa | ||
|
|
f0ce934bf8 | ||
|
|
99cd237f27 | ||
|
|
2bde8d3e8e | ||
|
|
8c9b029756 | ||
|
|
ae50065872 |
@@ -92,7 +92,7 @@ sub_symbol, sup_symbol
|
|||||||
newline_style
|
newline_style
|
||||||
Defines the style of marking linebreaks (``<br>``) in markdown. The default
|
Defines the style of marking linebreaks (``<br>``) in markdown. The default
|
||||||
value ``SPACES`` of this option will adopt the usual two spaces and a newline,
|
value ``SPACES`` of this option will adopt the usual two spaces and a newline,
|
||||||
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash an a
|
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash and a
|
||||||
newline). While the latter convention is non-standard, it is commonly
|
newline). While the latter convention is non-standard, it is commonly
|
||||||
preferred and supported by a lot of interpreters.
|
preferred and supported by a lot of interpreters.
|
||||||
|
|
||||||
@@ -130,6 +130,11 @@ keep_inline_images_in
|
|||||||
that should be allowed to contain inline images, for example ``['td']``.
|
that should be allowed to contain inline images, for example ``['td']``.
|
||||||
Defaults to an empty list.
|
Defaults to an empty list.
|
||||||
|
|
||||||
|
wrap, wrap_width
|
||||||
|
If ``wrap`` is set to ``True``, all text paragraphs are wrapped at
|
||||||
|
``wrap_width`` characters. Defaults to ``False`` and ``80``.
|
||||||
|
Use with ``newline_style=BACKSLASH`` to keep line breaks in paragraphs.
|
||||||
|
|
||||||
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
||||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from bs4 import BeautifulSoup, NavigableString, Comment, Doctype
|
from bs4 import BeautifulSoup, NavigableString, Comment, Doctype
|
||||||
|
from textwrap import fill
|
||||||
import re
|
import re
|
||||||
import six
|
import six
|
||||||
|
|
||||||
@@ -75,6 +76,8 @@ class MarkdownConverter(object):
|
|||||||
strong_em_symbol = ASTERISK
|
strong_em_symbol = ASTERISK
|
||||||
sub_symbol = ''
|
sub_symbol = ''
|
||||||
sup_symbol = ''
|
sup_symbol = ''
|
||||||
|
wrap = False
|
||||||
|
wrap_width = 80
|
||||||
|
|
||||||
class Options(DefaultOptions):
|
class Options(DefaultOptions):
|
||||||
pass
|
pass
|
||||||
@@ -331,6 +334,11 @@ class MarkdownConverter(object):
|
|||||||
def convert_p(self, el, text, convert_as_inline):
|
def convert_p(self, el, text, convert_as_inline):
|
||||||
if convert_as_inline:
|
if convert_as_inline:
|
||||||
return text
|
return text
|
||||||
|
if self.options['wrap']:
|
||||||
|
text = fill(text,
|
||||||
|
width=self.options['wrap_width'],
|
||||||
|
break_long_words=False,
|
||||||
|
break_on_hyphens=False)
|
||||||
return '%s\n\n' % text if text else ''
|
return '%s\n\n' % text if text else ''
|
||||||
|
|
||||||
def convert_pre(self, el, text, convert_as_inline):
|
def convert_pre(self, el, text, convert_as_inline):
|
||||||
@@ -370,8 +378,13 @@ class MarkdownConverter(object):
|
|||||||
if is_headrow and not el.previous_sibling:
|
if is_headrow and not el.previous_sibling:
|
||||||
# first row and is headline: print headline underline
|
# first row and is headline: print headline underline
|
||||||
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
||||||
elif not el.previous_sibling and not el.parent.name != 'table':
|
elif (not el.previous_sibling
|
||||||
# first row, not headline, and the parent is sth. like tbody:
|
and (el.parent.name == 'table'
|
||||||
|
or (el.parent.name == 'tbody'
|
||||||
|
and not el.parent.previous_sibling))):
|
||||||
|
# first row, not headline, and:
|
||||||
|
# - the parent is table or
|
||||||
|
# - the parent is tbody at the beginning of a table.
|
||||||
# print empty headline above this row
|
# print empty headline above this row
|
||||||
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
|
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
|
||||||
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
|||||||
pkgmeta = {
|
pkgmeta = {
|
||||||
'__title__': 'markdownify',
|
'__title__': 'markdownify',
|
||||||
'__author__': 'Matthew Tretter',
|
'__author__': 'Matthew Tretter',
|
||||||
'__version__': '0.10.3',
|
'__version__': '0.11.2',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -177,6 +177,11 @@ def test_kbd():
|
|||||||
|
|
||||||
def test_p():
|
def test_p():
|
||||||
assert md('<p>hello</p>') == 'hello\n\n'
|
assert md('<p>hello</p>') == 'hello\n\n'
|
||||||
|
assert md('<p>123456789 123456789</p>') == '123456789 123456789\n\n'
|
||||||
|
assert md('<p>123456789 123456789</p>', wrap=True, wrap_width=10) == '123456789\n123456789\n\n'
|
||||||
|
assert md('<p><a href="https://example.com">Some long link</a></p>', wrap=True, wrap_width=10) == '[Some long\nlink](https://example.com)\n\n'
|
||||||
|
assert md('<p>12345<br />67890</p>', wrap=True, wrap_width=10, newline_style=BACKSLASH) == '12345\\\n67890\n\n'
|
||||||
|
assert md('<p>12345678901<br />12345</p>', wrap=True, wrap_width=10, newline_style=BACKSLASH) == '12345678901\\\n12345\n\n'
|
||||||
|
|
||||||
|
|
||||||
def test_pre():
|
def test_pre():
|
||||||
|
|||||||
@@ -139,6 +139,26 @@ table_missing_head = """<table>
|
|||||||
</tr>
|
</tr>
|
||||||
</table>"""
|
</table>"""
|
||||||
|
|
||||||
|
table_body = """<table>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Firstname</td>
|
||||||
|
<td>Lastname</td>
|
||||||
|
<td>Age</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Jill</td>
|
||||||
|
<td>Smith</td>
|
||||||
|
<td>50</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Eve</td>
|
||||||
|
<td>Jackson</td>
|
||||||
|
<td>94</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>"""
|
||||||
|
|
||||||
|
|
||||||
def test_table():
|
def test_table():
|
||||||
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
@@ -148,3 +168,4 @@ def test_table():
|
|||||||
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
|
assert md(table_body) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
|
|||||||
Reference in New Issue
Block a user