Franck Pommereau

initial import

This diff is collapsed. Click to expand it.
\ProvidesPackage{pytex}
\RequirePackage{pgfopts}
\RequirePackage{sverb}
\RequirePackage{fancyvrb}
\makeatletter
%%
%%
\edef\py@port{12345}
\edef\py@base{\jobname.pytmp}
\pgfkeys{
/pytex/.cd,
base/.store in=\py@base,
port/.store in=\py@port,
}
\ProcessPgfOptions{/pytex}
\immediate\write18{pytex -b {\py@base} --listen {\py@port} {\jobname}}
\AtEndDocument{\immediate\write18{pytex -b {\py@base} --quit {\py@port}}}
\IfFileExists{\py@base/\jobname}
{\global\edef\py@base{\py@base/}}
{\global\edef\py@base{\py@base.}}
%%
%%
\newcounter{py@count}
\newwrite\py@file
\newwrite\py@jobs
\immediate\openout\py@jobs=\jobname.pytex
\def\py@basename{\py@base\thepy@count}
\def\py@next #1{%
\edef\py@last{\py@basename}%
\addtocounter{py@count}{1}%
\edef\py@basename{\py@base\thepy@count}%
\edef\py@src{\py@basename.py}%
\edef\py@@src{{\py@src}}%
\edef\py@out{{\py@basename.out}}%
\edef\py@tex{{\py@basename.tex}}%
\immediate\write\py@jobs{#1 \py@src}%
}
\def\py@open{\immediate\openout\py@file=\py@src}
\def\py@write{\immediate\write\py@file}
\def\py@close{\immediate\closeout\py@file}
\def\py@compile #1{\immediate\write18{pytex -b {\py@base} --call {\py@port} #1 {\py@src}}}
\def\py@input #1{\expandafter\expandafter\expandafter
\InputIfFileExists\csname py@#1\endcsname{}{}}
\def\pyv #1{%
\py@next{eval}%
\py@open
\py@write{#1}%
\py@close
\py@compile{eval}%
\py@input{out}%
}
\def\pyx #1{%
\py@next{exec}%
\py@open
\py@write{#1}%
\py@close
\py@compile{exec}%
\py@input{out}%
}
\def\pyc #1{\leavevmode
\py@next{code}%
\py@open
\py@write{#1}%
\py@close
\pyx{pygmentize("\py@last.py", inline=True)}%
}
\def\pyd #1#2{%
\py@next{eval}%
\py@open
\py@write{#2}%
\py@close
\py@compile{eval}%
\pyx{makedef("#1", "\py@last")}%
}
\newenvironment{pyeval}{%
\py@next{eval}%
\expandafter\verbwrite\py@@src
}{\endverbwrite
\py@compile{eval}%
\py@input{out}%
}
\newenvironment{pyexec}{%
\py@next{exec}%
\expandafter\verbwrite\py@@src
}{\endverbwrite
\py@compile{exec}%
\py@input{out}%
}
\newenvironment{pycode}{%
\py@next{exec}%
\expandafter\verbwrite\py@@src
}{\endverbwrite
\pyx{pygmentize("\py@last.py", inline=False)}
}
%%
%%
\makeatother
\pyx{reset()}
\ No newline at end of file
\documentclass{article}
\usepackage{pytex}
\parindent=0pt
\begin{document}
***\pyv{" ".join(s.capitalize() for s in "hello world!".split())}***
*\pyx{x=40}**\pyv{x+2}***
***\pyd{hello}{x+2}***\hello***
\begin{pyexec}
def fact (n) :
f = 1
for i in range(1, n+1) :
f *= i
return f
\end{pyexec}
\begin{pyeval}
[fact(n) for n in range(10)
if n % 2 == 0]
\end{pyeval}
**********************
Code ``\pyc{lambda x : x + 1}'' is inlined.
Code block:
%
\begin{pycode}
def fact (n) :
f = 1
for i in range(1, n+1) :
f *= i
return f
\end{pycode}
\IfFileExists{pytex.tmp}{found}{NO}.
\end{document}