refactor python code extractor: extract function

This commit is contained in:
Thibault Kruse 2016-09-10 09:09:37 +09:00
parent 8e282fa90c
commit 1842b88c21

View File

@ -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<stdio.h> // by md-split
#include<stdlib.h> // by md-split
#include<tuple> // 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):