From 12a73b43927d635caa4c5706b6f2af78c52a6c00 Mon Sep 17 00:00:00 2001 From: Kristaps Dz Date: Wed, 4 Dec 2024 22:40:53 -0800 Subject: [PATCH] Add --template functionality to -tlatex. --- latex.c | 43 +++++++++++++----------------- man/lowdown-diff.1 | 9 ++++++- man/lowdown.1 | 9 ++++++- regress/diff/metadata-add.latex | 1 - regress/diff/metadata-change.latex | 1 - regress/diff/metadata-remove.latex | 1 - share/latex/default.latex | 31 +++++++++++++++++++++ 7 files changed, 66 insertions(+), 29 deletions(-) create mode 100644 share/latex/default.latex diff --git a/latex.c b/latex.c index bf72f5f..d1cb9d7 100644 --- a/latex.c +++ b/latex.c @@ -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 */ }; /* @@ -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, @@ -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) @@ -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 @@ -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; @@ -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)) @@ -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: @@ -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; } diff --git a/man/lowdown-diff.1 b/man/lowdown-diff.1 index c09d00b..8d570df 100644 --- a/man/lowdown-diff.1 +++ b/man/lowdown-diff.1 @@ -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. @@ -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 diff --git a/man/lowdown.1 b/man/lowdown.1 index d5278b9..26a8975 100644 --- a/man/lowdown.1 +++ b/man/lowdown.1 @@ -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. @@ -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 diff --git a/regress/diff/metadata-add.latex b/regress/diff/metadata-add.latex index 9ac962b..e11af37 100644 --- a/regress/diff/metadata-add.latex +++ b/regress/diff/metadata-add.latex @@ -23,7 +23,6 @@ \title{} \author{kristaps} \maketitle - \hypertarget{section}{% \section{section}\label{section}} diff --git a/regress/diff/metadata-change.latex b/regress/diff/metadata-change.latex index aa79103..924d605 100644 --- a/regress/diff/metadata-change.latex +++ b/regress/diff/metadata-change.latex @@ -23,7 +23,6 @@ \title{} \author{shmistaps} \maketitle - \hypertarget{section}{% \section{section}\label{section}} diff --git a/regress/diff/metadata-remove.latex b/regress/diff/metadata-remove.latex index 4e31794..9da31c5 100644 --- a/regress/diff/metadata-remove.latex +++ b/regress/diff/metadata-remove.latex @@ -20,7 +20,6 @@ \usepackage{longtable} \usepackage{hyperref} \begin{document} - \hypertarget{section}{% \section{section}\label{section}} diff --git a/share/latex/default.latex b/share/latex/default.latex new file mode 100644 index 0000000..adce60d --- /dev/null +++ b/share/latex/default.latex @@ -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}