%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Base Document Settings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass%
[12pt] % Set default font size
{book} % Set document Class

%% Set page size and margins
\usepackage{geometry}
  \geometry{
  paperheight=203.2mm, %actual height measurement of the page
  paperwidth=127mm, %actual width measurement of the page
  total={90.54mm, 127mm}, % widht/height of the "text box"/text area on the page
  top=22.352mm, % top margin
  bottom=19.177mm, %bottom margin
  inner=19.177mm, %inner/gutter margin
  outer=19.177mm, %outer margin
  }
 
%% Set line spacing
% 1.0 = single space
% 1.3 = 1.5 space
% 1.6 = double space
\linespread{1.3}
 
%% Set no additional space between paragraphs
\setlength{\parskip}{0pt}
 
%% Use this package to enable tables  
\usepackage{longtable,booktabs}

%% Hide the toprule
\renewcommand{\toprule}{}

%% Hide the bottomrule
\renewcommand{\bottomrule}{}

\setlength{\LTpost}{0pt}

  
%% Set language and hyphenation rules
\usepackage[english]{babel}

%% Set encoding
\usepackage[utf8]{inputenc}

%% Enable widow/orphan control
%\usepackage[all,defaultlines=2]{nowidow}
\widowpenalty=10000
\clubpenalty=10000

%% Prevent overfull lines
\setlength{\emergencystretch}{3em}
\providecommand{\tightlist}{\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Title Settings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Needed for styling chapter title headings
\usepackage{titlesec}

% Turn off subsection numbering for titles
\renewcommand{\thesection}{}

%% Style the chapter title that appears on the first page of each chapter
\titlespacing*{\chapter}
{0pt}%left margin
{150pt}%top margin
{25pt}%bottom margin

% This tells it to style the titleformat for chapters
\titleformat{\chapter}[display]{\bfseries\titlefont}{\Large Chapter \thechapter \normalfont}{0.5mm}{\small}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Headers & Footers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Needed for headers and footers
\usepackage{fancyhdr}

%% Prevent headers from appearing on empty pages
\usepackage{emptypage}

%% Remove header/footer from the first page of every chapter
\fancypagestyle{plain}{ 
\fancyhf{} %remove/clear header/footer content
\renewcommand{\headrulewidth}{0.0pt} %remove black line/"rule" from header
}

%% Set the header height in: in, mm, cm, pt
\setlength{\headheight}{.35in}

%% Set the space between header and text
\setlength{\headsep}{.25in}

%% Set the header height in: in, mm, cm, pt
%\headheight=in

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Needed for images; used for cover
\usepackage{incgraph,tikz}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Fonts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Use TrueType system fonts
\usepackage{fontspec}

%% Set Main Fonts
\setmainfont[
  Ligatures=TeX,
  ItalicFont={Linux Libertine O:style=Italic},
]{Linux Libertine O}

%\setmainfont[
%  Ligatures=TeX,
%  BoldFont={Aboriginal Serif Bold},
%  ItalicFont={Aboriginal Serif Italic},
%  BoldItalicFont={Aboriginal Serif Bold Italic},
%  SmallCapsFont={TeX Gyre Termes},
%  SmallCapsFeatures={Letters=SmallCaps},
%]{Minion Pro}


%% Make our quotes curly
\defaultfontfeatures{Mapping=tex-text}

%% Provides Creative Commons Icons
\usepackage{ccicons}
\usepackage{tgadventor}
\usepackage[T1]{fontenc}
\newfontfamily\sansfont{TeX Gyre Adventor}


%% Set Minion Pro macro \primaryfont
% This command uses locally installed OTF/TTF Fonts
%\newcommand*{\primaryfont}{\fontspec{Minion Pro}\selectfont}
%\newcommand*{\primaryfontHead}{\fontspec{MinionPro-BoldIt}\selectfont}

% These commands use free TeX fonts from CTAN that are nearly identical
%\usepackage{gentium}
%\usepackage[T1]{fontenc}
%\newfontfamily\primaryfont{Gentium Book Basic}

\usepackage{libertine}
\usepackage[T1]{fontenc}
\newfontfamily\primaryfont[Ligatures=TeX, Numbers=OldStyle]{Linux Libertine O}
\newfontfamily\titlefont[Scale=1.75, Ligatures=TeX]{Linux Libertine O}

%\setmainfont
%\setsansfont
%\setmonofont

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Metadata Settings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Set Title from Yaml Metadata
\title{$title$}

%% Set subtitle if it exists in Yaml Metadata
$if(subtitle)$
\newcommand{\subtitle}{$subtitle$}
$endif$

%% Set author from Yaml Metadata
\author{$for(author)$$author$$endfor$}

%% Set editor from Yaml Metadata
\def\editor{$for(contributors)$$contributors.editor$$endfor$}

%% Set cover artist from Yaml Metadata
\def\artist{$for(contributors)$$contributors.artist$$endfor$}

%% Set designer from Yaml Metadata
\def\designer{$for(contributors)$$contributors.designer$$endfor$}

%% Set publisher from Yaml Metadata
\def\publisher{$for(publisher)$$publisher$$endfor$}

%% Set website from Yaml Metadata
\def\website{$for(website)$$website$$endfor$}

%% Use the year instead of the full date
\date{$year$}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Start the document
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

%% Start with the frontmatter
% These pages don't count for document page numbering
\frontmatter

% TeX 'magic' required so we can use macros/variables with @
\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Create a cover page
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% No headers or footers
\pagestyle{empty}

% The image
\incgraph[documentpaper]
[height=\paperheight]{$cover-image$} % Fit to height

%% Stretch image to fit page
%  [width=\paperwidth,height=\paperheight]{$cover-image$}

% End the page
\clearpage
%%%%%%%%%%%%%%%%%%%%%%
%% Create a titlepage
%%%%%%%%%%%%%%%%%%%%%%
% No headers/footers
\pagestyle{empty}
% Center everything
  \begin{center}

% Add a vertical space
  \vspace*{3cm}
  
% Write the Book Title
  \makeatletter \Huge \bfseries \titlefont \textbf{\textsc{\@title}} \par \makeatother    

% Add a vertical space
  \vspace{1cm}
    
% Write the subtitle
$if(subtitle)$
  \Large \primaryfont $subtitle$ \par
$endif$
    
% Add a vertical space
  \vspace{3cm}
    
% Write the author name
  \Large \normalfont \sansfont  \makeatletter \MakeUppercase\@author \makeatother \par

% Stop centering everythign
  \end{center}

% End the page
\clearpage


%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create a copyright page
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% No headers/footers
\pagestyle{empty}

% Center everything
  \begin{center} 

% Use Sans font
  \sansfont
  
% Write the Book Titlecopyright page
  \makeatletter  \scriptsize \@title \par \makeatother
    
% Write the CC logo, year, and author
    \scriptsize Copyright \ccLogo\ \makeatletter \@date \ \@author \makeatother \par
    \scriptsize Some rights reserved. \par
    
% Add a vertical space
    \vspace{0.3cm}

% Write what country it was published in
    \scriptsize Published in the United States by \par
    
% Write the publisher name
    \scriptsize \publisher \par
    
% Write the website
    \scriptsize \website \par
    
% Add a vertical space
    \vspace{0.3cm}

% Write the specific license name
    \scriptsize This book is distributed under a Creative Commons attribution-NonCommercial-Sharealike 4.0 License. \par

% Add a vertical space
    \vspace{0.3cm}

% Write the Creative Commons Icons
    \ccbyncsa
    
% Stop centering everythign
  \end{center}
       
% Write license text
   \scriptsize
   \noindent \sansfont \linespread{1.0}
    That means you are free:
      \begin{itemize}
        \setlength{\itemsep}{0pt}
        \setlength{\parskip}{0pt}
        \setlength{\parsep}{0pt} 
          \item \textbf{To Share} -- copy and redistribute the material in any medium or format.
         \item \textbf{To Adapt} -- remix, transform, and build upon the material.
      \end{itemize}
    The licensor cannot revoke these freedoms as long as you follow the license terms: \par
      \begin{itemize}
        \setlength{\itemsep}{0pt}
        \setlength{\parskip}{0pt}
        \setlength{\parsep}{0pt}
          \item \textbf{Attribution} -- You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. \par
          \item \textbf{NonCommercial} -- You may not use the material for commercial purposes. \par
          \item \textbf{Share Alike} -- If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. \par
      \end{itemize}
    \textbf No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits.
    
% Add a vertical space
    \vspace{.3cm}
    
% Start centering again
    \begin{center}

% Attribute the cover artist
      $for(contributors)$$if(contributors.artist)$ Cover Artist: \artist \par$endif$$endfor$
      
% Attribute the editor
      $for(contributors)$$if(contributors.editor)$ Editor: \editor \par$endif$$endfor$
      
% Attribute the designer (hey, that's me)
      $for(contributors)$$if(contributors.designer)$ Design: \designer \par$endif$$endfor$
   
% Add a vertical space
      \vspace{0.3cm}
      
% If ISBNs are defined in Yaml front matter then write them here
      $if(paperback-isbn)$Paperback ISBN: {$paperback-isbn$} \par $endif$
      $if(hardcover-isbn)$HardcoverISBN: {$hardcover-isbn$}\par $endif$
      $if(epub-isbn)$ePub ISBN: {$epub-isbn$} \par $endif$
      
% Stop centering
    \end{center}
    
% End the page
\clearpage \normalsize

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create the chapters
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This styles the header/footer for normal 'chapter' pages
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth {\@chapapp\ \thechapter \hspace{1mm}-\hspace{1mm}{#1}}{}}

\fancyfoot[C]{} % remove numbered footers
\fancyhead[RO,LE]{\primaryfont\bfseries \thepage} % add page numbers to the header
\fancyhead[LO]{\primaryfont\bfseries \textsc \leftmark} % add subtitle to header
\fancyhead[RE]{\primaryfont\bfseries \textsc \@title}
\renewcommand{\headrulewidth}{0.0pt} 

% This says to start the page numbering
\mainmatter

% Write the body/chapters
\primaryfont $body$

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Create the bio page
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add blank even page before backmatter starts
\newcommand*\cleartoleftpage{%
  \clearpage
  \ifodd\value{page}\hbox{}\thispagestyle{empty}\newpage\fi
}

\cleartoleftpage

% Label as backmatter
\backmatter

% No headers/footers
\pagestyle{empty}

% Add space before/after the page title
\titlespacing*{\section}
{0pt}%left
{150pt}%top
{25pt}%bottom

% This designs the page title
\titleformat{\section}[display]{\bfseries\titlefont}{}{0.5mm}{\centering \Large} % Don't say 'chapter X'

% Don't indent new paragraphs
\setlength{\parindent}{0mm}

% Add space between paragraphs
\parskip = \baselineskip

% Include the file(s) we specified via the pandoc command, in this case our bio.md
$include-after$
% End the document
\end{document} 
