Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
Bertrand Benjamin | fdb43cc18f | |
Bertrand Benjamin | 7bbaf1132d | |
Bertrand Benjamin | 90067b4068 | |
Bertrand Benjamin | 526d920bc9 | |
Bertrand Benjamin | 51d900844d | |
Bertrand Benjamin | 3030798f71 |
|
@ -96,3 +96,6 @@ ENV/
|
||||||
|
|
||||||
# mkdocs documentation
|
# mkdocs documentation
|
||||||
/site
|
/site
|
||||||
|
|
||||||
|
# pytest
|
||||||
|
.pytest_cache/
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
# Pytex
|
||||||
|
|
||||||
|
Pytex is a simple package which make a bridge between Latex and Python.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
pip install mypytex
|
||||||
|
```
|
||||||
|
|
||||||
|
## texenv: Bring Python inside latex
|
||||||
|
|
||||||
|
*texenv* is a jinja2 environment which allow to use python commands inside latex.
|
||||||
|
|
||||||
|
- **\Block{#}** is the equivalent of **{% block #%}** in Jinja2. Every python's commands will be run like in a script. **%-** is also available for line statement.
|
||||||
|
- **\Var{#}** is the equivalent of **{{#}}** which print the statment.
|
||||||
|
|
||||||
|
Use the environment with a string template.
|
||||||
|
|
||||||
|
``` python
|
||||||
|
>>> from pytex import texenv
|
||||||
|
|
||||||
|
>>> text = "Hello"
|
||||||
|
>>> template = """\
|
||||||
|
\Var{text}
|
||||||
|
\Block{set a = 2}
|
||||||
|
%-set b = 3
|
||||||
|
\Var{a}
|
||||||
|
\Var{b}
|
||||||
|
"""
|
||||||
|
|
||||||
|
>>> template = texenv.from_string(template)
|
||||||
|
>>> print(template.render(text = text)
|
||||||
|
Hello
|
||||||
|
|
||||||
|
2
|
||||||
|
3
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## feed: converting template into tex files
|
||||||
|
|
||||||
|
## pdflatex: Compile files with pdflatex
|
||||||
|
|
||||||
|
## update_export_dict: import extract tools inside template
|
||||||
|
|
||||||
|
|
||||||
|
|
25
README.rst
25
README.rst
|
@ -1,25 +0,0 @@
|
||||||
Pytex
|
|
||||||
=====
|
|
||||||
|
|
||||||
Pytex is a simple package which make a bridge between Latex and Python.
|
|
||||||
|
|
||||||
texenv: Bring Python inside latex
|
|
||||||
---------------------------------
|
|
||||||
|
|
||||||
*texenv* is a jinja2 environment which allow to use python commands inside latex.
|
|
||||||
|
|
||||||
- **\Block{#}** is the equivalent of **{% block #%}** in Jinja2. Every python's commands will be run like in a script. **%-** is also available for line statement.
|
|
||||||
- **\Var{#}** is the equivalent of **{{#}}** which print the statment.
|
|
||||||
|
|
||||||
|
|
||||||
feed: converting template into tex files
|
|
||||||
----------------------------------------
|
|
||||||
|
|
||||||
pdflatex: Compile files with pdflatex
|
|
||||||
-------------------------------------
|
|
||||||
|
|
||||||
update_export_dict: import extract tools inside template
|
|
||||||
--------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -40,30 +40,48 @@ def update_export_dict(new_dict):
|
||||||
"""
|
"""
|
||||||
EXPORT_DICT.update(new_dict)
|
EXPORT_DICT.update(new_dict)
|
||||||
|
|
||||||
|
def run_python(template:str, data:dict):
|
||||||
|
""" Runs python commands using data
|
||||||
|
|
||||||
def feed(template, data, output="", force=0):
|
:param template: string containing python commands
|
||||||
""" Feed template with data to output
|
:param data: dictionary with variables needed to run commands
|
||||||
|
|
||||||
:param template: jinja2 template with texenv environment
|
"""
|
||||||
:param data: Data dictionnary
|
jinja_template = texenv.from_string(template)
|
||||||
|
return jinja_template.render(**data)
|
||||||
|
|
||||||
|
def build_output(filename):
|
||||||
|
""" Replace tpl with a number in filename and insure that this file does not exists """
|
||||||
|
|
||||||
|
num = 1
|
||||||
|
output_p = Path(filename.replace('tpl', f'{num:02d}'))
|
||||||
|
while output_p.exists():
|
||||||
|
logger.debug(f"{output_p} exists. Try next one")
|
||||||
|
num += 1
|
||||||
|
output_p = Path(filename.replace('tpl', f'{num:02d}'))
|
||||||
|
|
||||||
|
return output_p
|
||||||
|
|
||||||
|
def feed(template_filename, data, output="", force=0):
|
||||||
|
""" Feed template_filename with data and write it to output
|
||||||
|
|
||||||
|
:param template_filename: jinja2 template with texenv environment
|
||||||
|
:param data: Data dictionary
|
||||||
:param output: name of the output file
|
:param output: name of the output file
|
||||||
(by default: tpl is replaced by a 2 digits number)
|
(by default: tpl is replaced by a 2 digits number)
|
||||||
:param force: Override is the output already exists
|
:param force: Override is the output already exists
|
||||||
|
|
||||||
:return: name of fed template
|
:return: output filename
|
||||||
"""
|
"""
|
||||||
logger.info(f"Getting template {template}")
|
logger.info(f"Getting template_filename {template_filename}")
|
||||||
tpl = texenv.get_template(str(template))
|
with open(template_filename, "r") as f:
|
||||||
|
fed = run_python("".join(f.readlines()), data)
|
||||||
|
|
||||||
if not output:
|
if not output:
|
||||||
num = 1
|
output_p = build_output(template_filename)
|
||||||
output_p = Path(template.replace('tpl', f'{num:02d}'))
|
|
||||||
while output_p.exists() and not force:
|
|
||||||
logger.debug(f"{output_p} exists. Try next one")
|
|
||||||
num += 1
|
|
||||||
output_p = Path(template.replace('tpl', f'{num:02d}'))
|
|
||||||
else:
|
else:
|
||||||
output_p = Path(output)
|
output_p = Path(output)
|
||||||
|
|
||||||
if not force and output_p.exists():
|
if not force and output_p.exists():
|
||||||
logger.error(f"{output} exists. Use force=1 do override it")
|
logger.error(f"{output} exists. Use force=1 do override it")
|
||||||
raise ValueError(f"{output} exists. Use force=1 do override it")
|
raise ValueError(f"{output} exists. Use force=1 do override it")
|
||||||
|
@ -74,8 +92,9 @@ def feed(template, data, output="", force=0):
|
||||||
output_dir.mkdir(exist_ok=True)
|
output_dir.mkdir(exist_ok=True)
|
||||||
|
|
||||||
with open(output_p, "w") as output_f:
|
with open(output_p, "w") as output_f:
|
||||||
output_f.write(tpl.render(**EXPORT_DICT, **data))
|
output_f.write(fed)
|
||||||
logger.info(f"{template} has been rendered to {output}.")
|
logger.info(f"{template_filename} has been rendered to {output}.")
|
||||||
|
|
||||||
return output_p
|
return output_p
|
||||||
|
|
||||||
|
|
||||||
|
@ -93,7 +112,7 @@ def pdflatex(tex_filename, output_dir=""):
|
||||||
os.chdir(output_dir)
|
os.chdir(output_dir)
|
||||||
compilation = subprocess.Popen(
|
compilation = subprocess.Popen(
|
||||||
[
|
[
|
||||||
"pdflatex",
|
"lualatex",
|
||||||
f"-output-directory={output_dir}",
|
f"-output-directory={output_dir}",
|
||||||
# "-halt-on-error",
|
# "-halt-on-error",
|
||||||
"-interaction=nonstopmode",
|
"-interaction=nonstopmode",
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -5,7 +5,7 @@ from setuptools import setup, find_packages
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='mypytex',
|
name='mypytex',
|
||||||
version='0.3',
|
version='0.4',
|
||||||
description='Writing latex files and compile it with python and jinja2',
|
description='Writing latex files and compile it with python and jinja2',
|
||||||
url='https://git.opytex.org/lafrite/Pytex',
|
url='https://git.opytex.org/lafrite/Pytex',
|
||||||
author='Bertrand Benjamin',
|
author='Bertrand Benjamin',
|
||||||
|
|
Loading…
Reference in New Issue