Changeset 5

Show
Ignore:
Timestamp:
06/11/07 15:40:00 (3 years ago)
Author:
matt_dorn@yahoo.com
Message:

refactored functionality into classes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • reSTAcademicUtils/rst_plus_command.py

    r4 r5  
    66import getopt 
    77from optparse import OptionParser 
    8 from reSTAcademicUtils.utils import replace_refs 
    9 from reSTAcademicUtils import templates 
    10 from docutils.core import publish_string 
    11 import re 
    12 from xml.dom import minidom 
     8from reSTAcademicUtils.core import Publisher 
    139 
    1410def main(argv): 
    15     template_path = os.path.split(templates.__file__)[0] 
    16     template_fullpath = os.path.join(template_path, 'header.txt') 
    17     header_file = open(template_fullpath) 
    18     header = unicode(header_file.read()) 
    19     header_file.close() 
    20      
     11    
    2112    usage = "usage: %prog [options] filename" 
    2213    version = "%prog 0.1" 
    2314    parser = OptionParser(usage=usage, version=version) 
    24     parser.add_option("-l", "--line-spacing", dest="linespacing",  
     15    parser.add_option("-l", "--line-spacing", dest="linespacing", default=1, 
    2516                      help="Specify linespacing (1, 1.5, 2)") 
    2617    parser.add_option("-e", "--endnotes", action="store_true", dest="endnotes",  
    27                       help="Use endnotes instead of footnotes") 
     18                      default=False, help="Use endnotes instead of footnotes") 
    2819    parser.add_option("-b", "--bibtex-style", dest="bibstyle",  
    2920                      help="Specify BibTex style") 
    3021    parser.add_option("-d", "--bibtex-db", dest="bibdb",  
    31                       help="Specify BibTex database") 
     22                      help="Specify path to BibTex database") 
    3223    parser.add_option("-c", "--nocite", action="store_true", dest="nocite", 
    33                       help="Include all non-cited items in bibliography") 
    34      
    35  
    36     #parser.add_option("-f", "--file", dest="filename", 
    37     #                  help="write report to FILE", metavar="FILE") 
     24                      default=False, help="Include all non-cited items in bibliography") 
    3825    #parser.add_option("-q", "--quiet", 
    3926    #                  action="store_false", dest="verbose", default=True, 
     
    4633    orig_file = args[0] 
    4734     
    48     #print options.bibstyle 
    49     #print options.bibdb 
     35    input = get_file_contents(orig_file) 
     36    bibtex_input = get_file_contents(options.bibdb) 
     37     
     38    pub = Publisher(bibtex_input, options.bibstyle, options.linespacing,  
     39                    options.endnotes, options.nocite, True) 
     40    pub.publish(input) 
     41 
     42def get_file_contents(filename): 
    5043    # get file, assign to input 
    51     # orig_file='matt2_raw.rst' 
    5244    try: 
    53         file_obj=file(orig_file, 'r') 
     45        file_obj=file(filename, 'r') 
    5446    except IOError: 
    55         print "*** ERROR: Can not open", orig_fil
     47        print "*** ERROR: Can not open", filenam
    5648        sys.exit(1) 
    5749    input=file_obj.read() 
    5850    file_obj.close() 
    59  
    60     # format the references properly based on selected style 
    61     result = replace_refs(input, options.bibstyle) 
    62     
    63     # GET HEADER/METADATA INTO LATEX DOCUMENT PROPERLY 
     51    return input 
    6452     
    65     # TODO: Instead of parsing XML, you can probably get more direct access 
    66     # to the document nodes (e.g., in a dictionary)  
    67     # but the documentation on this is incomplete as of this writing:  
    68     # http://docutils.sourceforge.net/docs/dev/hacking.html#the-node-interface 
    69     xmldoc = publish_string(result, writer_name='xml', 
    70                         settings_overrides = {'input_encoding': 'utf-8',  
    71                                               'output_encoding': 'utf-8'}) 
    72     xmldoc = minidom.parseString(xmldoc) 
    73     x=xmldoc.childNodes[2].childNodes 
    74     # the first 3 child Nodes seem to be the ones that contain doc metadata we need, 
    75     # too bad there's no uniformity to how the information (i.e. field data) is stored 
    76     title = x[0].childNodes[0].nodeValue 
    77     # stuff from the field list 
    78     author = x[1].getElementsByTagName('author')[0].childNodes[0].nodeValue 
    79     date = x[1].getElementsByTagName('date')[0].childNodes[0].nodeValue 
    80     # theoretically from field list too, but docutils treats it separately 
    81     abstract = x[2].childNodes[1].childNodes[0].nodeValue 
    82     # TODO: Keywords support -- a field list item not supported by docutils 
    83      
    84     # Now remove the abstract from the reST doc, so that it's not re-processed 
    85     # and placed wrongly in the LaTeX doc (NOTE for later: leave it alone for HTML  
    86     # processing) 
    87     regexp = re.compile(":Abstract:.*\n") 
    88     result = re.sub(regexp, "", result) 
    89      
    90     # write new file 
    91     orig_filepath_noext = orig_file.split('.')[0] 
    92     new_file = orig_filepath_noext + "_preprocessed.rst" 
    93     processed_rst = _write_file(result, new_file) 
    94     lchar_orig_path = orig_filepath_noext.rfind('/') 
    95     # python doesn't have a function to get the directory of a given file? 
    96     if lchar_orig_path > -1: 
    97         orig_path = orig_filepath_noext[:lchar_orig_path] 
    98         orig_filename_noext = orig_filepath_noext[lchar_orig_path+1:] 
    99     else: 
    100         orig_path = "./" 
    101         orig_filename_noext = orig_filepath_noext 
    102  
    103     # do the latex conversion and write the latex file 
    104     latex = publish_string(result, writer_name='latex', 
    105                            settings_overrides = {'input_encoding': 'utf-8',  
    106                                                  'output_encoding': 'utf-8'}) 
    107  
    108     # UGLY HACK: replace the uselessly complex preamble that docutils gives 
    109     #regexp = re.compile("\\\documentclass.*\\raggedbottom", re.DOTALL) 
    110     #latex = re.sub(regexp, "", latex) 
    111     #begin=latex.find("\\begin{document}") 
    112     begin = latex.find("\setlength{\locallinewidth}{\linewidth}") 
    113     begin = begin + 39 
    114     # print "%###################################" 
    115     latex=latex[begin:] 
    116     #latex=latex.replace("\setlength{\locallinewidth}{\linewidth}", "%\setlength{\locallinewidth}{\linewidth}") 
    117      
    118     # setting the linespacing by finding docutils-placed section breaks 
    119     # seems to be the best option, in order not to do it prematurely 
    120     regexp = re.compile("\%______.*\n") 
    121     latex = re.sub(regexp, "%______SECTION BREAK_______\n\setstretch{" \ 
    122                    + options.linespacing + "}\n", latex) 
    123  
    124     # END NOTES? 
    125     # abfnNrtuUvox -- need double slashes 
    126     if options.endnotes: 
    127         endnotes = "\\usepackage{endnotes}\n" 
    128         # Unnecessary?: \let\\footnote=\endnote\n 
    129         header = header.replace('$HEADER_OPTIONS', endnotes) 
    130         endnotes_footer = "\\theendnotes\n\\newpage\n" 
    131     else: 
    132         header = header.replace('$HEADER_OPTIONS', "") 
    133         endnotes_footer="" 
    134     if options.nocite: 
    135         nocite = "\\nocite{*}\n" 
    136     else: 
    137         nocite = "" 
    138      
    139     # do footer, with bibliography 
    140     latex = latex.replace("\\end{document}", "\\newpage\n\\setstretch{1}\n" \ 
    141                   "%s%s\\bibliographystyle{%s}\n\\bibliography{%s}\n" \ 
    142                   "\\end{document}\n" % (endnotes_footer, nocite, options.bibstyle, options.bibdb)) 
    143     # NOTE: docutils snapshot has use-bibtex option, not used here.     
    144      
    145     # get rid of space between end of sentence and footnote 
    146     latex = latex.replace(" \\footnote", "\\footnote") 
    147  
    148     header = header.replace('$TITLE', unicode(title)) 
    149     header = header.replace('$AUTHOR', unicode(author)) 
    150     header = header.replace('$DATE', unicode(date)) 
    151     header = header.replace('$ABSTRACT', unicode(abstract)) 
    152     # TODO: REMOVE the docutils-generated abstract :( 
    153      
    154     latex = header.encode('utf8') + latex 
    155     new_file = orig_filepath_noext + ".tex" 
    156     processed_latex = _write_file(latex, new_file) 
    157      
    158     os.chdir(orig_path) 
    159     # now do the latex / bibtex stuff 
    160     os.system('latex ' + orig_filename_noext) #  + ' > /tmp/silent') 
    161     os.system('bibtex ' + orig_filename_noext) # + ' > /tmp/silent') 
    162     os.system('latex ' + orig_filename_noext) # + ' > /tmp/silent') 
    163     os.system('latex ' + orig_filename_noext) # + ' > /tmp/silent') 
    164     # and convert to PDF 
    165     os.system('dvipdfm ' + orig_filename_noext + ".dvi") # + ' > /tmp/silent') 
    166      
    167      
    168 def _write_file(str, filename): 
    169     # write new file 
    170     try: 
    171         file_obj=file(filename, 'w') 
    172     except IOError: 
    173         print "*** ERROR: Can not write to file " + str(os.getcwd()) + "/" + origFile 
    174         sys.exit(1) 
    175     file_obj.write(str) 
    176     file_obj.close() 
    177     return True 
    178  
    179 def _recurse_nodes(nodes, level): 
    180     # currently not used, but useful for visualizing the DOM 
    181     # after processing a reST doc, but before publishing it 
    182     # NOTE: could be useful to turn items into a dictionary 
    183     for i in nodes: 
    184         print str(level) + " " + str(i.nodeName) + " = " + unicode(i.nodeValue) 
    185         #if i.nodeType == 3: # TEXT_NODE -- for viewing the values b/w opening and closing tags 
    186         #print str(level) + " " + i.parentNode.nodeName + " = " + unicode(i.nodeValue) 
    187         if i.hasChildNodes(): 
    188             _recurse_nodes(i.childNodes, level + 1) 
    189  
    190  
    19153if __name__ == "__main__": 
    19254    main(sys.argv[1:]) 
  • reSTAcademicUtils/templates/header.txt

    r4 r5  
    99\geometry{verbose,letterpaper,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in} 
    1010\usepackage{setspace} 
    11  
     11\usepackage{tabularx} 
     12\newlength{\docinfowidth} 
     13\setlength{\docinfowidth}{0.9\textwidth} 
     14\newlength{\locallinewidth} 
    1215\makeatletter 
    1316\usepackage{babel} 
     
    1619$HEADER_OPTIONS 
    1720 
    18 \usepackage{tabularx} 
    19 \usepackage{ifthen} 
    2021 
    21 \title{$TITLE} 
    22 \author{$AUTHOR} 
    23 \date{$DATE} 
    24 \hypersetup{ 
    25 pdftitle={$TITLE}, 
    26 pdfauthor={$AUTHOR} 
    27 } 
    28 %\raggedbottom 
    29 \begin{document} 
    30 \maketitle 
    31  
    32 \begin{abstract} 
    33 $ABSTRACT 
    34 \end{abstract}