@@ -108,7 +108,7 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
108108 if (not line .strip () == '```' ):
109109 if ('???' == no_comment_line or '...' == no_comment_line ):
110110 has_question_marks = True
111- linebuffer .append (dedent (line ) if not fenced else line )
111+ linebuffer .append (dedent (line , indent_depth ) if not fenced else line )
112112 try :
113113 line = read_filehandle .next ()
114114 linenum += 1
@@ -118,10 +118,33 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
118118 codefile = os .path .join (codedir , '%s%s.cpp' % (name , index ))
119119 if fenced :
120120 text_filehandle .write ('\n ' )
121+
121122 if (has_actual_code and not has_question_marks ):
122- # add commonly used headers, so that lines can compile
123- with io .open (codefile , 'w' ) as code_filehandle :
124- code_filehandle .write ('''\
123+ linebuffer = clean_trailing_newlines (linebuffer )
124+ write_with_harness (codefile , sourcefile , start_linenum , linebuffer )
125+ return (line , linenum )
126+
127+
128+ def clean_trailing_newlines (linebuffer ):
129+ result = []
130+ code_started = False
131+ linebuffer .reverse ()
132+ for line in linebuffer :
133+ if not code_started and line == '\n ' :
134+ continue
135+ code_started = True
136+ result .append (line )
137+ result .reverse ()
138+ return result
139+
140+
141+ def write_with_harness (codefile , sourcefile , start_linenum , linebuffer ):
142+ '''write output with additional lines to make code likely compilable'''
143+ # add commonly used headers, so that lines can likely compile.
144+ # This is work in progress, the main issue remains handling class
145+ # declarations in in-function code differently
146+ with io .open (codefile , 'w' ) as code_filehandle :
147+ code_filehandle .write ('''\
125148 #include<stdio.h> // by md-split
126149#include<stdlib.h> // by md-split
127150#include<tuple> // by md-split
@@ -137,10 +160,9 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co
137160using namespace std; // by md-split
138161// %s : %s
139162''' % (sourcefile , start_linenum ))
140- # TODO: if not toplevel code, wrap inside class
141- for codeline in linebuffer :
142- code_filehandle .write (codeline )
143- return (line , linenum )
163+ # TODO: if not toplevel code, wrap inside class
164+ for codeline in linebuffer :
165+ code_filehandle .write (codeline )
144166
145167
146168def is_code (line , indent_depth = 4 ):
@@ -150,17 +172,17 @@ def is_code(line, indent_depth = 4):
150172 return 0
151173
152174def is_inside_code (line , indent_depth ):
153- return is_code (line , indent_depth ) or line .strip () == ''
175+ return is_code (line , indent_depth ) > 0 or line .strip () == ''
154176
155177def stripped (line ):
156178 # Remove well-formed html tags, fixing mistakes by legitimate users
157179 sline = TAG_REGEX .sub ('' , line )
158180 sline = re .sub ('[()\[\]#*]' , ' ' , line )
159181 return sline
160182
161- def dedent (line ):
162- if line .startswith (' ' ):
163- return line [4 :]
183+ def dedent (line , indent_depth ):
184+ if line .startswith (' ' * indent_depth ):
185+ return line [indent_depth :]
164186 if line .startswith ('\t ' ):
165187 return line [1 :]
166188 return line
0 commit comments