Skip to content

Instantly share code, notes, and snippets.

@austinjp
Created June 20, 2016 15:05
Show Gist options
  • Select an option

  • Save austinjp/393f3d7f6b78e7ba90a4a705f80c0a5a to your computer and use it in GitHub Desktop.

Select an option

Save austinjp/393f3d7f6b78e7ba90a4a705f80c0a5a to your computer and use it in GitHub Desktop.
#!/bin/bash
# If this script fails you may have carriage returns at the end of each line. Try:
# sed -i -e 's/^M//g' batch.sh
# ... where you can create the ^M character by typing control-v control-m
# Then of course:
# chmod +x batch.sh
# Relies on some pandoc filters:
# https://github.com/tomduck/pandoc-fignos
# https://github.com/jgm/pandoc-citeproc
# You can probably install them with:
# pip install pandoc-fignos
# apt-get install pandoc-citeproc
# Also relies on xelatex which you can probably install with
# apt-get install texlive-xetex
# You may wish to install some nice fonts, see
# https://wiki.debian.org/SubstitutingCalibriAndCambriaFonts
# In summary try this:
# apt-get install fonts-crosextra-carlito
# Rough workflow:
# cd /tmp
# git clone https://github.com/me/my-gitbook.git gitbook
# cp /path/to/render-gitbook.sh /tmp/gitbook
# cd /tmp/gitbook
# wget https://raw.githubusercontent.com/austinjp/phd-application-03/master/gitbook-render.sh
# wget https://raw.githubusercontent.com/austinjp/phd-application-03/master/gitbook_citation_style.csl
# wget https://raw.githubusercontent.com/austinjp/phd-application-03/master/gitbook_tex_header.tex
# ./render-gitbook.sh /tmp/gitbook
declare wd=$1
if [ ! -d "$wd" ]; then
echo "First arg should be a directory"
exit 1
fi
# Download a nice citation style if one doesn't already exist
if [ ! -f "$wd/gitbook_citation_style.csl" ]; then
wget "https://raw.githubusercontent.com/citation-style-language/styles/master/harvard-imperial-college-london.csl" -O- > "$wd/gitbook_citation_style.csl"
fi
# Edit the citation style if you like.
# For example, look for <citation> and remove brackets in the prefix and suffix.
# FIXME use $wd in redirect
# cat "$wd/SUMMARY.md" | perl -ne "s/^.+\((.+)\)$/$wd/$1/msg; s/^\#.*$//msg; s/\s+/ /msg; //; print;" > "$wd/gitbook_file_list.txt"
cat "$wd/SUMMARY.md" | perl -ne 's/^.*\((.+)\).*$/$1/msg; s/^\#.*$//msg; s/\s+/ /msg; print $_."\n" if($_);' > "$wd/gitbook_file_list.txt"
declare gitbook_files=`cat "$wd/gitbook_file_list.txt"`
echo Running pandoc TeX...
eval "pandoc \
-V fontsize=12pt -V papersize:a4paper \
--latex-engine=xelatex \
-H '$wd/gitbook_tex_header.tex' \
--csl $wd/gitbook_citation_style.csl \
--bibliography $wd/references.bib \
-s \
"$gitbook_files$" \
-o gitbook_output.tex"
echo Running pandoc PDF...
# FIXME Hmm this doesn't work quite as well, latex in the markdown seems to get lost in the translation to TeX:
# eval "pandoc -V fontsize=12pt -V papersize:a4paper -H '$wd/gitbook_tex_header.tex' --latex-engine=xelatex -s gitbook_output.tex -o gitbook_output.pdf"
eval "pandoc \
-V fontsize=12pt -V papersize:a4paper \
--latex-engine=xelatex \
-H '$wd/gitbook_tex_header.tex' \
--csl $wd/gitbook_citation_style.csl \
--filter pandoc-fignos \
--filter pandoc-citeproc \
--bibliography $wd/references.bib \
-s \
"$gitbook_files$" \
-o gitbook_output.pdf"
exit 0
# For consistent output I prefer to generate a Word doc, then use Word to generate a PDF.
# But you can generate a Word doc from the markdown like this:
echo Running pandoc docx...
eval "pandoc \
-V fontsize=12pt -V papersize:a4paper \
--csl '$wd/gitbook_citation_style.csl' \
--filter pandoc-fignos \
--filter pandoc-citeproc \
--bibliography $wd/references.bib \
-s \
"$gitbook_files$" \
-o gitbook_output.docx"
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" version="1.0" class="in-text" demote-non-dropping-particle="sort-only" default-locale="en-GB" page-range-format="expanded">
<info>
<title>Harvard - Imperial College London</title>
<id>http://www.zotero.org/styles/harvard-imperial-college-london</id>
<link href="http://www.zotero.org/styles/harvard-imperial-college-london" rel="self"/>
<link href="http://www3.imperial.ac.uk/library/subjectsandsupport/referencemanagement/harvard/references" rel="documentation"/>
<author>
<name>Lawrence Greenfield</name>
</author>
<category citation-format="author-date"/>
<category field="medicine"/>
<summary>Harvard author-date style edited for Imperial College London</summary>
<updated>2012-09-27T22:06:38+00:00</updated>
<rights license="http://creativecommons.org/licenses/by-sa/3.0/">This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 License</rights>
</info>
<macro name="editor">
<names variable="editor" delimiter=", ">
<name and="symbol" delimiter=", "/>
<label form="short" prefix=" (" suffix=")"/>
</names>
</macro>
<macro name="anon">
<text term="anonymous" form="short" text-case="capitalize-first" strip-periods="true"/>
</macro>
<macro name="author">
<names variable="author">
<name and="symbol" name-as-sort-order="all" sort-separator=", " initialize-with="." delimiter-precedes-last="never" delimiter=", "/>
<label form="short" prefix=" "/>
<substitute>
<text macro="editor"/>
<text macro="anon"/>
</substitute>
</names>
</macro>
<macro name="author-short">
<names variable="author">
<name form="short" and="symbol" delimiter=", " delimiter-precedes-last="never"/>
<et-al font-style="italic"/>
<substitute>
<names variable="translator"/>
<text macro="anon"/>
</substitute>
</names>
</macro>
<macro name="access">
<choose>
<if variable="URL DOI" match="any">
<group>
<text value="Available from: "/>
<choose>
<if variable="DOI">
<text variable="DOI" prefix="doi:"/>
</if>
<else>
<text variable="URL"/>
</else>
</choose>
<group prefix=" [" suffix="]">
<text term="accessed" text-case="capitalize-first" suffix=": "/>
<date variable="accessed">
<date-part name="day" suffix=" "/>
<date-part name="month" suffix=" "/>
<date-part name="year"/>
</date>
</group>
</group>
</if>
</choose>
</macro>
<macro name="title">
<choose>
<if type="book webpage graphic" match="any">
<text variable="title" font-style="italic"/>
</if>
<else-if variable="container-title" match="none">
<text variable="title" font-style="italic"/>
</else-if>
<else>
<text variable="title"/>
</else>
</choose>
</macro>
<macro name="publisher">
<group delimiter=", " suffix=".">
<text variable="publisher-place"/>
<text variable="publisher"/>
</group>
</macro>
<macro name="year-date">
<choose>
<if variable="issued">
<date variable="issued">
<date-part name="year"/>
</date>
</if>
<else>
<text term="no date" form="short"/>
</else>
</choose>
</macro>
<macro name="edition">
<choose>
<if is-numeric="edition">
<group delimiter=" ">
<number variable="edition" form="ordinal"/>
<text term="edition" form="long" strip-periods="true"/>
</group>
</if>
<else>
<text variable="edition"/>
</else>
</choose>
</macro>
<macro name="pages">
<group>
<text variable="page"/>
</group>
</macro>
<macro name="online">
<choose>
<if variable="DOI URL" match="any">
<text value="[Online]"/>
</if>
</choose>
</macro>
<macro name="issued">
<choose>
<if type="article-newspaper paper-conference broadcast" match="any">
<date variable="issued">
<date-part name="day" suffix=" "/>
<date-part name="month"/>
</date>
</if>
</choose>
<choose>
<if type="paper-conference" match="any">
<date variable="issued">
<date-part name="year" prefix=" "/>
</date>
</if>
</choose>
</macro>
<citation et-al-min="4" et-al-use-first="1" disambiguate-add-year-suffix="true" collapse="year">
<layout prefix="" suffix="" delimiter="; ">
<group delimiter=" ">
<text macro="author-short"/>
<text macro="year-date"/>
<group>
<label variable="locator" form="short"/>
<text variable="locator"/>
</group>
</group>
</layout>
</citation>
<bibliography hanging-indent="true" et-al-min="5" et-al-use-first="4">
<sort>
<key macro="author"/>
<key variable="title"/>
</sort>
<layout>
<text macro="author"/>
<text macro="year-date" prefix=" (" suffix=")"/>
<choose>
<if type="thesis">
<group prefix=" " delimiter=". " suffix=".">
<text macro="title" font-style="italic"/>
<text macro="edition"/>
<text variable="genre"/>
<text macro="online"/>
<text macro="publisher"/>
</group>
</if>
<else-if type="webpage">
<group prefix=" " delimiter=". " suffix=".">
<text macro="title"/>
<text macro="edition"/>
<text macro="online"/>
</group>
<group prefix=" " delimiter=". " suffix=".">
<date variable="issued">
<date-part name="day" suffix=" "/>
<date-part name="month" suffix=" "/>
<date-part name="year"/>
</date>
<text variable="container-title"/>
</group>
</else-if>
<else-if type="article-journal article-newspaper broadcast personal_communication thesis" match="any">
<group suffix=".">
<text macro="title" prefix=" "/>
<text macro="editor" prefix=" "/>
</group>
<choose>
<if variable="author" match="any">
<text variable="container-title" font-style="italic" prefix=" " suffix="."/>
</if>
</choose>
<text macro="online" prefix=" "/>
<group prefix=" " suffix=".">
<text variable="volume"/>
<group delimiter=", ">
<text variable="issue" prefix=" (" suffix=")"/>
<text macro="issued"/>
<text macro="pages"/>
</group>
</group>
</else-if>
<else-if type="book graphic" match="any">
<group prefix=" " delimiter=". " suffix=".">
<text variable="note"/>
<text macro="title"/>
<group delimiter=" ">
<text variable="collection-title"/>
<text variable="collection-number"/>
</group>
<text macro="edition"/>
<text macro="editor"/>
<text macro="online"/>
<text macro="publisher"/>
</group>
</else-if>
<else-if type="chapter paper-conference" match="any">
<text macro="title" prefix=" " suffix=". "/>
<group delimiter=" ">
<text term="in" text-case="capitalize-first" suffix=":"/>
<text macro="editor" suffix=". "/>
<text variable="container-title" font-style="italic" suffix="."/>
<text variable="collection-title" suffix="."/>
<text macro="edition" prefix=" " suffix="."/>
<text macro="online" suffix="."/>
<text macro="issued"/>
<text macro="publisher" suffix="."/>
<label variable="page" form="short"/>
<text macro="pages" suffix="."/>
</group>
</else-if>
<else>
<group prefix=" " suffix="." delimiter=" ">
<text macro="title"/>
<text macro="editor"/>
</group>
<text variable="container-title" font-style="italic" prefix=" " suffix="."/>
<text macro="online" prefix=" " suffix=". "/>
<group suffix="." delimiter=" ">
<group>
<text variable="volume"/>
<text variable="issue" prefix=" (" suffix=")"/>
<text macro="issued"/>
</group>
<group>
<label variable="page" form="short"/>
<text macro="pages"/>
</group>
</group>
</else>
</choose>
<text prefix=" " macro="access" suffix="."/>
</layout>
</bibliography>
</style>
\listfiles
\usepackage{fontspec}
\setmainfont{Carlito}
\usepackage{setspace}
\doublespacing
\usepackage[a4paper, total={6in, 8in}]{geometry}
%\documentclass[12pt]{article}
%\documentclass[12pt, a4paper, twoside]{thesis}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment