Skip to content

Instantly share code, notes, and snippets.

@yoavram
Last active March 26, 2017 06:00
Show Gist options
  • Select an option

  • Save yoavram/b8adf6114eeed88b3a265ff88f72eb96 to your computer and use it in GitHub Desktop.

Select an option

Save yoavram/b8adf6114eeed88b3a265ff88f72eb96 to your computer and use it in GitHub Desktop.

Revisions

  1. Yoav Ram revised this gist Mar 26, 2017. No changes.
  2. Yoav Ram revised this gist Mar 26, 2017. No changes.
  3. Yoav Ram created this gist Mar 26, 2017.
    36 changes: 36 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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)
    35 changes: 35 additions & 0 deletions get_bib.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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()%
    19 changes: 19 additions & 0 deletions get_citations.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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()