xs/markdown.xs

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