mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Add support for abbreviations to reST translator (fixes #395).
This commit is contained in:
parent
be2f04f082
commit
636fd6cc38
4 changed files with 39 additions and 4 deletions
|
|
@ -63,6 +63,18 @@ def render_node_to_html(document, node):
|
||||||
return visitor.astext()
|
return visitor.astext()
|
||||||
|
|
||||||
|
|
||||||
|
class PelicanHTMLTranslator(HTMLTranslator):
|
||||||
|
|
||||||
|
def visit_abbreviation(self, node):
|
||||||
|
attrs = {}
|
||||||
|
if node.hasattr('explanation'):
|
||||||
|
attrs['title'] = node['explanation']
|
||||||
|
self.body.append(self.starttag(node, 'abbr', '', **attrs))
|
||||||
|
|
||||||
|
def depart_abbreviation(self, node):
|
||||||
|
self.body.append('</abbr>')
|
||||||
|
|
||||||
|
|
||||||
class RstReader(Reader):
|
class RstReader(Reader):
|
||||||
enabled = bool(docutils)
|
enabled = bool(docutils)
|
||||||
file_extensions = ['rst']
|
file_extensions = ['rst']
|
||||||
|
|
@ -92,6 +104,7 @@ class RstReader(Reader):
|
||||||
pub = docutils.core.Publisher(
|
pub = docutils.core.Publisher(
|
||||||
destination_class=docutils.io.StringOutput)
|
destination_class=docutils.io.StringOutput)
|
||||||
pub.set_components('standalone', 'restructuredtext', 'html')
|
pub.set_components('standalone', 'restructuredtext', 'html')
|
||||||
|
pub.writer.translator_class = PelicanHTMLTranslator
|
||||||
pub.process_programmatic_settings(None, extra_params, None)
|
pub.process_programmatic_settings(None, extra_params, None)
|
||||||
pub.set_source(source_path=filename)
|
pub.set_source(source_path=filename)
|
||||||
pub.publish()
|
pub.publish()
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from docutils import nodes
|
from docutils import nodes, utils
|
||||||
from docutils.parsers.rst import directives, Directive
|
from docutils.parsers.rst import directives, roles, Directive
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
from pygments.lexers import get_lexer_by_name, TextLexer
|
from pygments.lexers import get_lexer_by_name, TextLexer
|
||||||
|
import re
|
||||||
|
|
||||||
INLINESTYLES = False
|
INLINESTYLES = False
|
||||||
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)
|
DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)
|
||||||
|
|
@ -94,3 +95,18 @@ class YouTube(Directive):
|
||||||
nodes.raw('', '</div>', format='html')]
|
nodes.raw('', '</div>', format='html')]
|
||||||
|
|
||||||
directives.register_directive('youtube', YouTube)
|
directives.register_directive('youtube', YouTube)
|
||||||
|
|
||||||
|
_abbr_re = re.compile('\((.*)\)$')
|
||||||
|
|
||||||
|
class abbreviation(nodes.Inline, nodes.TextElement): pass
|
||||||
|
|
||||||
|
def abbr_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
|
||||||
|
text = utils.unescape(text)
|
||||||
|
m = _abbr_re.search(text)
|
||||||
|
if m is None:
|
||||||
|
return [abbreviation(text, text)], []
|
||||||
|
abbr = text[:m.start()].strip()
|
||||||
|
expl = m.group(1)
|
||||||
|
return [abbreviation(abbr, abbr, explanation=expl)], []
|
||||||
|
|
||||||
|
roles.register_local_role('abbr', abbr_role)
|
||||||
|
|
|
||||||
|
|
@ -2,3 +2,5 @@ Article title
|
||||||
#############
|
#############
|
||||||
|
|
||||||
This is some content. With some stuff to "typogrify".
|
This is some content. With some stuff to "typogrify".
|
||||||
|
|
||||||
|
Now with added support for :abbr:`TLA (three letter acronym)`.
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,9 @@ class RstReaderTest(unittest.TestCase):
|
||||||
# unmodified
|
# unmodified
|
||||||
content, _ = readers.read_file(_filename('article.rst'))
|
content, _ = readers.read_file(_filename('article.rst'))
|
||||||
expected = "<p>This is some content. With some stuff to "\
|
expected = "<p>This is some content. With some stuff to "\
|
||||||
""typogrify".</p>\n"
|
""typogrify".</p>\n<p>Now with added "\
|
||||||
|
'support for <abbr title="three letter acronym">'\
|
||||||
|
'TLA</abbr>.</p>\n'
|
||||||
|
|
||||||
self.assertEqual(content, expected)
|
self.assertEqual(content, expected)
|
||||||
|
|
||||||
|
|
@ -56,7 +58,9 @@ class RstReaderTest(unittest.TestCase):
|
||||||
content, _ = readers.read_file(_filename('article.rst'),
|
content, _ = readers.read_file(_filename('article.rst'),
|
||||||
settings={'TYPOGRIFY': True})
|
settings={'TYPOGRIFY': True})
|
||||||
expected = "<p>This is some content. With some stuff to "\
|
expected = "<p>This is some content. With some stuff to "\
|
||||||
"“typogrify”.</p>\n"
|
"“typogrify”.</p>\n<p>Now with added "\
|
||||||
|
'support for <abbr title="three letter acronym">'\
|
||||||
|
'TLA</abbr>.</p>\n'
|
||||||
|
|
||||||
self.assertEqual(content, expected)
|
self.assertEqual(content, expected)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue