markdownify 0.13.0__tar.gz → 0.14.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- markdownify-0.14.0/.github/workflows/python-app.yml +32 -0
- markdownify-0.14.0/.github/workflows/python-publish.yml +31 -0
- markdownify-0.14.0/.gitignore +12 -0
- markdownify-0.14.0/PKG-INFO +242 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/README.rst +2 -2
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify/__init__.py +105 -48
- markdownify-0.14.0/markdownify.egg-info/PKG-INFO +242 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify.egg-info/SOURCES.txt +6 -1
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify.egg-info/entry_points.txt +0 -1
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify.egg-info/top_level.txt +0 -1
- markdownify-0.14.0/pyproject.toml +45 -0
- markdownify-0.14.0/shell.nix +10 -0
- markdownify-0.14.0/tox.ini +15 -0
- markdownify-0.13.0/PKG-INFO +0 -241
- markdownify-0.13.0/markdownify.egg-info/PKG-INFO +0 -241
- markdownify-0.13.0/setup.py +0 -52
- {markdownify-0.13.0 → markdownify-0.14.0}/LICENSE +0 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/MANIFEST.in +0 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify/main.py +0 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify.egg-info/dependency_links.txt +0 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify.egg-info/not-zip-safe +0 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/markdownify.egg-info/requires.txt +0 -0
- {markdownify-0.13.0 → markdownify-0.14.0}/setup.cfg +0 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
|
|
2
|
+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
|
3
|
+
|
|
4
|
+
name: Python application
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ develop ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ develop ]
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v2
|
|
19
|
+
- name: Set up Python 3.8
|
|
20
|
+
uses: actions/setup-python@v2
|
|
21
|
+
with:
|
|
22
|
+
python-version: 3.8
|
|
23
|
+
- name: Install dependencies
|
|
24
|
+
run: |
|
|
25
|
+
python -m pip install --upgrade pip
|
|
26
|
+
pip install --upgrade setuptools setuptools_scm wheel build tox
|
|
27
|
+
- name: Lint and test
|
|
28
|
+
run: |
|
|
29
|
+
tox
|
|
30
|
+
- name: Build
|
|
31
|
+
run: |
|
|
32
|
+
python -m build -nwsx .
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# This workflow will upload a Python Package using Twine when a release is created
|
|
2
|
+
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
|
|
3
|
+
|
|
4
|
+
name: Upload Python Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
release:
|
|
8
|
+
types: [created]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
deploy:
|
|
12
|
+
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v2
|
|
17
|
+
- name: Set up Python
|
|
18
|
+
uses: actions/setup-python@v2
|
|
19
|
+
with:
|
|
20
|
+
python-version: '3.8'
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip
|
|
24
|
+
pip install --upgrade setuptools setuptools_scm wheel build twine
|
|
25
|
+
- name: Build and publish
|
|
26
|
+
env:
|
|
27
|
+
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
|
|
28
|
+
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
|
|
29
|
+
run: |
|
|
30
|
+
python -m build -nwsx .
|
|
31
|
+
twine upload dist/*
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: markdownify
|
|
3
|
+
Version: 0.14.0
|
|
4
|
+
Summary: Convert HTML to markdown.
|
|
5
|
+
Author-email: Matthew Tretter <m@tthewwithanm.com>
|
|
6
|
+
Project-URL: Homepage, http://github.com/matthewwithanm/python-markdownify
|
|
7
|
+
Project-URL: Download, http://github.com/matthewwithanm/python-markdownify/tarball/master
|
|
8
|
+
Classifier: Environment :: Web Environment
|
|
9
|
+
Classifier: Framework :: Django
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 2.5
|
|
14
|
+
Classifier: Programming Language :: Python :: 2.6
|
|
15
|
+
Classifier: Programming Language :: Python :: 2.7
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Topic :: Utilities
|
|
20
|
+
Description-Content-Type: text/x-rst
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: beautifulsoup4<5,>=4.9
|
|
23
|
+
Requires-Dist: six<2,>=1.15
|
|
24
|
+
|
|
25
|
+
|build| |version| |license| |downloads|
|
|
26
|
+
|
|
27
|
+
.. |build| image:: https://img.shields.io/github/actions/workflow/status/matthewwithanm/python-markdownify/python-app.yml?branch=develop
|
|
28
|
+
:alt: GitHub Workflow Status
|
|
29
|
+
:target: https://github.com/matthewwithanm/python-markdownify/actions/workflows/python-app.yml?query=workflow%3A%22Python+application%22
|
|
30
|
+
|
|
31
|
+
.. |version| image:: https://img.shields.io/pypi/v/markdownify
|
|
32
|
+
:alt: Pypi version
|
|
33
|
+
:target: https://pypi.org/project/markdownify/
|
|
34
|
+
|
|
35
|
+
.. |license| image:: https://img.shields.io/pypi/l/markdownify
|
|
36
|
+
:alt: License
|
|
37
|
+
:target: https://github.com/matthewwithanm/python-markdownify/blob/develop/LICENSE
|
|
38
|
+
|
|
39
|
+
.. |downloads| image:: https://pepy.tech/badge/markdownify
|
|
40
|
+
:alt: Pypi Downloads
|
|
41
|
+
:target: https://pepy.tech/project/markdownify
|
|
42
|
+
|
|
43
|
+
Installation
|
|
44
|
+
============
|
|
45
|
+
|
|
46
|
+
``pip install markdownify``
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
Usage
|
|
50
|
+
=====
|
|
51
|
+
|
|
52
|
+
Convert some HTML to Markdown:
|
|
53
|
+
|
|
54
|
+
.. code:: python
|
|
55
|
+
|
|
56
|
+
from markdownify import markdownify as md
|
|
57
|
+
md('<b>Yay</b> <a href="http://github.com">GitHub</a>') # > '**Yay** [GitHub](http://github.com)'
|
|
58
|
+
|
|
59
|
+
Specify tags to exclude:
|
|
60
|
+
|
|
61
|
+
.. code:: python
|
|
62
|
+
|
|
63
|
+
from markdownify import markdownify as md
|
|
64
|
+
md('<b>Yay</b> <a href="http://github.com">GitHub</a>', strip=['a']) # > '**Yay** GitHub'
|
|
65
|
+
|
|
66
|
+
\...or specify the tags you want to include:
|
|
67
|
+
|
|
68
|
+
.. code:: python
|
|
69
|
+
|
|
70
|
+
from markdownify import markdownify as md
|
|
71
|
+
md('<b>Yay</b> <a href="http://github.com">GitHub</a>', convert=['b']) # > '**Yay** GitHub'
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Options
|
|
75
|
+
=======
|
|
76
|
+
|
|
77
|
+
Markdownify supports the following options:
|
|
78
|
+
|
|
79
|
+
strip
|
|
80
|
+
A list of tags to strip. This option can't be used with the
|
|
81
|
+
``convert`` option.
|
|
82
|
+
|
|
83
|
+
convert
|
|
84
|
+
A list of tags to convert. This option can't be used with the
|
|
85
|
+
``strip`` option.
|
|
86
|
+
|
|
87
|
+
autolinks
|
|
88
|
+
A boolean indicating whether the "automatic link" style should be used when
|
|
89
|
+
a ``a`` tag's contents match its href. Defaults to ``True``.
|
|
90
|
+
|
|
91
|
+
default_title
|
|
92
|
+
A boolean to enable setting the title of a link to its href, if no title is
|
|
93
|
+
given. Defaults to ``False``.
|
|
94
|
+
|
|
95
|
+
heading_style
|
|
96
|
+
Defines how headings should be converted. Accepted values are ``ATX``,
|
|
97
|
+
``ATX_CLOSED``, ``SETEXT``, and ``UNDERLINED`` (which is an alias for
|
|
98
|
+
``SETEXT``). Defaults to ``UNDERLINED``.
|
|
99
|
+
|
|
100
|
+
bullets
|
|
101
|
+
An iterable (string, list, or tuple) of bullet styles to be used. If the
|
|
102
|
+
iterable only contains one item, it will be used regardless of how deeply
|
|
103
|
+
lists are nested. Otherwise, the bullet will alternate based on nesting
|
|
104
|
+
level. Defaults to ``'*+-'``.
|
|
105
|
+
|
|
106
|
+
strong_em_symbol
|
|
107
|
+
In markdown, both ``*`` and ``_`` are used to encode **strong** or
|
|
108
|
+
*emphasized* texts. Either of these symbols can be chosen by the options
|
|
109
|
+
``ASTERISK`` (default) or ``UNDERSCORE`` respectively.
|
|
110
|
+
|
|
111
|
+
sub_symbol, sup_symbol
|
|
112
|
+
Define the chars that surround ``<sub>`` and ``<sup>`` text. Defaults to an
|
|
113
|
+
empty string, because this is non-standard behavior. Could be something like
|
|
114
|
+
``~`` and ``^`` to result in ``~sub~`` and ``^sup^``. If the value starts
|
|
115
|
+
with ``<`` and ends with ``>``, it is treated as an HTML tag and a ``/`` is
|
|
116
|
+
inserted after the ``<`` in the string used after the text; this allows
|
|
117
|
+
specifying ``<sub>`` to use raw HTML in the output for subscripts, for
|
|
118
|
+
example.
|
|
119
|
+
|
|
120
|
+
newline_style
|
|
121
|
+
Defines the style of marking linebreaks (``<br>``) in markdown. The default
|
|
122
|
+
value ``SPACES`` of this option will adopt the usual two spaces and a newline,
|
|
123
|
+
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash and a
|
|
124
|
+
newline). While the latter convention is non-standard, it is commonly
|
|
125
|
+
preferred and supported by a lot of interpreters.
|
|
126
|
+
|
|
127
|
+
code_language
|
|
128
|
+
Defines the language that should be assumed for all ``<pre>`` sections.
|
|
129
|
+
Useful, if all code on a page is in the same programming language and
|
|
130
|
+
should be annotated with `````python`` or similar.
|
|
131
|
+
Defaults to ``''`` (empty string) and can be any string.
|
|
132
|
+
|
|
133
|
+
code_language_callback
|
|
134
|
+
When the HTML code contains ``pre`` tags that in some way provide the code
|
|
135
|
+
language, for example as class, this callback can be used to extract the
|
|
136
|
+
language from the tag and prefix it to the converted ``pre`` tag.
|
|
137
|
+
The callback gets one single argument, an BeautifylSoup object, and returns
|
|
138
|
+
a string containing the code language, or ``None``.
|
|
139
|
+
An example to use the class name as code language could be::
|
|
140
|
+
|
|
141
|
+
def callback(el):
|
|
142
|
+
return el['class'][0] if el.has_attr('class') else None
|
|
143
|
+
|
|
144
|
+
Defaults to ``None``.
|
|
145
|
+
|
|
146
|
+
escape_asterisks
|
|
147
|
+
If set to ``False``, do not escape ``*`` to ``\*`` in text.
|
|
148
|
+
Defaults to ``True``.
|
|
149
|
+
|
|
150
|
+
escape_underscores
|
|
151
|
+
If set to ``False``, do not escape ``_`` to ``\_`` in text.
|
|
152
|
+
Defaults to ``True``.
|
|
153
|
+
|
|
154
|
+
escape_misc
|
|
155
|
+
If set to ``True``, escape miscellaneous punctuation characters
|
|
156
|
+
that sometimes have Markdown significance in text.
|
|
157
|
+
Defaults to ``False``.
|
|
158
|
+
|
|
159
|
+
keep_inline_images_in
|
|
160
|
+
Images are converted to their alt-text when the images are located inside
|
|
161
|
+
headlines or table cells. If some inline images should be converted to
|
|
162
|
+
markdown images instead, this option can be set to a list of parent tags
|
|
163
|
+
that should be allowed to contain inline images, for example ``['td']``.
|
|
164
|
+
Defaults to an empty list.
|
|
165
|
+
|
|
166
|
+
wrap, wrap_width
|
|
167
|
+
If ``wrap`` is set to ``True``, all text paragraphs are wrapped at
|
|
168
|
+
``wrap_width`` characters. Defaults to ``False`` and ``80``.
|
|
169
|
+
Use with ``newline_style=BACKSLASH`` to keep line breaks in paragraphs.
|
|
170
|
+
|
|
171
|
+
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
|
172
|
+
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
Converting BeautifulSoup objects
|
|
176
|
+
================================
|
|
177
|
+
|
|
178
|
+
.. code:: python
|
|
179
|
+
|
|
180
|
+
from markdownify import MarkdownConverter
|
|
181
|
+
|
|
182
|
+
# Create shorthand method for conversion
|
|
183
|
+
def md(soup, **options):
|
|
184
|
+
return MarkdownConverter(**options).convert_soup(soup)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
Creating Custom Converters
|
|
188
|
+
==========================
|
|
189
|
+
|
|
190
|
+
If you have a special usecase that calls for a special conversion, you can
|
|
191
|
+
always inherit from ``MarkdownConverter`` and override the method you want to
|
|
192
|
+
change.
|
|
193
|
+
The function that handles a HTML tag named ``abc`` is called
|
|
194
|
+
``convert_abc(self, el, text, convert_as_inline)`` and returns a string
|
|
195
|
+
containing the converted HTML tag.
|
|
196
|
+
The ``MarkdownConverter`` object will handle the conversion based on the
|
|
197
|
+
function names:
|
|
198
|
+
|
|
199
|
+
.. code:: python
|
|
200
|
+
|
|
201
|
+
from markdownify import MarkdownConverter
|
|
202
|
+
|
|
203
|
+
class ImageBlockConverter(MarkdownConverter):
|
|
204
|
+
"""
|
|
205
|
+
Create a custom MarkdownConverter that adds two newlines after an image
|
|
206
|
+
"""
|
|
207
|
+
def convert_img(self, el, text, convert_as_inline):
|
|
208
|
+
return super().convert_img(el, text, convert_as_inline) + '\n\n'
|
|
209
|
+
|
|
210
|
+
# Create shorthand method for conversion
|
|
211
|
+
def md(html, **options):
|
|
212
|
+
return ImageBlockConverter(**options).convert(html)
|
|
213
|
+
|
|
214
|
+
.. code:: python
|
|
215
|
+
|
|
216
|
+
from markdownify import MarkdownConverter
|
|
217
|
+
|
|
218
|
+
class IgnoreParagraphsConverter(MarkdownConverter):
|
|
219
|
+
"""
|
|
220
|
+
Create a custom MarkdownConverter that ignores paragraphs
|
|
221
|
+
"""
|
|
222
|
+
def convert_p(self, el, text, convert_as_inline):
|
|
223
|
+
return ''
|
|
224
|
+
|
|
225
|
+
# Create shorthand method for conversion
|
|
226
|
+
def md(html, **options):
|
|
227
|
+
return IgnoreParagraphsConverter(**options).convert(html)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
Command Line Interface
|
|
231
|
+
======================
|
|
232
|
+
|
|
233
|
+
Use ``markdownify example.html > example.md`` or pipe input from stdin
|
|
234
|
+
(``cat example.html | markdownify > example.md``).
|
|
235
|
+
Call ``markdownify -h`` to see all available options.
|
|
236
|
+
They are the same as listed above and take the same arguments.
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
Development
|
|
240
|
+
===========
|
|
241
|
+
|
|
242
|
+
To run tests and the linter run ``pip install tox`` once, then ``tox``.
|
|
@@ -128,9 +128,9 @@ escape_underscores
|
|
|
128
128
|
Defaults to ``True``.
|
|
129
129
|
|
|
130
130
|
escape_misc
|
|
131
|
-
If set to ``
|
|
131
|
+
If set to ``True``, escape miscellaneous punctuation characters
|
|
132
132
|
that sometimes have Markdown significance in text.
|
|
133
|
-
Defaults to ``
|
|
133
|
+
Defaults to ``False``.
|
|
134
134
|
|
|
135
135
|
keep_inline_images_in
|
|
136
136
|
Images are converted to their alt-text when the images are located inside
|
|
@@ -7,7 +7,8 @@ import six
|
|
|
7
7
|
convert_heading_re = re.compile(r'convert_h(\d+)')
|
|
8
8
|
line_beginning_re = re.compile(r'^', re.MULTILINE)
|
|
9
9
|
whitespace_re = re.compile(r'[\t ]+')
|
|
10
|
-
all_whitespace_re = re.compile(r'[\
|
|
10
|
+
all_whitespace_re = re.compile(r'[\t \r\n]+')
|
|
11
|
+
newline_whitespace_re = re.compile(r'[\t \r\n]*[\r\n][\t \r\n]*')
|
|
11
12
|
html_heading_re = re.compile(r'h[1-6]')
|
|
12
13
|
|
|
13
14
|
|
|
@@ -66,6 +67,23 @@ def _todict(obj):
|
|
|
66
67
|
return dict((k, getattr(obj, k)) for k in dir(obj) if not k.startswith('_'))
|
|
67
68
|
|
|
68
69
|
|
|
70
|
+
def should_remove_whitespace_inside(el):
|
|
71
|
+
"""Return to remove whitespace immediately inside a block-level element."""
|
|
72
|
+
if not el or not el.name:
|
|
73
|
+
return False
|
|
74
|
+
if html_heading_re.match(el.name) is not None:
|
|
75
|
+
return True
|
|
76
|
+
return el.name in ('p', 'blockquote',
|
|
77
|
+
'ol', 'ul', 'li',
|
|
78
|
+
'table', 'thead', 'tbody', 'tfoot',
|
|
79
|
+
'tr', 'td', 'th')
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def should_remove_whitespace_outside(el):
|
|
83
|
+
"""Return to remove whitespace immediately outside a block-level element."""
|
|
84
|
+
return should_remove_whitespace_inside(el) or (el and el.name == 'pre')
|
|
85
|
+
|
|
86
|
+
|
|
69
87
|
class MarkdownConverter(object):
|
|
70
88
|
class DefaultOptions:
|
|
71
89
|
autolinks = True
|
|
@@ -76,7 +94,7 @@ class MarkdownConverter(object):
|
|
|
76
94
|
default_title = False
|
|
77
95
|
escape_asterisks = True
|
|
78
96
|
escape_underscores = True
|
|
79
|
-
escape_misc =
|
|
97
|
+
escape_misc = False
|
|
80
98
|
heading_style = UNDERLINED
|
|
81
99
|
keep_inline_images_in = []
|
|
82
100
|
newline_style = SPACES
|
|
@@ -119,27 +137,23 @@ class MarkdownConverter(object):
|
|
|
119
137
|
if not children_only and (isHeading or isCell):
|
|
120
138
|
convert_children_as_inline = True
|
|
121
139
|
|
|
122
|
-
# Remove whitespace-only textnodes
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (isinstance(el, NavigableString)
|
|
140
|
-
and six.text_type(el).strip() == ''
|
|
141
|
-
and can_extract):
|
|
142
|
-
el.extract()
|
|
140
|
+
# Remove whitespace-only textnodes just before, after or
|
|
141
|
+
# inside block-level elements.
|
|
142
|
+
should_remove_inside = should_remove_whitespace_inside(node)
|
|
143
|
+
for el in node.children:
|
|
144
|
+
# Only extract (remove) whitespace-only text node if any of the
|
|
145
|
+
# conditions is true:
|
|
146
|
+
# - el is the first element in its parent (block-level)
|
|
147
|
+
# - el is the last element in its parent (block-level)
|
|
148
|
+
# - el is adjacent to a block-level node
|
|
149
|
+
can_extract = (should_remove_inside and (not el.previous_sibling
|
|
150
|
+
or not el.next_sibling)
|
|
151
|
+
or should_remove_whitespace_outside(el.previous_sibling)
|
|
152
|
+
or should_remove_whitespace_outside(el.next_sibling))
|
|
153
|
+
if (isinstance(el, NavigableString)
|
|
154
|
+
and six.text_type(el).strip() == ''
|
|
155
|
+
and can_extract):
|
|
156
|
+
el.extract()
|
|
143
157
|
|
|
144
158
|
# Convert the children first
|
|
145
159
|
for el in node.children:
|
|
@@ -148,7 +162,13 @@ class MarkdownConverter(object):
|
|
|
148
162
|
elif isinstance(el, NavigableString):
|
|
149
163
|
text += self.process_text(el)
|
|
150
164
|
else:
|
|
151
|
-
|
|
165
|
+
text_strip = text.rstrip('\n')
|
|
166
|
+
newlines_left = len(text) - len(text_strip)
|
|
167
|
+
next_text = self.process_tag(el, convert_children_as_inline)
|
|
168
|
+
next_text_strip = next_text.lstrip('\n')
|
|
169
|
+
newlines_right = len(next_text) - len(next_text_strip)
|
|
170
|
+
newlines = '\n' * max(newlines_left, newlines_right)
|
|
171
|
+
text = text_strip + newlines + next_text_strip
|
|
152
172
|
|
|
153
173
|
if not children_only:
|
|
154
174
|
convert_fn = getattr(self, 'convert_%s' % node.name, None)
|
|
@@ -162,18 +182,26 @@ class MarkdownConverter(object):
|
|
|
162
182
|
|
|
163
183
|
# normalize whitespace if we're not inside a preformatted element
|
|
164
184
|
if not el.find_parent('pre'):
|
|
165
|
-
|
|
185
|
+
if self.options['wrap']:
|
|
186
|
+
text = all_whitespace_re.sub(' ', text)
|
|
187
|
+
else:
|
|
188
|
+
text = newline_whitespace_re.sub('\n', text)
|
|
189
|
+
text = whitespace_re.sub(' ', text)
|
|
166
190
|
|
|
167
191
|
# escape special characters if we're not inside a preformatted or code element
|
|
168
192
|
if not el.find_parent(['pre', 'code', 'kbd', 'samp']):
|
|
169
193
|
text = self.escape(text)
|
|
170
194
|
|
|
171
|
-
# remove
|
|
172
|
-
# -
|
|
173
|
-
#
|
|
174
|
-
if (el.
|
|
175
|
-
|
|
176
|
-
|
|
195
|
+
# remove leading whitespace at the start or just after a
|
|
196
|
+
# block-level element; remove traliing whitespace at the end
|
|
197
|
+
# or just before a block-level element.
|
|
198
|
+
if (should_remove_whitespace_outside(el.previous_sibling)
|
|
199
|
+
or (should_remove_whitespace_inside(el.parent)
|
|
200
|
+
and not el.previous_sibling)):
|
|
201
|
+
text = text.lstrip()
|
|
202
|
+
if (should_remove_whitespace_outside(el.next_sibling)
|
|
203
|
+
or (should_remove_whitespace_inside(el.parent)
|
|
204
|
+
and not el.next_sibling)):
|
|
177
205
|
text = text.rstrip()
|
|
178
206
|
|
|
179
207
|
return text
|
|
@@ -208,20 +236,32 @@ class MarkdownConverter(object):
|
|
|
208
236
|
if not text:
|
|
209
237
|
return ''
|
|
210
238
|
if self.options['escape_misc']:
|
|
211
|
-
text = re.sub(r'([\\&<`[
|
|
212
|
-
|
|
239
|
+
text = re.sub(r'([\\&<`[>~=+|])', r'\\\1', text)
|
|
240
|
+
# A sequence of one or more consecutive '-', preceded and
|
|
241
|
+
# followed by whitespace or start/end of fragment, might
|
|
242
|
+
# be confused with an underline of a header, or with a
|
|
243
|
+
# list marker.
|
|
244
|
+
text = re.sub(r'(\s|^)(-+(?:\s|$))', r'\1\\\2', text)
|
|
245
|
+
# A sequence of up to six consecutive '#', preceded and
|
|
246
|
+
# followed by whitespace or start/end of fragment, might
|
|
247
|
+
# be confused with an ATX heading.
|
|
248
|
+
text = re.sub(r'(\s|^)(#{1,6}(?:\s|$))', r'\1\\\2', text)
|
|
249
|
+
# '.' or ')' preceded by up to nine digits might be
|
|
250
|
+
# confused with a list item.
|
|
251
|
+
text = re.sub(r'((?:\s|^)[0-9]{1,9})([.)](?:\s|$))', r'\1\\\2',
|
|
252
|
+
text)
|
|
213
253
|
if self.options['escape_asterisks']:
|
|
214
254
|
text = text.replace('*', r'\*')
|
|
215
255
|
if self.options['escape_underscores']:
|
|
216
256
|
text = text.replace('_', r'\_')
|
|
217
257
|
return text
|
|
218
258
|
|
|
219
|
-
def indent(self, text,
|
|
220
|
-
return line_beginning_re.sub('
|
|
259
|
+
def indent(self, text, columns):
|
|
260
|
+
return line_beginning_re.sub(' ' * columns, text) if text else ''
|
|
221
261
|
|
|
222
262
|
def underline(self, text, pad_char):
|
|
223
263
|
text = (text or '').rstrip()
|
|
224
|
-
return '%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''
|
|
264
|
+
return '\n\n%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''
|
|
225
265
|
|
|
226
266
|
def convert_a(self, el, text, convert_as_inline):
|
|
227
267
|
prefix, suffix, text = chomp(text)
|
|
@@ -246,7 +286,7 @@ class MarkdownConverter(object):
|
|
|
246
286
|
def convert_blockquote(self, el, text, convert_as_inline):
|
|
247
287
|
|
|
248
288
|
if convert_as_inline:
|
|
249
|
-
return text
|
|
289
|
+
return ' ' + text.strip() + ' '
|
|
250
290
|
|
|
251
291
|
return '\n' + (line_beginning_re.sub('> ', text.strip()) + '\n\n') if text else ''
|
|
252
292
|
|
|
@@ -280,10 +320,11 @@ class MarkdownConverter(object):
|
|
|
280
320
|
if style == UNDERLINED and n <= 2:
|
|
281
321
|
line = '=' if n == 1 else '-'
|
|
282
322
|
return self.underline(text, line)
|
|
323
|
+
text = all_whitespace_re.sub(' ', text)
|
|
283
324
|
hashes = '#' * n
|
|
284
325
|
if style == ATX_CLOSED:
|
|
285
|
-
return '%s %s %s\n\n' % (hashes, text, hashes)
|
|
286
|
-
return '%s %s\n\n' % (hashes, text)
|
|
326
|
+
return '\n%s %s %s\n\n' % (hashes, text, hashes)
|
|
327
|
+
return '\n%s %s\n\n' % (hashes, text)
|
|
287
328
|
|
|
288
329
|
def convert_hr(self, el, text, convert_as_inline):
|
|
289
330
|
return '\n\n---\n\n'
|
|
@@ -317,8 +358,8 @@ class MarkdownConverter(object):
|
|
|
317
358
|
el = el.parent
|
|
318
359
|
if nested:
|
|
319
360
|
# remove trailing newline if nested
|
|
320
|
-
return '\n' +
|
|
321
|
-
return text + ('\n' if before_paragraph else '')
|
|
361
|
+
return '\n' + text.rstrip()
|
|
362
|
+
return '\n\n' + text + ('\n' if before_paragraph else '')
|
|
322
363
|
|
|
323
364
|
convert_ul = convert_list
|
|
324
365
|
convert_ol = convert_list
|
|
@@ -339,17 +380,33 @@ class MarkdownConverter(object):
|
|
|
339
380
|
el = el.parent
|
|
340
381
|
bullets = self.options['bullets']
|
|
341
382
|
bullet = bullets[depth % len(bullets)]
|
|
342
|
-
|
|
383
|
+
bullet = bullet + ' '
|
|
384
|
+
text = (text or '').strip()
|
|
385
|
+
text = self.indent(text, len(bullet))
|
|
386
|
+
if text:
|
|
387
|
+
text = bullet + text[len(bullet):]
|
|
388
|
+
return '%s\n' % text
|
|
343
389
|
|
|
344
390
|
def convert_p(self, el, text, convert_as_inline):
|
|
345
391
|
if convert_as_inline:
|
|
346
|
-
return text
|
|
392
|
+
return ' ' + text.strip() + ' '
|
|
347
393
|
if self.options['wrap']:
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
394
|
+
# Preserve newlines (and preceding whitespace) resulting
|
|
395
|
+
# from <br> tags. Newlines in the input have already been
|
|
396
|
+
# replaced by spaces.
|
|
397
|
+
lines = text.split('\n')
|
|
398
|
+
new_lines = []
|
|
399
|
+
for line in lines:
|
|
400
|
+
line = line.lstrip()
|
|
401
|
+
line_no_trailing = line.rstrip()
|
|
402
|
+
trailing = line[len(line_no_trailing):]
|
|
403
|
+
line = fill(line,
|
|
404
|
+
width=self.options['wrap_width'],
|
|
405
|
+
break_long_words=False,
|
|
406
|
+
break_on_hyphens=False)
|
|
407
|
+
new_lines.append(line + trailing)
|
|
408
|
+
text = '\n'.join(new_lines)
|
|
409
|
+
return '\n\n%s\n\n' % text if text else ''
|
|
353
410
|
|
|
354
411
|
def convert_pre(self, el, text, convert_as_inline):
|
|
355
412
|
if not text:
|