split DS_gene from jinja2 config and start working on filters

This commit is contained in:
Lafrite 2014-09-02 11:20:09 +02:00
parent 3215838d55
commit dee5ba4812
3 changed files with 63 additions and 17 deletions

View File

@ -1,20 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import jinja2, random, os import os
import sys import sys
import optparse import optparse
from path import path from path import path
from pymath.random_expression import RdExpression
texenv = jinja2.Environment( from texenv import texenv
block_start_string = '\Block{', from pymath.random_expression import RdExpression
# Gros WTF!! Si on le met en maj ça ne marche pas alors que c'est en maj dans le template...
block_end_string = '}',
variable_start_string = '\Var{',
variable_end_string = '}',
loader = jinja2.FileSystemLoader(os.path.abspath('.'))
)
def main(options): def main(options):
#template = report_renderer.get_template(options.template) #template = report_renderer.get_template(options.template)
@ -42,10 +35,12 @@ def main(options):
dest = path(str(subj) + output) dest = path(str(subj) + output)
with open( dest, 'w') as f: with open( dest, 'w') as f:
f.write(template.render( RdExpression = RdExpression , infos = {"subj" : subj})) f.write(template.render( RdExpression = RdExpression , infos = {"subj" : subj}))
#os.system("pdflatex " + dest)
#if not options.dirty: if not options.no_compil:
# os.system("rm *.aux *.log") os.system("pdflatex " + dest)
if not options.dirty:
os.system("rm *.aux *.log")
cwd.cd() cwd.cd()
@ -53,10 +48,11 @@ if __name__ == '__main__':
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-t","--tempalte",action="store",type="string",dest="template", help="File with the template. The name should have the following form tpl_... .") parser.add_option("-t","--template",action="store",type="string",dest="template", help="File with the template. The name should have the following form tpl_... .")
parser.add_option("-o","--output",action="store",type="string",dest="output",help="Base name for output )") parser.add_option("-o","--output",action="store",type="string",dest="output",help="Base name for output )")
parser.add_option("-n","--number_subjects", action="store",type="int", dest="num_subj", default = 1, help="The number of subjects to make") parser.add_option("-N","--number_subjects", action="store",type="int", dest="num_subj", default = 1, help="The number of subjects to make")
parser.add_option("-d","--dirty", action="store_true", dest="dirty", help="Do not clean after compilation") parser.add_option("-d","--dirty", action="store_true", dest="dirty", help="Do not clean after compilation")
parser.add_option("-n","--no-compile", action="store_true", dest="no_compil", help="Do not compile source code")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()

View File

@ -1,5 +1,5 @@
\documentclass[a4paper,10pt]{/media/documents/Cours/Prof/Enseignements/tools/style/classDS} \documentclass[a4paper,10pt]{/media/documents/Cours/Prof/Enseignements/Archive/2013-2014/tools/style/classDS}
\usepackage{/media/documents/Cours/Prof/Enseignements/2013_2014} \usepackage{/media/documents/Cours/Prof/Enseignements/Archive/2013-2014/2013_2014}
% Title Page % Title Page
\titre{Calcul littéral et statistiques} \titre{Calcul littéral et statistiques}
@ -26,6 +26,10 @@ Le barème est donné à titre indicatif, il pourra être modifié.
A = \Var{ A } \\ A = \Var{ A } \\
B = \Var{ B } B = \Var{ B }
\end{equation*} \end{equation*}
Solutions:
\Var{A.simplify() | calculus}
\Var{B.simplify() | calculus(name = "B")}
\end{Exo} \end{Exo}

46
texenv.py Normal file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env python
# encoding: utf-8
import jinja2, os
# Definition of jinja syntax for latex
texenv = jinja2.Environment(
block_start_string = '\Block{',
# Gros WTF!! Si on le met en maj ça ne marche pas alors que c'est en maj dans le template...
block_end_string = '}',
variable_start_string = '\Var{',
variable_end_string = '}',
loader = jinja2.FileSystemLoader(os.path.abspath('.'))
)
# Filters
def do_calculus(steps, name = "A"):
"""Display properly the calculus
:param steps: list of steps
:returns: latex string ready to be endbeded
"""
ans = "\\begin{eqnarray*}\n"
ans += " \\\\ \n".join([name + " & = & " + s for s in steps])
ans += "\n\\end{eqnarray*}\n"
return ans
texenv.filters['calculus'] = do_calculus
if __name__ == '__main__':
from pymath.expression import Expression
exp = Expression("2/4 + 18")
print(do_calculus(exp.simplify()))
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del