From 1842b88c21eaab515eadaa0cb359763d8ee296e9 Mon Sep 17 00:00:00 2001 From: Thibault Kruse Date: Sat, 10 Sep 2016 09:09:37 +0900 Subject: [PATCH] refactor python code extractor: extract function --- scripts/python/md-split.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/scripts/python/md-split.py b/scripts/python/md-split.py index 21af9d9..e06fe85 100755 --- a/scripts/python/md-split.py +++ b/scripts/python/md-split.py @@ -115,10 +115,19 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co codefile = os.path.join(codedir, '%s%s.cpp' % (name, index)) if fenced: text_filehandle.write('\n') + if (has_actual_code and not has_question_marks): - # add commonly used headers, so that lines can compile - with io.open(codefile, 'w') as code_filehandle: - code_filehandle.write('''\ + write_with_harness(codefile, sourcefile, start_linenum, linebuffer) + return (line, linenum) + + +def write_with_harness(codefile, sourcefile, start_linenum, linebuffer): + '''write output with additional lines to make code likely compilable''' + # add commonly used headers, so that lines can likely compile. + # This is work in progress, the main issue remains handling class + # declarations in in-function code differently + with io.open(codefile, 'w') as code_filehandle: + code_filehandle.write('''\ #include // by md-split #include // by md-split #include // by md-split @@ -134,10 +143,9 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co using namespace std; // by md-split // %s : %s ''' % (sourcefile, start_linenum)) - # TODO: if not toplevel code, wrap inside class - for codeline in linebuffer: - code_filehandle.write(codeline) - return (line, linenum) + # TODO: if not toplevel code, wrap inside class + for codeline in linebuffer: + code_filehandle.write(codeline) def is_code(line, indent_depth = 4):