summaryrefslogtreecommitdiff
path: root/hack/rst2html
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2023-03-19 15:27:45 +0100
committerjaseg <git@jaseg.de>2023-03-19 15:27:45 +0100
commit77469be23fe1777d776c0b0abd4c77f6e4080196 (patch)
treeb7fe0fbfb7c688837d491af89101d85614447ccf /hack/rst2html
parent92e3b5f49f6f5336530988e7839ab3ed283b86e4 (diff)
downloadblog-77469be23fe1777d776c0b0abd4c77f6e4080196.tar.gz
blog-77469be23fe1777d776c0b0abd4c77f6e4080196.tar.bz2
blog-77469be23fe1777d776c0b0abd4c77f6e4080196.zip
Make site mobile-friendly, make code listings pretty
Diffstat (limited to 'hack/rst2html')
-rwxr-xr-xhack/rst2html50
1 files changed, 50 insertions, 0 deletions
diff --git a/hack/rst2html b/hack/rst2html
new file mode 100755
index 0000000..e3a390d
--- /dev/null
+++ b/hack/rst2html
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+# https://gist.github.com/mastbaum/2655700
+
+import sys
+import re
+
+import docutils.core
+from docutils.transforms import Transform
+from docutils.nodes import TextElement, Inline, Text
+from docutils.parsers.rst import Directive, directives
+from docutils.writers.html4css1 import Writer, HTMLTranslator
+
+
+class UnfuckedHTMLTranslator(HTMLTranslator):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.in_literal_block = False
+
+ def visit_literal_block(self, node):
+ self.in_literal_block = True
+ self.body.append(self.starttag(node, 'pre', CLASS='literal-block'))
+ self.body.append('<span class="lineno"></span><span class="line">')
+
+ def depart_literal_block(self, node):
+ self.in_literal_block = False
+ self.body.append('\n</span></pre>\n')
+
+ def visit_Text(self, node):
+ if self.in_literal_block:
+ for match in re.finditer('([^\n]*)(\n|$)', node.astext()):
+ text, end = match.groups()
+
+ if text:
+ super().visit_Text(Text(text))
+
+ if end == '\n':
+ if isinstance(node.parent, Inline):
+ self.depart_inline(node.parent)
+ self.body.append(f'</span>\n<span class="lineno"></span><span class="line">')
+ if isinstance(node.parent, Inline):
+ self.visit_inline(node.parent)
+
+ else:
+ super().visit_Text(node)
+
+
+html_writer = Writer()
+html_writer.translator_class = UnfuckedHTMLTranslator
+docutils.core.publish_cmdline(writer=html_writer)
+