Skip to content

Instantly share code, notes, and snippets.

@rendaw
Last active May 27, 2021 19:56
Show Gist options
  • Save rendaw/13eae116d46022fca94e07b84eb73c3f to your computer and use it in GitHub Desktop.
Save rendaw/13eae116d46022fca94e07b84eb73c3f to your computer and use it in GitHub Desktop.
Preprocess literalinclude in .rst files
#!/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)
@bdsmith147
Copy link

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!

@rendaw
Copy link
Author

rendaw commented May 8, 2021

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.

@bdsmith147
Copy link

That was really helpful. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment