00001 __copyright__ = """
00002 Copyright 2008 Sean Ross-Ross
00003 """
00004 __license__ = """
00005 This file is part of SLIMpy .
00006
00007 SLIMpy is free software: you can redistribute it and/or modify
00008 it under the terms of the GNU Lesser General Public License as published by
00009 the Free Software Foundation, either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 SLIMpy is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU Lesser General Public License for more details.
00016
00017 You should have received a copy of the GNU Lesser General Public License
00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00019 """
00020
00021 from SCons.Script import Mkdir,WhereIs,Action
00022 from glob import glob
00023 from os.path import splitext,split,join,abspath
00024 from string import Template
00025 import os
00026 import re
00027
00028 def myAction(cmd_str):
00029 def act_on(function):
00030 return Action(function,cmd_str)
00031 return act_on
00032
00033 term_template = Template("""
00034 \\term{ ${term} }
00035
00036 ${abstract}
00037
00038 \\textbf{Author:} ${author}
00039
00040 \\citetitle[${link}]{${term}}
00041 """)
00042
00043 re_title = re.compile(r'\\title\{(.*?)\}')
00044 re_author = re.compile(r'\\author\{(.*?)\}')
00045 default_name = lambda all,def_: all and all[0] or def_
00046 def makedef( file_name, lines , link=None):
00047
00048 head, tail = split( file_name )
00049 name,ext = splitext( tail )
00050
00051
00052 line_less = lines.replace('\n',' ')
00053
00054 all_titles = re_title.findall( line_less )
00055 all_authors = re_author.findall( line_less )
00056
00057 term = default_name(all_titles, name)
00058
00059 auth = default_name(all_authors, "No Author" )
00060
00061
00062 if link is None:
00063 link = '../%(name)s/index.html' %vars()
00064
00065 if lines.find("\\begin{abstract}") != -1:
00066 abstract = lines.split("\\begin{abstract}")[-1].split("\\end{abstract}")[0]
00067 else:
00068 abstract = '\\emph{No abstract}'
00069
00070
00071
00072 return term_template.substitute( term=term,
00073 abstract=abstract,
00074 link=link,
00075 author=auth
00076 )
00077
00078
00079 def maketut( terms ):
00080
00081 if not terms:
00082 return "% no terms"
00083 begin = "\\begin{definitions}"
00084 end = "\\end{definitions}"
00085
00086 terms = "\n".join( [ term.get_contents() for term in terms] )
00087
00088 return "%(begin)s\n%(terms)s\n%(end)s" %vars()
00089
00090
00091 def how_to_gen( source, target, env, for_signature ):
00092
00093 env.Alias( 'mkhowto', target )
00094 html_dir = env.get('html_dir',None)
00095 if html_dir is None:
00096 name = splitext ( split (str(source[0]))[-1] )[0]
00097 html_dir = join("#", 'html', name)
00098
00099 dir = env.Dir( html_dir )
00100
00101 MKHOWTO = WhereIs('mkhowto') or os.environ.get('MKHOWTO')
00102 MKHOWTO = env.get('MKHOWTO',MKHOWTO)
00103 env['MKHOWTO'] = MKHOWTO
00104
00105 mk_act = ("${TEXINPUTS and 'TEXINPUTS='}${':'.join(TEXINPUTS)} "
00106 "${MKHOWTO} --quiet --html --dir=%(dir)s "
00107 "${ADDRESS and '--address='}${ADDRESS} "
00108 "${UP_LINK and '--up-link='}${UP_LINK} "
00109 "${UP_TITLE and '--up-title='}${'\"'+str(UP_TITLE)+'\"'} "
00110 "${SOURCE}" % vars() )
00111
00112 mk_act_str = "mkhowto --html --dir=${TARGET.dir} "
00113 mkhowto_action = Action(mk_act,mk_act_str)
00114 Mkdir( str(dir) )
00115 return [mkhowto_action]
00116
00117
00118 def howto_emitter( target, source, env):
00119
00120 html_dir = env.get('dir',None)
00121 if html_dir is None:
00122 name = splitext ( split (str(source[0]))[-1] )[0]
00123 html_dir = join("#", 'html', name)
00124
00125 index = join( html_dir , 'index.html' )
00126 target.pop( )
00127 target.append( env.File(index) )
00128
00129 dir = str( env.Dir(html_dir) )
00130 aux = map( abspath, glob( join( dir, "*" ) ) )
00131
00132 for tgt in target:
00133 abs_tgt = abspath(str(tgt))
00134 if aux.count( abs_tgt ):
00135 aux.remove( abs_tgt )
00136
00137 env.SideEffect( aux, target )
00138 env.Clean( target, aux )
00139
00140 env.Alias( 'mkhowto', target )
00141
00142 return target, source
00143
00144
00145 GLOBAL_TERMS_DEMOS = []
00146 GLOBAL_TERMS_TUTOR = []
00147
00148 add_to_demos = GLOBAL_TERMS_DEMOS.append
00149 add_to_tutorials = GLOBAL_TERMS_TUTOR.append
00150
00151 @myAction("Term Builder( '${SOURCE}' )")
00152 def term_builder(target,source,env):
00153 link = env.get('link',None)
00154 text = makedef( str(source[0]), source[0].get_contents() ,link=link )
00155 fd = open( str(target[0]) , 'w' )
00156 fd.write( text )
00157
00158
00159 def term_emitter( target, source, env ):
00160
00161
00162
00163 env.Alias( 'term' , target )
00164 return target, source
00165
00166 @myAction("Make Index( ['${TARGET}'] )")
00167 def tutorial_index( target, source, env ):
00168
00169 terms = [ src for src in source if src.suffix == '.term']
00170 text = maketut( terms )
00171
00172 open(str(target[0]) , 'w' ).write( text )
00173
00174
00175 def index_emitter( target, source, env ):
00176 global GLOBAL_TERMS_TUTOR
00177 for term in GLOBAL_TERMS_TUTOR:
00178 source.append( term )
00179
00180 return target, source
00181
00182 def index_emitter_demo( target, source, env ):
00183 global GLOBAL_TERMS_DEMOS
00184
00185 for term in GLOBAL_TERMS_DEMOS:
00186 source.append( term )
00187 return target, source