Created
July 15, 2021 15:37
-
-
Save erikrose/54ac58de3a0f72a18a2b551c5ac906b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
"""BBEdit UNIX filter that wraps one or more optionally-indented, | |
hash-commented lines to 79 chars, preserving indentation. | |
Just select all the lines (in their entirety) the comment spans, and invoke. | |
Works with #, --, and //-style comments. | |
For example, this... :: | |
# Upping this to 10000 makes it 3x faster. 10000 takes 15.936s. 5000 takes 16.303s. | |
...becomes this:: | |
# Upping this to 10000 makes it 3x faster. 10000 takes 15.936s. 5000 | |
# takes 16.303s. | |
And this... :: | |
'PRIMARY-10/14/2008': None, # A special Congressional primary to fill a vacancy that occurred on 8/20/08 in the 22th district | |
...becomes this:: | |
'PRIMARY-10/14/2008': None, # A special Congressional primary to fill a | |
# vacancy that occurred on 8/20/08 in the 22th | |
# district | |
Also tolerates and preserves leading decoration characters like the | |
asterisks in...:: | |
/* | |
* BLah blah blah. | |
* | |
* Snoofy woofy goofy doo. | |
* Etc. | |
*/ | |
""" | |
import re | |
from sys import stdin | |
from textwrap import wrap | |
UNCOMMENT_AND_UNINDENT = re.compile(r'^(.*?)(# |// |-- |\* )', re.MULTILINE) | |
text = stdin.read() | |
m = UNCOMMENT_AND_UNINDENT.search(text) | |
if m: | |
comment_prefix = m.group(1) | |
comment_chars = m.group(2) | |
stripped = UNCOMMENT_AND_UNINDENT.sub('', text) | |
print '\n'.join(wrap( | |
stripped, | |
79, | |
initial_indent=comment_prefix + comment_chars, | |
subsequent_indent=' ' * len(comment_prefix) + comment_chars)), | |
else: | |
print text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment