Skip to content

Commit

Permalink
Add --template functionality to -tlatex.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristapsdz committed Dec 5, 2024
1 parent 5244266 commit 12a73b4
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 29 deletions.
43 changes: 19 additions & 24 deletions latex.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
#include "extern.h"

struct latex {
unsigned int oflags; /* same as in lowdown_opts */
struct hentryq headers_used; /* headers we've seen */
ssize_t headers_offs; /* header offset */
size_t footsz; /* current footnote */
unsigned int oflags; /* same as in lowdown_opts */
struct hentryq headers_used; /* headers we've seen */
ssize_t headers_offs; /* header offset */
size_t footsz; /* current footnote */
const char *templ; /* output template */
};

/*
Expand Down Expand Up @@ -663,17 +664,8 @@ rndr_math(struct lowdown_buf *ob,
}

static int
rndr_doc_footer(struct lowdown_buf *ob, const struct latex *st)
{

if (st->oflags & LOWDOWN_STANDALONE)
return HBUF_PUTSL(ob, "\\end{document}\n");
return 1;
}

static int
rndr_doc_header(const struct latex *st, struct lowdown_buf *ob,
const struct lowdown_metaq *mq)
rndr_root(const struct latex *st, struct lowdown_buf *ob,
const struct lowdown_metaq *mq, const struct lowdown_buf *content)
{
const struct lowdown_meta *m;
const char *author = NULL, *title = NULL,
Expand All @@ -682,7 +674,9 @@ rndr_doc_header(const struct latex *st, struct lowdown_buf *ob,
*rcsdate = NULL, *header = NULL;

if (!(st->oflags & LOWDOWN_STANDALONE))
return 1;
return hbuf_putb(ob, content);
if (st->templ != NULL)
return lowdown_template(st->templ, content, ob, mq);

TAILQ_FOREACH(m, mq, entries)
if (strcasecmp(m->key, "author") == 0)
Expand Down Expand Up @@ -789,7 +783,10 @@ rndr_doc_header(const struct latex *st, struct lowdown_buf *ob,
!HBUF_PUTSL(ob, "\\maketitle\n"))
return 0;

return 1;
if (!hbuf_putb(ob, content))
return 0;

return HBUF_PUTSL(ob, "\\end{document}\n");
}

static int
Expand Down Expand Up @@ -850,6 +847,9 @@ rndr(struct lowdown_buf *ob,
if (!rndr_blockcode(ob, &n->rndr_blockcode))
return 0;
break;
case LOWDOWN_DOC_HEADER:
/* Don't output anything for this. */
break;
case LOWDOWN_BLOCKQUOTE:
if (!rndr_blockquote(ob, tmp))
return 0;
Expand All @@ -862,10 +862,6 @@ rndr(struct lowdown_buf *ob,
if (!rndr_definition_title(ob, tmp))
return 0;
break;
case LOWDOWN_DOC_HEADER:
if (!rndr_doc_header(st, ob, mq))
return 0;
break;
case LOWDOWN_META:
if (n->chng != LOWDOWN_CHNG_DELETE &&
!rndr_meta(st, n, mq))
Expand Down Expand Up @@ -972,9 +968,7 @@ rndr(struct lowdown_buf *ob,
return 0;
break;
case LOWDOWN_ROOT:
if (!hbuf_putb(ob, tmp))
return 0;
if (!rndr_doc_footer(ob, st))
if (!rndr_root(st, ob, mq, tmp))
return 0;
break;
default:
Expand Down Expand Up @@ -1022,6 +1016,7 @@ lowdown_latex_new(const struct lowdown_opts *opts)
return NULL;

p->oflags = opts == NULL ? 0 : opts->oflags;
p->templ = opts == NULL ? NULL : opts->templ;
return p;
}

Expand Down
9 changes: 8 additions & 1 deletion man/lowdown-diff.1
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ See
.Sx Templates .
This is experimental functionality.
Currently only for
.Fl t Ns Ar html .
.Fl t Ns Ar html
and
.Fl t Ns Ar latex .
.El
.Pp
What follows are per-output long options.
Expand Down Expand Up @@ -927,6 +929,11 @@ The default template used if
.Fl -template
is not provided to
.Fl t Ns Ar html .
.It Pa share/latex/default.latex
The default template used if
.Fl -template
is not provided to
.Fl t Ns Ar latex .
.It Pa share/odt/styles.xml
Default styles used when generating standalone
.Fl t Ns Ar fodt
Expand Down
9 changes: 8 additions & 1 deletion man/lowdown.1
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ See
.Sx Templates .
This is experimental functionality.
Currently only for
.Fl t Ns Ar html .
.Fl t Ns Ar html
and
.Fl t Ns Ar latex .
.El
.Pp
What follows are per-output long options.
Expand Down Expand Up @@ -920,6 +922,11 @@ The default template used if
.Fl -template
is not provided to
.Fl t Ns Ar html .
.It Pa share/latex/default.latex
The default template used if
.Fl -template
is not provided to
.Fl t Ns Ar latex .
.It Pa share/odt/styles.xml
Default styles used when generating standalone
.Fl t Ns Ar fodt
Expand Down
1 change: 0 additions & 1 deletion regress/diff/metadata-add.latex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
\title{}
\author{kristaps}
\maketitle

\hypertarget{section}{%
\section{section}\label{section}}

Expand Down
1 change: 0 additions & 1 deletion regress/diff/metadata-change.latex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
\title{}
\author{shmistaps}
\maketitle

\hypertarget{section}{%
\section{section}\label{section}}

Expand Down
1 change: 0 additions & 1 deletion regress/diff/metadata-remove.latex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
\usepackage{longtable}
\usepackage{hyperref}
\begin{document}

\hypertarget{section}{%
\section{section}\label{section}}

Expand Down
31 changes: 31 additions & 0 deletions share/latex/default.latex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[11pt,a4paper]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{hyperref}
$latexheader$
\begin{document}
$ifdef(or(title,author,date))$\title{$title.escapelatex$}$endif$
$ifdef(author)$
\author{$author.escapelatex$$ifdef(affiliation)$ \\ $affiliation.escapelatex$$endif$}
$endif$
$ifdef(date)$\date{$date.escapelatex$}$endif$
$ifdef(or(title,author,date))$\maketitle$endif$
$body$
\end{document}

0 comments on commit 12a73b4

Please sign in to comment.