]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - doxygen/thtml2doxy.py
correcting DDL bit for AD and HLT
[u/mrichter/AliRoot.git] / doxygen / thtml2doxy.py
index e0ced0d039ce1d2211cf8375935040c139f8d61d..a2c64729e58ca38a400698aa218a309bca7b4e77 100755 (executable)
@@ -37,6 +37,7 @@ import os
 import re
 import logging
 import getopt
+import hashlib
 import clang.cindex
 
 
@@ -88,22 +89,36 @@ class Comment:
 ## A data member comment.
 class MemberComment:
 
-  def __init__(self, text, is_transient, array_size, first_line, first_col, func):
+  def __init__(self, text, comment_flag, array_size, first_line, first_col, func):
     assert first_line > 0, 'Wrong line number'
+    assert comment_flag is None or comment_flag == '!' or comment_flag in [ '!', '||', '->' ]
     self.lines = [ text ]
-    self.is_transient = is_transient
+    self.comment_flag = comment_flag
     self.array_size = array_size
     self.first_line = first_line
     self.first_col = first_col
     self.func = func
 
+  def is_transient(self):
+    return self.comment_flag == '!'
+
+  def is_dontsplit(self):
+    return self.comment_flag == '||'
+
+  def is_ptr(self):
+    return self.comment_flag == '->'
+
   def has_comment(self, line):
     return line == self.first_line
 
   def __str__(self):
 
-    if self.is_transient:
+    if self.is_transient():
       tt = '!transient! '
+    elif self.is_dontsplit():
+      tt = '!dontsplit! '
+    elif self.is_ptr():
+      tt = '!ptr! '
     else:
       tt = ''
 
@@ -200,7 +215,7 @@ def comment_method(cursor, comments):
 
       if comment_line_start > 0:
 
-        comment = refactor_comment( comment )
+        comment = refactor_comment( comment, infilename=str(cursor.location.file) )
 
         if len(comment) > 0:
           logging.debug("Comment found for function %s" % Colt(comment_function).magenta())
@@ -240,7 +255,7 @@ def comment_datamember(cursor, comments):
   prev = None
   found = False
 
-  # Huge overkill
+  # Huge overkill: current line saved in "raw", previous in "prev"
   with open(str(cursor.location.file)) as fp:
     cur_line = 0
     for raw in fp:
@@ -252,14 +267,14 @@ def comment_datamember(cursor, comments):
 
   assert found, 'A line that should exist was not found in file' % cursor.location.file
 
-  recomm = r'(//(!)|///?)(\[(.*?)\])?<?\s*(.*?)\s*$'
-  recomm_doxyary = r'^\s*///\s*(.*?)\s*$'
+  recomm = r'(//(!|\|\||->)|///?)(\[([0-9,]+)\])?<?\s*(.*?)\s*$'
+  recomm_prevline = r'^\s*///\s*(.*?)\s*$'
 
   mcomm = re.search(recomm, raw)
   if mcomm:
     # If it does not match, we do not have a comment
     member_name = cursor.spelling;
-    is_transient = mcomm.group(2) is not None
+    comment_flag = mcomm.group(2)
     array_size = mcomm.group(4)
     text = mcomm.group(5)
 
@@ -268,16 +283,16 @@ def comment_datamember(cursor, comments):
     if array_size is not None and prev is not None:
       # ROOT arrays with comments already converted to Doxygen have the member description on the
       # previous line
