summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJensDiemer <git@jensdiemer.de>2022-08-23 07:55:57 +0200
committerJensDiemer <git@jensdiemer.de>2022-08-23 08:08:42 +0200
commitbb76f33f69ab1326d55a22cb2744e4939dfb5ec8 (patch)
treef2a288d434f607c599699c985252c49333ce819f
parentfdddbc5c6e7ed8b633e47a5e607c8f346cd08d0d (diff)
downloadcreole-bb76f33f69ab1326d55a22cb2744e4939dfb5ec8.tar.gz
Quote URLs
-rw-r--r--README.creole4
-rw-r--r--README.md6
-rw-r--r--README.rst6
-rw-r--r--creole/__init__.py2
-rw-r--r--creole/emitter/html2markdown_emitter.py21
-rw-r--r--creole/tests/test_html2markdown.py23
-rw-r--r--pyproject.toml2
7 files changed, 51 insertions, 13 deletions
diff --git a/README.creole b/README.creole
index b6c2dbb..658e3c4 100644
--- a/README.creole
+++ b/README.creole
@@ -221,9 +221,9 @@ Note: In this case you must install **docutils**! See above.
= history =
-* [[https://github.com/jedie/python-creole/compare/v1.5.0.rc2...master|*dev*]]
+* [[https://github.com/jedie/python-creole/compare/v1.5.0.rc3...master|*dev*]]
** TBC
-* [[https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0.rc2|v1.5.0.rc2 - 2022-08-20]]
+* [[https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0.rc3|v1.5.0.rc3 - 2022-08-20]]
** NEW: html2markdown
** creole2html bugfixes:
*** replace wrong {{{<tt>}}} with {{{<code>}}}
diff --git a/README.md b/README.md
index 81c75ed..c4bfc77 100644
--- a/README.md
+++ b/README.md
@@ -226,9 +226,9 @@ Note: In this case you must install **docutils**! See above.
# history
-* [*dev*](https://github.com/jedie/python-creole/compare/v1.5.0.rc2...master)
+* [*dev*](https://github.com/jedie/python-creole/compare/v1.5.0.rc3...master)
* TBC
-* [v1.5.0.rc2 - 2022-08-20](https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0.rc2)
+* [v1.5.0.rc3 - 2022-08-20](https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0.rc3)
* NEW: html2markdown
* creole2html bugfixes:
* replace wrong `<tt>` with `<code>`
@@ -438,4 +438,4 @@ first source code was written 27.11.2008: [Forum thread (de)](http://www.python-
------------
-``Note: this file is generated from README.creole 2022-08-21 00:49:59 with "python-creole"`` \ No newline at end of file
+``Note: this file is generated from README.creole 2022-08-23 07:53:38 with "python-creole"`` \ No newline at end of file
diff --git a/README.rst b/README.rst
index 5e6ded5..3991f0c 100644
--- a/README.rst
+++ b/README.rst
@@ -291,11 +291,11 @@ Note: In this case you must install **docutils**! See above.
history
=======
-* `*dev* <https://github.com/jedie/python-creole/compare/v1.5.0.rc2...master>`_
+* `*dev* <https://github.com/jedie/python-creole/compare/v1.5.0.rc3...master>`_
* TBC
-* `v1.5.0.rc2 - 2022-08-20 <https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0.rc2>`_
+* `v1.5.0.rc3 - 2022-08-20 <https://github.com/jedie/python-creole/compare/v1.4.10...v1.5.0.rc3>`_
* NEW: html2markdown
@@ -708,4 +708,4 @@ donation
------------
-``Note: this file is generated from README.creole 2022-08-21 00:49:59 with "python-creole"`` \ No newline at end of file
+``Note: this file is generated from README.creole 2022-08-23 07:53:38 with "python-creole"`` \ No newline at end of file
diff --git a/creole/__init__.py b/creole/__init__.py
index f3b056a..5d394f7 100644
--- a/creole/__init__.py
+++ b/creole/__init__.py
@@ -24,7 +24,7 @@ from creole.parser.creol2html_parser import CreoleParser
from creole.parser.html_parser import HtmlParser
-__version__ = "1.5.0.rc2"
+__version__ = "1.5.0.rc3"
__api__ = "1.0" # Creole 1.0 spec - http://wikicreole.org/
VERSION_STRING = __version__ # remove in future
diff --git a/creole/emitter/html2markdown_emitter.py b/creole/emitter/html2markdown_emitter.py
index 848add0..5e6140a 100644
--- a/creole/emitter/html2markdown_emitter.py
+++ b/creole/emitter/html2markdown_emitter.py
@@ -8,7 +8,7 @@
:license: GNU GPL v3 or above, see LICENSE for more details.
"""
-
+from urllib.parse import ParseResult, quote, urlparse, urlunparse
from creole.parser.html_parser import HtmlParser
from creole.shared.base_emitter import BaseEmitter
@@ -16,6 +16,19 @@ from creole.shared.document_tree import DocNode
from creole.shared.markup_table import MarkupTable
+def quote_link(uri):
+ """
+ >>> quote_link('http://foo.tld/a image with spaces.png')
+ 'http://foo.tld/a%20image%20with%20spaces.png'
+
+ >>> quote_link('https://foo.tld/a image.png?bar=1#anchor')
+ 'https://foo.tld/a%20image.png?bar=1#anchor'
+ """
+ scheme, netloc, url, params, query, fragment = urlparse(uri)
+ url = quote(url)
+ return urlunparse(ParseResult(scheme, netloc, url, params, query, fragment))
+
+
class MarkdownEmitter(BaseEmitter):
"""
Build from a document_tree (html2creole.parser.HtmlParser instance) a
@@ -133,7 +146,9 @@ class MarkdownEmitter(BaseEmitter):
def a_emit(self, node: DocNode):
link_text = self.emit_children(node)
- url = node.attrs['href']
+
+ url = quote_link(node.attrs['href'])
+
title = node.attrs.get('title')
if title:
return f'[{link_text}]({url} "{title}")'
@@ -141,7 +156,7 @@ class MarkdownEmitter(BaseEmitter):
return f'[{link_text}]({url})'
def img_emit(self, node: DocNode):
- src = node.attrs['src']
+ src = quote_link(node.attrs['src'])
title = node.attrs.get('title')
alt = node.attrs.get('alt', '')
diff --git a/creole/tests/test_html2markdown.py b/creole/tests/test_html2markdown.py
index 2651339..90d4daf 100644
--- a/creole/tests/test_html2markdown.py
+++ b/creole/tests/test_html2markdown.py
@@ -163,3 +163,26 @@ class MarkdownTests(BaseCreoleTest):
),
# debug=True,
)
+ def test_links_with_spaces(self):
+ self.assert_html2markdown(
+ markdown_string=cleandoc(
+ '''
+ [one](/foo%20bar.png)
+
+ [/somewhere/foo bar.exe](https://somewhere/foo%20bar.exe?bar=1#anchor "Foo Bar")
+
+ ![Alt text](https://foo.tld/a%20image.png?bar=1#anchor)
+ '''
+ ),
+ html_string=cleandoc(
+ '''
+ <p><a href="/foo bar.png">one</a></p>
+ <p>
+ <a href="https://somewhere/foo bar.exe?bar=1#anchor" title="Foo Bar">
+ /somewhere/foo bar.exe
+ </a>
+ </p>
+ <p><img alt="Alt text" src="https://foo.tld/a image.png?bar=1#anchor" /></p>
+ '''
+ ),
+ )
diff --git a/pyproject.toml b/pyproject.toml
index 1764142..166a480 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "python-creole"
-version = "1.5.0.rc2"
+version = "1.5.0.rc3"
description = "python-creole is an open-source (GPL) markup converter in pure Python for: creole2html, html2creole, html2ReSt, html2textile"
# Will be generated from README.creole with: "poetry run update_rst_readme"