From c5e7d3b2787d72eab286bd3fdf437c53cf81fb31 Mon Sep 17 00:00:00 2001 From: John Andersen Date: Mon, 28 Jan 2019 14:53:48 -0800 Subject: [PATCH 1/3] scripts: Code Block rst parser Signed-off-by: John Andersen --- .../scripts/_python/utilities/code-blocks.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 source/scripts/_python/utilities/code-blocks.py diff --git a/source/scripts/_python/utilities/code-blocks.py b/source/scripts/_python/utilities/code-blocks.py new file mode 100644 index 00000000..c1c0ea98 --- /dev/null +++ b/source/scripts/_python/utilities/code-blocks.py @@ -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() From d1ff18c7339d05b277f74acf0ba927d3b806fa16 Mon Sep 17 00:00:00 2001 From: Kevin Putnam Date: Fri, 22 Feb 2019 10:15:51 -0800 Subject: [PATCH 2/3] limited to bash code blocks and added description to the header --- source/scripts/_python/utilities/code-blocks.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/scripts/_python/utilities/code-blocks.py b/source/scripts/_python/utilities/code-blocks.py index c1c0ea98..be68ded2 100644 --- a/source/scripts/_python/utilities/code-blocks.py +++ b/source/scripts/_python/utilities/code-blocks.py @@ -1,7 +1,9 @@ ''' Usage: python code-blocks.py path/to/file.rst -Parses code blocks out of rst file +Default output is to stdout + +Parses bash code blocks out of rst file for instruction testing. ''' import sys @@ -11,7 +13,7 @@ def code_blocks(filename): in_section = [] for line in fd: indent = line[:len(line) - len(line.lstrip())] - if 'code-block' in line: + if 'code-block:: bash' in line: in_section = [line.strip()] c_indent = '' elif in_section: From ad16371260d8557984486e817b59566833a7bd27 Mon Sep 17 00:00:00 2001 From: Kevin Putnam Date: Mon, 25 Feb 2019 09:11:49 -0800 Subject: [PATCH 3/3] added ability to create a list of code blocks to parse --- source/scripts/_python/utilities/code-blocks.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/source/scripts/_python/utilities/code-blocks.py b/source/scripts/_python/utilities/code-blocks.py index be68ded2..2c805cc7 100644 --- a/source/scripts/_python/utilities/code-blocks.py +++ b/source/scripts/_python/utilities/code-blocks.py @@ -3,19 +3,26 @@ Usage: python code-blocks.py path/to/file.rst Default output is to stdout -Parses bash code blocks out of rst file for instruction testing. +Parses all code found in blocks listed in blockTypes out of rst file for instruction testing. ''' import sys +blockTypes = ["bash","console"] + def code_blocks(filename): with open(filename, 'r') as fd: c_indent = '' in_section = [] for line in fd: + blockFound = False indent = line[:len(line) - len(line.lstrip())] - if 'code-block:: bash' in line: + for blockType in blockTypes: + if 'code-block:: ' + blockType in line: + blockFound = True + if blockFound: in_section = [line.strip()] c_indent = '' + blockFound = False elif in_section: if not c_indent and line.strip(): c_indent = indent