mirror of
https://github.com/clearlinux/clear-linux-documentation.git
synced 2026-06-30 01:35:59 +00:00
scripts: Code Block rst parser
Signed-off-by: John Andersen <john.s.andersen@intel.com>
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
'''
|
||||
Usage: python code-blocks.py path/to/file.rst
|
||||
|
||||
Parses code blocks out of rst file
|
||||
'''
|
||||
import sys
|
||||
|
||||
def code_blocks(filename):
|
||||
with open(filename, 'r') as fd:
|
||||
c_indent = ''
|
||||
in_section = []
|
||||
for line in fd:
|
||||
indent = line[:len(line) - len(line.lstrip())]
|
||||
if 'code-block' in line:
|
||||
in_section = [line.strip()]
|
||||
c_indent = ''
|
||||
elif in_section:
|
||||
if not c_indent and line.strip():
|
||||
c_indent = indent
|
||||
if not (len(indent) >= len(c_indent)) and line.strip():
|
||||
yield in_section[2:]
|
||||
in_section = []
|
||||
else:
|
||||
in_section.append(line[len(c_indent):].rstrip())
|
||||
|
||||
def main():
|
||||
for code_block in code_blocks(sys.argv[1]):
|
||||
print('\n'.join(code_block) + '\n')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user