\documentclass[$if(fontsize)$$fontsize$$else$12pt$endif$,$if(lang)$$lang$,$endif$$if(papersize)$$papersize$,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}

% Colors ----------------------------------------

\usepackage[xcolor]{mdframed}
\definecolor{covercolor}{HTML}{$if(coverColor)$$coverColor$$else$777777$endif$}
% \definecolor{primarycolor}{HTML}{428BCA}
% \definecolor{successcolor}{HTML}{5CB85C}
\definecolor{warningcolor}{HTML}{F0AD4E}
\definecolor{dangercolor}{HTML}{D9534F}
\definecolor{infocolor}{HTML}{5BC0DE}

% Fonts -----------------------------------------

$if(fontfamily)$
\usepackage{$fontfamily$}
$else$
\usepackage{lmodern}
$endif$

$if(linestretch)$
\usepackage{setspace}
\setstretch{$linestretch$}
$endif$
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
$if(euro)$
  \usepackage{eurosym}
$endif$
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
    \usepackage{xltxtra,xunicode}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
  \newcommand{\euro}{€}

% to support CJK characters!
\usepackage{xeCJK}

$if(mainfont)$
    \setmainfont$mainfont$
$else$
    \setmainfont[%
      Color=primary,
      Path=$lib-dir$/fonts/Lato/,
      BoldItalicFont=Lato-BlackItalic,
      BoldFont=Lato-Bold,
      ItalicFont=Lato-Italic]{Lato-Regular}
$endif$

$if(CJKmainfont)$
    \setCJKmainfont{$CJKmainfont$}
$else$
    \setCJKmainfont[
      Color=primary,
      Path=$lib-dir$/fonts/Noto/,
      AutoFakeSlant=0.2,
      BoldFont=NotoSansCJK-Bold,
      ItalicFont=NotoSansCJK-Regular,
      BoldItalicFont=NotoSansCJK-Bold,
      ItalicFeatures={FakeSlant=0.2},
      BoldItalicFeatures={FakeSlant=0.2}]{NotoSansCJK-Regular}
$endif$

$if(sansfont)$
    \setsansfont{$sansfont$}
$else$
    \setsansfont[%
      Color=primary,
      Path=$lib-dir$/fonts/Lato/,
      BoldItalicFont=Lato-BlackItalic,
      BoldFont=Lato-Bold,
      ItalicFont=Lato-Italic]{Lato-Regular}
$endif$

$if(CJKsansfont)$
    \setCJKsansfont{$CJKsansfont$}
$else$
    \setCJKsansfont[
      Color=primary,
      Path=$lib-dir$/fonts/Noto/,
      AutoFakeSlant=0.2,
      BoldFont=NotoSansCJK-Bold,
      ItalicFont=NotoSansCJK-Regular,
      BoldItalicFont=NotoSansCJK-Bold,
      ItalicFeatures={FakeSlant=0.2},
      BoldItalicFeatures={FakeSlant=0.2}]{NotoSansCJK-Regular}
$endif$

$if(monofont)$
    \setmonofont[Mapping=tex-ansi]{$monofont$}
$else$
    \setmonofont[Mapping=tex-ansi]{Bitstream Vera Sans Mono}
$endif$

$if(CJKmonofont)$
    \setCJKmonofont{$CJKmonofont$}
$else$
    \setCJKmonofont[
      Path=$lib-dir$/fonts/Noto/,
      AutoFakeSlant=0.2,
      BoldFont=NotoSansCJK-Bold,
      ItalicFont=NotoSansCJK-Regular,
      BoldItalicFont=NotoSansCJK-Bold,
      ItalicFeatures={FakeSlant=0.2},
      BoldItalicFeatures={FakeSlant=0.2}]{NotoSansCJK-Regular}
$endif$

$if(mathfont)$
    \setmathfont(Digits,Latin,Greek){$mathfont$}
$endif$
\fi

% -----------------------------------------------

% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
  \usepackage{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
$if(natbib)$
\usepackage{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
$endif$
$if(biblatex)$
\usepackage{biblatex}
$if(biblio-files)$
\bibliography{$biblio-files$}
$endif$
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
$endif$

% Syntax highlighting ---------------------------

$if(highlighting-macros)$
$highlighting-macros$
$endif$

% -----------------------------------------------

\usepackage{fancyvrb}
$if(tables)$
\usepackage{longtable,booktabs}
$endif$
$if(graphics)$
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}

\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
$endif$
\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,
  bookmarks=true,
  pdfauthor={$author-meta$},
  pdftitle={$title-meta$},
  colorlinks=true,
  citecolor=$if(citecolor)$$citecolor$$else$blue$endif$,
  urlcolor=$if(urlcolor)$$urlcolor$$else$blue$endif$,
  linkcolor=$if(linkcolor)$$linkcolor$$else$magenta$endif$,
  pdfborder={0 0 0}}

\urlstyle{same} % don't use monospace font for urls

% Links as footnotes ----------------------------

$if(links-as-notes)$
% Make links footnotes instead of hotlinks:
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$
$if(strikeout)$
\usepackage[normalem]{ulem}
% avoid problems with \sout in headers with hyperref:
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
$endif$
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
$if(numbersections)$
\setcounter{secnumdepth}{5}
$else$
\setcounter{secnumdepth}{0}
$endif$
$if(verbatim-in-note)$
\VerbatimFootnotes % allows verbatim text in footnotes
$endif$
$if(lang)$
\ifxetex
  \usepackage{polyglossia}
  \setmainlanguage{$mainlang$}
\else
  \usepackage[$lang$]{babel}
\fi
$endif$

% Document metadata -----------------------------

$if(title)$
\title{$title$$if(subtitle)$\\\vspace{0.5em}{\large $subtitle$}$endif$}
$endif$
$if(author)$
\author{$for(author)$$author$$sep$ \and $endfor$}
$endif$
\date{$date$}
$for(header-includes)$
$header-includes$
$endfor$

% Utilities for the title page ------------------

\usepackage{tikz}

\newenvironment{bottompar}{\par\vspace*{\fill}}{\clearpage}

% Column support (see columns.coffee) -----------

\usepackage{multicol}

% Callout support (see callout.coffee) ----------

\newmdenv[%
  linecolor=infocolor,linewidth=5pt,
  topline=false,bottomline=false,rightline=false,
  innertopmargin=1em,innerbottommargin=1em,
  splittopskip=1em,splitbottomskip=1em,
  backgroundcolor=infocolor!30]{InfoCallout}
\newmdenv[%
  linecolor=warningcolor,linewidth=5pt,
  topline=false,bottomline=false,rightline=false,
  innertopmargin=1em,innerbottommargin=1em,
  splittopskip=1em,splitbottomskip=1em,
  backgroundcolor=warningcolor!30]{WarningCallout}
\newmdenv[%
  linecolor=dangercolor!60,linewidth=5pt,
  topline=false,bottomline=false,rightline=false,
  innertopmargin=1em,innerbottommargin=1em,
  splittopskip=1em,splitbottomskip=1em,
  backgroundcolor=dangercolor!25]{DangerCallout}

% \newmdenv[%
%   topline=false,bottomline=false,leftline=false,rightline=false,
%   innertopmargin=1em,innerbottommargin=1em,
%   splittopskip=1em,splitbottomskip=1em,
%   backgroundcolor=black!10]{Shaded}

% Customise default code blocks -----------------

\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},fontsize=\small}

\usepackage{listings}

\definecolor{codebgcolor}{HTML}{F7F7F7}
\definecolor{commentcolor}{HTML}{8E5A13}
\definecolor{keywordcolor}{HTML}{234A85}
\definecolor{literalcolor}{HTML}{0100CB}
\definecolor{stringcolor}{HTML}{519818}

