<s> import os <EOL> import sys <EOL> import shlex , subprocess <EOL> import tempita <EOL> from conf import bib_dir , build_dir , template_dir , html_dir <EOL> from options import get_config <EOL> class TeXTemplate ( tempita . Template ) : <EOL> def _repr ( self , value , pos ) : <EOL> if isinstance ( value , unicode ) : <EOL> value = value . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) <EOL> else : <EOL> value = str ( value ) <EOL> return value . encode ( '<STR_LIT:utf-8>' ) <EOL> def _from_template ( tmpl_basename , config , use_html = True ) : <EOL> tmpl = os . path . join ( template_dir , tmpl_basename + '<STR_LIT>' ) <EOL> if use_html : <EOL> template = tempita . HTMLTemplate ( open ( tmpl , '<STR_LIT:r>' ) . read ( ) ) <EOL> else : <EOL> template = TeXTemplate ( open ( tmpl , '<STR_LIT:r>' ) . read ( ) ) <EOL> return template . substitute ( config ) <EOL> def from_template ( tmpl_basename , config , dest_fn ) : <EOL> extension = os . path . splitext ( dest_fn ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> use_html = False if '<STR_LIT>' in extension else True <EOL> outfile = _from_template ( tmpl_basename , config , use_html = use_html ) <EOL> outname = os . path . join ( build_dir , extension , dest_fn ) <EOL> with open ( outname , mode = '<STR_LIT:w>' ) as f : <EOL> f . write ( outfile ) <EOL> def bib_from_tmpl ( bib_type , config , target ) : <EOL> tmpl_basename = bib_type + '<STR_LIT>' <EOL> dest_path = os . path . join ( bib_dir , target + '<STR_LIT>' ) <EOL> from_template ( tmpl_basename , config , dest_path ) <EOL> command_line = '<STR_LIT>' + dest_path <EOL> run = subprocess . Popen ( command_line , shell = True , stdout = subprocess . PIPE ) <EOL> out , err = run . communicate ( ) <EOL> def get_html_header ( config ) : <EOL> return _from_template ( '<STR_LIT>' , config ) <EOL> def get_html_content ( tmpl , config ) : <EOL> return _from_template ( tmpl , config ) <EOL> def html_from_tmpl ( src , config , target ) : <EOL> header = get_html_header ( config ) <EOL> content = _from_template ( src , config ) <EOL> outfile = header + content <EOL> dest_fn = os . path . join ( html_dir , target + '<STR_LIT>' ) <EOL> extension = os . path . splitext ( dest_fn ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] <EOL> outname = os . path . join ( build_dir , extension , dest_fn ) <EOL> with open ( outname , mode = '<STR_LIT:w>' ) as f : <EOL> f . write ( outfile ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if not len ( sys . argv ) == <NUM_LIT:2> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> dest_fn = sys . argv [ <NUM_LIT:1> ] <EOL> template_fn = os . path . join ( template_dir , dest_fn + '<STR_LIT>' ) <EOL> if not os . path . exists ( template_fn ) : <EOL> print "<STR_LIT>" <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> config = get_config ( ) <EOL> from_template ( dest_fn , config , dest_fn ) </s>
