Skip to content

Instantly share code, notes, and snippets.

@qookei
Created August 27, 2023 15:56
Show Gist options
  • Save qookei/c302b95ff716257b424e877f3dceb502 to your computer and use it in GitHub Desktop.
Save qookei/c302b95ff716257b424e877f3dceb502 to your computer and use it in GitHub Desktop.
from pelican.readers import RstReader
from docutils.writers.html5_polyglot import Writer, HTMLTranslator
from docutils import nodes
from docutils.writers import _html_base
from pelican import signals
class CustomWriter(Writer):
def __init__(self):
super().__init__()
self.translator_class = CustomHTMLTranslator
class CustomHTMLTranslator(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>')
def visit_image(self, node):
# set an empty alt if alt is not specified
# avoids that alt is taken from src
node['alt'] = node.get('alt', '')
return HTMLTranslator.visit_image(self, node)
class HTML5RstReader(RstReader):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.writer_class = CustomWriter
def get_reader(readers):
readers.reader_classes['rst'] = HTML5RstReader
def register():
signals.readers_init.connect(get_reader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment