Repository: getpelican/pelican

Issue:
Add support for acronym/abbr
Like in Sphinx, for example:

https://bitbucket.org/birkenfeld/sphinx/changeset/f69ec6dd2c54

Docutils has this in the To Do List, but I like Georg's syntax better.

http://docutils.sourceforge.net/docs/dev/todo.html



Discussion:
Feel free to issue a pull request for this. The way it should be done is by extending the docutils parser, I think, or by replacing it by some code from sphinx.

Is anyone interested in working on this feature sometime in the next few days? We are trying to put together a 3.0 release very soon, and it would be helpful to know whether we should mark this issue for inclusion.

Can you just not mark it for inclusion but include it anyway if I have time to pull it together?

@djco: Done. Thanks in advance for trying to get it in!

@djco any news on this?

Haven't gotten to it. Just go ahead and do the release. :)

Let me know if anybody wants to step in and implement this for 3.0. Otherwise I'll defer this feature until the next release.

Is there an ETA for 3.0?

something like: in the next days :)

As Alexis said, there's no hard-and-fast ETA, but it'd be nice to get a release out to folks soon. Once all the targeted milestone issues have either been completed or deferred, I think we should do a last round of testing and then package up a release.


Resolving patch:
diff --git a/pelican/readers.py b/pelican/readers.py
--- a/pelican/readers.py
+++ b/pelican/readers.py
@@ -63,6 +63,18 @@ def render_node_to_html(document, node):
     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):
     enabled = bool(docutils)
     file_extensions = ['rst']
@@ -92,6 +104,7 @@ def _get_publisher(self, filename):
         pub = docutils.core.Publisher(
                 destination_class=docutils.io.StringOutput)
         pub.set_components('standalone', 'restructuredtext', 'html')
+        pub.writer.translator_class = PelicanHTMLTranslator
         pub.process_programmatic_settings(None, extra_params, None)
         pub.set_source(source_path=filename)
         pub.publish()
diff --git a/pelican/rstdirectives.py b/pelican/rstdirectives.py
--- a/pelican/rstdirectives.py
+++ b/pelican/rstdirectives.py
@@ -1,9 +1,10 @@
 # -*- coding: utf-8 -*-
-from docutils import nodes
-from docutils.parsers.rst import directives, Directive
+from docutils import nodes, utils
+from docutils.parsers.rst import directives, roles, Directive
 from pygments.formatters import HtmlFormatter
 from pygments import highlight
 from pygments.lexers import get_lexer_by_name, TextLexer
+import re
 
 INLINESTYLES = False
 DEFAULT = HtmlFormatter(noclasses=INLINESTYLES)
@@ -94,3 +95,18 @@ def run(self):
             nodes.raw('', '</div>', format='html')]
 
 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)


Test patch:
diff --git a/tests/content/article.rst b/tests/content/article.rst
--- a/tests/content/article.rst
+++ b/tests/content/article.rst
@@ -2,3 +2,5 @@ Article title
 #############
 
 This is some content. With some stuff to "typogrify".
+
+Now with added support for :abbr:`TLA (three letter acronym)`.
diff --git a/tests/test_readers.py b/tests/test_readers.py
--- a/tests/test_readers.py
+++ b/tests/test_readers.py
@@ -47,7 +47,9 @@ def test_typogrify(self):
         # unmodified
         content, _ = readers.read_file(_filename('article.rst'))
         expected = "<p>This is some content. With some stuff to "\
-                   "&quot;typogrify&quot;.</p>\n"
+                   "&quot;typogrify&quot;.</p>\n<p>Now with added "\
+                   'support for <abbr title="three letter acronym">'\
+                   'TLA</abbr>.</p>\n'
 
         self.assertEqual(content, expected)
 
@@ -56,7 +58,9 @@ def test_typogrify(self):
             content, _ = readers.read_file(_filename('article.rst'),
                                            settings={'TYPOGRIFY': True})
             expected = "<p>This is some content. With some stuff to&nbsp;"\
-                       "&#8220;typogrify&#8221;.</p>\n"
+                       "&#8220;typogrify&#8221;.</p>\n<p>Now with added "\
+                       'support for <abbr title="three letter acronym">'\
+                       'TLA</abbr>.</p>\n'
 
             self.assertEqual(content, expected)
         except ImportError: