Last active
March 26, 2017 06:00
-
-
Save yoavram/b8adf6114eeed88b3a265ff88f72eb96 to your computer and use it in GitHub Desktop.
Makefile to convert Markdown to PDF. Depends on Pandoc 1.19.1, pandoc-citeproc 0.10.4.1, pandoc-crossref 0.2.5.0, Python 3, click 6.6, bibtexparser 0.6.2
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
from bibtexparser import load as load_bib | |
from bibtexparser.bwriter import BibTexWriter | |
from bibtexparser.bibdatabase import BibDatabase | |
import click | |
@click.command() | |
@click.argument('keys_filename', type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True)) | |
@click.argument('bibtex_filename', type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True)) | |
@click.argument('output_filename', type=click.Path(file_okay=True, dir_okay=False, writable=True)) | |
@click.option('-v/-V', '--verbose/--no-verbose', default=False) | |
def main(keys_filename, bibtex_filename, output_filename, verbose): | |
with open(keys_filename) as f: | |
citation_keys = (line.strip() for line in f.readlines()) | |
if verbose: | |
print("Read {} keys from {}".format(len(citation_keys), citation_keys)) | |
with open(bibtex_filename) as f: | |
main_bib = load_bib(f) | |
if verbose: | |
print("Read {} entries from {}".format(len(main_bib.entries), bibtex_filename)) | |
out_bib = BibDatabase() | |
for key in citation_keys: | |
e = main_bib.entries_dict[key] | |
out_bib.entries.append(e) | |
if verbose: | |
print("Writing {} entries to {}".format(len(out_bib.entries), output_filename)) | |
writer = BibTexWriter() | |
with open(output_filename, 'w') as f: | |
f.write(writer.write(out_bib)) | |
if __name__ == '__main__': | |
main()% |
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 re | |
import click | |
@click.command() | |
@click.argument('markdown_filename', type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True)) | |
@click.argument('keys_filename', type=click.Path(file_okay=True, dir_okay=False, writable=True)) | |
def main(markdown_filename, keys_filename): | |
pattern = re.compile('@(\w+\d{4})') | |
with open(markdown_filename) as f: | |
groups = (pattern.findall(line) for line in f) | |
groups = sum((g for g in groups if g), []) | |
groups = set(groups) | |
with open(keys_filename, 'wt') as f: | |
for g in groups: | |
print(g, file=f) | |
if __name__ == '__main__': | |
main() |
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
main_bib=~/library.bib | |
ms_bib=bibtex.bib | |
citation_keys=citation_keys | |
pdf=ms.pdf | |
md=ms.md | |
csl=evolution.csl | |
diagram_dot = diagram.dot | |
diagram_pdf = diagram.pdf | |
list: | |
@sh -c "$(MAKE) -p no_targets__ | awk -F':' '/^[a-zA-Z0-9][^\$$#\/\\t=]*:([^=]|$$)/ {split(\$$1,A,/ /);for(i in A)print A[i]}' | grep -v '__\$$' | grep -v 'make\[1\]' | grep -v 'Makefile' | sort" | |
no_targets__: | |
$(citation_keys): $(md) | |
python get_citations.py $(md) $(citation_keys) | |
$(ms_bib): $(citation_keys) $(main_bib) | |
python get_bibtex.py $(citation_keys) $(main_bib) $@ | |
$(pdf): $(md) $(ms_bib) $(diagram_pdf) | |
pandoc -r markdown+simple_tables+table_captions+yaml_metadata_block -s -S $< -o $@ --filter pandoc-crossref --filter pandoc-citeproc --bibliography=$(ms_bib) --csl $(csl) --latex-engine=xelatex --number-sections | |
@open $(pdf) | |
$(diagram_pdf): $(diagram_dot) | |
dot $< -Tpdf -Kdot -o$@ | |
model_diagram: $(diagram_pdf) | |
build: $(pdf) | |
edit: $(md) | |
@subl $< | |
read: $(pdf) | |
@open $(pdf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment