xs/markup_profile.xs

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 93 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_text.c ↑ Summary include/pdfmake_custom_ops.h »
1 MODULE = PDF::Make PACKAGE = PDF::Make::Markup::Profile
2  
3 PROTOTYPES: DISABLE
4  
5 # The template boundary. What a template may not do is decided in
6 # src/pdfmake_markup_profile.c; the engine is still built by calling
7 # Template::Stencil's constructor, because that is its interface, but the
8 # arguments it is built with are fixed here and cannot be passed in.
9 #
10 # The filters are XSUBs. Stencil takes filters as Perl coderefs, so the only
11 # way to offer formatting without offering a way back to arbitrary code is for
12 # the coderefs to be C.
13  
14 # money: 1240.5 -> 1,240.50
15 SV *
16 _filter_money(value)
17 SV *value
18 ALIAS:
19 _filter_number = 1
20 CODE:
21 {
22 STRLEN len;
23 const char *v;
24 char out[128];
25 size_t n;
26  
27 0 ▶ if (!value || !SvOK(value)) XSRETURN_PV("");
Branch: 2 branches
BranchTrueFalseTotal
if (!value || !SvOK(value)) XSRETURN_PV(""); 0 0 0
if (!value || !SvOK(value)) XSRETURN_PV(""); 0 0 0
28 0 v = pdfmake_markup_sv_bytes(aTHX_ value, &len);
29 0 n = ix ? pdfmake_profile_number(v, len, out, sizeof(out))
30 0 ▶ : pdfmake_profile_money(v, len, out, sizeof(out));
Branch: 1 branch
BranchTrueFalseTotal
: pdfmake_profile_money(v, len, out, sizeof(out)); 0 0 0
31 0 RETVAL = newSVpvn(out, n);
32 }
33 OUTPUT:
34 RETVAL
35  
36 # Refuse {% raw %}, with the line it is on.
37 IV
38 check_source(class, src)
39 SV *class
40 SV *src
41 CODE:
42 {
43 STRLEN len;
44 const char *bytes;
45 char err[PDFMAKE_PROFILE_ERR_LEN];
46 0 uint32_t line = 0;
47  
48 PERL_UNUSED_VAR(class);
49 0 bytes = pdfmake_markup_sv_bytes(aTHX_ src, &len);
50 0 ▶ if (!pdfmake_profile_check_source(bytes, len, &line, err, sizeof(err)))
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_profile_check_source(bytes, len, &line, err, sizeof(err))) 0 0 0
51 0 croak("template error at line %" UVuf ": %s", (UV)line, err);
52 0 ▶ RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 1; 0 0 0
53 }
54 OUTPUT:
55 RETVAL
56  
57 # The filter map, as coderefs onto the XSUBs above. A copy each time: handing
58 # out the engine's own map would let a caller replace an entry in it.
59 SV *
60 filters(class = NULL)
61 SV *class
62 CODE:
63 {
64 0 HV *out = newHV();
65 0 CV *money = get_cv("PDF::Make::Markup::Profile::_filter_money", 0);
66 0 CV *number = get_cv("PDF::Make::Markup::Profile::_filter_number", 0);
67 PERL_UNUSED_VAR(class);
68 0 ▶ if (money) (void)hv_stores(out, "money", newRV_inc((SV *)money));
Branch: 1 branch
BranchTrueFalseTotal
if (money) (void)hv_stores(out, "money", newRV_inc((SV *)money)); 0 0 0
69 0 ▶ if (number) (void)hv_stores(out, "number", newRV_inc((SV *)number));
Branch: 1 branch
BranchTrueFalseTotal
if (number) (void)hv_stores(out, "number", newRV_inc((SV *)number)); 0 0 0
70 0 RETVAL = newRV_noinc((SV *)out);
71 }
72 OUTPUT:
73 RETVAL
74  
75 void
76 filter_names(class = NULL)
77 SV *class
78 PPCODE:
79 PERL_UNUSED_VAR(class);
80 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
81 0 mPUSHp("money", 5);
82 0 mPUSHp("number", 6);
83 0 XSRETURN(2);
84  
85 # A Template::Stencil configured for untrusted templates. The settings that
86 # matter are not options: escaping is on, strictness is on, and the filter map
87 # is this module's.
88 SV *
89 engine(class, ...)
90 SV *class
91 CODE:
92 {
93 HV *args;
94 int i;
95  
96 PERL_UNUSED_VAR(class);
97 0 ▶ if ((items - 1) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2) 0 0 0
98 0 croak("PDF::Make::Markup::Profile->engine: odd number of options");
99  
100 0 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 0 0 0
101 STRLEN klen;
102 0 const char *k = SvPV(ST(i), klen);
103 0 ▶ if ((klen == 7 && memcmp(k, "filters", 7) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((klen == 7 && memcmp(k, "filters", 7) == 0) || 0 0 0
if ((klen == 7 && memcmp(k, "filters", 7) == 0) || 0 0 0
104 0 ▶ (klen == 11 && memcmp(k, "auto_escape", 11) == 0))
Branch: 2 branches
BranchTrueFalseTotal
(klen == 11 && memcmp(k, "auto_escape", 11) == 0)) 0 0 0
(klen == 11 && memcmp(k, "auto_escape", 11) == 0)) 0 0 0
105 0 croak("PDF::Make::Markup::Profile: '%.*s' cannot be set. "
106 "The profile exists to fix it.", (int)klen, k);
107 }
108  
109 0 args = newHV();
110 /* A value can never become markup. */
111 0 (void)hv_stores(args, "auto_escape", newSViv(1));
112 /* A missing field is an error, not a blank space. */
113 0 (void)hv_stores(args, "strict", newSViv(1));
114 /* Iteration order must not depend on the hash seed. */
115 0 (void)hv_stores(args, "sort_keys", newSViv(1));
116 0 (void)hv_stores(args, "cache", newSViv(1));
117 0 (void)hv_stores(args, "filters",
118 pdfmake_markup_profile_filters(aTHX));
119  
120 0 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 0 0 0
121 STRLEN klen;
122 0 const char *k = SvPV(ST(i), klen);
123 0 (void)hv_store(args, k, (I32)klen, newSVsv(ST(i + 1)), 0);
124 }
125  
126 0 RETVAL = pdfmake_markup_profile_engine(aTHX_ args);
127 }
128 OUTPUT:
129 RETVAL
130  
131 # Check the source, render it, and enforce the output cap. The result is
132 # markup and nothing more: it has not been parsed and nothing is trusted yet.
133 SV *
134 render(class, src, data = &PL_sv_undef, ...)
135 SV *class
136 SV *src
137 SV *data
138 CODE:
139 {
140 0 SV *engine = NULL;
141 0 UV max = 4 * 1024 * 1024;
142 0 HV *opts = newHV();
143 int i;
144  
145 PERL_UNUSED_VAR(class);
146 0 sv_2mortal((SV *)opts);
147  
148 0 ▶ if ((items - 3) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 3) % 2) 0 0 0
149 0 croak("PDF::Make::Markup::Profile->render: odd number of options");
150  
151 0 ▶ for (i = 3; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 3; i < items; i += 2) { 0 0 0
152 STRLEN klen;
153 0 const char *k = SvPV(ST(i), klen);
154 0 ▶ if (klen == 6 && memcmp(k, "engine", 6) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 6 && memcmp(k, "engine", 6) == 0) { 0 0 0
if (klen == 6 && memcmp(k, "engine", 6) == 0) { 0 0 0
155 0 engine = ST(i + 1);
156 0 ▶ } else if (klen == 10 && memcmp(k, "max_output", 10) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 10 && memcmp(k, "max_output", 10) == 0) { 0 0 0
} else if (klen == 10 && memcmp(k, "max_output", 10) == 0) { 0 0 0
157 0 max = SvUV(ST(i + 1));
158 } else {
159 0 (void)hv_store(opts, k, (I32)klen, newSVsv(ST(i + 1)), 0);
160 }
161 }
162  
163 0 RETVAL = pdfmake_markup_profile_render(aTHX_ src, data, engine,
164 opts, max);
165 }
166 OUTPUT:
167 RETVAL
« src/pdfmake_text.c ↑ Summary include/pdfmake_custom_ops.h »