Created
December 22, 2016 20:28
-
-
Save AKuederle/1faaf39acd9d966834526bec8869981a to your computer and use it in GitHub Desktop.
law-citing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jinja2 | |
import os | |
import shutil | |
test_cites = {'0':{'name': 'test0', 'case_id': 0}, '1':{'name': 'test1', 'case_id': 1}, '2':{'name': 'test2', 'case_id': 2}} | |
class Citation(object): | |
num_citations = 0 | |
cited = {} | |
def __init__(self, name, case_id): | |
self.last_cited = None | |
self.num_cited = 0 | |
self.name = name | |
self.case_id = case_id | |
self.keys = [name, case_id] | |
@property | |
def short(self): | |
return 'id: {}'.format(self.case_id) | |
@property | |
def long(self): | |
return '{}, id: {}'.format(self.name, self.case_id) | |
def cite(key): | |
if key in Citation.cited.keys(): | |
citation = Citation.cited[key] | |
elif key in test_cites.keys(): | |
citation = Citation(**test_cites[key]) | |
Citation.cited[key] = citation | |
else: | |
raise ValueError | |
if citation.last_cited and Citation.num_citations - citation.last_cited <= 5: | |
cite_text = citation.short | |
else: | |
cite_text = citation.long | |
Citation.num_citations += 1 | |
citation.last_cited = Citation.num_citations | |
citation.num_cited += 1 | |
return cite_text | |
def get_template(template_file): | |
"""Get a jinja template with latex tags. | |
modified from http://eosrei.net/articles/2015/11/latex-templates-python-and-jinja2-generate-pdfs | |
""" | |
latex_jinja_env = jinja2.Environment( | |
block_start_string = '<{', | |
block_end_string = '}>', | |
variable_start_string = '<#', | |
variable_end_string = '#>', | |
comment_start_string = '\#{', | |
comment_end_string = '}', | |
line_statement_prefix = '%%', | |
line_comment_prefix = '%#', | |
trim_blocks = True, | |
autoescape = False, | |
loader = jinja2.FileSystemLoader(os.path.abspath('/')) | |
) | |
template = latex_jinja_env.get_template(os.path.realpath(template_file)) | |
return template | |
def compile_pdf_from_template(template, insert_variables, out_path): | |
"""Render a template file and compile it to pdf""" | |
rendered_template = template.render(**insert_variables) | |
build_d = os.path.join(os.path.dirname(os.path.realpath(out_path)), '.build') | |
print(build_d) | |
if not os.path.exists(build_d): # create the build directory if not exisiting | |
os.makedirs(build_d) | |
temp_out = os.path.join(build_d, "tmp") | |
with open(temp_out+'.tex', "w") as f: # saves tex_code to output file | |
f.write(rendered_template) | |
os.system('pdflatex -output-directory {} {}'.format(build_d, temp_out+'.tex')) | |
shutil.copy2(temp_out+".pdf", os.path.relpath(out_path)) | |
if __name__ == '__main__': | |
template = get_template('./template.tex') | |
compile_pdf_from_template(template, {'cite': cite}, './out.pdf') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass[12pt,a4paper]{article} % din a4, 11 pt schrift, einseitig, | |
\begin{document} | |
\section{Product List by Category} | |
This is the first time i cite '0': <# cite('0') #>\\ | |
Then I cite '1' <# cite('1') #>\\ | |
When I now cite '0' again it should be in shortform: <# cite('0') #>\\ | |
Now I cite '2' a couple of times:\\ | |
<# cite('2') #>\\ | |
<# cite('2') #>\\ | |
<# cite('2') #>\\ | |
<# cite('2') #>\\ | |
<# cite('2') #>\\ | |
When I now cite '0' again it will be back in Longform <# cite('0') #>\\ | |
\end{document} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output pdf: https://postimg.org/image/efdqb65zb/