project/scipycon/proceedings/booklet/mk_booklet.py
changeset 94 87e77aa18610
equal deleted inserted replaced
93:e86755df35da 94:87e77aa18610
       
     1 # encoding: utf-8
       
     2 
       
     3 import os
       
     4 import sys
       
     5 import codecs
       
     6 import re
       
     7 
       
     8 try:
       
     9     from sanum import model
       
    10 except:
       
    11     root_dir = os.path.abspath(os.getcwd() + '/../../')
       
    12     os.chdir(root_dir)
       
    13     sys.path.append(root_dir)
       
    14     from sanum import model
       
    15 
       
    16 import sanum
       
    17 
       
    18 import turbogears
       
    19 turbogears.update_config(configfile="dev.cfg",
       
    20                          modulename="sanum.config")
       
    21 
       
    22 
       
    23 from mk_scipy_paper import tex2pdf, current_dir , copy_files, preamble, \
       
    24         render_abstract, addfile, sourcedir, outdir, outfilename
       
    25 
       
    26 
       
    27 def hack_include_graphics(latex_text, attach_dir):
       
    28     """ Replaces all the \includegraphics call with call that impose the
       
    29         width to be 0.9\linewidth.
       
    30     """
       
    31     latex_text = re.sub(r'\\includegraphics(\[.*\])?\{',
       
    32                         r'\includegraphics\1{' + attach_dir,
       
    33                         latex_text)
       
    34     return latex_text
       
    35 
       
    36 
       
    37 class MyStringIO(object):
       
    38     """ An unicode-friendly stringIO-like object.
       
    39     """
       
    40 
       
    41     def __init__(self):
       
    42         self.lines = []
       
    43 
       
    44     def write(self, line):
       
    45         self.lines.append(line)
       
    46 
       
    47     def getvalue(self):
       
    48         return u''.join(self.lines)
       
    49 
       
    50 def mk_booklet_tex(outfilename):
       
    51     """ Generate the entire booklet latex file.
       
    52     """
       
    53     outfile = codecs.open(outfilename, 'w', 'utf-8')
       
    54     preamble(outfile)
       
    55     copy_files()
       
    56     #addfile(outfile, sourcedir + os.sep + 'title.tex')
       
    57     addfile(outfile, sourcedir + os.sep + 'introduction.tex')
       
    58 
       
    59     #outfile.write(ur'\setcounter{page}{0}' + '\n')
       
    60 
       
    61     #from sanum.controllers import Root as Controller
       
    62     abstracts = model.Abstract.select()
       
    63     for abstract in abstracts:
       
    64         if not abstract.approved:
       
    65             continue
       
    66         print abstract.title
       
    67         # Hack: I don't use a stringIO, because it is not unicode-safe.
       
    68         tmpout = MyStringIO()
       
    69         # Hack: I don't wont to be bound to the controller, to be
       
    70         # abstractle to run without cherrypy.
       
    71         #attach_dir = Controller._paper_attach_dir(abstract.id)
       
    72         attach_dir = os.path.abspath(os.sep.join(
       
    73                     (os.path.dirname(sanum.__file__), 'static', 
       
    74                     'papers', '%i' % abstract.id))) + os.sep
       
    75         render_abstract(tmpout, abstract)
       
    76         outstring = hack_include_graphics(tmpout.getvalue(),
       
    77                             attach_dir)
       
    78         outfile.write(outstring)
       
    79         #outfile.write(ur'\fillbreak' + '\n')
       
    80 
       
    81     outfile.write(ur'\end{document}' + '\n')
       
    82 
       
    83 
       
    84 
       
    85 
       
    86 def mk_booklet(outfilename=outfilename):
       
    87     """ Generate the entire booklet pdf file.
       
    88     """
       
    89     name, ext = os.path.splitext(outfilename)
       
    90     mk_booklet_tex(name + '.tex')
       
    91     return tex2pdf(name, remove_tex=False, timeout=60)
       
    92 
       
    93 if __name__ == '__main__':
       
    94     mk_booklet(outfilename)