[Vim-latex-cvs] vimfiles/ftplugin/latex-suite outline.py,1.2,1.3
Brought to you by:
srinathava,
tmaas
From: <sri...@us...> - 2006-01-02 07:20:47
|
Update of /cvsroot/vim-latex/vimfiles/ftplugin/latex-suite In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24716 Modified Files: outline.py Log Message: Change: A slightly improved algorithm for producing the outline which ensures that sections which do not contain matches are never printed. Index: outline.py =================================================================== RCS file: /cvsroot/vim-latex/vimfiles/ftplugin/latex-suite/outline.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** outline.py 1 Jan 2006 07:54:25 -0000 1.2 --- outline.py 2 Jan 2006 07:20:37 -0000 1.3 *************** *** 22,26 **** fname = argin.group(3) ! contents = open(fname).read() # TODO what are all the ways in which a tex file can include another? --- 22,28 ---- fname = argin.group(3) ! # This longish thing is to make sure that all files are converted into ! # \n seperated lines. ! contents = '\n'.join(open(fname).read().splitlines()) # TODO what are all the ways in which a tex file can include another? *************** *** 31,80 **** # }}} ! # createOutline {{{ ! def createOutline(contents, prefix): ! """ ! Prints a formatted outline of the latex document on the standard ! output. The output is of the form: ! ! 1. Introduction ! 1.1 Introduction section ! > eqn:firsteqn ! : 2 == 2 ! > fig:figure1 ! : Caption for the figure ! 1.2 Second section name of introduction ! ! If ``prefix`` is given, then only labels which start with it are ! listed. ! """ ! # This is the hierarchical list of the sectioning of a latex document. ! outline_cmds = ['chapter', 'section', 'subsection', 'subsubsection'] ! outline_nums = {} ! for cmd in outline_cmds: ! outline_nums[cmd] = 0 ! pres_depth = 0 ! fname = '' prev_txt = '' inside_env = 0 prev_env = '' ! outstr = stringio.StringIO() ! ! for line in contents.splitlines(): ! # remember which file we are currently "in" ! if re.match('%==== FILENAME: ', line): ! fname = re.search('%==== FILENAME: (.*)', line).group(1) ! # throw away everything after the % ! line = re.sub('%.*', '', line) if not line: continue # throw away leading white-space ! line = line.lstrip() # we found a label! ! m = re.search(r'\\label{(%s.*?)}' % prefix, line) if m: # add the current line (except the \label command) to the text --- 33,79 ---- # }}} ! # stripComments {{{ ! def stripComments(contents): ! # remove all comments except those of the form ! # %%==== FILENAME: <filename.tex> ! uncomm = [re.sub('%(?!==== FILENAME: ).*', '', line) for line in contents.splitlines()] ! # also remove all only-whitespace lines. ! nonempty = [line for line in uncomm if line.strip()] ! return nonempty ! # }}} ! # addFileNameAndNumber {{{ ! def addFileNameAndNumber(lines): ! filename = '' ! retval = '' ! for line in lines: ! if re.match('%==== FILENAME: ', line): ! filename = line.split('%==== FILENAME: ')[1] ! else: ! retval += '<%s>%s\n' % (filename, line) ! return retval ! # }}} ! # getSectionLabels_Root {{{ ! def getSectionLabels_Root(lineinfo, section_prefix, label_prefix): prev_txt = '' inside_env = 0 prev_env = '' ! outstr = stringio.StringIO('') ! pres_depth = len(section_prefix) ! #print '+getSectionLabels_Root: lineinfo = [%s]' % lineinfo ! for line in lineinfo.splitlines(): if not line: continue # throw away leading white-space ! m = re.search('<(.*?)>(.*)', line) ! ! fname = m.group(1) ! line = m.group(2).lstrip() # we found a label! ! m = re.search(r'\\label{(%s.*?)}' % label_prefix, line) if m: # add the current line (except the \label command) to the text *************** *** 95,131 **** # # Use the current "section depth" for the leading indentation. ! print >>outstr, '>%s%s\t\t<%s>' % (' '*(2*pres_depth+6), m.group(1), fname) ! print >>outstr, ':%s%s' % (' '*(2*pres_depth+8), prev_txt) prev_txt = '' - # We found a TOC command, i.e one of \chapter, \section etc. - # We need to increase the numbering for the level which we just - # encoutered and reset all the nested levels to zero. Thus if we - # encounter a \section, the section number increments and the - # subsection numbers etc reset to 0. - ntot = len(outline_cmds) - sec_txt = '' - for i in range(ntot): - m = re.search(r'\\' + outline_cmds[i] + '{(.*?)}', line) - if m: - outline_nums[outline_cmds[i]] += 1 - for j in range(i+1, ntot): - outline_nums[outline_cmds[j]] = 0 - - # At the same time we form a string like - # - # 3.2.4 The section name - # - sec_txt = '' - for k in range(0, i+1): - sec_txt += '%d.' % outline_nums[outline_cmds[k]] - - sec_txt += (' ' + m.group(1)) - pres_depth = i - - print >>outstr, '%s%s\t<<<%d' % (' '*2*pres_depth, sec_txt, pres_depth+1) - break - # If we just encoutered the start or end of an environment or a # label, then do not remember this line. --- 94,102 ---- # # Use the current "section depth" for the leading indentation. ! print >>outstr, '>%s%s\t\t<%s>' % (' '*(2*pres_depth+2), m.group(1), fname) ! print >>outstr, ':%s%s' % (' '*(2*pres_depth+4), prev_txt) prev_txt = '' # If we just encoutered the start or end of an environment or a # label, then do not remember this line. *************** *** 155,163 **** return outstr.getvalue() # }}} ! # main(fname) {{{ ! def main(fname, prefix): [head, tail] = os.path.split(fname) if head: --- 126,169 ---- return outstr.getvalue() + + # }}} + # getSectionLabels {{{ + def getSectionLabels(lineinfo, + sectypes=['chapter', 'section', 'subsection', 'subsubsection'], + section_prefix='', label_prefix=''): + if not sectypes: + return getSectionLabels_Root(lineinfo, section_prefix, label_prefix) + ##print 'sectypes[0] = %s, section_prefix = [%s], lineinfo = [%s]' % ( + ## sectypes[0], section_prefix, lineinfo) + + sections = re.split(r'(<.*?>\\%s{.*})' % sectypes[0], lineinfo) + + # there will 1+2n sections, the first containing the "preamble" and the + # others containing the child sections as paris of [section_name, + # section_text] + + rettext = getSectionLabels(sections[0], sectypes[1:], section_prefix, label_prefix) + + for i in range(1,len(sections),2): + sec_num = (i+1)/2 + section_name = re.search(r'\\%s{(.*?)}' % sectypes[0], sections[i]).group(1) + section_label_text = getSectionLabels(sections[i] + sections[i+1], sectypes[1:], + section_prefix+('%d.' % sec_num), label_prefix) + + if section_label_text: + sec_heading = 2*' '*len(section_prefix) + section_prefix + sec_heading += '%d. %s' % (sec_num, section_name) + sec_heading += '<<<%d\n' % (len(section_prefix)/2+1) + + rettext += sec_heading + section_label_text + + return rettext + # }}} ! ! # main {{{ ! def main(fname, label_prefix): [head, tail] = os.path.split(fname) if head: *************** *** 166,173 **** [root, ext] = os.path.splitext(tail) contents = getFileContents(root, ext) ! outline = createOutline(contents, prefix) ! return outline ! # }}} --- 172,179 ---- [root, ext] = os.path.splitext(tail) contents = getFileContents(root, ext) ! nonempty = stripComments(contents) ! lineinfo = addFileNameAndNumber(nonempty) ! return getSectionLabels(lineinfo, label_prefix=label_prefix) # }}} *************** *** 180,182 **** --- 186,189 ---- print main(sys.argv[1], prefix) + # vim: fdm=marker |