Skip to content

Commit

Permalink
docutils: Warn about long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
cpitclaudel committed Jun 20, 2020
1 parent dc29a51 commit 7c836f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/getpelican/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

Expand Down
10 changes: 10 additions & 0 deletions alectryon/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
13 changes: 12 additions & 1 deletion alectryon/docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -63,6 +63,8 @@ class alectryon_pending_toggle(Special, Invisible, Element):
Display all goals and responses
</label>""".replace('\n', '')

LONG_LINE_THRESHOLD = 72

class AlectryonTransform(Transform):
default_priority = 995
auto_toggle = True
Expand All @@ -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))
Expand All @@ -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'))
Expand Down

0 comments on commit 7c836f0

Please sign in to comment.