From 7c836f0f42c5b91de54a689b8a8de136c6d5d845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= Date: Fri, 19 Jun 2020 07:13:36 -0400 Subject: [PATCH] docutils: Warn about long lines --- README.rst | 7 +++++++ alectryon/core.py | 10 ++++++++++ alectryon/docutils.py | 13 ++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 0f6468a6..8f285723 100644 --- a/README.rst +++ b/README.rst @@ -123,6 +123,13 @@ See |help(docutils)|_ for more information. The ``.. coq::`` directive accepts To ensure that Coq blocks render properly, you'll need to tell your blogging platform to include ``alectryon.css``. Using a git submodule or vendoring a copy of Alectryon is an easy way to ensure that this stylesheet is accessible to your blogging software. +By default, Alectryon will raise warnings for lines over 72 characters. You can change the threshold or silence the warnings by adjusting ``alectryon.docutils.LONG_LINE_THRESHOLD``. With `Pelican `_, use the following snippet to make warnings non-fatal:: + + DOCUTILS_SETTINGS = { + 'halt_level': 3, # Error + 'warning_stream': None # stderr + } + .. |help(docutils)| replace:: ``help(alectryon.docutils)`` .. _help(docutils): alectryon/docutils.py diff --git a/alectryon/core.py b/alectryon/core.py index 7c685241..b2125e41 100755 --- a/alectryon/core.py +++ b/alectryon/core.py @@ -380,3 +380,13 @@ def group_whitespace_with_code(fragments): else: grouped.append(fr) return grouped + +def find_long_lines(fragments, threshold): + prefix = "" + for fr in fragments: + suffix = "".join(w.contents for w in getattr(fr, "wsp", ())) + lines = (prefix + fr.contents + suffix).split("\n") + for line in lines: + if len(line) > threshold: + yield line + prefix = lines[-1] diff --git a/alectryon/docutils.py b/alectryon/docutils.py index ccd076ff..1c3ca579 100644 --- a/alectryon/docutils.py +++ b/alectryon/docutils.py @@ -47,7 +47,7 @@ from docutils.parsers.rst import directives, roles, Directive from docutils.transforms import Transform -from .core import annotate, group_whitespace_with_code, process_io_annotations +from .core import annotate, group_whitespace_with_code, process_io_annotations, find_long_lines from .html import HtmlWriter from .pygments import highlight @@ -63,6 +63,8 @@ class alectryon_pending_toggle(Special, Invisible, Element): Display all goals and responses """.replace('\n', '') +LONG_LINE_THRESHOLD = 72 + class AlectryonTransform(Transform): default_priority = 995 auto_toggle = True @@ -74,6 +76,14 @@ def set_fragment_annots(fragments, options): if hasattr(fr, 'annots') and not fr.annots: fr.annots.add('all') + def check_for_long_lines(self, node, fragments): + if LONG_LINE_THRESHOLD is None: + return + for line in find_long_lines(fragments, threshold=LONG_LINE_THRESHOLD): + msg = "Long line: {!r} ({} characters)".format(line, len(line)) + self.document.reporter.warning(msg, base_node=node) + return + def apply_coq(self): writer = HtmlWriter(highlight) nodes = list(self.document.traverse(alectryon_pending)) @@ -85,6 +95,7 @@ def apply_coq(self): else: classes = ("alectryon-floating",) fragments = group_whitespace_with_code(process_io_annotations(fragments)) + self.check_for_long_lines(node, fragments) AlectryonTransform.set_fragment_annots(fragments, options) html = writer.gen_fragments_html(fragments, classes=classes).render(pretty=False) node.replace_self(raw(node['content'], html, format='html'))