\makeatletter
\lst@CCPutMacro\lst@ProcessOther {"2D}{\lst@ttfamily{-{}}{-{}}}
\@empty\z@\@empty
\makeatother

\lstset{
  basicstyle=\tt\footnotesize,
  frame=single,
  breaklines=true
}

\lstdefinestyle{scala}{
  breakatwhitespace=false,
  language=scala,
  frame=none,
  commentstyle=\tt\color{commentcolor},
  extendedchars=true,
  keepspaces=true,
  keywordstyle=\tt\color{keywordcolor},
  emphstyle=\tt\color{cyan},
  rulecolor=\tt\color{black},
  showspaces=false,
  showstringspaces=false,
  showtabs=false,
  stringstyle=\tt\color{stringcolor},
  tabsize=2,
  aboveskip=0em,
  belowskip=0em,
}

\surroundwithmdframed[
  hidealllines=true,
  backgroundcolor=codebgcolor,
  innerleftmargin=6pt,
  innerrightmargin=6pt,
  innertopmargin=10pt,
  innerbottommargin=6pt]{lstlisting}

\lstdefinelanguage{scala}{
  morekeywords={abstract,case,catch,class,def,%
    do,else,extends,false,final,finally,%
    for,if,implicit,import,match,mixin,%
    new,null,object,override,package,%
    private,protected,requires,return,sealed,%
    super,this,throw,trait,true,try,%
    type,val,var,while,with,yield},
  moreemph={},
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
  sensitive=true,
  morecomment=[l]{//},
  morecomment=[n]{/*}{*/},
  morestring=[b]",
  morestring=[b]',
  morestring=[b]"""
}

% Needed for recent update to Pandoc ------------

\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

% ===============================================
% Document
% ===============================================

\begin{document}
\pagecolor{white}

% Title page ------------------------------------

\begin{titlepage}
\pagecolor{covercolor}
\color{white}

\begin{tikzpicture}[overlay, remember picture]
\node[anchor=north west, xshift=-.2cm, yshift=.2cm]
     at (current page.north west)
     {\includegraphics{$lib-dir$/templates/images/hero-left-overlay-white.pdf}};
\end{tikzpicture}

\begin{tikzpicture}[overlay, remember picture]
\node[anchor=south east, xshift=.2cm, yshift=-.2cm]
     at (current page.south east)
     {\includegraphics{$lib-dir$/templates/images/hero-right-overlay-white.pdf}};
\end{tikzpicture}

\begin{center}
\vspace*{5cm}

$if(title)$
{\fontsize{1.5cm}{1em} \textsf{$title$}}
$endif$

$if(subtitle)$
\vspace{.25em}

{\fontsize{1cm}{1em} \textsf{$subtitle$}}
$endif$

\vspace{1.25em}

$for(author)$
{\fontsize{1cm}{1em} \textsf{$author$}}
$endfor$

\vspace{1em}

$if(date)$
{\fontsize{.5cm}{1em} \textsf{$date$}}
$endif$

\begin{bottompar}
\begin{center}
\includegraphics[width=5cm]{$lib-dir$/templates/images/brand.pdf}

\vspace{1cm}

Copyright $copyright$ $author$.
\end{center}
\end{bottompar}

\end{center}
\end{titlepage}

% $if(title)$
% \maketitle
% $endif$

\pagecolor{white}

% Abstract --------------------------------------

$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$

% Include-before hook ---------------------------

\vspace*{\fill}

\begin{center}

{\Large $title$$if(subtitle)$ $subtitle$$endif$}

$date$

Copyright $copyright$ $author$.

$if(license)$
Licensed under $license$.
$endif$

Published by \href{http://underscore.io}{Underscore Consulting LLP}, Brighton, UK.

\end{center}

\vspace{1em}

$for(include-before)$
$include-before$

$endfor$

\vspace*{\fill}

% Tables of contents, etc. ----------------------

$if(toc)$
{
\hypersetup{linkcolor=black}
\setcounter{tocdepth}{$if(toc-depth)$$toc-depth$$else$3$endif$}
\tableofcontents
}
$endif$
$if(lot)$
\listoftables
$endif$
$if(lof)$
\listoffigures
$endif$

% Main document body ----------------------------

$body$

% Bibliography ----------------------------------

$if(natbib)$
$if(biblio-files)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\bibliography{$biblio-files$}

$endif$
$endif$
$if(biblatex)$
\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$

$endif$

% Include-after hook ----------------------------

$for(include-after)$
$include-after$

$endfor$

% The end ---------------------------------------

\end{document}
