xs/markdown.xs

filter: Statementstmt 96.4% 244 / 253 Branchbran 73.2% 167 / 228 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.4% 411 / 481 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/computed.xs ↑ Summary include/punk/punk_response.h »
1 MODULE = Punk PACKAGE = Punk::Mount::Markdown
2  
3 PROTOTYPES: DISABLE
4  
5 # Punk::Mount::Markdown->app($dir, \%opts, $prefix) - the PSGI coderef
6 # serving that directory of markdown as a documentation site. The request
7 # path itself is punk_md_cb in include/punk/punk_markdown.h; everything
8 # below runs once, at boot.
9 #
10 # lib/Punk/Mount/Markdown.pm is documentation only.
11 SV *
12 app(class, dir, opts = NULL, prefix = NULL)
13 SV *class
14 SV *dir
15 SV *opts
16 SV *prefix
17 CODE:
18 {
19 126 const mds_abi *md = punk_mds(aTHX);
20 125 const sg_abi *sg = NULL;
21 125 AV *cap = newAV();
22 HV *ohv;
23 AV *rels, *order;
24 HV *pages, *docmap;
25 Stat_t st;
26 /* idx_sv stays NULL without an index so the av_store below makes a
27 * fresh undef to own. &PL_sv_undef must never go in: av_store takes
28 * over a reference, and the matching decrement when the capture is
29 * freed lands on the immortal - fatal before perl 5.20, where
30 * immortals are not yet immune to refcount juggling. */
31 125 SV *root, *tmpl_dir = NULL, *idx_sv = NULL;
32 SSize_t i, n;
33 const char *index_name;
34 int want_search;
35 125 void *idx = NULL;
36  
37 PERL_UNUSED_VAR(class);
38  
39 125 ▶ if (PerlLIO_stat(SvPV_nolen(dir), &st) < 0 || !S_ISDIR(st.st_mode))
Branch: 2 branches
BranchTrueFalseTotal
if (PerlLIO_stat(SvPV_nolen(dir), &st) < 0 || !S_ISDIR(st.st_mode)) 124 1 125
if (PerlLIO_stat(SvPV_nolen(dir), &st) < 0 || !S_ISDIR(st.st_mode)) 0 124 124
40 1 croak("Punk: markdown '%s' is not a directory", SvPV_nolen(dir));
41  
42 124 ▶ ohv = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
ohv = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 124 0 124
ohv = (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 124 0 124
43 248 ▶ ? (HV *)SvRV(opts) : newHV();
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(opts) : newHV(); 124 0 124
44 124 ▶ if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 124 0 124
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 124 0 124
if (opts && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) 124 0 124
45 124 ohv = (HV *)SvRV(newRV_inc((SV *)ohv)); /* borrow, +1 below */
46  
47 /* keep the root without a trailing slash, so joining a PATH_INFO
48 * (which always starts with one) cannot double it */
49 {
50 STRLEN dlen;
51 124 const char *p = SvPV_const(dir, dlen);
52 124 ▶ while (dlen > 1 && p[dlen - 1] == '/') dlen--;
Branch: 2 branches
BranchTrueFalseTotal
while (dlen > 1 && p[dlen - 1] == '/') dlen--; 124 0 124
while (dlen > 1 && p[dlen - 1] == '/') dlen--; 0 124 124
53 124 root = newSVpvn(p, dlen);
54 }
55  
56 124 index_name = pmd_opt_str(aTHX_ ohv, "index", "index.md");
57 124 want_search = pmd_opt_bool(aTHX_ ohv, "search", 1);
58 {
59 124 const char *how = pmd_opt_str(aTHX_ ohv, "sort", "order");
60 124 SV *sm = pmd_opt(aTHX_ ohv, "sitemap");
61 124 ▶ if (sm) {
Branch: 1 branch
BranchTrueFalseTotal
if (sm) { 3 121 124
62 STRLEN sl;
63 3 const char *sp = SvPV_const(sm, sl);
64 3 ▶ if (!(sl == 1 && (sp[0] == '0' || sp[0] == '1')))
Branch: 3 branches
BranchTrueFalseTotal
if (!(sl == 1 && (sp[0] == '0' || sp[0] == '1'))) 2 1 3
if (!(sl == 1 && (sp[0] == '0' || sp[0] == '1'))) 0 2 2
if (!(sl == 1 && (sp[0] == '0' || sp[0] == '1'))) 0 0 0
65 1 croak("Punk: markdown sitemap takes 0 or 1, not '%s'", sp);
66 }
67 123 SV *vars = pmd_opt(aTHX_ ohv, "vars");
68 123 ▶ if (vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (vars) { 11 112 123
69 HV *vh;
70 HE *he;
71 int k;
72 11 ▶ if (!(SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV)) 11 0 11
if (!(SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV)) 1 10 11
73 1 croak("Punk: markdown vars takes a hashref");
74 10 vh = (HV *)SvRV(vars);
75 139 ▶ for (k = 0; pmd_reserved[k]; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; pmd_reserved[k]; k++) 136 3 139
76 136 ▶ if (hv_exists(vh, pmd_reserved[k],
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(vh, pmd_reserved[k], 7 129 136
77 (I32)strlen(pmd_reserved[k])))
78 7 croak("Punk: markdown vars may not use '%s', which "
79 "the mount fills itself", pmd_reserved[k]);
80 /* a shallow copy, so the caller changing its hash after the
81 * keyword changes nothing the pages were built from */
82 {
83 3 HV *copy = newHV();
84 3 hv_iterinit(vh);
85 11 ▶ while ((he = hv_iternext(vh)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(vh))) 8 3 11
86 8 (void)hv_store_ent(copy, hv_iterkeysv(he),
87 newSVsv(HeVAL(he)), 0);
88 3 (void)hv_stores(ohv, "vars", newRV_noinc((SV *)copy));
89 }
90 }
91 115 ▶ if (!strEQ(how, "order") && !strEQ(how, "alpha")
Branch: 2 branches
BranchTrueFalseTotal
if (!strEQ(how, "order") && !strEQ(how, "alpha") 7 108 115
if (!strEQ(how, "order") && !strEQ(how, "alpha") 6 1 7
92 6 ▶ && !strEQ(how, "date"))
Branch: 1 branch
BranchTrueFalseTotal
&& !strEQ(how, "date")) 1 5 6
93 1 croak("Punk: markdown sort must be order, alpha or date, "
94 "not '%s'", how);
95 }
96  
97 114 pages = newHV();
98 114 order = newAV();
99 114 docmap = newHV();
100  
101 /* The engines, created once here: three Perl calls at boot, so a bad
102 * option fails with the rest of the configuration rather than on the
103 * first request. */
104 {
105 SV *argv[1];
106 114 HV *mopts = newHV();
107 114 (void)hv_stores(mopts, "gfm",
108 newSViv(pmd_opt_bool(aTHX_ ohv, "gfm", 1)));
109 114 (void)hv_stores(mopts, "highlight",
110 newSViv(pmd_opt_bool(aTHX_ ohv, "highlight", 1)));
111 114 (void)hv_stores(mopts, "heading_ids", newSViv(1));
112 114 argv[0] = sv_2mortal(newRV_noinc((SV *)mopts));
113 114 av_store(cap, PMDC_MD,
114 pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Markdown::Simple")),
115 "new", argv, 1, 1));
116 }
117 {
118 SV *argv[1];
119 114 HV *sopts = newHV();
120 114 SV *tdir = pmd_opt(aTHX_ ohv, "template_dir");
121 114 ▶ if (tdir) tmpl_dir = sv_2mortal(newSVsv(tdir));
Branch: 1 branch
BranchTrueFalseTotal
if (tdir) tmpl_dir = sv_2mortal(newSVsv(tdir)); 16 98 114
122 else {
123 /* the templates that ship beside lib/Punk/Mount/Markdown.pm,
124 * located the way Open::API::UI locates its own */
125 98 HV *inc = get_hv("INC", 0);
126 SV **pm;
127 98 (void)pk_require_once(aTHX_ PK_MOUNT_MD, FALSE);
128 98 inc = get_hv("INC", 0);
129 98 ▶ pm = inc ? hv_fetchs(inc, "Punk/Mount/Markdown.pm", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pm = inc ? hv_fetchs(inc, "Punk/Mount/Markdown.pm", 0) : NULL; 98 0 98
130 98 ▶ if (!pm || !*pm || !SvOK(*pm))
Branch: 3 branches
BranchTrueFalseTotal
if (!pm || !*pm || !SvOK(*pm)) 98 0 98
if (!pm || !*pm || !SvOK(*pm)) 98 0 98
if (!pm || !*pm || !SvOK(*pm)) 0 98 98
131 0 croak("Punk::Mount::Markdown: cannot locate my own "
132 "installation to find the default templates");
133 98 tmpl_dir = sv_2mortal(newSVsv(*pm));
134 98 ▶ if (SvCUR(tmpl_dir) > 3)
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(tmpl_dir) > 3) 98 0 98
135 98 SvCUR_set(tmpl_dir, SvCUR(tmpl_dir) - 3);
136 98 sv_catpvs(tmpl_dir, "/templates");
137 }
138 114 (void)hv_stores(sopts, "template_dir", newSVsv(tmpl_dir));
139 114 (void)hv_stores(sopts, "wrapper",
140 newSVpv(pmd_opt_str(aTHX_ ohv, "wrapper", "wrapper.tmpl"), 0));
141 {
142 114 SV *f = pmd_opt(aTHX_ ohv, "filters");
143 114 ▶ if (f) {
Branch: 1 branch
BranchTrueFalseTotal
if (f) { 3 111 114
144 3 ▶ if (!(SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVHV))
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVHV)) 3 0 3
if (!(SvROK(f) && SvTYPE(SvRV(f)) == SVt_PVHV)) 1 2 3
145 1 croak("Punk: markdown filters takes a hashref of "
146 "name => coderef");
147 2 (void)hv_stores(sopts, "filters", newSVsv(f));
148 }
149 }
150 /* punk_st resolves the ABI but the constructor below is an
151 * ordinary method call, so the class has to be loaded too. */
152 113 (void)punk_st(aTHX);
153 113 ▶ if (!pk_require_once(aTHX_ "Template::Stencil", FALSE))
Branch: 1 branch
BranchTrueFalseTotal
if (!pk_require_once(aTHX_ "Template::Stencil", FALSE)) 0 113 113
154 0 ▶ croak("Punk::Mount::Markdown: Template::Stencil failed to "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Mount::Markdown: Template::Stencil failed to " 0 0 0
155 "load: %s", SvPV_nolen(ERRSV));
156 113 argv[0] = sv_2mortal(newRV_noinc((SV *)sopts));
157 113 av_store(cap, PMDC_STENCIL,
158 pcx_call_meth(aTHX_ sv_2mortal(newSVpvs("Template::Stencil")),
159 "new", argv, 1, 1));
160 }
161 113 ▶ if (want_search) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_search) { 39 74 113
162 39 sg = punk_sg(aTHX);
163 38 idx_sv = pcx_call_meth(aTHX_
164 sv_2mortal(newSVpvs("Search::Trigram")),
165 "new", NULL, 0, 1);
166 38 ▶ idx = idx_sv ? sg->index_of(aTHX_ idx_sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
idx = idx_sv ? sg->index_of(aTHX_ idx_sv) : NULL; 38 0 38
167 }
168  
169 112 av_store(cap, PMDC_DIR, root);
170 112 av_store(cap, PMDC_PAGES, newRV_noinc((SV *)pages));
171 112 av_store(cap, PMDC_ORDER, newRV_noinc((SV *)order));
172 112 av_store(cap, PMDC_OPTS, newRV_inc((SV *)ohv));
173 112 ▶ av_store(cap, PMDC_INDEX, idx_sv ? idx_sv : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_store(cap, PMDC_INDEX, idx_sv ? idx_sv : newSV(0)); 74 38 112
174 112 av_store(cap, PMDC_DOCMAP, newRV_noinc((SV *)docmap));
175 112 ▶ av_store(cap, PMDC_PREFIX,
Branch: 2 branches
BranchTrueFalseTotal
av_store(cap, PMDC_PREFIX, 112 0 112
av_store(cap, PMDC_PREFIX, 112 0 112
176 prefix && SvOK(prefix) ? newSVsv(prefix) : newSVpvs(""));
177  
178 /* ---- walk, then read every page's metadata ---- */
179 112 rels = (AV *)sv_2mortal((SV *)newAV());
180 224 pmd_walk_dir(aTHX_ SvPV_nolen(root), sv_2mortal(newSVpvs("")), rels,
181 pmd_opt_bool(aTHX_ ohv, "recursive", 1));
182  
183 112 n = av_len(rels) + 1;
184 594 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 496 98 594
185 496 SV *rel = *av_fetch(rels, i, 0);
186 496 AV *page = newAV();
187 496 SV *full = sv_2mortal(newSVsv(root));
188 496 SV *src, *title = NULL, *nav = NULL;
189 496 HV *meta = (HV *)sv_2mortal((SV *)newHV());
190 STRLEN blen, off;
191 const char *bytes;
192 SV *v;
193 Stat_t pst;
194  
195 496 sv_catpvs(full, "/"); sv_catsv(full, rel);
196 496 src = pmd_slurp(aTHX_ SvPV_nolen(full));
197 564 ▶ if (!src) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!src) continue; 0 496 496
198 496 bytes = SvPV_const(src, blen);
199 496 off = pmd_front_matter(aTHX_ bytes, blen, meta);
200  
201 496 ▶ if ((v = pmd_opt(aTHX_ meta, "draft")) && SvTRUE(v)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if ((v = pmd_opt(aTHX_ meta, "draft")) && SvTRUE(v)) continue; 68 428 496
if ((v = pmd_opt(aTHX_ meta, "draft")) && SvTRUE(v)) continue; 68 0 68
202  
203 428 av_extend(page, PMD__COUNT - 1);
204 428 av_store(page, PMD_URL, pmd_url_for(aTHX_ rel, index_name));
205 428 av_store(page, PMD_FILE, newSVsv(full));
206 428 av_store(page, PMD_SECTION, pmd_section_for(aTHX_ rel));
207 428 pmd_page_dates(aTHX_ page, meta);
208  
209 414 ▶ if ((v = pmd_opt(aTHX_ meta, "title"))) title = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmd_opt(aTHX_ meta, "title"))) title = newSVsv(v); 70 344 414
210 414 ▶ if (!title) title = pmd_first_h1(aTHX_ bytes + off, blen - off);
Branch: 1 branch
BranchTrueFalseTotal
if (!title) title = pmd_first_h1(aTHX_ bytes + off, blen - off); 344 70 414
211 414 ▶ if (!title) {
Branch: 1 branch
BranchTrueFalseTotal
if (!title) { 0 414 414
212 /* the basename, prettified. memrchr is a GNU extension, so
213 * scan back by hand rather than assume it. */
214 STRLEN rl, k;
215 0 const char *r = SvPV_const(rel, rl);
216 0 const char *b = r;
217 STRLEN bl;
218 0 ▶ for (k = rl; k > 0; k--)
Branch: 1 branch
BranchTrueFalseTotal
for (k = rl; k > 0; k--) 0 0 0
219 0 ▶ if (r[k - 1] == '/') { b = r + k; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (r[k - 1] == '/') { b = r + k; break; } 0 0 0
220 0 bl = rl - (STRLEN)(b - r);
221 0 ▶ if (bl > 3) bl -= 3; /* drop ".md" */
Branch: 1 branch
BranchTrueFalseTotal
if (bl > 3) bl -= 3; /* drop ".md" */ 0 0 0
222 0 title = pmd_title_from_name(aTHX_ b, bl);
223 }
224 414 av_store(page, PMD_TITLE, title);
225  
226 414 ▶ if ((v = pmd_opt(aTHX_ meta, "nav"))) nav = newSVsv(v);
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmd_opt(aTHX_ meta, "nav"))) nav = newSVsv(v); 54 360 414
227 414 ▶ av_store(page, PMD_NAV, nav ? nav : newSVsv(title));
Branch: 1 branch
BranchTrueFalseTotal
av_store(page, PMD_NAV, nav ? nav : newSVsv(title)); 360 54 414
228  
229 /* An index file is the landing page for whatever it sits in, so
230 * it leads its section unless it says otherwise. Without this it
231 * would sort last, the front page of a guide arriving underneath
232 * the pages it introduces. */
233 414 v = pmd_opt(aTHX_ meta, "order");
234 414 ▶ if (v) av_store(page, PMD_ORDER, newSViv(SvIV(v)));
Branch: 1 branch
BranchTrueFalseTotal
if (v) av_store(page, PMD_ORDER, newSViv(SvIV(v))); 116 298 414
235 else {
236 298 STRLEN rl, il = strlen(index_name);
237 298 const char *r = SvPV_const(rel, rl);
238 245 ▶ int is_index = rl >= il && memEQ(r + rl - il, index_name, il)
Branch: 1 branch
BranchTrueFalseTotal
int is_index = rl >= il && memEQ(r + rl - il, index_name, il) 68 177 245
239 543 ▶ && (rl == il || r[rl - il - 1] == '/');
Branch: 3 branches
BranchTrueFalseTotal
&& (rl == il || r[rl - il - 1] == '/'); 245 53 298
&& (rl == il || r[rl - il - 1] == '/'); 7 61 68
&& (rl == il || r[rl - il - 1] == '/'); 7 0 7
240 298 ▶ av_store(page, PMD_ORDER, newSViv(is_index ? IV_MIN : IV_MAX));
Branch: 1 branch
BranchTrueFalseTotal
av_store(page, PMD_ORDER, newSViv(is_index ? IV_MIN : IV_MAX)); 68 230 298
241 }
242  
243 /* A section's index page may name its section and place it
244 * among the others: `section:` is the sidebar group's label,
245 * `section_order:` its position. Kept on every page here; only a
246 * section index's are read, below. */
247 414 v = pmd_opt(aTHX_ meta, "section");
248 414 ▶ av_store(page, PMD_SECLABEL, v ? newSVsv(v) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_store(page, PMD_SECLABEL, v ? newSVsv(v) : newSV(0)); 2 412 414
249 414 v = pmd_opt(aTHX_ meta, "section_order");
250 414 ▶ av_store(page, PMD_SECORDER, v ? newSViv(SvIV(v)) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
av_store(page, PMD_SECORDER, v ? newSViv(SvIV(v)) : newSV(0)); 3 411 414
251  
252 414 ▶ av_store(page, PMD_MTIME,
Branch: 1 branch
BranchTrueFalseTotal
av_store(page, PMD_MTIME, 414 0 414
253 newSViv(PerlLIO_stat(SvPV_nolen(full), &pst) == 0
254 ? (IV)pst.st_mtime : 0));
255  
256 /* plain text for the index and for snippets */
257 {
258 414 SV *plain = newSVpvs("");
259 414 SV *err = NULL;
260 414 (void)md->strip(aTHX_ bytes + off, blen - off, plain, &err);
261 414 av_store(page, PMD_PLAIN, plain);
262 414 ▶ if (idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx) { 177 237 414
263 177 SV *doc = sv_2mortal(newSVsv(*av_fetch(page, PMD_TITLE, 0)));
264 uint32_t id;
265 177 sv_catpvs(doc, "\n");
266 177 sv_catsv(doc, plain);
267 177 id = sg->add(idx, SvPVX(doc), (uint32_t)SvCUR(doc));
268 177 av_store(page, PMD_DOCID, newSVuv(id));
269 177 (void)hv_store_ent(docmap, sv_2mortal(newSVuv(id)),
270 newSVsv(*av_fetch(page, PMD_URL, 0)), 0);
271 }
272 237 else av_store(page, PMD_DOCID, newSViv(-1));
273 }
274 414 av_store(page, PMD_BODY, newSVpvs(""));
275  
276 414 (void)hv_store_ent(pages, *av_fetch(page, PMD_URL, 0),
277 newRV_noinc((SV *)page), 0);
278 414 av_push(order, newSVsv(*av_fetch(page, PMD_URL, 0)));
279 }
280 98 ▶ if (idx) sg->optimize(idx);
Branch: 1 branch
BranchTrueFalseTotal
if (idx) sg->optimize(idx); 38 60 98
281  
282 /* ---- name and place every section ----
283 * From its index page (`guide/index.md` for `guide`) when it says;
284 * otherwise the directory's name, prettified, and after every
285 * section that was given a place, in name order as before. Root
286 * pages have no section and come first. */
287 {
288 98 HV *secs = (HV *)sv_2mortal((SV *)newHV());
289 98 SSize_t j, m = av_len(order) + 1;
290 512 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 414 98 512
291 414 SV **u = av_fetch(order, j, 0);
292 414 ▶ HE *he = u ? hv_fetch_ent(pages, *u, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = u ? hv_fetch_ent(pages, *u, 0, 0) : NULL; 414 0 414
293 AV *pg;
294 SV *sec, *url;
295 STRLEN sl, ul;
296 const char *ss, *us;
297 414 ▶ if (!he) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!he) continue; 0 414 414
298 414 pg = (AV *)SvRV(HeVAL(he));
299 414 sec = *av_fetch(pg, PMD_SECTION, 0);
300 414 url = *av_fetch(pg, PMD_URL, 0);
301 414 ▶ ss = SvPV_const(sec, sl);
Branch: 1 branch
BranchTrueFalseTotal
ss = SvPV_const(sec, sl); 414 0 414
302 414 us = SvPV_const(url, ul);
303 414 ▶ if (sl && ul == sl + 1 && us[0] == '/' && memEQ(us + 1, ss, sl))
Branch: 4 branches
BranchTrueFalseTotal
if (sl && ul == sl + 1 && us[0] == '/' && memEQ(us + 1, ss, sl)) 245 169 414
if (sl && ul == sl + 1 && us[0] == '/' && memEQ(us + 1, ss, sl)) 7 238 245
if (sl && ul == sl + 1 && us[0] == '/' && memEQ(us + 1, ss, sl)) 7 0 7
if (sl && ul == sl + 1 && us[0] == '/' && memEQ(us + 1, ss, sl)) 7 0 7
304 7 (void)hv_store(secs, ss, (I32)sl, newRV_inc((SV *)pg), 0);
305 }
306 512 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 414 98 512
307 414 SV **u = av_fetch(order, j, 0);
308 414 ▶ HE *he = u ? hv_fetch_ent(pages, *u, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *he = u ? hv_fetch_ent(pages, *u, 0, 0) : NULL; 414 0 414
309 414 AV *pg, *ix = NULL;
310 414 SV *sec, **ixp, *label = NULL;
311 IV place;
312 STRLEN sl;
313 const char *ss;
314 583 ▶ if (!he) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!he) continue; 0 414 414
315 414 pg = (AV *)SvRV(HeVAL(he));
316 414 sec = *av_fetch(pg, PMD_SECTION, 0);
317 414 ss = SvPV_const(sec, sl);
318 414 ▶ if (!sl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sl) { 169 245 414
319 169 av_store(pg, PMD_SECLABEL, newSVpvs(""));
320 169 av_store(pg, PMD_SECORDER, newSViv(IV_MIN));
321 169 continue;
322 }
323 245 ixp = hv_fetch(secs, ss, (I32)sl, 0);
324 245 ▶ if (ixp) ix = (AV *)SvRV(*ixp);
Branch: 1 branch
BranchTrueFalseTotal
if (ixp) ix = (AV *)SvRV(*ixp); 14 231 245
325 245 ▶ if (ix && SvOK(*av_fetch(ix, PMD_SECLABEL, 0)))
Branch: 2 branches
BranchTrueFalseTotal
if (ix && SvOK(*av_fetch(ix, PMD_SECLABEL, 0))) 14 231 245
if (ix && SvOK(*av_fetch(ix, PMD_SECLABEL, 0))) 8 6 14
326 8 label = newSVsv(*av_fetch(ix, PMD_SECLABEL, 0));
327 14 ▶ place = ix && SvOK(*av_fetch(ix, PMD_SECORDER, 0))
Branch: 1 branch
BranchTrueFalseTotal
place = ix && SvOK(*av_fetch(ix, PMD_SECORDER, 0)) 9 5 14
328 259 ▶ ? SvIV(*av_fetch(ix, PMD_SECORDER, 0)) : IV_MAX;
Branch: 1 branch
BranchTrueFalseTotal
? SvIV(*av_fetch(ix, PMD_SECORDER, 0)) : IV_MAX; 14 231 245
329 245 ▶ av_store(pg, PMD_SECLABEL,
Branch: 1 branch
BranchTrueFalseTotal
av_store(pg, PMD_SECLABEL, 237 8 245
330 label ? label : pmd_title_from_name(aTHX_ ss, sl));
331 245 av_store(pg, PMD_SECORDER, newSViv(place));
332 }
333 }
334  
335 /* ---- order the nav ---- */
336 {
337 98 const char *how = pmd_opt_str(aTHX_ ohv, "sort", "order");
338 /* `order` groups by section then honours front-matter order:,
339 * falling back to the title. `alpha` is the walk order, which is
340 * already sorted by path. Both keep a section's pages together,
341 * which is what a sidebar needs. */
342 98 ▶ if (strEQ(how, "order")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(how, "order")) { 92 6 98
343 92 AV *sorted = newAV();
344 92 SSize_t j, m = av_len(order) + 1;
345 92 SSize_t placed = 0;
346 474 ▶ while (placed < m) {
Branch: 1 branch
BranchTrueFalseTotal
while (placed < m) { 382 92 474
347 382 SSize_t best = -1;
348 2280 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 1898 382 2280
349 1898 SV **u = av_fetch(order, j, 0);
350 HE *he;
351 AV *pg, *bp;
352 1898 ▶ if (!u || !*u || !SvOK(*u)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!u || !*u || !SvOK(*u)) continue; 1898 0 1898
if (!u || !*u || !SvOK(*u)) continue; 1898 0 1898
if (!u || !*u || !SvOK(*u)) continue; 758 1140 1898
353 1140 he = hv_fetch_ent(pages, *u, 0, 0);
354 1140 ▶ if (!he || !HeVAL(he)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!he || !HeVAL(he)) continue; 1140 0 1140
if (!he || !HeVAL(he)) continue; 0 1140 1140
355 1140 pg = (AV *)SvRV(HeVAL(he));
356 1140 ▶ if (best < 0) { best = j; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (best < 0) { best = j; continue; } 382 758 1140
357 {
358 758 SV **b = av_fetch(order, best, 0);
359 758 HE *bhe = hv_fetch_ent(pages, *b, 0, 0);
360 int c;
361 IV ps, bs;
362 758 bp = (AV *)SvRV(HeVAL(bhe));
363 758 ps = SvIV(*av_fetch(pg, PMD_SECORDER, 0));
364 758 bs = SvIV(*av_fetch(bp, PMD_SECORDER, 0));
365 758 ▶ if (ps < bs) { best = j; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (ps < bs) { best = j; continue; } 75 683 758
366 683 ▶ if (ps > bs) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (ps > bs) continue; 172 511 683
367 511 c = sv_cmp(*av_fetch(pg, PMD_SECTION, 0),
368 *av_fetch(bp, PMD_SECTION, 0));
369 511 ▶ if (c < 0) { best = j; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0) { best = j; continue; } 0 511 511
370 511 ▶ if (c > 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c > 0) continue; 216 295 511
371 590 ▶ if (SvIV(*av_fetch(pg, PMD_ORDER, 0)) <
Branch: 1 branch
BranchTrueFalseTotal
if (SvIV(*av_fetch(pg, PMD_ORDER, 0)) < 115 180 295
372 295 SvIV(*av_fetch(bp, PMD_ORDER, 0)))
373 115 best = j;
374 }
375 }
376 382 ▶ if (best < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (best < 0) break; 0 382 382
377 382 av_push(sorted, newSVsv(*av_fetch(order, best, 0)));
378 382 av_store(order, best, newSV(0));
379 382 placed++;
380 }
381 92 av_clear(order);
382 474 ▶ for (j = 0; j <= av_len(sorted); j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j <= av_len(sorted); j++) 382 92 474
383 382 av_push(order, newSVsv(*av_fetch(sorted, j, 0)));
384 92 SvREFCNT_dec((SV *)sorted);
385 }
386 /* `date` is a blog: flat, newest first, sections ignored. Ties go
387 * to the title and then the path, so the order is the same on
388 * every filesystem; undated pages follow, in path order. */
389 6 ▶ else if (strEQ(how, "date")) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(how, "date")) { 5 1 6
390 5 SSize_t j, m = av_len(order) + 1;
391 pmd_sortkey *keys;
392 5 ▶ Newx(keys, m > 0 ? m : 1, pmd_sortkey);
Branch: 3 branches
BranchTrueFalseTotal
Newx(keys, m > 0 ? m : 1, pmd_sortkey); 5 0 5
Newx(keys, m > 0 ? m : 1, pmd_sortkey); 0 5 5
Newx(keys, m > 0 ? m : 1, pmd_sortkey); 5 0 5
393 5 SAVEFREEPV(keys);
394 33 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 28 5 33
395 28 SV *u = *av_fetch(order, j, 0);
396 28 HE *he = hv_fetch_ent(pages, u, 0, 0);
397 28 AV *pg = (AV *)SvRV(HeVAL(he));
398 28 SV *d = *av_fetch(pg, PMD_DATE, 0);
399 28 SV *t = *av_fetch(pg, PMD_TITLE, 0);
400 28 keys[j].dated = SvOK(d) ? 1 : 0;
401 28 ▶ keys[j].date = SvOK(d) ? SvIV(d) : 0;
Branch: 1 branch
BranchTrueFalseTotal
keys[j].date = SvOK(d) ? SvIV(d) : 0; 19 9 28
402 28 keys[j].title = SvPV(t, keys[j].tlen);
403 28 ▶ keys[j].url = SvPV(u, keys[j].ulen);
Branch: 1 branch
BranchTrueFalseTotal
keys[j].url = SvPV(u, keys[j].ulen); 28 0 28
404 28 keys[j].sv = u;
405 }
406 5 qsort(keys, (size_t)m, sizeof *keys, pmd_sortkey_cmp);
407 {
408 5 AV *sorted = (AV *)sv_2mortal((SV *)newAV());
409 33 ▶ for (j = 0; j < m; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) 28 5 33
410 28 av_push(sorted, newSVsv(keys[j].sv));
411 5 av_clear(order);
412 33 ▶ for (j = 0; j < m; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) 28 5 33
413 28 av_push(order, newSVsv(*av_fetch(sorted, j, 0)));
414 }
415 }
416 }
417  
418 /* ---- the shipped stylesheet, read once ----
419 * Slurped rather than rendered: CSS is full of braces and has no
420 * business going through a template engine. It lives beside the
421 * templates so that a template_dir override carries its own
422 * stylesheet, which is what a caller replacing the shell wants. */
423 {
424 98 SV *cssfile = sv_2mortal(newSVsv(tmpl_dir));
425 SV *css;
426 98 sv_catpvs(cssfile, "/app.css");
427 98 css = pmd_slurp(aTHX_ SvPV_nolen(cssfile));
428 98 ▶ av_store(cap, PMDC_CSS, css ? newSVsv(css) : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
av_store(cap, PMDC_CSS, css ? newSVsv(css) : newSVpvs("")); 98 0 98
429 }
430  
431 /* ---- render every page, and the 404 ---- */
432 98 av_store(cap, PMDC_NOTFOUND, newSVpvs(""));
433 98 n = av_len(order) + 1;
434 511 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 414 97 511
435 414 HE *he = hv_fetch_ent(pages, *av_fetch(order, i, 0), 0, 0);
436 414 ▶ if (he && HeVAL(he) && SvROK(HeVAL(he)))
Branch: 3 branches
BranchTrueFalseTotal
if (he && HeVAL(he) && SvROK(HeVAL(he))) 414 0 414
if (he && HeVAL(he) && SvROK(HeVAL(he))) 414 0 414
if (he && HeVAL(he) && SvROK(HeVAL(he))) 414 0 414
437 414 pmd_render_page(aTHX_ cap, (AV *)SvRV(HeVAL(he)));
438 }
439 {
440 97 SV *nav = sv_2mortal(pmd_nav_html(aTHX_ order, pages,
441 *av_fetch(cap, PMDC_PREFIX, 0),
442 sv_2mortal(newSVpvs("\x01none"))));
443 97 SV *body = pmd_shell(aTHX_ cap, "notfound",
444 sv_2mortal(newSVpvs("Not Found")), nav,
445 sv_2mortal(newSVpvs(
446 "<h1>Not Found</h1>\n<p>There is no page at "
447 "this address.</p>\n")),
448 NULL, sv_2mortal(newSVpvs("")), NULL);
449 97 av_store(cap, PMDC_NOTFOUND, body);
450 }
451  
452 97 RETVAL = punk_closure(aTHX_ punk_md_cb, cap);
453 }
454 OUTPUT:
455 RETVAL
456  
457 MODULE = Punk PACKAGE = Punk
458  
459 # Whether the Markdown::Simple / Search::Trigram C ABIs resolved, for
460 # `punk doctor`. Not part of the public Perl API.
461 IV
462 _mds_available()
463 CODE:
464 5 ▶ RETVAL = punk_mds_try(aTHX) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_mds_try(aTHX) ? 1 : 0; 0 5 5
465 OUTPUT:
466 RETVAL
467  
468 IV
469 _mds_abi_version()
470 CODE:
471 5 ▶ RETVAL = MDS_ABI_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = MDS_ABI_VERSION; 0 5 5
472 OUTPUT:
473 RETVAL
474  
475 IV
476 _sg_available()
477 CODE:
478 5 ▶ RETVAL = punk_sg_try(aTHX) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = punk_sg_try(aTHX) ? 1 : 0; 0 5 5
479 OUTPUT:
480 RETVAL
481  
482 IV
483 _sg_abi_version()
484 CODE:
485 5 ▶ RETVAL = SG_ABI_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SG_ABI_VERSION; 0 5 5
486 OUTPUT:
487 RETVAL
« xs/computed.xs ↑ Summary include/punk/punk_response.h »