include/punk/punk_markdown.h

filter: Statementstmt 98.4% 698 / 714 (5 uncoverable) Branchbran 75.2% 552 / 780 (73 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.3% 1250 / 1494 (78 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_cachefront.h ↑ Summary include/punk/punk_request.h »
1 /* punk_markdown.h - a directory of markdown as a documentation site.
2 *
3 * `markdown '/docs' => 'docs', title => 'Guide'` compiles to a PSGI coderef:
4 * a magic CV carrying the finished site, the closure pattern punk_static.h
5 * uses. The whole site is built at boot - the tree walked, every page
6 * rendered through Markdown::Simple and wrapped by Template::Stencil, the
7 * search index filled - and frozen into a lookup table, so a request is a
8 * hash hit and a triplet with no markdown parse, no template render and no
9 * Perl frame. A bad template or an unreadable directory is a configuration
10 * error and fails at boot with the rest of the configuration, which is the
11 * same stance Open::API::UI takes.
12 *
13 * Two provider ABIs are resolved at runtime here: Markdown::Simple's mds_abi
14 * for rendering, heading anchors and the plain text the index is built from,
15 * and Search::Trigram's sg_abi for search. Both are consumed C to C.
16 *
17 * Rides the existing mount table, so include/punk/punk_serve.h needs no
18 * change: prefix dispatch, longest-prefix ordering and the SCRIPT_NAME /
19 * PATH_INFO rewrite come free, and the mount inherits the documented
20 * behaviour that a mounted app bypasses before/after hooks, guards and CSRF,
21 * exactly as `static` does.
22 *
23 * Include prerequisites: punk_names.h, punk_static.h (punk_closure,
24 * ps_serve_file, ps_plain, ps_path_unsafe, ps_http_date), punk_stencil.h
25 * (punk_st), punk_request.h (pq_parse_pairs), and the Perl headers.
26 *
27 * A note on the parser's globals: Markdown::Simple keeps a file-scope image
28 * stack, so two interpreters must not render concurrently. Every render this
29 * mount performs happens either at boot or under `reload`, both single
30 * threaded and sequential, so that constraint is met and is not something to
31 * work around here.
32 */
33  
34 #ifndef PUNK_MARKDOWN_H
35 #define PUNK_MARKDOWN_H
36  
37 #include "mds_abi.h"
38 #include "sg_abi.h"
39  
40 /* ---- the provider ABIs ---------------------------------------------------
41 * The shape punk_stencil.h established: a static pointer plus a TRIED flag so
42 * a failed probe is not retried, an eval_pv of the require followed by a
43 * SPAGAIN (the require may have reallocated the value stack - the missing
44 * SPAGAIN was a shipped bug in every resolver here once), and a >= gate,
45 * since these tables only ever grow at the tail. The _try twin returns NULL
46 * for a caller that has a fall-back; the plain one croaks naming the version
47 * to upgrade to, because this mount has no fall-back worth having. */
48  
49 static const mds_abi *PUNK_MDS = NULL;
50 static int PUNK_MDS_TRIED = 0;
51  
52 578 static const mds_abi *punk_mds_try(pTHX) {
53 578 ▶ if (!PUNK_MDS && !PUNK_MDS_TRIED) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!PUNK_MDS && !PUNK_MDS_TRIED) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 22 556 578
if (!PUNK_MDS && !PUNK_MDS_TRIED) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 22 0 22
54 22 dSP; int count, ok; IV p = 0;
55 22 PUNK_MDS_TRIED = 1;
56 22 ▶ if (PerlEnv_getenv("PUNK_FAKE_MDS_BAD")) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (PerlEnv_getenv("PUNK_FAKE_MDS_BAD")) return NULL; 1 21 22
57 21 ok = pk_require_once(aTHX_ "Markdown::Simple", FALSE);
58 21 SPAGAIN;
59 /*
60 # uncoverable branch false
61 # uncoverable condition right count:1 class:ignore_covered_err */
62 21 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 21 0 21
63 21 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 21 21
64 21 count = call_pv("Markdown::Simple::_abi_ptr", G_SCALAR | G_EVAL);
65 21 SPAGAIN;
66 /*
67 # uncoverable branch false
68 # uncoverable condition right count:1 class:ignore_covered_err
69 # uncoverable condition right count:2 class:ignore_covered_err */
70 21 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 21 0 21
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 21 0 21
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 21 0 21
71 /*
72 # uncoverable statement
73 # uncoverable branch true
74 # uncoverable branch false
75 # uncoverable condition left count:1
76 # uncoverable condition right count:1 */
77 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
78 21 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 21 0 21
79 /*
80 # uncoverable branch false
81 # uncoverable condition right count:1 class:ignore_covered_err */
82 21 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 21 0 21
83 21 const mds_abi *a = INT2PTR(const mds_abi *, p);
84 /*
85 # uncoverable branch false
86 # uncoverable condition right count:1 class:ignore_covered_err
87 # uncoverable condition right count:2 class:ignore_covered_err */
88 21 ▶ if (a && a->abi_version >= MDS_ABI_VERSION) PUNK_MDS = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= MDS_ABI_VERSION) PUNK_MDS = a; 21 0 21
if (a && a->abi_version >= MDS_ABI_VERSION) PUNK_MDS = a; 21 0 21
89 }
90 }
91 }
92 577 return PUNK_MDS;
93 }
94  
95 572 static const mds_abi *punk_mds(pTHX) {
96 572 const mds_abi *a = punk_mds_try(aTHX);
97 572 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 1 571 572
98 1 croak("Punk: the markdown keyword needs Markdown::Simple 0.20+ "
99 "with its C ABI (mds_abi version %d)", MDS_ABI_VERSION);
100 571 return a;
101 }
102  
103 static const sg_abi *PUNK_SG = NULL;
104 static int PUNK_SG_TRIED = 0;
105  
106 64 static const sg_abi *punk_sg_try(pTHX) {
107 64 ▶ if (!PUNK_SG && !PUNK_SG_TRIED) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!PUNK_SG && !PUNK_SG_TRIED) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 50 64
if (!PUNK_SG && !PUNK_SG_TRIED) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 14 0 14
108 14 dSP; int count, ok; IV p = 0;
109 14 PUNK_SG_TRIED = 1;
110 14 ▶ if (PerlEnv_getenv("PUNK_FAKE_SG_BAD")) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (PerlEnv_getenv("PUNK_FAKE_SG_BAD")) return NULL; 1 13 14
111 13 ok = pk_require_once(aTHX_ "Search::Trigram", FALSE);
112 13 SPAGAIN;
113 /*
114 # uncoverable branch false
115 # uncoverable condition right count:1 class:ignore_covered_err */
116 13 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 13 0 13
117 13 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 13 13
118 13 count = call_pv("Search::Trigram::_abi_ptr", G_SCALAR | G_EVAL);
119 13 SPAGAIN;
120 /*
121 # uncoverable branch false
122 # uncoverable condition right count:1 class:ignore_covered_err
123 # uncoverable condition right count:2 class:ignore_covered_err */
124 13 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 13 0 13
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 13 0 13
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 13 0 13
125 /*
126 # uncoverable statement
127 # uncoverable branch true
128 # uncoverable branch false
129 # uncoverable condition left count:1
130 # uncoverable condition right count:1 */
131 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
132 13 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 13 0 13
133 /*
134 # uncoverable branch false
135 # uncoverable condition right count:1 class:ignore_covered_err */
136 13 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 13 0 13
137 13 const sg_abi *a = INT2PTR(const sg_abi *, p);
138 /*
139 # uncoverable branch false
140 # uncoverable condition right count:1 class:ignore_covered_err
141 # uncoverable condition right count:2 class:ignore_covered_err */
142 13 ▶ if (a && a->abi_version >= SG_ABI_VERSION) PUNK_SG = a;
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->abi_version >= SG_ABI_VERSION) PUNK_SG = a; 13 0 13
if (a && a->abi_version >= SG_ABI_VERSION) PUNK_SG = a; 13 0 13
143 }
144 }
145 }
146 63 return PUNK_SG;
147 }
148  
149 45 static const sg_abi *punk_sg(pTHX) {
150 45 const sg_abi *a = punk_sg_try(aTHX);
151 45 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 1 44 45
152 1 croak("Punk: markdown search needs Search::Trigram 0.02+ with its "
153 "C ABI (sg_abi version %d); pass search => 0 to do without it",
154 SG_ABI_VERSION);
155 44 return a;
156 }
157  
158 /* ---- slots ---------------------------------------------------------------
159 * A page and the mount's capture are both AVs with a fixed slot enum, the way
160 * Punk::Context (PCX_*) and Punk::View::Stencil (PVS_*) do it: refcount
161 * managed for free and cloneable under ithreads, which a C struct behind an
162 * IV would not be. */
163  
164 enum { PMD_URL = 0, /* mount-relative, no trailing slash; "" is the root */
165 PMD_FILE, /* absolute source path */
166 PMD_TITLE, /* page title */
167 PMD_NAV, /* sidebar label, usually the title */
168 PMD_SECTION, /* top-level directory, "" at the root */
169 PMD_ORDER, /* front matter order:, else IV_MAX */
170 PMD_BODY, /* the finished response bytes for the whole page */
171 PMD_MTIME,
172 PMD_DOCID, /* the Search::Trigram document id */
173 PMD_PLAIN, /* plain text, for snippets */
174 PMD_META, /* RV to the front-matter HV, every key as written */
175 PMD_DATE, /* front matter date:, as an epoch, else undef */
176 PMD_UPDATED, /* updated:, else the date, else undef */
177 PMD_SECLABEL, /* the section's sidebar label */
178 PMD_SECORDER, /* the section's place among the sections */
179 PMD__COUNT };
180  
181 enum { PMDC_DIR = 0, /* the docs root, no trailing slash */
182 PMDC_PAGES, /* HV: url -> the page AV above */
183 PMDC_ORDER, /* AV of urls, in nav order */
184 PMDC_OPTS, /* HV of resolved options */
185 PMDC_STENCIL, /* the Template::Stencil object */
186 PMDC_MD, /* the Markdown::Simple object */
187 PMDC_INDEX, /* the Search::Trigram object, or undef */
188 PMDC_DOCMAP, /* HV: doc_id -> url */
189 PMDC_PREFIX, /* the mount prefix, for building hrefs */
190 PMDC_NOTFOUND, /* the pre-rendered 404 body */
191 PMDC_CSS, /* the shipped stylesheet, slurped at boot */
192 PMDC__COUNT };
193  
194 /* ---- small helpers -------------------------------------------------------- */
195  
196 11516 static SV *pmd_opt(pTHX_ HV *opts, const char *k) {
197 11516 ▶ SV **v = opts ? hv_fetch(opts, k, (I32)strlen(k), 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **v = opts ? hv_fetch(opts, k, (I32)strlen(k), 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 11516 0 11516
198 /*
199 # uncoverable condition right count:2 class:ignore_covered_err
200 # uncoverable condition right count:3 class:ignore_covered_err */
201 11516 ▶ return (v && *v && SvOK(*v)) ? *v : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (v && *v && SvOK(*v)) ? *v : NULL; 793 10723 11516
return (v && *v && SvOK(*v)) ? *v : NULL; 793 0 793
return (v && *v && SvOK(*v)) ? *v : NULL; 793 0 793
202 }
203  
204 999 static int pmd_opt_bool(pTHX_ HV *opts, const char *k, int dflt) {
205 999 SV *v = pmd_opt(aTHX_ opts, k);
206 999 ▶ return v ? (SvTRUE(v) ? 1 : 0) : dflt;
Branch: 1 branch
BranchTrueFalseTotal
return v ? (SvTRUE(v) ? 1 : 0) : dflt; 97 902 999
207 }
208  
209 1818 static const char *pmd_opt_str(pTHX_ HV *opts, const char *k,
210 const char *dflt) {
211 1818 SV *v = pmd_opt(aTHX_ opts, k);
212 1847 ▶ return v ? SvPV_nolen(v) : dflt;
Branch: 1 branch
BranchTrueFalseTotal
return v ? SvPV_nolen(v) : dflt; 29 1789 1818
213 }
214  
215 /* Read a file whole, as bytes. The UTF-8 flag stays off deliberately: a PSGI
216 * body must be octets or Content-Length will not match what goes on the wire,
217 * and the parser is byte-oriented throughout. Returns a mortal SV, or NULL. */
218 1247 static SV *pmd_slurp(pTHX_ const char *path) {
219 1247 PerlIO *fp = PerlIO_open(path, "rb");
220 SV *out;
221 char buf[8192];
222 SSize_t got;
223 /*
224 # uncoverable branch true class:ignore_covered_err
225 # uncoverable condition left count:1 class:ignore_covered_err */
226 1247 ▶ if (!fp) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return NULL; 1 1246 1247
227 1246 out = sv_2mortal(newSVpvs(""));
228 2480 ▶ while ((got = PerlIO_read(fp, buf, sizeof buf)) > 0)
Branch: 1 branch
BranchTrueFalseTotal
while ((got = PerlIO_read(fp, buf, sizeof buf)) > 0) 1234 1246 2480
229 1234 sv_catpvn(out, buf, (STRLEN)got);
230 1246 PerlIO_close(fp);
231 1246 return out;
232 }
233  
234 /* Append `n` bytes of `s` to `out` with the five HTML metacharacters escaped.
235 * Used for everything the mount puts into its own markup: titles in the nav,
236 * the query echoed on the search page. The rendered markdown is not escaped
237 * here - Markdown::Simple has already made it safe. */
238 9404 static void pmd_esc(pTHX_ SV *out, const char *s, STRLEN n) {
239 9404 punk_html_escape(aTHX_ out, s, n);
240 9404 }
241  
242 /* The same, with runs of whitespace collapsed to a single space and the
243 * leading run dropped. Search snippets are cut out of a document's plain
244 * text, which still carries its paragraph breaks; a snippet is one line. */
245 11 static void pmd_esc_collapsed(pTHX_ SV *out, const char *s, STRLEN n) {
246 11 STRLEN i = 0, run = 0;
247 11 int started = 0;
248 /*
249 # uncoverable condition right count:1 class:ignore_covered_err
250 # uncoverable condition left count:2 class:ignore_covered_err
251 # uncoverable condition left count:3 class:ignore_covered_err
252 # uncoverable condition left count:5 class:ignore_covered_err */
253 17 ▶ while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' ||
Branch: 4 branches
BranchTrueFalseTotal
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 17 0 17
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 0 17 17
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 0 17 17
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 6 11 17
254 17 ▶ s[i] == '\r')) i++;
Branch: 1 branch
BranchTrueFalseTotal
s[i] == '\r')) i++; 0 11 11
255 809 ▶ for (; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < n; i++) { 798 11 809
256 798 char c = s[i];
257 /*
258 # uncoverable condition left count:2 class:ignore_covered_err
259 # uncoverable condition left count:4 class:ignore_covered_err */
260 798 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 702 96 798
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 702 0 702
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 637 65 702
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 0 637 637
261 161 ▶ if (run) { pmd_esc(aTHX_ out, s + i - run, run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { pmd_esc(aTHX_ out, s + i - run, run); run = 0; } 125 36 161
262 161 started = 1;
263 161 continue;
264 }
265 637 ▶ if (started && !run) sv_catpvs(out, " ");
Branch: 2 branches
BranchTrueFalseTotal
if (started && !run) sv_catpvs(out, " "); 556 81 637
if (started && !run) sv_catpvs(out, " "); 114 442 556
266 637 run++;
267 }
268 /*
269 # uncoverable branch true
270 # uncoverable condition left count:1 class:ignore_covered_err */
271 11 ▶ if (run) pmd_esc(aTHX_ out, s + n - run, run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) pmd_esc(aTHX_ out, s + n - run, run); 0 11 11
272 11 }
273  
274 6590 static void pmd_esc_sv(pTHX_ SV *out, SV *in) {
275 STRLEN n;
276 6590 const char *s = SvPV_const(in, n);
277 6590 pmd_esc(aTHX_ out, s, n);
278 6590 }
279  
280 /* How many of `n` bytes to keep so that at most `max` survive and a UTF-8
281 * sequence is not cut in half. Shared by the search snippets and the page
282 * summaries. */
283 72 static STRLEN pmd_cut(const char *s, STRLEN n, STRLEN max) {
284 72 STRLEN take = n < max ? n : max;
285 80 ▶ while (take && take < n && ((unsigned char)s[take] & 0xC0) == 0x80) take--;
Branch: 3 branches
BranchTrueFalseTotal
while (take && take < n && ((unsigned char)s[take] & 0xC0) == 0x80) take--; 68 12 80
while (take && take < n && ((unsigned char)s[take] & 0xC0) == 0x80) take--; 16 52 68
while (take && take < n && ((unsigned char)s[take] & 0xC0) == 0x80) take--; 8 8 16
286 72 return take;
287 }
288  
289 /* Runs of whitespace collapsed to one space, the leading run dropped, NOT
290 * escaped: for a summary handed to Perl, which escapes where it prints. */
291 61 static void pmd_collapsed(pTHX_ SV *out, const char *s, STRLEN n) {
292 61 STRLEN i = 0;
293 61 int space = 0;
294 /*
295 # uncoverable branch true count:2
296 # uncoverable branch true class:ignore_covered_err
297 # uncoverable condition left count:1 class:ignore_covered_err
298 # uncoverable condition right count:1 class:ignore_covered_err
299 # uncoverable condition left count:2 class:ignore_covered_err
300 # uncoverable condition left count:3 class:ignore_covered_err
301 # uncoverable condition left count:4 class:ignore_covered_err
302 # uncoverable condition left count:5 class:ignore_covered_err */
303 61 ▶ while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' ||
Branch: 4 branches
BranchTrueFalseTotal
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 61 0 61
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 0 61 61
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 0 61 61
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || 0 61 61
304 61 ▶ s[i] == '\r')) i++;
Branch: 1 branch
BranchTrueFalseTotal
s[i] == '\r')) i++; 0 61 61
305 3924 ▶ for (; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i < n; i++) { 3863 61 3924
306 3863 char c = s[i];
307 /*
308 # uncoverable condition left count:2 class:ignore_covered_err
309 # uncoverable condition left count:4 class:ignore_covered_err */
310 3863 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { space = 1; continue; }
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { space = 1; continue; } 3729 134 3863
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { space = 1; continue; } 3729 0 3729
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { space = 1; continue; } 3554 175 3729
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { space = 1; continue; } 0 3554 3554
311 3554 ▶ if (space) { sv_catpvs(out, " "); space = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (space) { sv_catpvs(out, " "); space = 0; } 191 3363 3554
312 3554 sv_catpvn(out, &c, 1);
313 }
314 61 }
315  
316 /* ---- front matter ---------------------------------------------------------
317 * A restricted `---\n key: value \n---\n` block at byte 0. Scalar values
318 * only, and the first line that is not `key: value` ends the block. This is
319 * deliberately not YAML: running a YAML parser per page at boot to read two
320 * scalars would put a dependency on the C path for no benefit. Unknown keys
321 * are ignored rather than an error, so a document carrying front matter for
322 * some other tool still renders, and every key, recognised or not, reaches
323 * the templates as `meta`.
324 *
325 * Returns the offset of the body, and fills `meta` with every key it read. */
326 956 static STRLEN pmd_front_matter(pTHX_ const char *s, STRLEN n, HV *meta) {
327 956 STRLEN i = 0, line;
328 956 ▶ if (n < 4 || memNE(s, "---\n", 4)) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n < 4 || memNE(s, "---\n", 4)) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 956 0 956
if (n < 4 || memNE(s, "---\n", 4)) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 448 508 956
329 508 i = 4;
330 /*
331 # uncoverable branch false
332 # uncoverable condition right count:1 class:ignore_covered_err */
333 1459 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 1459 0 1459
334 STRLEN k0, kend, v0, vend;
335 /* end of block? */
336 1459 ▶ if (n - i >= 4 && memEQ(s + i, "---\n", 4)) return i + 4; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n - i >= 4 && memEQ(s + i, "---\n", 4)) return i + 4; /* # uncoverable condition right count:1 class:ignore_covered_err */ 1459 0 1459
if (n - i >= 4 && memEQ(s + i, "---\n", 4)) return i + 4; /* # uncoverable condition right count:1 class:ignore_covered_err */ 502 957 1459
337 /*
338 # uncoverable branch true
339 # uncoverable condition left count:1 class:ignore_covered_err
340 # uncoverable condition left count:2 class:ignore_covered_err
341 # uncoverable condition right count:2 class:ignore_covered_err */
342 957 ▶ if (n - i == 3 && memEQ(s + i, "---", 3)) return n;
Branch: 2 branches
BranchTrueFalseTotal
if (n - i == 3 && memEQ(s + i, "---", 3)) return n; 0 957 957
if (n - i == 3 && memEQ(s + i, "---", 3)) return n; 0 0 0
343 957 line = i;
344 15752 ▶ while (line < n && s[line] != '\n') line++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (line < n && s[line] != '\n') line++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 15752 0 15752
while (line < n && s[line] != '\n') line++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 14795 957 15752
345 /* key: value */
346 957 k0 = i;
347 957 kend = k0;
348 5834 ▶ while (kend < line && s[kend] != ':') kend++;
Branch: 2 branches
BranchTrueFalseTotal
while (kend < line && s[kend] != ':') kend++; 5828 6 5834
while (kend < line && s[kend] != ':') kend++; 4877 951 5828
349 957 ▶ if (kend >= line) return 0; /* not a mapping: not ours */
Branch: 1 branch
BranchTrueFalseTotal
if (kend >= line) return 0; /* not a mapping: not ours */ 6 951 957
350 951 v0 = kend + 1;
351 /*
352 # uncoverable condition right count:1 class:ignore_covered_err
353 # uncoverable condition left count:3 class:ignore_covered_err */
354 1902 ▶ while (v0 < line && (s[v0] == ' ' || s[v0] == '\t')) v0++;
Branch: 3 branches
BranchTrueFalseTotal
while (v0 < line && (s[v0] == ' ' || s[v0] == '\t')) v0++; 1902 0 1902
while (v0 < line && (s[v0] == ' ' || s[v0] == '\t')) v0++; 951 951 1902
while (v0 < line && (s[v0] == ' ' || s[v0] == '\t')) v0++; 0 951 951
355 951 vend = line;
356 /*
357 # uncoverable branch true count:2
358 # uncoverable branch true class:ignore_covered_err
359 # uncoverable condition left count:1 class:ignore_covered_err
360 # uncoverable condition right count:1 class:ignore_covered_err
361 # uncoverable condition left count:2 class:ignore_covered_err
362 # uncoverable condition left count:3 class:ignore_covered_err
363 # uncoverable condition left count:4 class:ignore_covered_err */
364 951 ▶ while (vend > v0 && (s[vend - 1] == ' ' || s[vend - 1] == '\t' ||
Branch: 3 branches
BranchTrueFalseTotal
while (vend > v0 && (s[vend - 1] == ' ' || s[vend - 1] == '\t' || 951 0 951
while (vend > v0 && (s[vend - 1] == ' ' || s[vend - 1] == '\t' || 0 951 951
while (vend > v0 && (s[vend - 1] == ' ' || s[vend - 1] == '\t' || 0 951 951
365 951 ▶ s[vend - 1] == '\r')) vend--;
Branch: 1 branch
BranchTrueFalseTotal
s[vend - 1] == '\r')) vend--; 0 951 951
366 /* strip one layer of matching quotes */
367 /*
368 # uncoverable condition right count:4 class:ignore_covered_err */
369 951 ▶ if (vend - v0 >= 2 && (s[v0] == '"' || s[v0] == '\'') &&
Branch: 3 branches
BranchTrueFalseTotal
if (vend - v0 >= 2 && (s[v0] == '"' || s[v0] == '\'') && 645 306 951
if (vend - v0 >= 2 && (s[v0] == '"' || s[v0] == '\'') && 635 10 645
if (vend - v0 >= 2 && (s[v0] == '"' || s[v0] == '\'') && 6 629 635
370 16 ▶ s[vend - 1] == s[v0]) { v0++; vend--; }
Branch: 1 branch
BranchTrueFalseTotal
s[vend - 1] == s[v0]) { v0++; vend--; } 16 0 16
371 /*
372 # uncoverable branch false
373 # uncoverable condition right count:1 class:ignore_covered_err */
374 951 ▶ if (kend > k0)
Branch: 1 branch
BranchTrueFalseTotal
if (kend > k0) 951 0 951
375 951 (void)hv_store(meta, s + k0, (I32)(kend - k0),
376 newSVpvn(s + v0, vend - v0), 0);
377 951 i = line + 1;
378 }
379 0 return 0; /* never closed: not ours */ /* # uncoverable statement */
380 }
381  
382 /* ---- dates ----------------------------------------------------------------
383 * `date:` and `updated:` in front matter: YYYY-MM-DD (midnight UTC), or
384 * YYYY-MM-DDTHH:MM[:SS] followed by Z or a +HH:MM / -HH:MM offset. Parsed by
385 * hand rather than through strptime/timegm, which Windows lacks, with the
386 * days-from-civil arithmetic (proleptic Gregorian) doing the calendar. */
387  
388 143 static IV pmd_days_from_civil(IV y, IV m, IV d) {
389 IV era, yoe, doy, doe;
390 143 y -= m <= 2;
391 143 ▶ era = (y >= 0 ? y : y - 399) / 400; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
era = (y >= 0 ? y : y - 399) / 400; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 143 143
392 143 yoe = y - era * 400;
393 143 ▶ doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1;
Branch: 1 branch
BranchTrueFalseTotal
doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; 131 12 143
394 143 doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
395 143 return era * 146097 + doe - 719468;
396 }
397  
398 250 static void pmd_civil_from_days(IV z, IV *y, IV *m, IV *d) {
399 IV era, doe, yoe, doy, mp;
400 250 z += 719468;
401 250 ▶ era = (z >= 0 ? z : z - 146096) / 146097; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
era = (z >= 0 ? z : z - 146096) / 146097; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 250 250
402 250 doe = z - era * 146097;
403 250 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
404 250 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
405 250 mp = (5 * doy + 2) / 153;
406 250 *d = doy - (153 * mp + 2) / 5 + 1;
407 250 ▶ *m = mp + (mp < 10 ? 3 : -9);
Branch: 1 branch
BranchTrueFalseTotal
*m = mp + (mp < 10 ? 3 : -9); 228 22 250
408 250 *y = yoe + era * 400 + (*m <= 2);
409 250 }
410  
411 585 static int pmd_digits(const char *s, int n, IV *out) {
412 int i;
413 585 IV v = 0;
414 2065 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1480 585 2065
415 /*
416 # uncoverable branch true
417 # uncoverable condition left count:1 class:ignore_covered_err
418 # uncoverable condition left count:2 class:ignore_covered_err */
419 1480 ▶ if (s[i] < '0' || s[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] < '0' || s[i] > '9') return 0; 1480 0 1480
if (s[i] < '0' || s[i] > '9') return 0; 0 1480 1480
420 1480 v = v * 10 + (s[i] - '0');
421 }
422 585 *out = v;
423 585 return 1;
424 }
425  
426 /* Returns 1 and the epoch in *out, or 0 for anything that is not one of the
427 * forms above, including an impossible day (2026-02-30) and an epoch past
428 * what an IV holds on this perl. */
429 160 static int pmd_parse_date(const char *s, STRLEN n, IV *out) {
430 160 IV y, mo, d, hh = 0, mi = 0, ss = 0, off = 0, days;
431 static const int mdays[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
432 NV secs;
433 160 ▶ if (n < 10 || s[4] != '-' || s[7] != '-') return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (n < 10 || s[4] != '-' || s[7] != '-') return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 157 3 160
if (n < 10 || s[4] != '-' || s[7] != '-') return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 155 2 157
if (n < 10 || s[4] != '-' || s[7] != '-') return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 155 155
434 /*
435 # uncoverable branch true
436 # uncoverable condition left count:1 class:ignore_covered_err
437 # uncoverable condition left count:2 class:ignore_covered_err
438 # uncoverable condition left count:3 class:ignore_covered_err */
439 155 ▶ if (!pmd_digits(s, 4, &y) || !pmd_digits(s + 5, 2, &mo)
Branch: 2 branches
BranchTrueFalseTotal
if (!pmd_digits(s, 4, &y) || !pmd_digits(s + 5, 2, &mo) 155 0 155
if (!pmd_digits(s, 4, &y) || !pmd_digits(s + 5, 2, &mo) 155 0 155
440 155 ▶ || !pmd_digits(s + 8, 2, &d)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
|| !pmd_digits(s + 8, 2, &d)) return 0; 0 155 155
441 155 ▶ if (mo < 1 || mo > 12 || d < 1) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (mo < 1 || mo > 12 || d < 1) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 154 1 155
if (mo < 1 || mo > 12 || d < 1) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 152 2 154
if (mo < 1 || mo > 12 || d < 1) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 152 152
442 157 ▶ if (d > mdays[mo - 1] + (mo == 2 && ((y % 4 == 0 && y % 100 != 0)
Branch: 4 branches
BranchTrueFalseTotal
if (d > mdays[mo - 1] + (mo == 2 && ((y % 4 == 0 && y % 100 != 0) 6 146 152
if (d > mdays[mo - 1] + (mo == 2 && ((y % 4 == 0 && y % 100 != 0) 4 2 6
if (d > mdays[mo - 1] + (mo == 2 && ((y % 4 == 0 && y % 100 != 0) 3 1 4
if (d > mdays[mo - 1] + (mo == 2 && ((y % 4 == 0 && y % 100 != 0) 3 149 152
443 5 ▶ || y % 400 == 0)))
Branch: 1 branch
BranchTrueFalseTotal
|| y % 400 == 0))) 2 3 5
444 3 return 0;
445 149 ▶ if (n > 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 10) { 34 115 149
446 34 STRLEN i = 16;
447 /*
448 # uncoverable condition left count:2 class:ignore_covered_err
449 # uncoverable condition left count:3 class:ignore_covered_err
450 # uncoverable condition left count:4 class:ignore_covered_err */
451 34 ▶ if (n < 17 || (s[10] != 'T' && s[10] != ' ') || s[13] != ':')
Branch: 4 branches
BranchTrueFalseTotal
if (n < 17 || (s[10] != 'T' && s[10] != ' ') || s[13] != ':') 32 2 34
if (n < 17 || (s[10] != 'T' && s[10] != ' ') || s[13] != ':') 2 30 32
if (n < 17 || (s[10] != 'T' && s[10] != ' ') || s[13] != ':') 2 0 2
if (n < 17 || (s[10] != 'T' && s[10] != ' ') || s[13] != ':') 0 32 32
452 2 return 0;
453 /*
454 # uncoverable branch true
455 # uncoverable condition left count:1 class:ignore_covered_err
456 # uncoverable condition left count:2 class:ignore_covered_err */
457 32 ▶ if (!pmd_digits(s + 11, 2, &hh) || !pmd_digits(s + 14, 2, &mi))
Branch: 2 branches
BranchTrueFalseTotal
if (!pmd_digits(s + 11, 2, &hh) || !pmd_digits(s + 14, 2, &mi)) 32 0 32
if (!pmd_digits(s + 11, 2, &hh) || !pmd_digits(s + 14, 2, &mi)) 0 32 32
458 0 return 0; /* # uncoverable statement */
459 32 ▶ if (n > 16 && s[16] == ':') { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (n > 16 && s[16] == ':') { /* # uncoverable condition right count:1 class:ignore_covered_err */ 32 0 32
if (n > 16 && s[16] == ':') { /* # uncoverable condition right count:1 class:ignore_covered_err */ 20 12 32
460 /*
461 # uncoverable branch true
462 # uncoverable condition left count:1 class:ignore_covered_err
463 # uncoverable condition left count:2 class:ignore_covered_err */
464 20 ▶ if (n < 19 || !pmd_digits(s + 17, 2, &ss)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 19 || !pmd_digits(s + 17, 2, &ss)) return 0; 20 0 20
if (n < 19 || !pmd_digits(s + 17, 2, &ss)) return 0; 0 20 20
465 20 i = 19;
466 }
467 32 ▶ if (hh > 23 || mi > 59 || ss > 59) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (hh > 23 || mi > 59 || ss > 59) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 31 1 32
if (hh > 23 || mi > 59 || ss > 59) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 30 1 31
if (hh > 23 || mi > 59 || ss > 59) return 0; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 30 30
468 /*
469 # uncoverable branch true
470 # uncoverable condition left count:1 class:ignore_covered_err */
471 30 ▶ if (i == n) return 0; /* a time needs its zone */
Branch: 1 branch
BranchTrueFalseTotal
if (i == n) return 0; /* a time needs its zone */ 0 30 30
472 30 ▶ if (s[i] == 'Z' && i + 1 == n) off = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] == 'Z' && i + 1 == n) off = 0; 11 19 30
if (s[i] == 'Z' && i + 1 == n) off = 0; 10 1 11
473 20 ▶ else if ((s[i] == '+' || s[i] == '-') && i + 6 == n /* # uncoverable condition right count:4 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
else if ((s[i] == '+' || s[i] == '-') && i + 6 == n /* # uncoverable condition right count:4 class:ignore_covered_err */ 13 7 20
else if ((s[i] == '+' || s[i] == '-') && i + 6 == n /* # uncoverable condition right count:4 class:ignore_covered_err */ 12 1 13
else if ((s[i] == '+' || s[i] == '-') && i + 6 == n /* # uncoverable condition right count:4 class:ignore_covered_err */ 18 1 19
474 18 ▶ && s[i + 3] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
&& s[i + 3] == ':') { 18 0 18
475 IV oh, om;
476 /*
477 # uncoverable branch true
478 # uncoverable condition left count:1 class:ignore_covered_err
479 # uncoverable condition left count:2 class:ignore_covered_err
480 # uncoverable condition left count:3 class:ignore_covered_err
481 # uncoverable condition left count:4 class:ignore_covered_err */
482 18 ▶ if (!pmd_digits(s + i + 1, 2, &oh)
Branch: 1 branch
BranchTrueFalseTotal
if (!pmd_digits(s + i + 1, 2, &oh) 18 0 18
483 18 ▶ || !pmd_digits(s + i + 4, 2, &om)
Branch: 1 branch
BranchTrueFalseTotal
|| !pmd_digits(s + i + 4, 2, &om) 18 0 18
484 18 ▶ || oh > 23 || om > 59) return 0;
Branch: 2 branches
BranchTrueFalseTotal
|| oh > 23 || om > 59) return 0; 18 0 18
|| oh > 23 || om > 59) return 0; 0 18 18
485 18 ▶ off = (oh * 3600 + om * 60) * (s[i] == '-' ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
off = (oh * 3600 + om * 60) * (s[i] == '-' ? -1 : 1); 12 6 18
486 }
487 2 else return 0;
488 }
489 143 days = pmd_days_from_civil(y, mo, d);
490 143 secs = (NV)days * 86400.0 + (NV)(hh * 3600 + mi * 60 + ss) - (NV)off;
491 /*
492 # uncoverable branch true
493 # uncoverable condition left count:1 class:ignore_covered_err
494 # uncoverable condition left count:2 class:ignore_covered_err */
495 143 ▶ if (secs > (NV)IV_MAX || secs < (NV)IV_MIN) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (secs > (NV)IV_MAX || secs < (NV)IV_MIN) return 0; 143 0 143
if (secs > (NV)IV_MAX || secs < (NV)IV_MIN) return 0; 0 143 143
496 143 *out = (IV)secs;
497 143 return 1;
498 }
499  
500 /* An epoch as YYYY-MM-DDTHH:MM:SSZ, for <time datetime> and article:*_time. */
501 130 static SV *pmd_iso(pTHX_ IV t) {
502 130 IV days = t / 86400, rem = t % 86400, y, m, d;
503 /*
504 # uncoverable branch true
505 # uncoverable condition left count:1 class:ignore_covered_err */
506 130 ▶ if (rem < 0) { rem += 86400; days--; }
Branch: 1 branch
BranchTrueFalseTotal
if (rem < 0) { rem += 86400; days--; } 0 130 130
507 130 pmd_civil_from_days(days, &y, &m, &d);
508 130 return newSVpvf("%04" IVdf "-%02" IVdf "-%02" IVdf "T%02" IVdf ":%02"
509 IVdf ":%02" IVdf "Z", y, m, d, rem / 3600,
510 (rem % 3600) / 60, rem % 60);
511 }
512  
513 /* An epoch as "19 October 2026", in UTC. English only: a site wanting
514 * another language has the epoch. */
515 120 static SV *pmd_human(pTHX_ IV t) {
516 static const char *const names[] = { "January", "February", "March",
517 "April", "May", "June", "July", "August", "September", "October",
518 "November", "December" };
519 120 IV days = t / 86400, y, m, d;
520 /*
521 # uncoverable branch true
522 # uncoverable condition left count:1 class:ignore_covered_err */
523 120 ▶ if (t % 86400 < 0) days--;
Branch: 1 branch
BranchTrueFalseTotal
if (t % 86400 < 0) days--; 0 120 120
524 120 pmd_civil_from_days(days, &y, &m, &d);
525 120 return newSVpvf("%" IVdf " %s %" IVdf, d, names[m - 1], y);
526 }
527  
528 /* Read date: and updated: from a page's front matter into its slots. A value
529 * that does not parse croaks naming the file, at boot with the rest of the
530 * configuration; under `reload` the same croak reaches the developer as an
531 * error page on the request that read the bad edit. */
532 888 static void pmd_page_dates(pTHX_ AV *page, HV *meta) {
533 888 SV *v, *file = *av_fetch(page, PMD_FILE, 0);
534 IV t;
535 888 int have = 0;
536 888 ▶ if ((v = pmd_opt(aTHX_ meta, "date")) && SvCUR(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pmd_opt(aTHX_ meta, "date")) && SvCUR(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 134 754 888
if ((v = pmd_opt(aTHX_ meta, "date")) && SvCUR(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 134 0 134
537 STRLEN l;
538 134 const char *p = SvPV_const(v, l);
539 134 ▶ if (!pmd_parse_date(p, l, &t))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmd_parse_date(p, l, &t)) 15 119 134
540 15 croak("Punk::Mount::Markdown: %s has date '%s', which is not "
541 "YYYY-MM-DD or ISO-8601", SvPV_nolen(file), p);
542 119 av_store(page, PMD_DATE, newSViv(t));
543 119 have = 1;
544 }
545 754 else av_store(page, PMD_DATE, newSV(0));
546 897 ▶ if ((v = pmd_opt(aTHX_ meta, "updated")) && SvCUR(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pmd_opt(aTHX_ meta, "updated")) && SvCUR(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 26 847 873
if ((v = pmd_opt(aTHX_ meta, "updated")) && SvCUR(v)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 26 0 26
547 STRLEN l;
548 26 const char *p = SvPV_const(v, l);
549 IV u;
550 26 ▶ if (!pmd_parse_date(p, l, &u))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmd_parse_date(p, l, &u)) 2 24 26
551 2 croak("Punk::Mount::Markdown: %s has updated '%s', which is not "
552 "YYYY-MM-DD or ISO-8601", SvPV_nolen(file), p);
553 24 av_store(page, PMD_UPDATED, newSViv(u));
554 }
555 847 ▶ else av_store(page, PMD_UPDATED, have ? newSViv(t) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
else av_store(page, PMD_UPDATED, have ? newSViv(t) : newSV(0)); 94 753 847
556 871 }
557  
558 /* The `sort => 'date'` key: copied out of the page AVs so the comparator is
559 * plain C and needs no interpreter. */
560 typedef struct {
561 int dated;
562 IV date;
563 const char *title, *url;
564 STRLEN tlen, ulen;
565 SV *sv;
566 } pmd_sortkey;
567  
568 6 static int pmd_bytecmp(const char *a, STRLEN al, const char *b, STRLEN bl) {
569 6 int c = memcmp(a, b, al < bl ? al : bl);
570 /*
571 # uncoverable branch false
572 # uncoverable condition right count:1 class:ignore_covered_err */
573 6 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 6 0 6
574 /*
575 # uncoverable statement
576 # uncoverable condition left count:1
577 # uncoverable condition right count:1 */
578 0 ▶ return al < bl ? -1 : al > bl ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
return al < bl ? -1 : al > bl ? 1 : 0; 0 0 0
579 }
580  
581 46 static int pmd_sortkey_cmp(const void *pa, const void *pb) {
582 46 const pmd_sortkey *a = (const pmd_sortkey *)pa;
583 46 const pmd_sortkey *b = (const pmd_sortkey *)pb;
584 int c;
585 46 ▶ if (a->dated != b->dated) return a->dated ? -1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
if (a->dated != b->dated) return a->dated ? -1 : 1; 17 29 46
if (a->dated != b->dated) return a->dated ? -1 : 1; 8 9 17
586 29 ▶ if (a->dated && a->date != b->date) return a->date > b->date ? -1 : 1;
Branch: 3 branches
BranchTrueFalseTotal
if (a->dated && a->date != b->date) return a->date > b->date ? -1 : 1; 25 4 29
if (a->dated && a->date != b->date) return a->date > b->date ? -1 : 1; 23 2 25
if (a->dated && a->date != b->date) return a->date > b->date ? -1 : 1; 12 11 23
587 6 ▶ if (a->dated) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->dated) { 2 4 6
588 2 c = pmd_bytecmp(a->title, a->tlen, b->title, b->tlen);
589 /*
590 # uncoverable branch false
591 # uncoverable condition right count:1 class:ignore_covered_err */
592 2 ▶ if (c) return c;
Branch: 1 branch
BranchTrueFalseTotal
if (c) return c; 2 0 2
593 }
594 4 return pmd_bytecmp(a->url, a->ulen, b->url, b->ulen);
595 }
596  
597 /* The first `# H1`, as a title. Looks only at the top of the document, since
598 * a title further down is a section heading and not the page's name. */
599 357 static SV *pmd_first_h1(pTHX_ const char *s, STRLEN n) {
600 357 STRLEN i = 0;
601 414 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 411 3 414
602 411 STRLEN line = i;
603 4409 ▶ while (line < n && s[line] != '\n') line++; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
while (line < n && s[line] != '\n') line++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 4409 0 4409
while (line < n && s[line] != '\n') line++; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3998 411 4409
604 411 ▶ if (line > i + 1 && s[i] == '#' && s[i + 1] == ' ') { /* # uncoverable condition right count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (line > i + 1 && s[i] == '#' && s[i + 1] == ' ') { /* # uncoverable condition right count:3 class:ignore_covered_err */ 357 54 411
if (line > i + 1 && s[i] == '#' && s[i + 1] == ' ') { /* # uncoverable condition right count:3 class:ignore_covered_err */ 354 3 357
if (line > i + 1 && s[i] == '#' && s[i + 1] == ' ') { /* # uncoverable condition right count:3 class:ignore_covered_err */ 354 0 354
605 354 STRLEN t0 = i + 2, t1 = line;
606 /*
607 # uncoverable branch true class:ignore_covered_err
608 # uncoverable condition left count:1 class:ignore_covered_err
609 # uncoverable condition right count:1 class:ignore_covered_err
610 # uncoverable condition left count:2 class:ignore_covered_err */
611 354 ▶ while (t0 < t1 && s[t0] == ' ') t0++;
Branch: 2 branches
BranchTrueFalseTotal
while (t0 < t1 && s[t0] == ' ') t0++; 354 0 354
while (t0 < t1 && s[t0] == ' ') t0++; 0 354 354
612 /*
613 # uncoverable branch true count:2
614 # uncoverable branch true class:ignore_covered_err
615 # uncoverable condition left count:1 class:ignore_covered_err
616 # uncoverable condition right count:1 class:ignore_covered_err
617 # uncoverable condition left count:2 class:ignore_covered_err
618 # uncoverable condition left count:3 class:ignore_covered_err
619 # uncoverable condition left count:4 class:ignore_covered_err */
620 354 ▶ while (t1 > t0 && (s[t1 - 1] == ' ' || s[t1 - 1] == '\r' ||
Branch: 3 branches
BranchTrueFalseTotal
while (t1 > t0 && (s[t1 - 1] == ' ' || s[t1 - 1] == '\r' || 354 0 354
while (t1 > t0 && (s[t1 - 1] == ' ' || s[t1 - 1] == '\r' || 0 354 354
while (t1 > t0 && (s[t1 - 1] == ' ' || s[t1 - 1] == '\r' || 0 354 354
621 354 ▶ s[t1 - 1] == '#')) t1--;
Branch: 1 branch
BranchTrueFalseTotal
s[t1 - 1] == '#')) t1--; 0 354 354
622 /*
623 # uncoverable branch false
624 # uncoverable condition right count:1 class:ignore_covered_err */
625 354 ▶ if (t1 > t0) return newSVpvn(s + t0, t1 - t0);
Branch: 1 branch
BranchTrueFalseTotal
if (t1 > t0) return newSVpvn(s + t0, t1 - t0); 354 0 354
626 }
627 57 i = line + 1;
628 }
629 3 return NULL;
630 }
631  
632 /* A filename as a title: hyphens and underscores to spaces, each word
633 * capitalised. The last resort, when there is neither front matter nor an
634 * H1 to take the name from. */
635 240 static SV *pmd_title_from_name(pTHX_ const char *s, STRLEN n) {
636 240 SV *out = newSVpvs("");
637 STRLEN i;
638 240 int start = 1;
639 1837 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1597 240 1837
640 1597 char c = s[i];
641 /*
642 # uncoverable branch true
643 # uncoverable condition left count:1 class:ignore_covered_err
644 # uncoverable condition left count:2 class:ignore_covered_err */
645 1597 ▶ if (c == '-' || c == '_') { sv_catpvs(out, " "); start = 1; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '-' || c == '_') { sv_catpvs(out, " "); start = 1; continue; } 1597 0 1597
if (c == '-' || c == '_') { sv_catpvs(out, " "); start = 1; continue; } 0 1597 1597
646 /*
647 # uncoverable condition right count:2 class:ignore_covered_err
648 # uncoverable condition right count:3 class:ignore_covered_err */
649 1597 ▶ if (start && c >= 'a' && c <= 'z') c = (char)(c - 'a' + 'A');
Branch: 3 branches
BranchTrueFalseTotal
if (start && c >= 'a' && c <= 'z') c = (char)(c - 'a' + 'A'); 240 1357 1597
if (start && c >= 'a' && c <= 'z') c = (char)(c - 'a' + 'A'); 240 0 240
if (start && c >= 'a' && c <= 'z') c = (char)(c - 'a' + 'A'); 240 0 240
650 1597 sv_catpvn(out, &c, 1);
651 1597 start = 0;
652 }
653 240 return out;
654 }
655  
656 /* ---- walking the tree -----------------------------------------------------
657 * Modelled on poa_walk_dir in punk_oamount.h - PerlDir_open, dot entries
658 * skipped, sortsv for a traversal that is stable across filesystems, recurse
659 * into directories - but that one is @INC-relative and builds class names out
660 * of what it finds, so this is a sibling rather than a reuse.
661 *
662 * Only `.md` files are collected. Everything else in the tree is an asset and
663 * is served on demand through ps_serve_file, so there is nothing to enumerate
664 * for it here. */
665  
666 251 static void pmd_walk_dir(pTHX_ const char *root, SV *rel, AV *out,
667 int recursive) {
668 251 SV *dir = sv_2mortal(newSVpv(root, 0));
669 DIR *dh;
670 Direntry_t *de;
671 251 AV *ents = (AV *)sv_2mortal((SV *)newAV());
672 SSize_t i, n;
673  
674 251 ▶ if (SvCUR(rel)) { sv_catpvs(dir, "/"); sv_catsv(dir, rel); }
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(rel)) { sv_catpvs(dir, "/"); sv_catsv(dir, rel); } 133 118 251
675 251 dh = PerlDir_open(SvPV_nolen(dir));
676 /*
677 # uncoverable branch true
678 # uncoverable condition left count:1 class:ignore_covered_err */
679 251 ▶ if (!dh) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!dh) return; 0 251 251
680 1464 ▶ while ((de = PerlDir_read(dh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((de = PerlDir_read(dh))) { 1213 251 1464
681 1213 const char *nm = de->d_name;
682 1213 ▶ if (nm[0] == '.') continue; /* . .. and dotfiles alike */
Branch: 1 branch
BranchTrueFalseTotal
if (nm[0] == '.') continue; /* . .. and dotfiles alike */ 505 708 1213
683 708 av_push(ents, newSVpv(nm, 0));
684 }
685 251 PerlDir_close(dh);
686  
687 251 n = av_len(ents) + 1;
688 251 ▶ if (n > 1) sortsv(AvARRAY(ents), (STRLEN)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(ents), (STRLEN)n, Perl_sv_cmp); 198 53 251
689  
690 959 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 708 251 959
691 708 SV *ent = *av_fetch(ents, i, 0);
692 STRLEN el;
693 708 const char *es = SvPV_const(ent, el);
694 708 SV *child = sv_2mortal(newSVsv(rel));
695 708 SV *full = sv_2mortal(newSVsv(dir));
696 Stat_t st;
697 708 ▶ if (SvCUR(child)) sv_catpvs(child, "/");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(child)) sv_catpvs(child, "/"); 352 356 708
698 708 sv_catsv(child, ent);
699 708 sv_catpvs(full, "/"); sv_catsv(full, ent);
700  
701 708 ▶ if (PerlLIO_stat(SvPV_nolen(full), &st) == 0 && S_ISDIR(st.st_mode)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (PerlLIO_stat(SvPV_nolen(full), &st) == 0 && S_ISDIR(st.st_mode)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 708 0 708
if (PerlLIO_stat(SvPV_nolen(full), &st) == 0 && S_ISDIR(st.st_mode)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 133 575 708
702 /*
703 # uncoverable branch false
704 # uncoverable condition right count:1 class:ignore_covered_err */
705 133 ▶ if (recursive) pmd_walk_dir(aTHX_ root, child, out, recursive);
Branch: 1 branch
BranchTrueFalseTotal
if (recursive) pmd_walk_dir(aTHX_ root, child, out, recursive); 133 0 133
706 133 continue;
707 }
708 575 ▶ if (el > 3 && memEQ(es + el - 3, ".md", 3)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (el > 3 && memEQ(es + el - 3, ".md", 3)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 575 0 575
if (el > 3 && memEQ(es + el - 3, ".md", 3)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 519 56 575
709 519 av_push(out, newSVsv(child));
710 }
711 }
712  
713 /* `guide/intro.md` -> `/guide/intro`, `index.md` -> ``, `guide/index.md` ->
714 * `/guide`. The mount root is the empty string rather than "/" so that
715 * joining it to the prefix cannot double a slash. */
716 449 static SV *pmd_url_for(pTHX_ SV *rel, const char *index_name) {
717 449 STRLEN rl, il = strlen(index_name);
718 449 const char *r = SvPV_const(rel, rl);
719 SV *url;
720 449 STRLEN keep = rl - 3; /* drop ".md" */
721  
722 /* an index file collapses to its containing directory */
723 449 ▶ if (rl >= il && memEQ(r + rl - il, index_name, il)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rl >= il && memEQ(r + rl - il, index_name, il)) { 376 73 449
if (rl >= il && memEQ(r + rl - il, index_name, il)) { 72 304 376
724 72 ▶ if (rl == il) return newSVpvs(""); /* the root index */
Branch: 1 branch
BranchTrueFalseTotal
if (rl == il) return newSVpvs(""); /* the root index */ 65 7 72
725 /*
726 # uncoverable branch false
727 # uncoverable condition right count:1 class:ignore_covered_err */
728 7 ▶ if (r[rl - il - 1] == '/') keep = rl - il - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r[rl - il - 1] == '/') keep = rl - il - 1; 7 0 7
729 }
730 384 url = newSVpvs("/");
731 384 sv_catpvn(url, r, keep);
732 384 return url;
733 }
734  
735 /* The top-level directory a page sits in, or "" for one at the root. */
736 449 static SV *pmd_section_for(pTHX_ SV *rel) {
737 STRLEN rl;
738 449 const char *r = SvPV_const(rel, rl);
739 449 const char *slash = (const char *)memchr(r, '/', rl);
740 449 ▶ if (!slash) return newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
if (!slash) return newSVpvs(""); 204 245 449
741 245 return newSVpvn(r, (STRLEN)(slash - r));
742 }
743  
744 /* ---- the navigation -------------------------------------------------------
745 * Built in C and injected into the template with {% raw nav %}. Stencil has
746 * no recursion and no dynamic includes, so an arbitrarily deep tree cannot be
747 * looped in a template, and flattening to a fixed depth would silently
748 * truncate a three-level docs tree.
749 *
750 * The nav is rebuilt per page so the current entry can carry a class, rather
751 * than marking it in JavaScript. Every page is rendered at boot anyway, so
752 * this costs pages x nav-size once at startup - nothing for a docs tree - and
753 * it means the highlight works with JavaScript off. */
754  
755 553 static SV *pmd_nav_html(pTHX_ AV *order, HV *pages, SV *prefix, SV *current) {
756 553 SV *out = newSVpvs("");
757 553 SSize_t i, n = av_len(order) + 1;
758 553 SV *open_section = NULL;
759 STRLEN cl;
760 553 const char *cur = SvPV_const(current, cl);
761  
762 553 sv_catpvs(out, "<ul class=\"punk-md-nav\">\n");
763 3242 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2689 553 3242
764 2689 SV **up = av_fetch(order, i, 0);
765 HE *he;
766 AV *page;
767 SV *url, *label, *section;
768 STRLEN ul;
769 const char *us;
770 /*
771 # uncoverable branch true
772 # uncoverable condition left count:1 class:ignore_covered_err
773 # uncoverable condition left count:2 class:ignore_covered_err */
774 2689 ▶ if (!up || !*up) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!up || !*up) continue; 2689 0 2689
if (!up || !*up) continue; 0 2689 2689
775 2689 he = hv_fetch_ent(pages, *up, 0, 0);
776 /*
777 # uncoverable branch true
778 # uncoverable condition left count:1 class:ignore_covered_err
779 # uncoverable condition left count:2 class:ignore_covered_err
780 # uncoverable condition left count:3 class:ignore_covered_err */
781 2689 ▶ if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) continue; 2689 0 2689
if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) continue; 2689 0 2689
if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) continue; 0 2689 2689
782 2689 page = (AV *)SvRV(HeVAL(he));
783  
784 2689 url = *av_fetch(page, PMD_URL, 0);
785 2689 label = *av_fetch(page, PMD_NAV, 0);
786 2689 section = *av_fetch(page, PMD_SECTION, 0);
787  
788 /* open or close a section group as the ordered list crosses one */
789 2689 ▶ if (!open_section || sv_cmp(open_section, section) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!open_section || sv_cmp(open_section, section) != 0) { 2136 553 2689
if (!open_section || sv_cmp(open_section, section) != 0) { 842 1294 2136
790 1395 ▶ if (open_section && SvCUR(open_section))
Branch: 2 branches
BranchTrueFalseTotal
if (open_section && SvCUR(open_section)) 842 553 1395
if (open_section && SvCUR(open_section)) 379 463 842
791 379 sv_catpvs(out, "</ul></details></li>\n");
792 1395 ▶ if (SvCUR(section)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(section)) { 827 568 1395
793 827 sv_catpvs(out, "<li class=\"punk-md-section\">"
794 "<details open><summary>");
795 827 pmd_esc_sv(aTHX_ out, *av_fetch(page, PMD_SECLABEL, 0));
796 827 sv_catpvs(out, "</summary><ul>\n");
797 }
798 1395 ▶ if (open_section) SvREFCNT_dec(open_section);
Branch: 1 branch
BranchTrueFalseTotal
if (open_section) SvREFCNT_dec(open_section); 842 553 1395
799 1395 open_section = newSVsv(section);
800 }
801  
802 2689 us = SvPV_const(url, ul);
803 2689 sv_catpvs(out, "<li><a href=\"");
804 2689 pmd_esc_sv(aTHX_ out, prefix);
805 2689 pmd_esc(aTHX_ out, us, ul);
806 2689 ▶ if (ul == cl && (ul == 0 || memEQ(us, cur, ul)))
Branch: 3 branches
BranchTrueFalseTotal
if (ul == cl && (ul == 0 || memEQ(us, cur, ul))) 558 2131 2689
if (ul == cl && (ul == 0 || memEQ(us, cur, ul))) 493 65 558
if (ul == cl && (ul == 0 || memEQ(us, cur, ul))) 374 119 493
807 439 sv_catpvs(out, "\" class=\"current\" aria-current=\"page\">");
808 else
809 2250 sv_catpvs(out, "\">");
810 2689 pmd_esc_sv(aTHX_ out, label);
811 2689 sv_catpvs(out, "</a></li>\n");
812 }
813 /*
814 # uncoverable branch false
815 # uncoverable condition right count:1 class:ignore_covered_err */
816 553 ▶ if (open_section) {
Branch: 1 branch
BranchTrueFalseTotal
if (open_section) { 553 0 553
817 553 ▶ if (SvCUR(open_section)) sv_catpvs(out, "</ul></details></li>\n");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(open_section)) sv_catpvs(out, "</ul></details></li>\n"); 448 105 553
818 553 SvREFCNT_dec(open_section);
819 }
820 553 sv_catpvs(out, "</ul>\n");
821 553 return out;
822 }
823  
824 /* The per-page table of contents, from the heading list mds_abi handed back.
825 * h2 and h3 only: h1 is the page title, and deeper than h3 is noise in a
826 * sidebar. */
827 439 static SV *pmd_toc_html(pTHX_ AV *toc) {
828 439 SV *out = newSVpvs("");
829 439 SSize_t i, n = av_len(toc) + 1;
830 439 int any = 0;
831 1044 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 605 439 1044
832 605 SV **e = av_fetch(toc, i, 0);
833 HV *h;
834 SV **lv, **tx, **id;
835 IV level;
836 /*
837 # uncoverable branch true
838 # uncoverable condition left count:1 class:ignore_covered_err
839 # uncoverable condition left count:2 class:ignore_covered_err
840 # uncoverable condition left count:3 class:ignore_covered_err */
841 605 ▶ if (!e || !*e || !SvROK(*e)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvROK(*e)) continue; 605 0 605
if (!e || !*e || !SvROK(*e)) continue; 605 0 605
if (!e || !*e || !SvROK(*e)) continue; 0 605 605
842 605 h = (HV *)SvRV(*e);
843 605 lv = hv_fetchs(h, "level", 0);
844 605 tx = hv_fetchs(h, "text", 0);
845 605 id = hv_fetchs(h, "id", 0);
846 /*
847 # uncoverable branch true
848 # uncoverable condition left count:1 class:ignore_covered_err
849 # uncoverable condition left count:2 class:ignore_covered_err
850 # uncoverable condition left count:3 class:ignore_covered_err */
851 605 ▶ if (!lv || !tx || !id) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!lv || !tx || !id) continue; 605 0 605
if (!lv || !tx || !id) continue; 605 0 605
if (!lv || !tx || !id) continue; 0 605 605
852 605 level = SvIV(*lv);
853 605 ▶ if (level < 2 || level > 3) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (level < 2 || level > 3) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 169 436 605
if (level < 2 || level > 3) continue; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 169 169
854 169 ▶ if (!any) { sv_catpvs(out, "<ul class=\"punk-md-toc\">\n"); any = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!any) { sv_catpvs(out, "<ul class=\"punk-md-toc\">\n"); any = 1; } 110 59 169
855 /* sv_catpvs wants a literal it can size at compile time, so the
856 * branch has to be outside it rather than in a conditional. */
857 169 ▶ if (level == 2) sv_catpvs(out, "<li class=\"lvl2\"><a href=\"#");
Branch: 1 branch
BranchTrueFalseTotal
if (level == 2) sv_catpvs(out, "<li class=\"lvl2\"><a href=\"#"); 166 3 169
858 3 else sv_catpvs(out, "<li class=\"lvl3\"><a href=\"#");
859 169 pmd_esc_sv(aTHX_ out, *id);
860 169 sv_catpvs(out, "\">");
861 169 pmd_esc_sv(aTHX_ out, *tx);
862 169 sv_catpvs(out, "</a></li>\n");
863 }
864 439 ▶ if (any) sv_catpvs(out, "</ul>\n");
Branch: 1 branch
BranchTrueFalseTotal
if (any) sv_catpvs(out, "</ul>\n"); 110 329 439
865 439 return out;
866 }
867  
868 /* ---- the page shell -------------------------------------------------------
869 * One Stencil render, through the st_abi table. Per that ABI's contract
870 * engine_of is called on every render and the pointer is never cached: an
871 * ithread clone drops its engine and rebuilds lazily, so a cached handle
872 * would outlive what it points at. That is nearly moot at boot but matters
873 * under `reload`. */
874  
875 /* Everything this mount handles is UTF-8 octets: the source files are read
876 * raw, and Markdown::Simple renders bytes to bytes. Template::Stencil,
877 * though, deals in characters and encodes its output, so handing it octets
878 * would encode them a second time and turn every accented character into
879 * mojibake.
880 *
881 * So decode on the way in and let Stencil do the one encode. sv_utf8_decode
882 * validates: bytes that are not well-formed UTF-8 are left alone rather than
883 * flagged, which keeps a mis-encoded source file from corrupting the render
884 * of everything around it. */
885 6383 static SV *pmd_chars(pTHX_ SV *sv) {
886 /*
887 # uncoverable branch false
888 # uncoverable condition right count:1 class:ignore_covered_err
889 # uncoverable condition right count:2 class:ignore_covered_err
890 # uncoverable condition right count:3 class:ignore_covered_err */
891 6383 ▶ if (sv && SvOK(sv) && !SvUTF8(sv)) (void)sv_utf8_decode(sv);
Branch: 3 branches
BranchTrueFalseTotal
if (sv && SvOK(sv) && !SvUTF8(sv)) (void)sv_utf8_decode(sv); 6383 0 6383
if (sv && SvOK(sv) && !SvUTF8(sv)) (void)sv_utf8_decode(sv); 6383 0 6383
if (sv && SvOK(sv) && !SvUTF8(sv)) (void)sv_utf8_decode(sv); 6383 0 6383
892 6383 return sv;
893 }
894  
895 /* ---- SEO fields ----------------------------------------------------------
896 * What the shipped wrapper needs to write a page's meta tags, worked out here
897 * because a template cannot do path arithmetic:
898 *
899 * host the app's `host`, passed in at compile; "" without one
900 * canonical front-matter `canonical`, else host + prefix + url; "" with
901 * neither, and always "" off a page
902 * image front-matter `image` made absolute: an http(s) URL as is, a
903 * rooted path on the host, anything else beside the source
904 * file (host + prefix + its directory)
905 * og_type front-matter `og_type`, else `article` with a `date`, else
906 * `website`
907 * twitter_card `summary_large_image` with an image, else `summary`
908 *
909 * The origin is never the request's: the page is frozen at boot, before there
910 * is one, and a canonical taken from a Host header is how a cache is taught
911 * somebody else's hostname. */
912 /* The names the mount itself puts in front of the templates; `vars` may not
913 * use them. */
914 static const char *const pmd_reserved[] = {
915 "kind", "title", "site_title", "nav", "content", "toc", "url", "prefix",
916 "search", "search_path", "assets", "footer", "meta", "host", "canonical",
917 "image", "og_type", "twitter_card", "date", "updated", "date_iso",
918 "updated_iso", "date_human", "updated_human", NULL
919 };
920  
921 552 static void pmd_seo(pTHX_ AV *cap, HV *data, AV *page, HV *meta, SV *url) {
922 552 HV *opts = (HV *)SvRV(*av_fetch(cap, PMDC_OPTS, 0));
923 552 SV *host = pmd_opt(aTHX_ opts, "host");
924 552 SV *prefix = *av_fetch(cap, PMDC_PREFIX, 0);
925 552 SV *canon = newSVpvs(""), *image = newSVpvs("");
926 SV *v, **dp, **up;
927 552 const char *ogt = "website";
928  
929 552 ▶ if (page) {
Branch: 1 branch
BranchTrueFalseTotal
if (page) { 438 114 552
930 /*
931 # uncoverable condition right count:1 class:ignore_covered_err
932 # uncoverable condition right count:3 class:ignore_covered_err */
933 438 ▶ if (meta && (v = pmd_opt(aTHX_ meta, "canonical")) && SvCUR(v))
Branch: 3 branches
BranchTrueFalseTotal
if (meta && (v = pmd_opt(aTHX_ meta, "canonical")) && SvCUR(v)) 438 0 438
if (meta && (v = pmd_opt(aTHX_ meta, "canonical")) && SvCUR(v)) 3 435 438
if (meta && (v = pmd_opt(aTHX_ meta, "canonical")) && SvCUR(v)) 3 0 3
934 3 sv_setsv(canon, v);
935 435 ▶ else if (host && SvCUR(host)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (host && SvCUR(host)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 13 422 435
else if (host && SvCUR(host)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 13 0 13
936 13 sv_setsv(canon, host);
937 13 sv_catsv(canon, prefix);
938 13 sv_catsv(canon, url);
939 /*
940 # uncoverable branch true
941 # uncoverable condition left count:1 class:ignore_covered_err
942 # uncoverable condition left count:2 class:ignore_covered_err
943 # uncoverable condition right count:2 class:ignore_covered_err */
944 13 ▶ if (!SvCUR(prefix) && !SvCUR(url)) sv_catpvs(canon, "/");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvCUR(prefix) && !SvCUR(url)) sv_catpvs(canon, "/"); 0 13 13
if (!SvCUR(prefix) && !SvCUR(url)) sv_catpvs(canon, "/"); 0 0 0
945 }
946  
947 /*
948 # uncoverable condition right count:1 class:ignore_covered_err
949 # uncoverable condition right count:3 class:ignore_covered_err */
950 438 ▶ if (meta && (v = pmd_opt(aTHX_ meta, "image")) && SvCUR(v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (meta && (v = pmd_opt(aTHX_ meta, "image")) && SvCUR(v)) { 438 0 438
if (meta && (v = pmd_opt(aTHX_ meta, "image")) && SvCUR(v)) { 17 421 438
if (meta && (v = pmd_opt(aTHX_ meta, "image")) && SvCUR(v)) { 17 0 17
951 STRLEN il;
952 17 const char *ip = SvPV_const(v, il);
953 /*
954 # uncoverable condition left count:1 class:ignore_covered_err
955 # uncoverable condition left count:2 class:ignore_covered_err */
956 17 ▶ if ((il > 7 && memEQ(ip, "http://", 7)) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((il > 7 && memEQ(ip, "http://", 7)) || 11 6 17
if ((il > 7 && memEQ(ip, "http://", 7)) || 11 0 11
957 17 ▶ (il > 8 && memEQ(ip, "https://", 8)))
Branch: 2 branches
BranchTrueFalseTotal
(il > 8 && memEQ(ip, "https://", 8))) 11 6 17
(il > 8 && memEQ(ip, "https://", 8))) 3 8 11
958 3 sv_setsv(image, v);
959 else {
960 14 ▶ if (host) sv_setsv(image, host);
Branch: 1 branch
BranchTrueFalseTotal
if (host) sv_setsv(image, host); 8 6 14
961 14 ▶ if (ip[0] == '/') sv_catpvn(image, ip, il);
Branch: 1 branch
BranchTrueFalseTotal
if (ip[0] == '/') sv_catpvn(image, ip, il); 5 9 14
962 else {
963 /* beside the source file: its directory under the root */
964 9 SV *root = *av_fetch(cap, PMDC_DIR, 0);
965 9 SV *file = *av_fetch(page, PMD_FILE, 0);
966 STRLEN rl, fl, k, dend;
967 9 ▶ const char *fp = SvPV_const(file, fl);
Branch: 1 branch
BranchTrueFalseTotal
const char *fp = SvPV_const(file, fl); 9 0 9
968 (void)SvPV_const(root, rl);
969 9 dend = rl;
970 /*
971 # uncoverable branch false
972 # uncoverable condition right count:1 class:ignore_covered_err */
973 72 ▶ for (k = fl; k > rl; k--)
Branch: 1 branch
BranchTrueFalseTotal
for (k = fl; k > rl; k--) 72 0 72
974 72 ▶ if (fp[k - 1] == '/') { dend = k - 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (fp[k - 1] == '/') { dend = k - 1; break; } 9 63 72
975 9 sv_catsv(image, prefix);
976 9 ▶ if (dend > rl) sv_catpvn(image, fp + rl, dend - rl);
Branch: 1 branch
BranchTrueFalseTotal
if (dend > rl) sv_catpvn(image, fp + rl, dend - rl); 6 3 9
977 9 sv_catpvs(image, "/");
978 9 sv_catpvn(image, ip, il);
979 }
980 }
981 }
982  
983 /*
984 # uncoverable condition right count:1 class:ignore_covered_err
985 # uncoverable condition right count:3 class:ignore_covered_err */
986 438 ▶ if (meta && (v = pmd_opt(aTHX_ meta, "og_type")) && SvCUR(v))
Branch: 3 branches
BranchTrueFalseTotal
if (meta && (v = pmd_opt(aTHX_ meta, "og_type")) && SvCUR(v)) 438 0 438
if (meta && (v = pmd_opt(aTHX_ meta, "og_type")) && SvCUR(v)) 3 435 438
if (meta && (v = pmd_opt(aTHX_ meta, "og_type")) && SvCUR(v)) 3 0 3
987 3 ogt = NULL;
988 /*
989 # uncoverable condition right count:1 class:ignore_covered_err
990 # uncoverable condition right count:2 class:ignore_covered_err */
991 435 ▶ else if ((dp = av_fetch(page, PMD_DATE, 0)) && *dp && SvOK(*dp))
Branch: 3 branches
BranchTrueFalseTotal
else if ((dp = av_fetch(page, PMD_DATE, 0)) && *dp && SvOK(*dp)) 435 0 435
else if ((dp = av_fetch(page, PMD_DATE, 0)) && *dp && SvOK(*dp)) 435 0 435
else if ((dp = av_fetch(page, PMD_DATE, 0)) && *dp && SvOK(*dp)) 60 375 435
992 60 ogt = "article";
993  
994 438 dp = av_fetch(page, PMD_DATE, 0);
995 438 up = av_fetch(page, PMD_UPDATED, 0);
996 /*
997 # uncoverable condition right count:1 class:ignore_covered_err
998 # uncoverable condition right count:2 class:ignore_covered_err */
999 438 ▶ if (dp && *dp && SvOK(*dp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (dp && *dp && SvOK(*dp)) { 438 0 438
if (dp && *dp && SvOK(*dp)) { 438 0 438
if (dp && *dp && SvOK(*dp)) { 60 378 438
1000 60 IV t = SvIV(*dp);
1001 60 (void)hv_stores(data, "date", newSViv(t));
1002 60 (void)hv_stores(data, "date_iso", pmd_iso(aTHX_ t));
1003 60 (void)hv_stores(data, "date_human", pmd_human(aTHX_ t));
1004 }
1005 /*
1006 # uncoverable condition right count:1 class:ignore_covered_err
1007 # uncoverable condition right count:2 class:ignore_covered_err */
1008 438 ▶ if (up && *up && SvOK(*up)) {
Branch: 3 branches
BranchTrueFalseTotal
if (up && *up && SvOK(*up)) { 438 0 438
if (up && *up && SvOK(*up)) { 438 0 438
if (up && *up && SvOK(*up)) { 60 378 438
1009 60 IV t = SvIV(*up);
1010 60 (void)hv_stores(data, "updated", newSViv(t));
1011 60 (void)hv_stores(data, "updated_iso", pmd_iso(aTHX_ t));
1012 60 (void)hv_stores(data, "updated_human", pmd_human(aTHX_ t));
1013 }
1014 }
1015  
1016 552 ▶ (void)hv_stores(data, "host",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(data, "host", 19 533 552
1017 pmd_chars(aTHX_ host ? newSVsv(host) : newSVpvs("")));
1018 552 (void)hv_stores(data, "canonical", pmd_chars(aTHX_ canon));
1019 552 (void)hv_stores(data, "image", pmd_chars(aTHX_ image));
1020 552 ▶ (void)hv_stores(data, "og_type",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(data, "og_type", 549 3 552
1021 ogt ? newSVpv(ogt, 0)
1022 : pmd_chars(aTHX_ newSVsv(pmd_opt(aTHX_ meta, "og_type"))));
1023 552 ▶ (void)hv_stores(data, "twitter_card",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(data, "twitter_card", 17 535 552
1024 newSVpv(SvCUR(image) ? "summary_large_image" : "summary", 0));
1025 552 }
1026  
1027 /* `kind` tells the template which of the three things it is rendering:
1028 * "page" for a document, "search" for the results page, "notfound" for the
1029 * 404. All three go through the same template, so without it an overriding
1030 * template has no way to tell them apart and cannot, say, drop the table of
1031 * contents from a results page.
1032 *
1033 * `page` is the document being rendered, or NULL for the search page and the
1034 * 404. Its front matter arrives as `meta` (an empty hash without a page, so
1035 * `{% if meta.x %}` needs no kind check). The values are copied before
1036 * decoding: the page keeps its own bytes, and a reload renders from them
1037 * again. */
1038 552 static SV *pmd_shell(pTHX_ AV *cap, const char *kind, SV *title, SV *nav,
1039 SV *content, SV *toc, SV *url, AV *page) {
1040 552 const st_abi *st = punk_st(aTHX);
1041 552 HV *opts = (HV *)SvRV(*av_fetch(cap, PMDC_OPTS, 0));
1042 552 SV *engine_sv = *av_fetch(cap, PMDC_STENCIL, 0);
1043 552 void *engine = st->engine_of(aTHX_ engine_sv);
1044 552 HV *data = (HV *)sv_2mortal((SV *)newHV());
1045 552 SV *tmpl = sv_2mortal(newSVpvs("page"));
1046 552 SV *err = NULL, *out;
1047 552 SV *site = pmd_opt(aTHX_ opts, "title");
1048 552 SV *foot = pmd_opt(aTHX_ opts, "footer");
1049 552 HV *mcopy = newHV();
1050 552 HV *meta = NULL;
1051  
1052 552 ▶ if (page) {
Branch: 1 branch
BranchTrueFalseTotal
if (page) { 438 114 552
1053 438 SV **mp = av_fetch(page, PMD_META, 0);
1054 /*
1055 # uncoverable branch false
1056 # uncoverable condition right count:1 class:ignore_covered_err
1057 # uncoverable condition right count:2 class:ignore_covered_err
1058 # uncoverable condition right count:3 class:ignore_covered_err */
1059 438 ▶ if (mp && *mp && SvROK(*mp)) meta = (HV *)SvRV(*mp);
Branch: 3 branches
BranchTrueFalseTotal
if (mp && *mp && SvROK(*mp)) meta = (HV *)SvRV(*mp); 438 0 438
if (mp && *mp && SvROK(*mp)) meta = (HV *)SvRV(*mp); 438 0 438
if (mp && *mp && SvROK(*mp)) meta = (HV *)SvRV(*mp); 438 0 438
1060 }
1061 552 ▶ if (meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (meta) { 438 114 552
1062 HE *he;
1063 438 hv_iterinit(meta);
1064 841 ▶ while ((he = hv_iternext(meta)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(meta))) 403 438 841
1065 403 (void)hv_store_ent(mcopy, hv_iterkeysv(he),
1066 pmd_chars(aTHX_ newSVsv(HeVAL(he))), 0);
1067 }
1068 552 (void)hv_stores(data, "meta", newRV_noinc((SV *)mcopy));
1069 552 pmd_seo(aTHX_ cap, data, page, meta, url);
1070  
1071 /* `vars`: the site's own values (its name, its links), the same on every
1072 * page. The names the mount fills are refused at boot, so nothing here
1073 * can shadow one; a value is a fresh copy so a template cannot reach the
1074 * caller's data. */
1075 {
1076 552 SV *vars = pmd_opt(aTHX_ opts, "vars");
1077 /*
1078 # uncoverable condition right count:2 class:ignore_covered_err
1079 # uncoverable condition right count:3 class:ignore_covered_err */
1080 552 ▶ if (vars && SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (vars && SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV) { 7 545 552
if (vars && SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV) { 7 0 7
if (vars && SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV) { 7 0 7
1081 7 HV *vh = (HV *)SvRV(vars);
1082 HE *he;
1083 7 hv_iterinit(vh);
1084 26 ▶ while ((he = hv_iternext(vh)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(vh))) 19 7 26
1085 19 (void)hv_store_ent(data, hv_iterkeysv(he),
1086 newSVsv(HeVAL(he)), 0);
1087 }
1088 }
1089  
1090 /*
1091 # uncoverable branch true
1092 # uncoverable condition left count:1 class:ignore_covered_err */
1093 552 ▶ if (!engine)
Branch: 1 branch
BranchTrueFalseTotal
if (!engine) 0 552 552
1094 0 croak("Punk::Mount::Markdown: the view engine is not a " /* # uncoverable statement */
1095 "Template::Stencil");
1096  
1097 552 (void)hv_stores(data, "kind", newSVpv(kind, 0));
1098 552 (void)hv_stores(data, "title", pmd_chars(aTHX_ newSVsv(title)));
1099 552 ▶ (void)hv_stores(data, "site_title", pmd_chars(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(data, "site_title", pmd_chars(aTHX_ 72 480 552
1100 site ? newSVsv(site)
1101 : newSVsv(title)));
1102 552 (void)hv_stores(data, "nav", pmd_chars(aTHX_ newSVsv(nav)));
1103 552 (void)hv_stores(data, "content", pmd_chars(aTHX_ newSVsv(content)));
1104 552 ▶ (void)hv_stores(data, "toc", pmd_chars(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(data, "toc", pmd_chars(aTHX_ 438 114 552
1105 toc ? newSVsv(toc)
1106 : newSVpvs("")));
1107 552 (void)hv_stores(data, "url", pmd_chars(aTHX_ newSVsv(url)));
1108 552 (void)hv_stores(data, "prefix", newSVsv(*av_fetch(cap, PMDC_PREFIX, 0)));
1109 552 (void)hv_stores(data, "search",
1110 newSViv(SvOK(*av_fetch(cap, PMDC_INDEX, 0)) ? 1 : 0));
1111 552 (void)hv_stores(data, "search_path",
1112 newSVpv(pmd_opt_str(aTHX_ opts, "search_path", "/search"), 0));
1113 552 (void)hv_stores(data, "assets",
1114 newSVpv(pmd_opt_str(aTHX_ opts, "assets", "/_punk"), 0));
1115 552 ▶ (void)hv_stores(data, "footer", pmd_chars(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(data, "footer", pmd_chars(aTHX_ 0 552 552
1116 foot ? newSVsv(foot)
1117 : newSVpvs("")));
1118  
1119 552 out = st->render(aTHX_ engine, tmpl, data, NULL, &err);
1120 552 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 1 551 552
1121 1 ▶ croak_sv(err ? err /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
croak_sv(err ? err /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 1 1
1122 : sv_2mortal(newSVpvs("Punk::Mount::Markdown: the page "
1123 "template failed to render")));
1124 551 return out;
1125 }
1126  
1127 /* Render one page from its source and freeze the finished bytes into
1128 * PMD_BODY. Shared by the boot pass and the reload path. */
1129 440 static void pmd_render_page(pTHX_ AV *cap, AV *page) {
1130 440 const mds_abi *md = punk_mds(aTHX);
1131 440 HV *pages = (HV *)SvRV(*av_fetch(cap, PMDC_PAGES, 0));
1132 440 AV *order = (AV *)SvRV(*av_fetch(cap, PMDC_ORDER, 0));
1133 440 HV *opts = (HV *)SvRV(*av_fetch(cap, PMDC_OPTS, 0));
1134 440 SV *file = *av_fetch(page, PMD_FILE, 0);
1135 440 SV *src, *html, *nav, *toc_html = NULL, *body, *err = NULL;
1136 440 AV *toc = NULL;
1137 STRLEN blen, off;
1138 const char *bytes;
1139 440 HV *meta = (HV *)sv_2mortal((SV *)newHV());
1140  
1141 440 src = pmd_slurp(aTHX_ SvPV_nolen(file));
1142 /*
1143 # uncoverable branch true class:ignore_covered_err
1144 # uncoverable condition left count:1 class:ignore_covered_err */
1145 440 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 1 439 440
1146 1 croak("Punk::Mount::Markdown: cannot read '%s'", SvPV_nolen(file)); /* # uncoverable statement class:ignore_covered_err */
1147 439 bytes = SvPV_const(src, blen);
1148 439 off = pmd_front_matter(aTHX_ bytes, blen, meta);
1149  
1150 439 html = sv_2mortal(newSVpvs(""));
1151 /*
1152 # uncoverable branch false
1153 # uncoverable condition right count:1 class:ignore_covered_err */
1154 439 ▶ if (pmd_opt_bool(aTHX_ opts, "toc", 1))
Branch: 1 branch
BranchTrueFalseTotal
if (pmd_opt_bool(aTHX_ opts, "toc", 1)) 439 0 439
1155 439 toc = (AV *)sv_2mortal((SV *)newAV());
1156 /*
1157 # uncoverable branch true
1158 # uncoverable condition left count:1 class:ignore_covered_err */
1159 439 ▶ if (md->session_render(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
if (md->session_render(aTHX_ 0 439 439
1160 439 md->session_of(aTHX_ *av_fetch(cap, PMDC_MD, 0)),
1161 bytes + off, blen - off, html, toc, &err) != 0)
1162 /*
1163 # uncoverable statement
1164 # uncoverable condition left count:1
1165 # uncoverable condition right count:1 */
1166 0 ▶ croak_sv(err ? err : sv_2mortal(newSVpvs(
Branch: 1 branch
BranchTrueFalseTotal
croak_sv(err ? err : sv_2mortal(newSVpvs( 0 0 0
1167 "Punk::Mount::Markdown: a document failed to render")));
1168  
1169 /*
1170 # uncoverable branch false
1171 # uncoverable condition right count:1 class:ignore_covered_err */
1172 439 ▶ if (toc) toc_html = sv_2mortal(pmd_toc_html(aTHX_ toc));
Branch: 1 branch
BranchTrueFalseTotal
if (toc) toc_html = sv_2mortal(pmd_toc_html(aTHX_ toc)); 439 0 439
1173  
1174 439 nav = sv_2mortal(pmd_nav_html(aTHX_ order, pages,
1175 *av_fetch(cap, PMDC_PREFIX, 0),
1176 *av_fetch(page, PMD_URL, 0)));
1177 439 av_store(page, PMD_META, newRV_inc((SV *)meta));
1178 439 pmd_page_dates(aTHX_ page, meta);
1179 438 body = pmd_shell(aTHX_ cap, "page", *av_fetch(page, PMD_TITLE, 0), nav,
1180 438 html, toc_html, *av_fetch(page, PMD_URL, 0), page);
1181 437 av_store(page, PMD_BODY, body);
1182 437 }
1183  
1184 /* ---- responses ------------------------------------------------------------ */
1185  
1186 /* An HTML triplet over frozen bytes, with the mount's header block. */
1187 105 static SV *pmd_html_response(pTHX_ AV *cap, IV status, SV *body, int is_head) {
1188 105 AV *resp = newAV(), *headers = newAV(), *bodyav = newAV();
1189 105 HV *opts = (HV *)SvRV(*av_fetch(cap, PMDC_OPTS, 0));
1190 105 SV *hdrs = pmd_opt(aTHX_ opts, "headers");
1191 STRLEN n;
1192 105 const char *b = SvPV_const(body, n);
1193  
1194 105 av_push(headers, newSVpvs("Content-Type"));
1195 105 av_push(headers, newSVpvs("text/html; charset=utf-8"));
1196 105 av_push(headers, newSVpvs("Content-Length"));
1197 105 av_push(headers, newSViv((IV)n));
1198 /*
1199 # uncoverable branch true
1200 # uncoverable condition left count:1 class:ignore_covered_err
1201 # uncoverable condition left count:2 class:ignore_covered_err
1202 # uncoverable condition right count:2 class:ignore_covered_err
1203 # uncoverable condition left count:3 class:ignore_covered_err
1204 # uncoverable condition right count:3 class:ignore_covered_err */
1205 105 ▶ if (hdrs && SvROK(hdrs) && SvTYPE(SvRV(hdrs)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (hdrs && SvROK(hdrs) && SvTYPE(SvRV(hdrs)) == SVt_PVHV) { 0 105 105
if (hdrs && SvROK(hdrs) && SvTYPE(SvRV(hdrs)) == SVt_PVHV) { 0 0 0
if (hdrs && SvROK(hdrs) && SvTYPE(SvRV(hdrs)) == SVt_PVHV) { 0 0 0
1206 0 HV *hh = (HV *)SvRV(hdrs); /* # uncoverable statement */
1207 HE *he;
1208 0 hv_iterinit(hh); /* # uncoverable statement */
1209 /*
1210 # uncoverable statement
1211 # uncoverable branch true
1212 # uncoverable branch false
1213 # uncoverable condition left count:1
1214 # uncoverable condition right count:1 */
1215 0 ▶ while ((he = hv_iternext(hh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hh))) { 0 0 0
1216 0 av_push(headers, newSVsv(hv_iterkeysv(he))); /* # uncoverable statement */
1217 0 av_push(headers, newSVsv(hv_iterval(hh, he))); /* # uncoverable statement */
1218 }
1219 }
1220 105 ▶ av_push(bodyav, is_head ? newSVpvs("") : newSVpvn(b, n));
Branch: 1 branch
BranchTrueFalseTotal
av_push(bodyav, is_head ? newSVpvs("") : newSVpvn(b, n)); 1 104 105
1221 105 av_push(resp, newSViv(status));
1222 105 av_push(resp, newRV_noinc((SV *)headers));
1223 105 av_push(resp, newRV_noinc((SV *)bodyav));
1224 105 return newRV_noinc((SV *)resp);
1225 }
1226  
1227 /* A 301 to the canonical url for a page, so a trailing slash or a `.md`
1228 * suffix does not become a second address for the same document. */
1229 7 static SV *pmd_redirect(pTHX_ SV *location) {
1230 7 AV *resp = newAV(), *headers = newAV(), *body = newAV();
1231 STRLEN ll, i;
1232 7 const char *lp = SvPV_const(location, ll);
1233 /* A header value is \r-delimited, so one control byte in here is a
1234 * response split. Callers are supposed to hand us a location built from
1235 * the mount prefix and a page key, never from the request; stopping at
1236 * the first control byte is the belt to that braces. */
1237 /*
1238 # uncoverable condition right count:2 class:ignore_covered_err
1239 # uncoverable condition right count:3 class:ignore_covered_err */
1240 103 ▶ for (i = 0; i < ll && (unsigned char)lp[i] >= 0x20 && lp[i] != 0x7f; i++)
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < ll && (unsigned char)lp[i] >= 0x20 && lp[i] != 0x7f; i++) 96 7 103
for (i = 0; i < ll && (unsigned char)lp[i] >= 0x20 && lp[i] != 0x7f; i++) 96 0 96
for (i = 0; i < ll && (unsigned char)lp[i] >= 0x20 && lp[i] != 0x7f; i++) 96 0 96
1241 ;
1242 7 av_push(headers, newSVpvs("Location"));
1243 7 av_push(headers, newSVpvn(lp, i));
1244 7 av_push(headers, newSVpvs("Content-Type"));
1245 7 av_push(headers, newSVpvs("text/plain; charset=utf-8"));
1246 7 av_push(headers, newSVpvs("Content-Length"));
1247 7 av_push(headers, newSViv(0));
1248 7 av_push(body, newSVpvs(""));
1249 7 av_push(resp, newSViv(301));
1250 7 av_push(resp, newRV_noinc((SV *)headers));
1251 7 av_push(resp, newRV_noinc((SV *)body));
1252 7 return newRV_noinc((SV *)resp);
1253 }
1254  
1255 /* ---- search ---------------------------------------------------------------
1256 * The one request path that is not frozen. The query string is parsed with
1257 * pq_parse_pairs from punk_request.h, the index queried through sg_abi into a
1258 * stack array, and the hits mapped back to pages through the doc_id map. */
1259  
1260 13 static SV *pmd_search_page(pTHX_ AV *cap, HV *env) {
1261 13 const sg_abi *sg = punk_sg_try(aTHX);
1262 13 SV *idx_sv = *av_fetch(cap, PMDC_INDEX, 0);
1263 13 HV *pages = (HV *)SvRV(*av_fetch(cap, PMDC_PAGES, 0));
1264 13 AV *order = (AV *)SvRV(*av_fetch(cap, PMDC_ORDER, 0));
1265 13 HV *docmap = (HV *)SvRV(*av_fetch(cap, PMDC_DOCMAP, 0));
1266 13 SV *prefix = *av_fetch(cap, PMDC_PREFIX, 0);
1267 13 SV *content = sv_2mortal(newSVpvs(""));
1268 13 SV *query = NULL, *nav, *title;
1269 void *idx;
1270 sg_abi_hit hits[64];
1271 13 uint32_t n = 0, i;
1272  
1273 { /* ?q=... */
1274 13 SV **qs = hv_fetchs(env, "QUERY_STRING", 0);
1275 /*
1276 # uncoverable branch false
1277 # uncoverable condition right count:1 class:ignore_covered_err
1278 # uncoverable condition right count:2 class:ignore_covered_err
1279 # uncoverable condition right count:3 class:ignore_covered_err */
1280 13 ▶ if (qs && *qs && SvOK(*qs)) {
Branch: 3 branches
BranchTrueFalseTotal
if (qs && *qs && SvOK(*qs)) { 13 0 13
if (qs && *qs && SvOK(*qs)) { 13 0 13
if (qs && *qs && SvOK(*qs)) { 13 0 13
1281 STRLEN ql;
1282 13 const char *q = SvPV_const(*qs, ql);
1283 13 HV *pairs = pq_parse_pairs(aTHX_ q, ql);
1284 13 SV **v = hv_fetchs(pairs, "q", 0);
1285 /*
1286 # uncoverable condition right count:2 class:ignore_covered_err
1287 # uncoverable condition right count:3 class:ignore_covered_err */
1288 13 ▶ if (v && *v && SvOK(*v)) query = sv_2mortal(newSVsv(*v));
Branch: 3 branches
BranchTrueFalseTotal
if (v && *v && SvOK(*v)) query = sv_2mortal(newSVsv(*v)); 10 3 13
if (v && *v && SvOK(*v)) query = sv_2mortal(newSVsv(*v)); 10 0 10
if (v && *v && SvOK(*v)) query = sv_2mortal(newSVsv(*v)); 10 0 10
1289 13 SvREFCNT_dec((SV *)pairs);
1290 }
1291 }
1292  
1293 13 sv_catpvs(content, "<h1 class=\"punk-md-search-title\">Search</h1>\n");
1294 13 sv_catpvs(content, "<form class=\"punk-md-search\" method=\"get\">"
1295 "<input type=\"search\" name=\"q\" value=\"");
1296 13 ▶ if (query) pmd_esc_sv(aTHX_ content, query);
Branch: 1 branch
BranchTrueFalseTotal
if (query) pmd_esc_sv(aTHX_ content, query); 10 3 13
1297 13 sv_catpvs(content, "\" placeholder=\"Search the docs\" autofocus>"
1298 "</form>\n");
1299  
1300 13 ▶ if (!query || !SvCUR(query)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!query || !SvCUR(query)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 10 3 13
if (!query || !SvCUR(query)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 10 10
1301 3 sv_catpvs(content, "<p class=\"punk-md-hint\">"
1302 "Type a query to search these pages.</p>\n");
1303 }
1304 /*
1305 # uncoverable branch false
1306 # uncoverable condition right count:1 class:ignore_covered_err
1307 # uncoverable condition right count:2 class:ignore_covered_err
1308 # uncoverable condition right count:3 class:ignore_covered_err */
1309 20 ▶ else if (sg && SvOK(idx_sv) && (idx = sg->index_of(aTHX_ idx_sv)) != NULL) {
Branch: 3 branches
BranchTrueFalseTotal
else if (sg && SvOK(idx_sv) && (idx = sg->index_of(aTHX_ idx_sv)) != NULL) { 10 0 10
else if (sg && SvOK(idx_sv) && (idx = sg->index_of(aTHX_ idx_sv)) != NULL) { 10 0 10
else if (sg && SvOK(idx_sv) && (idx = sg->index_of(aTHX_ idx_sv)) != NULL) { 10 0 10
1310 STRLEN ql;
1311 10 const char *q = SvPV_const(query, ql);
1312 10 n = sg->search(idx, q, (uint32_t)ql, 20, hits,
1313 (uint32_t)(sizeof hits / sizeof hits[0]));
1314 10 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 4 6 10
1315 4 sv_catpvs(content, "<p class=\"punk-md-empty\">No results for "
1316 "<strong>");
1317 4 pmd_esc_sv(aTHX_ content, query);
1318 4 sv_catpvs(content, "</strong>.</p>\n");
1319 }
1320 else {
1321 6 sv_catpvs(content, "<ol class=\"punk-md-results\">\n");
1322 17 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 11 6 17
1323 11 SV *key = sv_2mortal(newSVuv(hits[i].doc_id));
1324 11 HE *he = hv_fetch_ent(docmap, key, 0, 0);
1325 HE *pe;
1326 AV *page;
1327 SV *plain;
1328 /*
1329 # uncoverable branch true
1330 # uncoverable condition left count:1 class:ignore_covered_err
1331 # uncoverable condition left count:2 class:ignore_covered_err */
1332 11 ▶ if (!he || !HeVAL(he)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!he || !HeVAL(he)) continue; 11 0 11
if (!he || !HeVAL(he)) continue; 0 11 11
1333 11 pe = hv_fetch_ent(pages, HeVAL(he), 0, 0);
1334 /*
1335 # uncoverable branch true
1336 # uncoverable condition left count:1 class:ignore_covered_err
1337 # uncoverable condition left count:2 class:ignore_covered_err
1338 # uncoverable condition left count:3 class:ignore_covered_err */
1339 11 ▶ if (!pe || !HeVAL(pe) || !SvROK(HeVAL(pe))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!pe || !HeVAL(pe) || !SvROK(HeVAL(pe))) continue; 11 0 11
if (!pe || !HeVAL(pe) || !SvROK(HeVAL(pe))) continue; 11 0 11
if (!pe || !HeVAL(pe) || !SvROK(HeVAL(pe))) continue; 0 11 11
1340 11 page = (AV *)SvRV(HeVAL(pe));
1341 11 sv_catpvs(content, "<li><a href=\"");
1342 11 pmd_esc_sv(aTHX_ content, prefix);
1343 11 pmd_esc_sv(aTHX_ content, *av_fetch(page, PMD_URL, 0));
1344 11 sv_catpvs(content, "\">");
1345 11 pmd_esc_sv(aTHX_ content, *av_fetch(page, PMD_TITLE, 0));
1346 11 sv_catpvs(content, "</a>");
1347 11 plain = *av_fetch(page, PMD_PLAIN, 0);
1348 /*
1349 # uncoverable branch false
1350 # uncoverable condition right count:1 class:ignore_covered_err
1351 # uncoverable condition right count:2 class:ignore_covered_err */
1352 11 ▶ if (SvOK(plain) && SvCUR(plain)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvOK(plain) && SvCUR(plain)) { 11 0 11
if (SvOK(plain) && SvCUR(plain)) { 11 0 11
1353 STRLEN pl;
1354 11 const char *p = SvPV_const(plain, pl);
1355 11 STRLEN take = pmd_cut(p, pl, 180);
1356 11 sv_catpvs(content, "<p class=\"punk-md-snippet\">");
1357 11 pmd_esc_collapsed(aTHX_ content, p, take);
1358 /*
1359 # uncoverable branch true
1360 # uncoverable condition left count:1 class:ignore_covered_err */
1361 11 ▶ if (take < pl) sv_catpvs(content, "...");
Branch: 1 branch
BranchTrueFalseTotal
if (take < pl) sv_catpvs(content, "..."); 0 11 11
1362 11 sv_catpvs(content, "</p>");
1363 }
1364 11 sv_catpvs(content, "</li>\n");
1365 }
1366 6 sv_catpvs(content, "</ol>\n");
1367 }
1368 }
1369 else {
1370 0 sv_catpvs(content, "<p class=\"punk-md-empty\">" /* # uncoverable statement */
1371 "Search is not available.</p>\n");
1372 }
1373  
1374 13 title = sv_2mortal(newSVpvs("Search"));
1375 13 nav = sv_2mortal(pmd_nav_html(aTHX_ order, pages, prefix,
1376 sv_2mortal(newSVpvs("\x01none"))));
1377 13 return pmd_shell(aTHX_ cap, "search", title, nav, content, NULL,
1378 13 sv_2mortal(newSVpv(
1379 pmd_opt_str(aTHX_
1380 (HV *)SvRV(*av_fetch(cap, PMDC_OPTS, 0)),
1381 "search_path", "/search"), 0)), NULL);
1382 }
1383  
1384 /* ---- the listing ----------------------------------------------------------
1385 * $app->markdown_pages($prefix): the built pages, in the mount's order, as
1386 * fresh Perl data, so a landing page, an index or a feed can be made from
1387 * the same table the mount serves. Drafts are not in it, since the walk
1388 * never kept them. Every value is a copy, decoded to characters: nothing a
1389 * caller does to the list reaches a frozen page. */
1390  
1391 /* A page's summary: front-matter `description`, else its plain text with the
1392 * leading title dropped, whitespace collapsed, cut to 200 bytes on a UTF-8
1393 * boundary with "..." when anything was cut. */
1394 84 static SV *pmd_summary(pTHX_ AV *page, HV *meta) {
1395 SV *v, *flat, *out;
1396 84 SV **pp = av_fetch(page, PMD_PLAIN, 0);
1397 84 SV *title = *av_fetch(page, PMD_TITLE, 0);
1398 STRLEN fl, tl, take;
1399 const char *f, *t;
1400 /*
1401 # uncoverable condition right count:1 class:ignore_covered_err
1402 # uncoverable condition right count:3 class:ignore_covered_err */
1403 84 ▶ if (meta && (v = pmd_opt(aTHX_ meta, "description")) && SvCUR(v))
Branch: 3 branches
BranchTrueFalseTotal
if (meta && (v = pmd_opt(aTHX_ meta, "description")) && SvCUR(v)) 84 0 84
if (meta && (v = pmd_opt(aTHX_ meta, "description")) && SvCUR(v)) 23 61 84
if (meta && (v = pmd_opt(aTHX_ meta, "description")) && SvCUR(v)) 23 0 23
1404 23 return pmd_chars(aTHX_ newSVsv(v));
1405 61 flat = sv_2mortal(newSVpvs(""));
1406 /*
1407 # uncoverable branch false
1408 # uncoverable condition right count:1 class:ignore_covered_err
1409 # uncoverable condition right count:2 class:ignore_covered_err
1410 # uncoverable condition right count:3 class:ignore_covered_err */
1411 61 ▶ if (pp && *pp && SvOK(*pp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (pp && *pp && SvOK(*pp)) { 61 0 61
if (pp && *pp && SvOK(*pp)) { 61 0 61
if (pp && *pp && SvOK(*pp)) { 61 0 61
1412 STRLEN pl;
1413 61 const char *p = SvPV_const(*pp, pl);
1414 61 pmd_collapsed(aTHX_ flat, p, pl);
1415 }
1416 61 ▶ f = SvPV_const(flat, fl);
Branch: 1 branch
BranchTrueFalseTotal
f = SvPV_const(flat, fl); 61 0 61
1417 61 t = SvPV_const(title, tl);
1418 /*
1419 # uncoverable branch false
1420 # uncoverable condition right count:1 class:ignore_covered_err
1421 # uncoverable condition right count:2 class:ignore_covered_err
1422 # uncoverable condition right count:3 class:ignore_covered_err
1423 # uncoverable condition right count:4 class:ignore_covered_err
1424 # uncoverable condition right count:5 class:ignore_covered_err */
1425 61 ▶ if (tl && fl >= tl && memEQ(f, t, tl) && (fl == tl || f[tl] == ' ')) {
Branch: 5 branches
BranchTrueFalseTotal
if (tl && fl >= tl && memEQ(f, t, tl) && (fl == tl || f[tl] == ' ')) { 61 0 61
if (tl && fl >= tl && memEQ(f, t, tl) && (fl == tl || f[tl] == ' ')) { 61 0 61
if (tl && fl >= tl && memEQ(f, t, tl) && (fl == tl || f[tl] == ' ')) { 61 0 61
if (tl && fl >= tl && memEQ(f, t, tl) && (fl == tl || f[tl] == ' ')) { 49 12 61
if (tl && fl >= tl && memEQ(f, t, tl) && (fl == tl || f[tl] == ' ')) { 49 0 49
1426 61 f += tl; fl -= tl;
1427 110 ▶ while (fl && *f == ' ') { f++; fl--; }
Branch: 2 branches
BranchTrueFalseTotal
while (fl && *f == ' ') { f++; fl--; } 98 12 110
while (fl && *f == ' ') { f++; fl--; } 49 49 98
1428 }
1429 61 take = pmd_cut(f, fl, 200);
1430 61 out = newSVpvn(f, take);
1431 61 ▶ if (take < fl) sv_catpvs(out, "...");
Branch: 1 branch
BranchTrueFalseTotal
if (take < fl) sv_catpvs(out, "..."); 8 53 61
1432 61 return pmd_chars(aTHX_ out);
1433 }
1434  
1435 24 static AV *pmd_pages_av(pTHX_ AV *cap) {
1436 24 HV *pages = (HV *)SvRV(*av_fetch(cap, PMDC_PAGES, 0));
1437 24 AV *order = (AV *)SvRV(*av_fetch(cap, PMDC_ORDER, 0));
1438 24 SV *prefix = *av_fetch(cap, PMDC_PREFIX, 0);
1439 24 AV *out = newAV();
1440 24 SSize_t i, n = av_len(order) + 1;
1441 108 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 84 24 108
1442 84 SV **u = av_fetch(order, i, 0);
1443 HE *he;
1444 AV *page;
1445 84 HV *rec, *mcopy = newHV(), *meta = NULL;
1446 SV **mp, **dp, **up, *url, *path;
1447 /*
1448 # uncoverable branch true
1449 # uncoverable condition left count:1 class:ignore_covered_err
1450 # uncoverable condition left count:2 class:ignore_covered_err */
1451 84 ▶ if (!u || !*u) { SvREFCNT_dec((SV *)mcopy); continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (!u || !*u) { SvREFCNT_dec((SV *)mcopy); continue; } 84 0 84
if (!u || !*u) { SvREFCNT_dec((SV *)mcopy); continue; } 0 84 84
1452 84 he = hv_fetch_ent(pages, *u, 0, 0);
1453 /*
1454 # uncoverable branch true
1455 # uncoverable condition left count:1 class:ignore_covered_err
1456 # uncoverable condition left count:2 class:ignore_covered_err
1457 # uncoverable condition left count:3 class:ignore_covered_err */
1458 84 ▶ if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) { 84 0 84
if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) { 84 0 84
if (!he || !HeVAL(he) || !SvROK(HeVAL(he))) { 0 84 84
1459 0 SvREFCNT_dec((SV *)mcopy); /* # uncoverable statement */
1460 0 continue; /* # uncoverable statement */
1461 }
1462 84 page = (AV *)SvRV(HeVAL(he));
1463 84 rec = newHV();
1464  
1465 84 mp = av_fetch(page, PMD_META, 0);
1466 /*
1467 # uncoverable branch false
1468 # uncoverable condition right count:1 class:ignore_covered_err
1469 # uncoverable condition right count:2 class:ignore_covered_err
1470 # uncoverable condition right count:3 class:ignore_covered_err */
1471 84 ▶ if (mp && *mp && SvROK(*mp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (mp && *mp && SvROK(*mp)) { 84 0 84
if (mp && *mp && SvROK(*mp)) { 84 0 84
if (mp && *mp && SvROK(*mp)) { 84 0 84
1472 HE *me;
1473 84 meta = (HV *)SvRV(*mp);
1474 84 hv_iterinit(meta);
1475 205 ▶ while ((me = hv_iternext(meta)))
Branch: 1 branch
BranchTrueFalseTotal
while ((me = hv_iternext(meta))) 121 84 205
1476 121 (void)hv_store_ent(mcopy, hv_iterkeysv(me),
1477 pmd_chars(aTHX_ newSVsv(HeVAL(me))), 0);
1478 }
1479  
1480 84 url = newSVsv(prefix);
1481 84 sv_catsv(url, *u);
1482 /*
1483 # uncoverable branch true
1484 # uncoverable condition left count:1 class:ignore_covered_err */
1485 84 ▶ if (!SvCUR(url)) sv_catpvs(url, "/");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvCUR(url)) sv_catpvs(url, "/"); 0 84 84
1486 84 ▶ path = SvCUR(*u) ? newSVsv(*u) : newSVpvs("/"); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
path = SvCUR(*u) ? newSVsv(*u) : newSVpvs("/"); /* # uncoverable condition right count:1 class:ignore_covered_err */ 84 0 84
1487  
1488 84 (void)hv_stores(rec, "url", pmd_chars(aTHX_ url));
1489 84 (void)hv_stores(rec, "path", pmd_chars(aTHX_ path));
1490 84 (void)hv_stores(rec, "title",
1491 pmd_chars(aTHX_ newSVsv(*av_fetch(page, PMD_TITLE, 0))));
1492 84 dp = av_fetch(page, PMD_DATE, 0);
1493 84 up = av_fetch(page, PMD_UPDATED, 0);
1494 84 ▶ (void)hv_stores(rec, "date",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(rec, "date", 84 0 84
(void)hv_stores(rec, "date", 84 0 84
(void)hv_stores(rec, "date", 54 30 84
1495 (dp && *dp && SvOK(*dp)) ? newSViv(SvIV(*dp)) : newSV(0));
1496 84 ▶ (void)hv_stores(rec, "updated",
Branch: 3 branches
BranchTrueFalseTotal
(void)hv_stores(rec, "updated", 84 0 84
(void)hv_stores(rec, "updated", 84 0 84
(void)hv_stores(rec, "updated", 54 30 84
1497 (up && *up && SvOK(*up)) ? newSViv(SvIV(*up)) : newSV(0));
1498 84 (void)hv_stores(rec, "summary", pmd_summary(aTHX_ page, meta));
1499 84 (void)hv_stores(rec, "meta", newRV_noinc((SV *)mcopy));
1500 84 av_push(out, newRV_noinc((SV *)rec));
1501 }
1502 24 return out;
1503 }
1504  
1505 /* ---- the app ---------------------------------------------------------------
1506 * The PSGI coderef's body: a magic CV whose capture is the built site. */
1507  
1508 XS_INTERNAL(punk_md_cb);
1509 123 XS_INTERNAL(punk_md_cb) {
1510 123 dXSARGS;
1511 123 AV *cap = punk_clos_cap(aTHX_ cv);
1512 HV *env, *pages, *opts;
1513 SV **e;
1514 const char *path, *method;
1515 123 STRLEN plen, mlen = 3;
1516 123 int is_head = 0;
1517 123 SV *key, *resp = NULL;
1518 HE *he;
1519  
1520 /*
1521 # uncoverable branch true
1522 # uncoverable condition left count:1 class:ignore_covered_err
1523 # uncoverable condition left count:2 class:ignore_covered_err */
1524 123 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 123 0 123
if (!cap || items < 1) XSRETURN_EMPTY; 0 123 123
1525 123 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 122 1 123
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 122 122
1526 1 croak("Punk::Mount::Markdown: the app takes a PSGI env hashref");
1527 122 env = (HV *)SvRV(ST(0));
1528 122 pages = (HV *)SvRV(*av_fetch(cap, PMDC_PAGES, 0));
1529 122 opts = (HV *)SvRV(*av_fetch(cap, PMDC_OPTS, 0));
1530  
1531 122 e = hv_fetchs(env, "REQUEST_METHOD", 0);
1532 /*
1533 # uncoverable condition right count:1 class:ignore_covered_err
1534 # uncoverable condition right count:2 class:ignore_covered_err
1535 # uncoverable condition right count:3 class:ignore_covered_err */
1536 244 ▶ method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET";
Branch: 3 branches
BranchTrueFalseTotal
method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET"; 122 0 122
method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET"; 122 0 122
method = (e && *e && SvOK(*e)) ? SvPV_const(*e, mlen) : "GET"; 122 0 122
1537 122 ▶ if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; 2 120 122
if (mlen == 4 && memEQ(method, "HEAD", 4)) is_head = 1; 1 1 2
1538 121 ▶ else if (!(mlen == 3 && memEQ(method, "GET", 3))) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (!(mlen == 3 && memEQ(method, "GET", 3))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 120 1 121
else if (!(mlen == 3 && memEQ(method, "GET", 3))) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 120 120
1539 1 ST(0) = sv_2mortal(ps_plain(aTHX_ 405, "Method Not Allowed",
1540 "Allow", "GET, HEAD"));
1541 1 XSRETURN(1);
1542 }
1543  
1544 121 e = hv_fetchs(env, "PATH_INFO", 0);
1545 /*
1546 # uncoverable branch false class:ignore_covered_err
1547 # uncoverable condition right count:1 class:ignore_covered_err
1548 # uncoverable condition right count:2 class:ignore_covered_err
1549 # uncoverable condition right count:3 class:ignore_covered_err */
1550 242 ▶ if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen); 121 0 121
if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen); 121 0 121
if (e && *e && SvOK(*e)) path = SvPV_const(*e, plen); 121 0 121
1551 0 else { path = "/"; plen = 1; } /* # uncoverable statement class:ignore_covered_err */
1552  
1553 /* The mount loop hands us "/" for the bare prefix; the page table keys
1554 * the root as "". */
1555 121 ▶ if (plen == 1 && path[0] == '/') { path = ""; plen = 0; } /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (plen == 1 && path[0] == '/') { path = ""; plen = 0; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 111 121
if (plen == 1 && path[0] == '/') { path = ""; plen = 0; } /* # uncoverable condition right count:2 class:ignore_covered_err */ 10 0 10
1556  
1557 /* Canonicalise: a trailing slash or a .md suffix redirects rather than
1558 * becoming a second address for the same document.
1559 *
1560 * The target has to be a page we actually hold before we will name it in
1561 * a Location header. PATH_INFO arrives percent-decoded, so reflecting it
1562 * means echoing bytes the client chose: "//evil.example/" would come back
1563 * as a protocol-relative 301 off the site, and a decoded CR/LF would end
1564 * the header and split the response. Looking the stripped target up in
1565 * the page table first means the only thing we can ever echo is one of
1566 * our own keys. A path that is not a page falls through to the 404 it was
1567 * always going to get. */
1568 121 ▶ if ((plen > 1 && path[plen - 1] == '/') ||
Branch: 2 branches
BranchTrueFalseTotal
if ((plen > 1 && path[plen - 1] == '/') || 111 10 121
if ((plen > 1 && path[plen - 1] == '/') || 106 5 111
1569 116 ▶ (plen > 3 && memEQ(path + plen - 3, ".md", 3))) {
Branch: 2 branches
BranchTrueFalseTotal
(plen > 3 && memEQ(path + plen - 3, ".md", 3))) { 85 31 116
(plen > 3 && memEQ(path + plen - 3, ".md", 3))) { 6 79 85
1570 11 ▶ STRLEN keep = path[plen - 1] == '/' ? plen - 1 : plen - 3;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN keep = path[plen - 1] == '/' ? plen - 1 : plen - 3; 5 6 11
1571 11 SV *ckey = sv_2mortal(newSVpvn(path, keep));
1572 11 ▶ if (hv_exists_ent(pages, ckey, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(pages, ckey, 0)) { 5 6 11
1573 5 SV *loc = sv_2mortal(newSVsv(*av_fetch(cap, PMDC_PREFIX, 0)));
1574 5 sv_catsv(loc, ckey);
1575 5 ST(0) = sv_2mortal(pmd_redirect(aTHX_ loc));
1576 5 XSRETURN(1);
1577 }
1578 }
1579  
1580 /* the search page */
1581 {
1582 116 const char *sp = pmd_opt_str(aTHX_ opts, "search_path", "/search");
1583 116 STRLEN spl = strlen(sp);
1584 116 ▶ if (plen == spl && memEQ(path, sp, spl)) {
Branch: 2 branches
BranchTrueFalseTotal
if (plen == spl && memEQ(path, sp, spl)) { 18 98 116
if (plen == spl && memEQ(path, sp, spl)) { 13 5 18
1585 13 SV *body = sv_2mortal(pmd_search_page(aTHX_ cap, env));
1586 13 ST(0) = sv_2mortal(pmd_html_response(aTHX_ cap, 200, body,
1587 is_head));
1588 13 XSRETURN(1);
1589 }
1590 }
1591  
1592 /* the shipped stylesheet, slurped at boot and served from the mount's
1593 * assets path rather than from the documents directory */
1594 {
1595 103 const char *ap = pmd_opt_str(aTHX_ opts, "assets", "/_punk");
1596 103 STRLEN apl = strlen(ap);
1597 103 SV *css = *av_fetch(cap, PMDC_CSS, 0);
1598 /*
1599 # uncoverable condition right count:3 class:ignore_covered_err
1600 # uncoverable condition right count:4 class:ignore_covered_err */
1601 103 ▶ if (plen == apl + 8 && memEQ(path, ap, apl) &&
Branch: 2 branches
BranchTrueFalseTotal
if (plen == apl + 8 && memEQ(path, ap, apl) && 5 98 103
if (plen == apl + 8 && memEQ(path, ap, apl) && 2 3 5
1602 2 ▶ memEQ(path + apl, "/app.css", 8) && SvOK(css)) {
Branch: 2 branches
BranchTrueFalseTotal
memEQ(path + apl, "/app.css", 8) && SvOK(css)) { 2 0 2
memEQ(path + apl, "/app.css", 8) && SvOK(css)) { 2 0 2
1603 2 AV *resp = newAV(), *headers = newAV(), *bodyav = newAV();
1604 STRLEN cn;
1605 2 const char *cb = SvPV_const(css, cn);
1606 2 av_push(headers, newSVpvs("Content-Type"));
1607 2 av_push(headers, newSVpvs("text/css; charset=utf-8"));
1608 2 av_push(headers, newSVpvs("Content-Length"));
1609 2 av_push(headers, newSViv((IV)cn));
1610 2 ▶ av_push(bodyav, is_head ? newSVpvs("") : newSVpvn(cb, cn)); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
av_push(bodyav, is_head ? newSVpvs("") : newSVpvn(cb, cn)); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 2 2
1611 2 av_push(resp, newSViv(200));
1612 2 av_push(resp, newRV_noinc((SV *)headers));
1613 2 av_push(resp, newRV_noinc((SV *)bodyav));
1614 2 ST(0) = sv_2mortal(newRV_noinc((SV *)resp));
1615 2 XSRETURN(1);
1616 }
1617 }
1618  
1619 /* a page */
1620 101 key = sv_2mortal(newSVpvn(path, plen));
1621 101 he = hv_fetch_ent(pages, key, 0, 0);
1622 /*
1623 # uncoverable condition right count:2 class:ignore_covered_err
1624 # uncoverable condition right count:3 class:ignore_covered_err */
1625 101 ▶ if (he && HeVAL(he) && SvROK(HeVAL(he))) {
Branch: 3 branches
BranchTrueFalseTotal
if (he && HeVAL(he) && SvROK(HeVAL(he))) { 72 29 101
if (he && HeVAL(he) && SvROK(HeVAL(he))) { 72 0 72
if (he && HeVAL(he) && SvROK(HeVAL(he))) { 72 0 72
1626 72 AV *page = (AV *)SvRV(HeVAL(he));
1627 72 ▶ if (pmd_opt_bool(aTHX_ opts, "reload", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmd_opt_bool(aTHX_ opts, "reload", 0)) { 12 60 72
1628 Stat_t st;
1629 12 SV *file = *av_fetch(page, PMD_FILE, 0);
1630 24 if (PerlLIO_stat(SvPV_nolen(file), &st) == 0 && /* # uncoverable condition right count:1 class:ignore_covered_err */
1631 12 (IV)st.st_mtime != SvIV(*av_fetch(page, PMD_MTIME, 0))) {
1632 7 av_store(page, PMD_MTIME, newSViv((IV)st.st_mtime));
1633 7 pmd_render_page(aTHX_ cap, page);
1634 }
1635 }
1636 70 resp = pmd_html_response(aTHX_ cap, 200,
1637 70 *av_fetch(page, PMD_BODY, 0), is_head);
1638 70 ST(0) = sv_2mortal(resp);
1639 70 XSRETURN(1);
1640 }
1641  
1642 /* A tree with no index file has nothing at its root, but answering the
1643 * mount's own address with a 404 while holding a nav full of pages is
1644 * unhelpful. Send them to the first page instead. */
1645 29 ▶ if (plen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (plen == 0) { 2 27 29
1646 2 AV *order = (AV *)SvRV(*av_fetch(cap, PMDC_ORDER, 0));
1647 /*
1648 # uncoverable branch false
1649 # uncoverable condition right count:1 class:ignore_covered_err */
1650 2 ▶ if (av_len(order) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(order) >= 0) { 2 0 2
1651 2 SV *loc = sv_2mortal(newSVsv(*av_fetch(cap, PMDC_PREFIX, 0)));
1652 2 sv_catsv(loc, *av_fetch(order, 0, 0));
1653 2 ST(0) = sv_2mortal(pmd_redirect(aTHX_ loc));
1654 2 XSRETURN(1);
1655 }
1656 }
1657  
1658 /* an asset sitting in the tree: same stat / 304 / sendfile behaviour as
1659 * the static mount, because it is literally the same function.
1660 *
1661 * Never a source. Anything that reaches here ending in .md is a document
1662 * the page table chose not to publish under that name - a draft, or an
1663 * index whose page lives at the directory's URL - and serving the file
1664 * would publish it anyway, front matter and all. The suffix is compared
1665 * without case because some filesystems open draft.MD as draft.md. And
1666 * never a dotfile, which the walk skipped for the same reason. */
1667 27 ▶ if (!ps_path_unsafe(path, plen) && !ps_path_dotted(path, plen, 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!ps_path_unsafe(path, plen) && !ps_path_dotted(path, plen, 0) 25 2 27
if (!ps_path_unsafe(path, plen) && !ps_path_dotted(path, plen, 0) 22 3 25
1668 22 ▶ && !(plen >= 3 && path[plen - 3] == '.'
Branch: 2 branches
BranchTrueFalseTotal
&& !(plen >= 3 && path[plen - 3] == '.' 22 0 22
&& !(plen >= 3 && path[plen - 3] == '.' 6 16 22
1669 6 ▶ && toLOWER((U8)path[plen - 2]) == 'm'
Branch: 3 branches
BranchTrueFalseTotal
&& toLOWER((U8)path[plen - 2]) == 'm' 2 4 6
&& toLOWER((U8)path[plen - 2]) == 'm' 2 0 2
&& toLOWER((U8)path[plen - 2]) == 'm' 4 0 4
1670 6 ▶ && toLOWER((U8)path[plen - 1]) == 'd')) {
Branch: 3 branches
BranchTrueFalseTotal
&& toLOWER((U8)path[plen - 1]) == 'd')) { 1 5 6
&& toLOWER((U8)path[plen - 1]) == 'd')) { 0 1 1
&& toLOWER((U8)path[plen - 1]) == 'd')) { 0 5 5
1671 16 SV *dir = *av_fetch(cap, PMDC_DIR, 0);
1672 STRLEN dlen;
1673 16 const char *d = SvPV_const(dir, dlen);
1674 char file[MAXPATHLEN + 1];
1675 /*
1676 # uncoverable branch false
1677 # uncoverable condition right count:1 class:ignore_covered_err */
1678 16 ▶ if (dlen + plen <= MAXPATHLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (dlen + plen <= MAXPATHLEN) { 16 0 16
1679 16 memcpy(file, d, dlen);
1680 16 memcpy(file + dlen, path, plen);
1681 16 file[dlen + plen] = '\0';
1682 16 resp = ps_serve_file(aTHX_ env, file, dlen + plen, is_head);
1683 16 ▶ if (resp) { ST(0) = sv_2mortal(resp); XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (resp) { ST(0) = sv_2mortal(resp); XSRETURN(1); } 5 11 16
1684 }
1685 }
1686  
1687 22 ST(0) = sv_2mortal(pmd_html_response(aTHX_ cap, 404,
1688 *av_fetch(cap, PMDC_NOTFOUND, 0), is_head));
1689 22 XSRETURN(1);
1690 }
1691  
1692 #endif /* PUNK_MARKDOWN_H */
« include/punk/punk_cachefront.h ↑ Summary include/punk/punk_request.h »