diff --git a/scripts/python/Makefile.in b/scripts/python/Makefile.in index 55c17a7..4776246 100644 --- a/scripts/python/Makefile.in +++ b/scripts/python/Makefile.in @@ -11,8 +11,13 @@ CXX_SRCS := $(wildcard *.cpp) CXX_LINT := ${CXX_SRCS:.cpp=.lint} .PHONY: cpplint-all +<<<<<<< HEAD cpplint-all: $(CXX_LINT) %.lint: %.cpp @python ../../python/cpplint.py --verbose=0 --linelength=100 --filter=-legal/copyright,-build/include_order,-build/namespaces,-build/include,-build/include_subdir,-readability/inheritance,-readability/casting,-readability/namespace,-readability/alt_tokens,-readability/braces,-readability/fn_size,-whitespace/comments,-whitespace/braces,-whitespace/empty_loop_body,-whitespace/indent,-whitespace/newline,-runtime/explicit,-runtime/arrays,-runtime/int,-runtime/references,-runtime/string,-runtime/operator $< || (cat $< | nl -ba | grep -v 'md-split' && false) +======= +cpplint-all: + @python ../../python/cpplint_wrap.py *.cpp +>>>>>>> 084eb29d426688617b6e19846f508b6d1cb3a564 diff --git a/scripts/python/cpplint_wrap.py b/scripts/python/cpplint_wrap.py new file mode 100644 index 0000000..fd7a9f9 --- /dev/null +++ b/scripts/python/cpplint_wrap.py @@ -0,0 +1,32 @@ +## wraps local cpplint to produce verbose output without code harness +import cpplint +import sys + +def main(): + FILTERS='cpplint --verbose=0 --linelength=100 --filter=-legal/copyright,-build/include_order,-build/c++11,-build/namespaces,-build/class,-build/include,-build/include_subdir,-readability/inheritance,-readability/function,-readability/casting,-readability/namespace,-readability/alt_tokens,-readability/braces,-readability/fn_size,-whitespace/comments,-whitespace/braces,-whitespace/empty_loop_body,-whitespace/indent,-whitespace/newline,-runtime/explicit,-runtime/arrays,-runtime/int,-runtime/references,-runtime/string,-runtime/operator'.split(' ') + + result = False + files = sys.argv[1:] + for loopfile in files: + newargs = FILTERS + [loopfile] + sys.argv = newargs + + try: + cpplint.main() + except SystemExit as e: + last_result = e.args[0] + result = result or last_result + if (last_result): + write_code_lines(loopfile) + sys.exit(result) + +def write_code_lines(filename): + with open(filename, 'r') as f: + linenum = 1 + for line in f: + if (not '// by md-split' in line): + sys.stdout.write('%3d %s' % (linenum, line)) + linenum += 1 + +if __name__ == '__main__': + main() diff --git a/scripts/python/md-split.py b/scripts/python/md-split.py index 2e8e063..befc7b8 100755 --- a/scripts/python/md-split.py +++ b/scripts/python/md-split.py @@ -108,7 +108,7 @@ def process_code(read_filehandle, text_filehandle, line, linenum, sourcefile, co if (not line.strip() == '```'): if ('???' == no_comment_line or '...' == no_comment_line): has_question_marks = True - linebuffer.append(dedent(line) if not fenced else line) + linebuffer.append(dedent(line, indent_depth) if not fenced else line) try: line = read_filehandle.next() linenum += 1 @@ -118,10 +118,33 @@ 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('''\ + linebuffer = clean_trailing_newlines(linebuffer) + write_with_harness(codefile, sourcefile, start_linenum, linebuffer) + return (line, linenum) + + +def clean_trailing_newlines(linebuffer): + result = [] + code_started = False + linebuffer.reverse() + for line in linebuffer: + if not code_started and line == '\n': + continue + code_started = True + result.append(line) + result.reverse() + return result + + +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 @@ -137,10 +160,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): @@ -150,7 +172,7 @@ def is_code(line, indent_depth = 4): return 0 def is_inside_code(line, indent_depth): - return is_code(line, indent_depth) or line.strip() == '' + return is_code(line, indent_depth) > 0 or line.strip() == '' def stripped(line): # Remove well-formed html tags, fixing mistakes by legitimate users @@ -158,9 +180,9 @@ def stripped(line): sline = re.sub('[()\[\]#*]', ' ', line) return sline -def dedent(line): - if line.startswith(' '): - return line[4:] +def dedent(line, indent_depth): + if line.startswith(' ' * indent_depth): + return line[indent_depth:] if line.startswith('\t'): return line[1:] return line