-      mcomm_doxyary = re.search(recomm_doxyary, prev)
-      if mcomm_doxyary:
-        text = mcomm_doxyary.group(1)
+      mcomm_prevline = re.search(recomm_prevline, prev)
+      if mcomm_prevline:
+        text = mcomm_prevline.group(1)
         comments.append(RemoveComment(line_num-1, line_num-1))
 
     logging.debug('Comment found for member %s' % Colt(member_name).magenta())
 
     comments.append( MemberComment(
       text,
-      is_transient,
+      comment_flag,
       array_size,
       line_num,
       col_num,
@@ -300,7 +315,7 @@ def comment_classdesc(filename, comments):
 
   recomm = r'^\s*///?(\s*.*?)\s*/*\s*$'
 
-  reclass_doxy = r'(?i)^\s*\\class:?\s*(.*?)\s*$'
+  reclass_doxy = r'(?i)^\s*\\(class|file):?\s*([^.]*)'
   class_name_doxy = None
 
   reauthor = r'(?i)^\s*\\?authors?:?\s*(.*?)\s*(,?\s*([0-9./-]+))?\s*$'
@@ -315,6 +330,8 @@ def comment_classdesc(filename, comments):
 
   line_num = 0
 
+  is_macro = filename.endswith('.C')
+
   with open(filename, 'r') as fp:
 
     for raw in fp:
@@ -323,7 +340,6 @@ def comment_classdesc(filename, comments):
 
       if raw.strip() == '' and start_line > 0:
         # Skip empty lines
-        end_line = line_num - 1
         continue
 
       stripped = strip_html(raw)
@@ -345,11 +361,12 @@ def comment_classdesc(filename, comments):
 
           start_line = line_num
 
+        end_line = line_num
         append = True
 
         mclass_doxy = re.search(reclass_doxy, mcomm.group(1))
         if mclass_doxy:
-          class_name_doxy = mclass_doxy.group(1)
+          class_name_doxy = mclass_doxy.group(2)
           append = False
         else:
           mauthor = re.search(reauthor, mcomm.group(1))
@@ -357,7 +374,7 @@ def comment_classdesc(filename, comments):
             author = mauthor.group(1)
             if date is None:
               # Date specified in the standalone \date field has priority
-              date = mauthor.group(2)
+              date = mauthor.group(3)
             append = False
           else:
             mdate = re.search(redate, mcomm.group(1))
@@ -370,9 +387,6 @@ def comment_classdesc(filename, comments):
 
       else:
         if start_line > 0:
-          # End of our comment
-          if end_line == -1:
-            end_line = line_num - 1
           break
 
   if class_name_doxy is None:
@@ -387,8 +401,11 @@ def comment_classdesc(filename, comments):
 
   if start_line > 0:
 
-    # Prepend \class specifier (and an empty line)
-    comment_lines[:0] = [ '\\class ' + class_name_doxy ]
+    # Prepend \class or \file specifier (and an empty line)
+    if is_macro:
+      comment_lines[:0] = [ '\\file ' + class_name_doxy + '.C' ]
+    else:
+      comment_lines[:0] = [ '\\class ' + class_name_doxy ]
 
     # Append author and date if they exist
     comment_lines.append('')
@@ -399,7 +416,7 @@ def comment_classdesc(filename, comments):
     if date is not None:
       comment_lines.append( '\\date ' + date )
 
-    comment_lines = refactor_comment(comment_lines, do_strip_html=False)
+    comment_lines = refactor_comment(comment_lines, do_strip_html=False, infilename=filename)
     logging.debug('Comment found for class %s' % Colt(class_name_doxy).magenta())
     comments.append(Comment(
       comment_lines,
@@ -428,17 +445,20 @@ def traverse_ast(cursor, filename, comments, recursion=0):
   text = cursor.spelling or cursor.displayname
   kind = str(cursor.kind)[str(cursor.kind).index('.')+1:]
 
+  is_macro = filename.endswith('.C')
+
   indent = ''
   for i in range(0, recursion):
     indent = indent + '  '
 
-  if cursor.kind == clang.cindex.CursorKind.CXX_METHOD or cursor.kind == clang.cindex.CursorKind.CONSTRUCTOR or cursor.kind == clang.cindex.CursorKind.DESTRUCTOR:
+  if cursor.kind in [ clang.cindex.CursorKind.CXX_METHOD, clang.cindex.CursorKind.CONSTRUCTOR,
+    clang.cindex.CursorKind.DESTRUCTOR, clang.cindex.CursorKind.FUNCTION_DECL ]:
 
     # cursor ran into a C++ method
     logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, Colt(kind).magenta(), Colt(text).blue()) )
     comment_method(cursor, comments)
 
-  elif cursor.kind == clang.cindex.CursorKind.FIELD_DECL:
+  elif not is_macro and cursor.kind in [ clang.cindex.CursorKind.FIELD_DECL, clang.cindex.CursorKind.VAR_DECL ]:
 
     # cursor ran into a data member declaration
     logging.debug( "%5d %s%s(%s)" % (cursor.location.line, indent, Colt(kind).magenta(), Colt(text).blue()) )
@@ -459,23 +479,62 @@ def traverse_ast(cursor, filename, comments, recursion=0):
 #
 #  @param s Input string
 def strip_html(s):
-  rehtml = r'(?i)</?(P|H[0-9]|BR)/?>'
+  rehtml = r'(?i)</?(P|BR)/?>'
   return re.sub(rehtml, '', s)
 
 
 ## Remove garbage from comments and convert special tags from THtml to Doxygen.
 #
 #  @param comment An array containing the lines of the original comment
-def refactor_comment(comment, do_strip_html=True):
+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*$'
 
+  # Support for LaTeX blocks spanning on multiple lines
+  relatex = r'(?i)^((.*?)\s+)?(BEGIN|END)_LATEX([.,;:\s]+.*)?$'
+  in_latex = False
+  latex_block = False
+
+  # Support for LaTeX blocks on a single line
+  reinline_latex = r'(?i)(.*)BEGIN_LATEX\s+(.*?)\s+END_LATEX(.*)$'
+
+  # Match <pre> (to turn it into the ~~~ Markdown syntax)
+  reblock = r'(?i)^(\s*)</?PRE>\s*$'
+
+  # Macro blocks for pictures generation
+  in_macro = False
+  current_macro = []
+  remacro = r'(?i)^\s*(BEGIN|END)_MACRO(\((.*?)\))?\s*$'
+
   new_comment = []
   insert_blank = False
   wait_first_non_blank = True
   for line_comment in comment:
 
+    # Check if we are in a macro block
+    mmacro = re.search(remacro, line_comment)
+    if mmacro:
+      if in_macro:
+        in_macro = False
+
+        # Dump macro
+        outimg = write_macro(infilename, current_macro) + '.png'
+        current_macro = []
+
+        # Insert image
+        new_comment.append( '![Picture from ROOT macro](%s)' % (outimg) )
+
+        logging.debug( 'Found macro for generating image %s' % Colt(outimg).magenta() )
+
+      else:
+        in_macro = True
+
+      continue
+    elif in_macro:
+      current_macro.append( line_comment )
+      continue
+
     # Strip some HTML tags
     if do_strip_html:
       line_comment = strip_html(line_comment)
@@ -492,7 +551,90 @@ def refactor_comment(comment, do_strip_html=True):
           new_comment.append('')
         insert_blank = False
         wait_first_non_blank = False
-        new_comment.append( new_line_comment )
+
+        # Postprocessing: LaTeX formulas in ROOT format
+        # Marked by BEGIN_LATEX ... END_LATEX and they use # in place of \
+        # There can be several ROOT LaTeX forumlas per line
+        while True:
+          minline_latex = re.search( reinline_latex, new_line_comment )
+          if minline_latex:
+            new_line_comment = '%s\\f$%s\\f$%s' % \
+              ( minline_latex.group(1), minline_latex.group(2).replace('#', '\\'),
+                minline_latex.group(3) )
+          else:
+            break
+
+        # ROOT LaTeX: do we have a Begin/End_LaTeX block?
+        # Note: the presence of LaTeX "closures" does not exclude the possibility to have a begin
+        # block here left without a corresponding ending block
+        mlatex = re.search( relatex, new_line_comment )
+        if mlatex:
+
+          # before and after parts have been already stripped
+          l_before = mlatex.group(2)
+          l_after = mlatex.group(4)
+          is_begin = mlatex.group(3).upper() == 'BEGIN'  # if not, END
+
+          if l_before is None:
+            l_before = ''
+          if l_after is None:
+            l_after = ''
+
+          if is_begin:
+
+            # Begin of LaTeX part
+
+            in_latex = True
+            if l_before == '' and l_after == '':
+
+              # Opening tag alone: mark the beginning of a block: \f[ ... \f]
+              latex_block = True
+              new_comment.append( '\\f[' )
+
+            else:
+              # Mark the beginning of inline: \f$ ... \f$
+              latex_block = False
+              new_comment.append(
+                '%s \\f$%s' % ( l_before, l_after.replace('#', '\\') )
+              )
+
+          else:
+
+            # End of LaTeX part
+            in_latex = False
+
+            if latex_block:
+
+              # Closing a LaTeX block
+              if l_before != '':
+                new_comment.append( l_before.replace('#', '\\') )
+              new_comment.append( '\\f]' )
+              if l_after != '':
+                new_comment.append( l_after )
+
+            else:
+
+              # Closing a LaTeX inline
+              new_comment.append(
+                '%s\\f$%s' % ( l_before.replace('#', '\\'), l_after )
+              )
+
+          # Prevent appending lines (we have already done that)
+          new_line_comment = None
+
+        # If we are not in a LaTeX block, look for <pre> tags and transform them into Doxygen code
+        # blocks (using ~~~ ... ~~~). Only <pre> tags on a single line are supported
+        if new_line_comment is not None and not in_latex:
+
+          mblock = re.search( reblock, new_line_comment  )
+          if mblock:
+            new_comment.append( mblock.group(1)+'~~~' )
+            new_line_comment = None
+
+        if new_line_comment is not None:
+          if in_latex:
+            new_line_comment = new_line_comment.replace('#', '\\')
+          new_comment.append( new_line_comment )
 
     else:
       assert False, 'Comment regexp does not match'
@@ -500,6 +642,44 @@ def refactor_comment(comment, do_strip_html=True):
   return new_comment
 
 
+## Dumps an image-generating macro to the correct place. Returns a string with the image path,
+#  without the extension.
+#
+#  @param infilename  File name of the source file
+#  @param macro_lines Array of macro lines
+def write_macro(infilename, macro_lines):
+
+  # Calculate hash
+  digh = hashlib.sha1()
+  for l in macro_lines:
+    digh.update(l)
+    digh.update('\n')
+  short_digest = digh.hexdigest()[0:7]
+
+  outdir = '%s/imgdoc' % os.path.dirname(infilename)
+  outprefix = '%s/%s_%s' % (
+    outdir,
+    os.path.basename(infilename).replace('.', '_'),
+    short_digest
+  )
+  outmacro = '%s.C' % outprefix
+
+  # Make directory
+  if not os.path.isdir(outdir):
+    # do not catch: let everything die on error
+    logging.debug('Creating directory %s' % Colt(outdir).magenta())
+    os.mkdir(outdir)
+
+  # Create file (do not catch errors either)
+  with open(outmacro, 'w') as omfp:
+    logging.debug('Writing macro %s' % Colt(outmacro).magenta())
+    for l in macro_lines:
+      omfp.write(l)
+      omfp.write('\n')
+
+  return outprefix
+
+
 ## Rewrites all comments from the given file handler.
 #
 #  @param fhin     The file handler to read from
@@ -515,6 +695,17 @@ def rewrite_comments(fhin, fhout, comments):
 
   rindent = r'^(\s*)'
 
+
+  def dump_comment_block(cmt):
+   text_indent = ''
+   for i in range(0, cmt.indent):
+     text_indent = text_indent + ' '
+
+   for lc in cmt.lines:
+     fhout.write( "%s/// %s\n" % (text_indent, lc) );
+   fhout.write('\n')
+
+
   for line in fhin:
 
     line_num = line_num + 1
@@ -529,28 +720,50 @@ def rewrite_comments(fhin, fhout, comments):
     if comm:
 
       if isinstance(comm, MemberComment):
+
+        # end comment block
+        if in_comment:
+          dump_comment_block(prev_comm)
+          in_comment = False
+
         non_comment = line[ 0:comm.first_col-1 ]
 
-        if comm.array_size is not None:
+        if comm.array_size is not None or comm.is_dontsplit() or comm.is_ptr():
 
+          # This is a special case: comment will be split in two lines: one before the comment for
+          # Doxygen as "member description", and the other right after the comment on the same line
+          # to be parsed by ROOT's C++ parser
+
+          # Keep indent on the generated line of comment before member definition
           mindent = re.search(rindent, line)
-          if comm.is_transient:
-            tt = '!'
+
+          # Get correct comment flag, if any
+          if comm.comment_flag is not None:
+            cflag = comm.comment_flag
           else:
-            tt = ''
+            cflag = ''
 
-          # Special case: we need multiple lines not to confuse ROOT's C++ parser
-          fhout.write('%s/// %s\n%s//%s[%s]\n' % (
+          # Get correct array size, if any
+          if comm.array_size is not None:
+            asize = '[%s]' % comm.array_size
+          else:
+            asize = ''
+
+          # Write on two lines
+          fhout.write('%s/// %s\n%s//%s%s\n' % (
             mindent.group(1),
             comm.lines[0],
             non_comment,
-            tt,
-            comm.array_size
+            cflag,
+            asize
           ))
 
         else:
 
-          if comm.is_transient:
+          # Single-line comments with the "transient" flag can be kept on one line in a way that
+          # they are correctly interpreted by both ROOT and Doxygen
+
+          if comm.is_transient():
             tt = '!'
           else:
             tt = '/'
@@ -562,10 +775,13 @@ def rewrite_comments(fhin, fhout, comments):
           ))
 
       elif isinstance(comm, RemoveComment):
-        # Do nothing: just skip line
-        pass
+        # End comment block and skip this line
+        if in_comment:
+          dump_comment_block(prev_comm)
+          in_comment = False
 
       elif prev_comm is None:
+
         # Beginning of a new comment block of type Comment
         in_comment = True
 
@@ -579,16 +795,8 @@ def rewrite_comments(fhin, fhout, comments):
       if in_comment:
 
         # We have just exited a comment block of type Comment
+        dump_comment_block(prev_comm)
         in_comment = False
-
-        # Dump revamped comment, if applicable
-        text_indent = ''
-        for i in range(0,prev_comm.indent):
-          text_indent = text_indent + ' '
-
-        for lc in prev_comm.lines:
-          fhout.write( "%s/// %s\n" % (text_indent, lc) );
-        fhout.write('\n')
         skip_empty = True
 
       line_out = line.rstrip('\n')
@@ -615,8 +823,9 @@ def main(argv):
 
   # Parse command-line options
   output_on_stdout = False
+  include_flags = []
   try:
-    opts, args = getopt.getopt( argv, 'od', [ 'debug=', 'stdout' ] )
+    opts, args = getopt.getopt( argv, 'odI:', [ 'debug=', 'stdout' ] )
     for o, a in opts:
       if o == '--debug':
         log_level = getattr( logging, a.upper(), None )
@@ -625,8 +834,13 @@ def main(argv):
       elif o == '-d':
         log_level = logging.DEBUG
       elif o == '-o' or o == '--stdout':
-        logging.debug('Output on stdout instead of replacing original files')
         output_on_stdout = True
+      elif o == '-I':
+        if os.path.isdir(a):
+          include_flags.extend( [ '-I', a ] )
+        else:
+          logging.fatal('Include directory not found: %s' % Colt(a).magenta())
+          return 2
       else:
         assert False, 'Unhandled argument'
   except getopt.GetoptError as e:
@@ -658,7 +872,9 @@ def main(argv):
 
     logging.info('Input file: %s' % Colt(fn).magenta())
     index = clang.cindex.Index.create()
-    translation_unit = index.parse(fn, args=['-x', 'c++'])
+    clang_args = [ '-x', 'c++' ]
+    clang_args.extend( include_flags )
+    translation_unit = index.parse(fn, args=clang_args)
 
     comments = []
     traverse_ast( translation_unit.cursor, fn, comments )
@@ -668,10 +884,14 @@ def main(argv):
 
       if isinstance(c, MemberComment):
 
-        if c.is_transient:
-          transient_text = Colt('transient ').yellow()
+        if c.is_transient():
+          flag_text = Colt('transient ').yellow()
+        elif c.is_dontsplit():
+          flag_text = Colt('dontsplit ').yellow()
+        elif c.is_ptr():
+          flag_text = Colt('ptr ').yellow()
         else:
-          transient_text = ''
+          flag_text = ''
 
         if c.array_size is not None:
           array_text = Colt('arraysize=%s ' % c.array_size).yellow()
@@ -681,7 +901,7 @@ def main(argv):
         logging.debug(
           "%s %s%s{%s}" % ( \
             Colt("[%d,%d]" % (c.first_line, c.first_col)).green(),
-            transient_text,
+            flag_text,
             array_text,
             Colt(c.lines[0]).cyan()
         ))