File python-django-waliki-reimplement-autolinks.patch of Package python-django-waliki
From cecb37c4cedc0c5ac7ed8bbfa2976114fd0f785a Mon Sep 17 00:00:00 2001
From: Florian Wagner <florian@wagner-flo.net>
Date: Thu, 29 Jun 2017 11:59:02 +0200
Subject: [PATCH] Reimplement Autolinks using docutils
unknown_reference_resolvers.
---
docs/settings.rst | 2 +-
setup.py | 2 +-
tests/test_models.py | 30 +++++++++++++++++++++------
waliki/_markups.py | 20 ------------------
waliki/directives/writer.py | 41 +++++++++++++++++++++++++++++++++++++
waliki/settings.py | 4 ++--
6 files changed, 69 insertions(+), 30 deletions(-)
create mode 100644 waliki/directives/writer.py
diff --git a/docs/settings.rst b/docs/settings.rst
index 0ca2e3d..280ca74 100644
--- a/docs/settings.rst
+++ b/docs/settings.rst
@@ -83,7 +83,7 @@ You can override any settings in your project's :file:`settings.py` file
'syntax_highlight': 'short',
'halt_level': 5,
},
- 'writer': HTML5Writer(),
+ 'writer': WalikiHTML5Writer(),
'writer_name': 'html5',
},
'Markdown': {
diff --git a/setup.py b/setup.py
index 8565700..af863db 100755
--- a/setup.py
+++ b/setup.py
@@ -22,7 +22,7 @@ if sys.argv[-1] == 'publish':
readme = open('README.rst').read()
history = open('HISTORY.rst').read().replace('.. :changelog:', '')
-install_requires = ['django', 'Markups', 'sh', 'docutils', 'genshi', 'pyquery']
+install_requires = ['django', 'Markups', 'sh', 'docutils', 'genshi']
extras_require = { # noqa
diff --git a/tests/test_models.py b/tests/test_models.py
index 8aa1cf7..07c374d 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -81,12 +81,30 @@ class TestRestructuredText(TestCase):
self.assertEqual(Page.preview('reStructuredText', rst), rst_html)
def test_link_explicit(self):
- with mock.patch('waliki._markups.get_url') as get_url:
- get_url.return_value = 'xxx'
- html = Page.preview('reStructuredText', 'a link_')
- self.assertEqual(html, '\n <p>a <a href="xxx">link</a></p>\n')
-
- def test_missing_text(self):
+ html = Page.preview('reStructuredText', 'a link_')
+ self.assertEqual(html, '\n <p>a <a href="/link">link</a></p>\n')
+
+ def test_link_indirect(self):
+ html = Page.preview('reStructuredText', 'a text_\n\n.. _text: link_')
+ self.assertEqual(html, '\n <p>a <a href="/link">text</a></p>\n')
+
+ def test_link_indirect_anonymous(self):
+ html = Page.preview('reStructuredText', 'a `long text`__\n\n__ link_')
+ self.assertEqual(html, '\n <p>a <a href="/link">long text</a></p>\n')
+
+ def test_link_indirect_embedded(self):
+ html = Page.preview('reStructuredText', ('a `long text <meep_>`_\n\n'
+ '.. _meep: link_'))
+ self.assertEqual(html, '\n <p>a <a href="/link">long text</a></p>\n')
+
+ def test_link_crossref(self):
+ html = Page.preview('reStructuredText', ('a crossref_\n\n'
+ '.. _crossref:\n\n'
+ 'the crossref target'))
+ self.assertEqual(html, ('\n <p>a <a href="#crossref">crossref</a></p>'
+ '\n <p id="crossref">the crossref target</p>\n'))
+
+ def test_link_invalid_slug(self):
html = Page.preview('reStructuredText', '`***`_')
self.assertIn('problematic', html)
diff --git a/waliki/_markups.py b/waliki/_markups.py
index 821c70e..a26f846 100644
--- a/waliki/_markups.py
+++ b/waliki/_markups.py
@@ -3,7 +3,6 @@ import sys
from markups import (MarkdownMarkup as MarkdownMarkupBase,
TextileMarkup as TextileMarkupBase,
ReStructuredTextMarkup as ReStructuredTextMarkupBase)
-from pyquery import PyQuery
from .utils import get_url
from waliki.settings import WALIKI_AVAILABLE_MARKUPS
@@ -54,25 +53,6 @@ class ReStructuredTextMarkup(ReStructuredTextMarkupBase):
self._publish_parts = override_publish_parts(self._publish_parts)
- def get_document_body(self, text):
- html = super(ReStructuredTextMarkup, self).get_document_body(text)
- if not html:
- return html
-
- # Convert unknown links to internal wiki links.
- # Examples:
- # Something_ will link to '/something'
- # `something great`_ to '/something-great'
- # `another thing <thing>`_ '/thing'
- refs = [a.text[:-1] for a in PyQuery(html)('a.problematic') if not re.match(r'\|(.*)\|', a.text)]
- # substitions = [a.text[:-1] for a in PyQuery(html)('a.problematic') if re.match(r'\|(.*)\|', a.text)]
- if refs:
- refs = '\n'.join('.. _%s: %s' % (ref, get_url(ref))
- for ref in refs if get_url(ref))
- html = super(ReStructuredTextMarkup, self).get_document_body(
- text + '\n\n' + refs)
- return html
-
class TextileMarkup(TextileMarkupBase):
codemirror_mode_name = codemirror_mode = 'textile'
diff --git a/waliki/directives/writer.py b/waliki/directives/writer.py
new file mode 100644
index 0000000..3137388
--- /dev/null
+++ b/waliki/directives/writer.py
@@ -0,0 +1,41 @@
+from ..rst2html5 import HTML5Writer
+from ..utils import get_url
+
+class WalikiAutolinksMixin(object):
+ def __init__(self, *args, **kwargs):
+ super(WalikiAutolinksMixin, self).__init__(*args, **kwargs)
+ self.unknown_reference_resolvers = (
+ (self.wiki_resolver,) + self.unknown_reference_resolvers)
+
+ def wiki_resolver(self, node):
+ refuri = None
+ if hasattr(node, 'indirect_reference_name'):
+ refuri = node.indirect_reference_name
+ elif len(node['ids']) != 0:
+ # If the node has an id then it's probably an internal link. Let
+ # docutils generate an error.
+ pass
+ elif node.hasattr('name'):
+ refuri = node['name']
+ else:
+ refuri = node['refname']
+
+ refuri = self.wiki_resolve_url(refuri)
+ if not refuri:
+ return False
+
+ node['refuri'] = refuri
+ del node['refname']
+ node.resolved = 1
+ return True
+
+ wiki_resolver.priority = 1
+
+
+class WalikiHTML5Writer(HTML5Writer,WalikiAutolinksMixin):
+ def __init__(self):
+ HTML5Writer.__init__(self)
+ WalikiAutolinksMixin.__init__(self)
+
+ def wiki_resolve_url(self, slug):
+ return get_url(slug)
diff --git a/waliki/settings.py b/waliki/settings.py
index 7fac6db..ae2dd70 100644
--- a/waliki/settings.py
+++ b/waliki/settings.py
@@ -3,7 +3,7 @@ from importlib import import_module
import collections
from django.conf import settings
from .utils import get_url
-from waliki.rst2html5 import HTML5Writer
+from .directives.writer import WalikiHTML5Writer
try:
from django.utils.module_loading import import_string
except ImportError:
@@ -44,7 +44,7 @@ def _get_markup_settings(user_settings):
'syntax_highlight': 'short',
'halt_level': 5,
},
- 'writer': HTML5Writer(),
+ 'writer': WalikiHTML5Writer(),
'writer_name': 'html5',
},
'Markdown': {
--
2.24.1