Last active
May 27, 2021 19:56
-
-
Save rendaw/13eae116d46022fca94e07b84eb73c3f to your computer and use it in GitHub Desktop.
Preprocess literalinclude in .rst files
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
#!/usr/bin/env python3 | |
# AUTHOR: rendaw | |
# LICENSE: https://opensource.org/licenses/BSD-2-Clause | |
import glob | |
import re | |
import os.path | |
for source in glob.glob('./**/*.rst.src', recursive=True): | |
dirname = os.path.dirname(source) | |
def literalinclude(match): | |
with open(os.path.join(dirname, match.group('filename')), 'r') as f: | |
body = f.read() | |
out = '.. code-block::' | |
if match.group('language'): | |
out += ' ' + match.group('language') | |
out += '\n' | |
body = body.splitlines() | |
start = match.group('start') | |
end = match.group('end') | |
if start is not None: | |
body = body[int(start) - 1:int(end)] | |
deindent = None | |
for line in body: | |
line_indent = len(re.search('^( *)', line).group(1)) | |
if deindent is None or line_indent < deindent: | |
deindent = line_indent | |
out += '\n' | |
out += '\n'.join(' ' + line[deindent:] for line in body) | |
return out | |
dest = re.sub('\\.src', '', source) | |
with open(source, 'r') as f: | |
text = f.read() | |
text = re.sub( | |
'^\\.\\. literalinclude:: (?P<filename>.*)$' | |
'(?:\\s^(?:' | |
'(?: :language: (?P<language>.*))|' | |
'(?: (?P<linenos>:linenos:))|' | |
'(?: :lines: (?P<start>.*)-(?P<end>.*))' | |
')$)*', | |
literalinclude, | |
text, | |
flags=re.M, | |
) | |
with open(dest, 'w') as f: | |
f.write(text) |
Can you recommend a way to modify this script to include normal *.rst
files into the README.rst - files that don't necessarily need a code-block block? Sorry, newbie here. Thanks!
Sure! I think you can just change the regex to ^\\.\\. include:: (?P<filename>.*)$
(removing all the extra lines with language/linenos/lines) and the literalinclude
function can just return body
right away. That will replace any .. include:: my_other_file.rst
with the contents of my_other_file.rst
.
That was really helpful. Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will convert all files named
*.rst.src
in the current subtree to.rst
files with literalinclude blocks replaced by code-block blocks.