Skip to content
Snippets Groups Projects
Commit 4e75bfeb authored by James Vasile's avatar James Vasile
Browse files

Jinja templating and drafts

parent a260e4b6
No related branches found
No related tags found
No related merge requests found
...@@ -7,11 +7,16 @@ REVBIN := $(OTS_DOCTOOLS_DIR)/get_revision ...@@ -7,11 +7,16 @@ REVBIN := $(OTS_DOCTOOLS_DIR)/get_revision
ifeq ("$(wildcard $(REVBIN))","") ifeq ("$(wildcard $(REVBIN))","")
REVBIN := $(shell find . -type f -name get_revision -print -quit) REVBIN := $(shell find . -type f -name get_revision -print -quit)
endif endif
REVBIN := $(shell ${REVBIN})
LTX=$(wildcard *.ltx) LTX=$(wildcard *.ltx)
PDFLATEX="pdflatex" PDFLATEX="pdflatex"
# This is a script that takes yaml-fronted latex and feeds it to a
# jinja2 templating engine. See the jinja directory for more.
JINJIFY=${OTS_DOCTOOLS_DIR}/jinja/jinjify.py
default: build-or-help default: build-or-help
build-or-help: build-or-help:
...@@ -69,31 +74,33 @@ all-drafts: ...@@ -69,31 +74,33 @@ all-drafts:
# but no content was changed, then 'latexmk' will run very quickly: # but no content was changed, then 'latexmk' will run very quickly:
# it'll wake up, issue its cheery version-header greeting, realize # it'll wake up, issue its cheery version-header greeting, realize
# that nothing actually needs to be done, and exit.) # that nothing actually needs to be done, and exit.)
LTX_SRCS := $(shell find . -name '*.ltx' ! -path './.\#*') LTX_SRCS := $(shell find . -name dd'*.ltx' ! -path './.\#*')
%.pdf: %.ltx Makefile #$(LTX_SRCS) %.pdf: %.ltx Makefile venv #$(LTX_SRCS)
@latexmk -pdf -pdflatex=$(PDFLATEX) -halt-on-error $< venv/bin/python3 ${JINJIFY} -o draft True $< > $(<:.ltx=.intermediate_ltx)
@latexmk -pdf -pdflatex=$(PDFLATEX) -halt-on-error $(<:.ltx=.intermediate_ltx)
# This builds the draft. It can handle underscores in the jobname, # This builds the draft. It can handle underscores in the jobname,
# but will produce spurious "type a command or say \end" notices. # but will produce spurious "type a command or say \end" notices.
%.draft.pdf: %.ltx Makefile %.draft.pdf: %.ltx Makefile venv
@if [ -L $(shell basename $< .ltx).draft.pdf ]; then rm $(shell basename $< .ltx).draft.pdf; fi @if [ -L $(shell basename $< .ltx).draft.pdf ]; then rm $(shell basename $< .ltx).draft.pdf; fi
@if test $(findstring _, $<); then \ venv/bin/python3 ${JINJIFY} -o draft True $< > $(<:.ltx=.intermediate_ltx)
export SVNREVISION="$(shell $(REVBIN) $<)" ; \ latexmk -pdf -pdflatex=$(PDFLATEX) -halt-on-error --shell-escape $(<:.ltx=.intermediate_ltx)
export DRAFT=yes ; \ mv $(shell basename $< .ltx).pdf $(shell basename $< .ltx)-$(REVBIN).pdf
sed "s/\\\getenv\\[\\\JOBNAME.*/\\\newcommand{\\\JOBNAME}{$(subst _,\\\_,$<)}/" $< | \ ln -sf $(shell basename $< .ltx)-$(REVBIN).pdf $(shell basename $< .ltx).draft.pdf
latexmk -pdf -pdflatex=$(PDFLATEX) -halt-on-error --shell-escape -jobname=$(addsuffix , $(basename $@)); \
else \ # OTS Doctools's Jinjify needs some python dependencies. Not sure
export SVNREVISION="$(shell $(REVBIN) $<)" ; \ # this is the best place to do this-- it wastes diskspace and clutters
export DRAFT=yes ; \ # document dirs with a venv dir. OTOH, it happens automatically, so
export JOBNAME="$<" ; \ # it's one less thing for a user to think about.
latexmk -pdf -pdflatex=$(PDFLATEX) -halt-on-error --shell-escape -jobname=$(addsuffix , $(basename $@)) $< ; \ venv:
fi; virtualenv -p python3 venv
mv $(shell basename $< .ltx).draft.pdf $(shell basename $< .ltx)-$(shell $(REVBIN) $<).pdf venv/bin/pip3 install python-frontmatter jinja2
ln -sf $(shell basename $< .ltx)-$(shell $(REVBIN) $<).pdf $(shell basename $< .ltx).draft.pdf
# LaTeX litters a lot # LaTeX litters a lot
clean_latex: clean_latex:
@latexmk -c -f $(patsubst %.yaml.ltx,%.ltx,$(wildcard *.yaml.ltx)) $(wildcard *.ltx) $(wildcard *.tex) @latexmk -c -f $(wildcard *.ltx) $(wildcard *.tex)
@rm -f $(patsubst %.ltx,%.bbl,$(wildcard *.ltx))
@rm -f $(patsubst %.ltx,%.run.xml,$(wildcard *.ltx))
# We don't remove .pdf files by default, even though they're generated # We don't remove .pdf files by default, even though they're generated
# files, because in practice one usually wants to keep them around. # files, because in practice one usually wants to keep them around.
...@@ -118,6 +125,7 @@ clean: clean_latex ...@@ -118,6 +125,7 @@ clean: clean_latex
) )
@if [ -s "latex2docx" ]; then rm -f latex2docx; fi @if [ -s "latex2docx" ]; then rm -f latex2docx; fi
@if [ -s "latex2odt" ]; then rm -f latex2odt; fi @if [ -s "latex2odt" ]; then rm -f latex2odt; fi
@rm -f $(patsubst %.ltx,%.intermediate_ltx,$(wildcard *.ltx))
# Don't delete intermediate files # Don't delete intermediate files
......
\documentclass[12pt]{article}
\usepackage{ots}
\BLOCK{if draft is defined and draft=='True'}
\usepackage{draftwatermark}
\SetWatermarkText{DRAFT}
\SetWatermarkScale{6}
\BLOCK{endif}
\usepackage{enumerate}
\ifxetex
\usepackage[setpagesize=false, % page size defined by xetex
unicode=false, % unicode breaks when used with xetex
xetex]{hyperref}
\else
\usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true, pdfborder={0 0 0}}
\setlength{\parindent}{0pt}
\setlength{\parskip}{7pt plus 2pt minus 1pt}
\setcounter{secnumdepth}{0}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\usepackage{multicol} % We use this for the Contact Information section
% Set up the bibliography style
\usepackage[backend=biber,style=authortitle]{biblatex} %backend=biber is 'better'
\addbibresource{bibliography.bib}
\setlength\bibitemsep{0.5\baselineskip}
\setlength\bibnamesep{0.5\baselineskip}
\setlength\bibinitsep{0.5\baselineskip}
% For logo and accompanying contact info.
\usepackage{graphicx}
\usepackage{float}
\usepackage{color}
\definecolor{dkgreen}{RGB}{50, 109, 72}
\definecolor{dkergreen}{RGB}{0, 100, 0}
\DeclareFixedFont{\viiisf}{OT1}{cmss}{m}{n}{8}
\newcommand{\circlesep}{\raisebox{1.7em}{\hspace{1.8em}\includegraphics[clip, trim=0cm 11cm 19.3cm 11cm, scale=0.05]{otslogo.pdf}\hspace{1.8em}}}
\begin{document}
\raggedright
\BLOCK{block letterhead}
\BLOCK{endblock} %# letterhead
\BLOCK{block title}
\BLOCK{ if title is defined }
\begin{center}
\LARGE{\VAR{title}}\\
\BLOCK{ if date is defined }
\vspace{1em}
\normalsize{\VAR{date}}
\BLOCK{endif}
\end{center}
\BLOCK{endif}
\BLOCK{endblock}
\BLOCK{block body}
This is default text.
\section{What is Lorem Ipsum?}
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only
five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s with
the release of Letraset sheets containing Lorem Ipsum passages, and
more recently with desktop publishing software like Aldus PageMaker
including versions of Lorem Ipsum.
\subsection{Why do we use it?}
It is a long established fact that a reader will be distracted by the
readable content of a page when looking at its layout. The point of
using Lorem Ipsum is that it has a more-or-less normal distribution of
letters, as opposed to using 'Content here, content here', making it
look like readable English. Many desktop publishing packages and web
page editors now use Lorem Ipsum as their default model text, and a
search for 'lorem ipsum' will uncover many web sites still in their
infancy. Various versions have evolved over the years, sometimes by
accident, sometimes on purpose (injected humour and the like).
\subsection{Where does it come from?}
Contrary to popular belief, Lorem Ipsum is not simply random text. It
has roots in a piece of classical Latin literature from 45 BC, making
it over 2000 years old. Richard McClintock, a Latin professor at
Hampden-Sydney College in Virginia, looked up one of the more obscure
Latin words, consectetur, from a Lorem Ipsum passage, and going
through the cites of the word in classical literature, discovered the
undoubtable source. Lorem Ipsum comes from sections 1.10.32 and
1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and
Evil) by Cicero, written in 45 BC. This book is a treatise on the
theory of ethics, very popular during the Renaissance. The first line
of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in
section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced
below for those interested. Sections 1.10.32 and 1.10.33 from "de
Finibus Bonorum et Malorum" by Cicero are also reproduced in their
exact original form, accompanied by English versions from the 1914
translation by H. Rackham.
\subsection{Where can I get some?}
There are many variations of passages of Lorem Ipsum available, but
the majority have suffered alteration in some form, by injected
humour, or randomised words which don't look even slightly
believable. If you are going to use a passage of Lorem Ipsum, you need
to be sure there isn't anything embarrassing hidden in the middle of
text. All the Lorem Ipsum generators on the Internet tend to repeat
predefined chunks as necessary, making this the first true generator
on the Internet. It uses a dictionary of over 200 Latin words,
combined with a handful of model sentence structures, to generate
Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is
therefore always free from repetition, injected humour, or
non-characteristic words etc. Try https://lipsum.com/
\BLOCK{endblock}%# end body block
\end{document}
#!/usr/bin/env python3
"""This script takes two parameters. The first is a latex file with
yaml frontmatter. The body after the frontmatter is latex use as a
jinja2 template. We pass the yaml frontmatter to that jinja2 template
and print the results.
"""
import os
# Search path for Jinja templates, in order of preference
TEMPLATE_DIRS = [ os.path.abspath(os.getcwd()),
os.path.join(os.getenv('OTSDIR'), 'forms', 'jinja'),
os.path.join(os.getenv('OTSDIR'), 'forms', 'latex'),
os.path.join(os.getenv('OTS_DOCTOOLS_DIR'), 'jinja'),
os.path.join(os.getenv('OTS_DOCTOOLS_DIR'), 'latex')]
import click
import frontmatter
import jinja2
import subprocess
import sys
# We're not using this func right now, but I do want to add markdown
# support back in later, once I've figure out how to fit it in
# appropriately.
def md2tex(string):
"""Pass markdown STRING through pandoc and return the resulting latex"""
p = subprocess.Popen("pandoc -f markdown -t latex",
shell=True,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE
)
return p.communicate(input=string.encode('UTF-8'))[0].decode('UTF-8')
@click.command()
@click.argument('filename')
@click.option('--option', '-o', nargs=2, multiple=True, default='', help='Variables and their values.')
def jinjify(filename, option):
"""Read and parse yaml+latex file."""
doc = frontmatter.load(filename)
template = doc.content # treat latex as a jinja2 template
args = doc.metadata # treat yaml frontmatter as jinja2 arguments
# Pass the environment in as a var
args['environment'] = os.environ
# Pass to jinja any options submitted on the command line
for o in option:
args[o[0]] = o[1]
# Make a version of a Jinja2 parser that plays nice with LaTeX
latex_jinja_env = jinja2.Environment(
block_start_string = '\BLOCK{',
block_end_string = '}',
variable_start_string = '\VAR{',
variable_end_string = '}',
comment_start_string = '\#{',
comment_end_string = '}',
line_statement_prefix = '%%',
line_comment_prefix = '%#',
trim_blocks = True,
autoescape = False,
loader = jinja2.ChoiceLoader([
jinja2.FileSystemLoader(TEMPLATE_DIRS),
jinja2.DictLoader({'jinjify_me.ltx':template,
'proposal.ltx':'test'}),
])
)
template = latex_jinja_env.get_template('jinjify_me.ltx')
# Quick example of calling a python func from our template. Evoke with
# \VAR{testfunc()}
#def testfunc():
# return u'TEST'
#template.globals.update(testfunc=testfunc)
rendered = template.render(**args)
print(rendered)
if __name__ == '__main__':
jinjify()
...@@ -128,27 +128,7 @@ ...@@ -128,27 +128,7 @@
{\setlength{\headheight}{10pt}\setlength{\headsep}{30pt} {\setlength{\headheight}{10pt}\setlength{\headsep}{30pt}
\renewcommand{\@oddhead}{\usebox{\firmhead}} \renewcommand{\@oddhead}{\usebox{\firmhead}}
} }
% Some stuff for the draft version
\usepackage{eso-pic}
%\usepackage[yyyymmdd,hhmmss]{datetime}
\usepackage{datetime2}
\usepackage{catchfile}
\newcommand{\getenv}[2][]{%
\CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{}%
\if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
\getenv[\DRAFT]{DRAFT}
\getenv[\JOBNAME]{JOBNAME}
\getenv[\SVNREVISION]{SVNREVISION}
\usepackage{xstring}
%% If this is a draft, add a draft watermark
\IfSubStr{\DRAFT}{yes}{
\usepackage{draftwatermark}
\SetWatermarkText{DRAFT}
\SetWatermarkScale{6}
}{}
%% For reference, the 10 default LaTeX font sizes are: %% For reference, the 10 default LaTeX font sizes are:
%% %%
%% \tiny %% \tiny
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment