include/punk/punk_markdown.h

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