-
-
Save qookei/c302b95ff716257b424e877f3dceb502 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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