\documentclass{standalone}
\usepackage{fontspec}
\usepackage{xcolor}
\definecolor{texsmithLinkColor}{HTML}{0B3D91}
\VAR{extra_packages}

\usepackage[colorlinks=true, linkcolor=texsmithLinkColor, urlcolor=texsmithLinkColor, citecolor=texsmithLinkColor]{hyperref}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\BLOCK{ if border }
\begin{tikzpicture}
  \def\textwidthbox{\VAR{width}}
  \def\margin{\VAR{margin}}
  \def\e{\VAR{dogear}}

  \node[inner sep=\margin, outer sep=0pt] (content) {%
    \begin{minipage}{\textwidthbox}
\BLOCK{ endif }
\VAR{mainmatter}
\VAR{fragment_backmatter}
\BLOCK{ if border }
    \end{minipage}
  };

  \coordinate (A) at (content.north west);
  \coordinate (B) at (content.north east);
  \coordinate (C) at (content.south east);
  \coordinate (D) at (content.south west);

  \coordinate (Bdown) at ($(B)-(0,\e)$);
  \coordinate (Bleft) at ($(B)-(\e,0)$);
  \coordinate (Corner) at ($(B)-(\e,\e)$);

  \draw (A) -- (Bleft) -- (Bdown) -- (C) -- (D) -- cycle;
  \BLOCK{ if dogear }
  \draw (Corner)
   .. controls ($(Corner)+(0.3*\e,0.1*\e)$) and ($(Bdown)+(-0.3*\e,-0.1*\e)$) ..
   (Bdown);
  \draw (Bleft)
   .. controls ($(Bleft)+(0.1*\e,-0.3*\e)$) and ($(Corner)+(-0.1*\e,0.3*\e)$) ..
   (Corner);
  \BLOCK{ endif }
\BLOCK{ endif }
\end{tikzpicture}
\end{document}
