From 1250a08f7620b851a94faa442689784cae1159db Mon Sep 17 00:00:00 2001 From: Eric Plumb Date: Thu, 8 Aug 2013 17:06:50 -0700 Subject: [PATCH] Create html_entity plugin --- html_entity/README.rst | 30 +++++++++++++++++++++++++ html_entity/__init__.py | 0 html_entity/html_entity.py | 46 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 html_entity/README.rst create mode 100644 html_entity/__init__.py create mode 100644 html_entity/html_entity.py diff --git a/html_entity/README.rst b/html_entity/README.rst new file mode 100644 index 0000000..c772707 --- /dev/null +++ b/html_entity/README.rst @@ -0,0 +1,30 @@ +HTML Entities for reStructuredText +################################## + +This plugin allows you to enter HTML entities such as ©, <, • inline in a RST document, as opposed +to the tedious method of creating substitution definitions. + +Roles +===== + +Adds one inline role: + +:: + + :html_entity: + +Usage +===== + +:: + + :html_entity:`copy` 2013 :html_entity:`lt` Pelican :html_entity:`gt` Industries :html_entity:`149` All Rights Reserved + +produces: + +|copy| 2013 |lt| Pelican |gt| Industries |bullet| All Rights Reserved + +.. |copy| unicode:: U+000A9 .. COPYRIGHT SIGN +.. |lt| unicode:: U+003C .. LESS THAN +.. |gt| unicode:: U+003E .. GREATER THAN +.. |bullet| unicode:: U+2022 .. BULLET diff --git a/html_entity/__init__.py b/html_entity/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/html_entity/html_entity.py b/html_entity/html_entity.py new file mode 100644 index 0000000..ffb70e7 --- /dev/null +++ b/html_entity/html_entity.py @@ -0,0 +1,46 @@ +""" +HTML Entities for reStructured Text +=================================== + +Allows user to use HTML entities (©, •, etc.) in RST documents. + +Usage: +:html_entity:`copy` +:html_entity:`149` +:html_entity:`#149` +""" +from __future__ import unicode_literals +from docutils import nodes, utils +from docutils.parsers.rst import roles +from pelican.readers import PelicanHTMLTranslator +import six + + +class html_entity(nodes.Inline, nodes.Node): + # Subclassing Node directly since TextElement automatically appends the escaped element + def __init__(self, rawsource, text): + self.rawsource = rawsource + self.text = text + self.children = [] + self.attributes = {} + + def astext(self): + return self.text + + +def entity_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): + text = utils.unescape(text) + entity_code = text + try: + entity_code = "#{}".format(six.u(int(entity_code))) + except ValueError: + pass + entity_code = "&{};".format(entity_code) + return [html_entity(text, entity_code)], [] + + +def register(): + roles.register_local_role('html_entity', entity_role) + +PelicanHTMLTranslator.visit_html_entity = lambda self, node: self.body.append(node.astext()) +PelicanHTMLTranslator.depart_html_entity = lambda self, node: None