with open(filename, 'r') as fp:
- line_classimp = -1
- line_startcond = -1
- line_endcond = -1
- classimp_class = None
- classimp_indent = None
-
for line in fp:
+ # Reset to nothing found
+ line_classimp = -1
+ line_startcond = -1
+ line_endcond = -1
+ classimp_class = None
+ classimp_indent = None
+
line_num = line_num + 1
mclassimp = re.search(reclassimp, line)
in_classimp_cond = False
line_endcond = line_num
- # Did we find something?
- if line_classimp != -1:
+ # Did we find something?
+ if line_classimp != -1:
- if line_startcond != -1:
- comments.append(Comment(
- ['\cond CLASSIMP'],
- line_startcond, 1, line_startcond, 1,
- classimp_indent, 'ClassImp/Def(%s)' % classimp_class,
- append_empty=False
- ))
- else:
- comments.append(PrependComment(
- ['\cond CLASSIMP'],
- line_classimp, 1, line_classimp, 1,
- classimp_indent, 'ClassImp/Def(%s)' % classimp_class
- ))
-
- if line_endcond != -1:
- comments.append(Comment(
- ['\endcond'],
- line_endcond, 1, line_endcond, 1,
- classimp_indent, 'ClassImp/Def(%s)' % classimp_class,
- append_empty=False
- ))
- else:
- comments.append(PrependComment(
- ['\endcond'],
- line_classimp+1, 1, line_classimp+1, 1,
- classimp_indent, 'ClassImp/Def(%s)' % classimp_class
- ))
+ if line_startcond != -1:
+ comments.append(Comment(
+ ['\cond CLASSIMP'],
+ line_startcond, 1, line_startcond, 1,
+ classimp_indent, 'ClassImp/Def(%s)' % classimp_class,
+ append_empty=False
+ ))
+ else:
+ comments.append(PrependComment(
+ ['\cond CLASSIMP'],
+ line_classimp, 1, line_classimp, 1,
+ classimp_indent, 'ClassImp/Def(%s)' % classimp_class
+ ))
+
+ if line_endcond != -1:
+ comments.append(Comment(
+ ['\endcond'],
+ line_endcond, 1, line_endcond, 1,
+ classimp_indent, 'ClassImp/Def(%s)' % classimp_class,
+ append_empty=False
+ ))
+ else:
+ comments.append(PrependComment(
+ ['\endcond'],
+ line_classimp+1, 1, line_classimp+1, 1,
+ classimp_indent, 'ClassImp/Def(%s)' % classimp_class
+ ))
## Traverse the AST recursively starting from the current cursor.
# @param comment An array containing the lines of the original comment
def refactor_comment(comment, do_strip_html=True, infilename=None):
- recomm = r'^(/{2,}|/\*)? ?(\s*.*?)\s*((/{2,})?\s*|\*/)$'
- regarbage = r'^(?i)\s*([\s*=-_#]+|(Begin|End)_Html)\s*$'
+ recomm = r'^(/{2,}|/\*)? ?(\s*)(.*?)\s*((/{2,})?\s*|\*/)$'
+ regarbage = r'^(?i)\s*([\s*=_#-]+|(Begin|End)_Html)\s*$'
# Support for LaTeX blocks spanning on multiple lines
relatex = r'(?i)^((.*?)\s+)?(BEGIN|END)_LATEX([.,;:\s]+.*)?$'
current_macro = []
remacro = r'(?i)^\s*(BEGIN|END)_MACRO(\((.*?)\))?\s*$'
+ # Minimum indent level: scale back everything
+ lowest_indent_level = None
+
+ # Indentation threshold: if too much indented, don't indent at all
+ indent_level_threshold = 15
+
new_comment = []
insert_blank = False
wait_first_non_blank = True
current_macro = []
# Insert image
- new_comment.append( '![Picture from ROOT macro](%s)' % (outimg) )
+ new_comment.append( '![Picture from ROOT macro](%s)' % (os.path.basename(outimg)) )
logging.debug( 'Found macro for generating image %s' % Colt(outimg).magenta() )
mcomm = re.search( recomm, line_comment )
if mcomm:
- new_line_comment = mcomm.group(2)
+ new_line_comment = mcomm.group(2) + mcomm.group(3) # indent + comm
+
mgarbage = re.search( regarbage, new_line_comment )
+ if mgarbage is None and not mcomm.group(3).startswith('\\') and mcomm.group(3) != '':
+ # not a special command line: count indent
+ indent_level = len( mcomm.group(2) )
+ if lowest_indent_level is None or indent_level < lowest_indent_level:
+ lowest_indent_level = indent_level
+
+ # if indentation level is too much, consider it zero
+ if indent_level > indent_level_threshold:
+ new_line_comment = mcomm.group(3) # remove ALL indentation
+
if new_line_comment == '' or mgarbage is not None:
insert_blank = True
else:
else:
assert False, 'Comment regexp does not match'
+ # Fixing indentation level
+ if lowest_indent_level is not None:
+ logging.debug('Lowest indentation level found: %d' % lowest_indent_level)
+
+ new_comment_indent = []
+ reblankstart = r'^\s+'
+ for line in new_comment:
+ if re.search(reblankstart, line):
+ new_comment_indent.append( line[lowest_indent_level:] )
+ else:
+ new_comment_indent.append( line )
+
+ new_comment = new_comment_indent
+
+ else:
+ logging.debug('No indentation scaling applied')
+
return new_comment
# Find current comment
prev_comm = comm
comm = None
+ comm_list = []
for c in comments:
if c.has_comment(line_num):
comm = c
+ comm_list.append(c)
+
+ if len(comm_list) > 1:
+
+ merged = True
+
+ if len(comm_list) == 2:
+ c1,c2 = comm_list
+ if isinstance(c1, Comment) and isinstance(c2, Comment):
+ c1.lines = c1.lines + c2.lines # list merge
+ comm = c1
+ logging.debug('Two adjacent comments merged. Result: {%s}' % Colt(comm).cyan())
+ else:
+ merged = False
+ else:
+ merged = False
+
+ if merged == False:
+ logging.warning('Too many unmergeable comments on the same line (%d), picking the last one' % len(comm_list))
+ for c in comm_list:
+ logging.warning('>> %s' % c)
+ comm = c # considering the last one
if comm:
# First thing to check: are we in the same comment as before?
- if comm is not prev_comm and isinstance(comm, Comment) and isinstance(prev_comm, Comment) \
- and not isinstance(prev_comm, RemoveComment):
+ if comm is not prev_comm and \
+ isinstance(comm, Comment) and \
+ isinstance(prev_comm, Comment) and \
+ not isinstance(prev_comm, RemoveComment):
+
+ # We are NOT in the same comment as before, and this comment is dumpable
skip_empty = dump_comment_block(prev_comm, restore_lines)
in_comment = False
in_comment = False
restore_lines = None
- elif prev_comm is None:
+ elif restore_lines is None:
# Beginning of a new comment block of type Comment or PrependComment
in_comment = True
restore_lines.append( line.rstrip('\n') )
else:
- assert False, 'Unhandled parser state. line=%d comm=%s prev_comm=%s' % \
+ assert False, 'Unhandled parser state: line=%d comm={%s} prev_comm={%s}' % \
(line_num, comm, prev_comm)
else: