bard 2.0.1

Creates PDF and HTML songbooks out of easy-to-write Markdown sources.
Documentation
{{!--
 The content of all variables is by default escaped with a TeX
 escaping function similar to how special characters are escaped in HTML.
 The escaping function doesn't escape spaces.
 To preserve spaces, use {{{ pre variable }}}, which replaces spaces
 with the non-breaking space TeX entity '~'.
--}}

{{~ version_check "1.2.0" ~}}

{{!-- Document header --}}

\documentclass[{{output.font_size}}pt,a5paper,openany]{memoir}
\raggedbottom

\usepackage{geometry}
\geometry{
  a5paper,
  top=8mm,
  left=18mm,
  right=21mm,
  bottom=10mm,
%  showframe,
}
\usepackage{fontspec}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage[defaultlines=5,all]{nowidow}
\usepackage[pdfusetitle,colorlinks=true]{hyperref}
\usepackage{float}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{marginnote}
\usepackage{amssymb}

%% Page style / pagination
\makepagestyle{songs}
\newcommand\ThePage\{{ {{~#if output.sans_font }}\sffamily{{/if}}\thepage}}
% Hack: Right pagination is moved right by using the margin
\makeoddfoot{songs}{}{}{\marginnote{\hspace{2mm}\ThePage}}
% Hack: Left pagination is moved left with a kern
\makeevenfoot{songs}{\kern-8mm\ThePage}{}{}
% Patch cleardoublepage to not get blank pages after title & contents pages:
\renewcommand\cleardoublepage{\clearpage}

%% Fonts and colours
\input{./fonts/fonts.tex}
\colorlet{LightRed}{red!65!}
\colorlet{DarkGray}{black!70!}

%% Spacings
\setlength{\parindent}{0pt}
\setlength{\tabcolsep}{0pt}
\setlength{\parskip}{1mm}
\setlength{\footskip}{2mm}

%% ToC style
% Suppress page numbers
\aliaspagestyle{chapter}{empty}
% Hide the title of the ToC:
\renewcommand\tocheadstart{}
\renewcommand\printtoctitle[1]{}
\renewcommand\aftertoctitle{}
% Hide section numbers in the ToC:
\renewcommand\numberline[1]{}
\renewcommand\cftdotsep{1}
{{~#if output.sans_font }}
% sans_font support
\renewcommand{\cftchapterfont}{\normalfont\sffamily}
\renewcommand{\cftsectionfont}{\normalfont\sffamily}
\renewcommand{\cftchapterpagefont}{\normalfont\sffamily}
\renewcommand{\cftsectionpagefont}{\normalfont\sffamily}
{{~/if}}

%% Hyperlinks setup
\hypersetup{
  bookmarks=true,
  linkcolor=.,
  urlcolor=blue,
  pdfcreator={{ program.name }} v. {{ program.version }} - {{ program.homepage }},
}

%% Song title and subtitle formats
\titleformat{\section}
  {\large\bfseries}{}{0pt}{\underline}
\titlespacing*{\section}
  {0pt}{7mm}{0pt}
\newcommand\songtitle[1]{%
  % This is a trick to only layout a song on the current page
  % if it fits, otherwise a pagebreak is inserted
  \FloatBlock
  \vfil
  \pagebreak[2]
  \vfilneg
  \section{#1}
}
\newcommand\subtitle[1]{%
  \emph{#1}
}

%% Verse layout command
\makeatletter
% The verse & label layout code was written by Jonathan P. Spratte
% under the Beerware license: As long as you retain this notice you
% can do whatever you want with this stuff. If we meet some day, and you think
% this stuff is worth it, you can buy me a beer in return. Jonathan P. Spratte
\newlength\verse@indent
\newlength\verse@labelsep
\newlength\verse@vskip
\AtBeginDocument{% setting AtBeginDocument since earlier we can't rely on em being correct
  \verse@indent=9mm
  \verse@labelsep=1mm
  \verse@vskip=\smallskipamount
}
\newcommand\Verse[1]{%
    \par
    \vskip\verse@vskip
    \noindent\kern-\verse@indent
    \sbox0{\textbf{\footnotesize{#1}}}%
    \ifdim\wd0>\dimexpr\verse@indent-\verse@labelsep\relax
      \usebox0\kern\verse@labelsep
    \else
      \makebox[\verse@indent]{\usebox0}%
    \fi
    \ignorespaces
}
\makeatother

{{!-- HB inlines: Custom extensions. You can add your own - see documentation. --}}

{{!-- {{#*inline "h-foo"}}example{{/inline}} --}}

{{!-- HB inlines: Helpers --}}

{{#*inline "verse-label"}}
  {{~#if verse}}{{verse}}.{{/if~}}
  {{~#if (contains this "chorus")}}{{@root.book.chorus_label}}{{chorus}}.{{/if~}} {{!-- contains, because .chords may be falsy --}}
  {{~#if custom}}{{custom}}{{/if~}}
{{/inline}}

{{!-- HB inlines: Block types --}}

{{#*inline "b-verse"~}}
  {{#each paragraphs~}}
    {{#if @first}}\Verse{ {{~>verse-label ../label ~}} }{{/if}} {{#each this}}{{> (lookup this "type") }}{{/each}}

    \vspace{\parskip}

  {{/each}}
{{/inline}}

{{#*inline "b-bullet-list"~}}
  \begin{itemize}[noitemsep]{{#each items}}\item {{ this }}
{{/each}}
  \end{itemize}
{{/inline}}

{{#*inline "b-horizontal-line"}}
  \vphantom{}\hrule
{{/inline}}

{{#*inline "b-pre"}}
  \begin{verbatim}{{{ text }}}\end{verbatim}
{{/inline}}

{{#*inline "b-html-block"}}
  {{~#each inlines}}{{> (lookup this "type") }}{{/each~}}
{{/inline}}

{{!-- HB inlines: Inline types --}}

{{#*inline "i-text"}}{{{ pre text }}}{{/inline}}

{{#*inline "chord-style"~}}
  {{!-- Helper for i-chord for setting styles based on ` vs `` chords --}}
  {{~#if (eq backticks 1) }}\textbf{\sffamily\color{red}{{/if~}}{{~#unless (eq backticks 1) }}\small{\sffamily\color{LightRed}{{/unless~}}
{{~/inline~}}
{{#*inline "i-chord"~}}
  \begin{tabular}[b]{l}
    {{> chord-style}}{ {{~{ pre chord }~}} }}{{#if alt_chord}}\\
    {{> chord-style}}\color{blue}{ {{~{ pre alt_chord }~}} }}{{/if}}{{#unless baseline}}\\
    {{~#each inlines}}{{> (lookup this "type") }}{{/each~}}{{/unless}}\mbox{}\end{tabular}
{{~/inline}}

{{!-- Nb. the i-break element is a line separator, not terminator,
  ie. no i-break after the last inline element. --}}
{{#*inline "i-break"}}\\
{{/inline}}
{{#*inline "i-emph"}}\emph{ {{~#each inlines}}{{> (lookup this "type") }}{{/each~}} }{{/inline}}
{{#*inline "i-strong"}}\textbf{ {{~#each inlines}}{{> (lookup this "type") }}{{/each~}} }{{/inline}}
{{#*inline "i-link"}}\href{ {{~ url ~}} }{ {{~{ pre text }~}} }{{/inline}}
{{#*inline "i-chorus-ref"}}{{ prefix_space }}\emph{ {{~ @root.book.chorus_label }}{{ num }}.}{{/inline}}

{{#*inline "i-image"}}
  {{~#if (eq class "center") }}

    \begin{figure}[H]
      \centering
      \includegraphics[width={{ px2mm width }}mm]{ {{~ path ~}} }
    \end{figure}

  {{/if~}}
  {{~#if (eq class "right") }}
    \hfill\hspace{0pt}\vspace{-1em}
    {
    \begin{wrapfigure}{r}{ {{~ px2mm width }}mm}
      \centering
      \includegraphics[width={{ px2mm width }}mm]{ {{~ path ~}} }
    \end{wrapfigure}
    }
  {{/if~}}
  {{~#unless class }}\includegraphics[width={{ px2mm width }}mm]{ {{~ path ~}} }{{/unless~}}
{{/inline}}

{{#*inline "i-tag"}}{{> (cat "h-" (lookup this "name")) attrs }}{{/inline}}

{{!-- Main content --}}

% Metadata
\title{ {{~ book.title ~}} }

% Document
\begin{document}

{{~#if output.sans_font }}\sffamily{{/if}}

%% Title page
\frontmatter*
\newgeometry{margin=5mm}
\begin{titlingpage*}
  \begin{vplace}[0.5]
    \begin{center}
      \Huge{\textbf{ {{~ book.title ~}} }} \\
      \vspace{0.5cm}
      \LARGE{ {{~ book.subtitle ~}} } \\
      {{#if book.front_img}}
        \vspace{1cm}
        \includegraphics[width={{ px2mm (img_w book.front_img) }}mm]{ {{~ book.front_img ~}} }
      {{/if}}
    \end{center}
  \end{vplace}

  \mbox{}
  \vfill
  \begin{center}\small{ {{~ book.title_note ~}} }\end{center}
\end{titlingpage*}
\restoregeometry

%% Contents page
\pagestyle{empty} % Suppresses ToC continuation page header
\tableofcontents*

%% Songs
\mainmatter*

\pagestyle{songs}
{{#each songs -}}
  %% song {{ @index }}
  \songtitle{ {{~ title ~}} }

  {{#if subtitles ~}}
    {{#each subtitles}}\subtitle{ {{~ this ~}} }{{#unless @last}}\\\{{/unless}}{{/each}}
    \vspace{2mm}
  {{/if}}
  {{#unless subtitles}}\vspace{2mm}{}{{/unless}}

  {{!-- Dispatch to block HB inlines prefixed b- , see above --}}
  {{#each blocks}}{{> (lookup this "type") }}{{/each}}
{{/each}}

\backmatter

\end{document}