include/eshu_pod.h

filter: Statementstmt 100% 120 / 120 Branchbran 77.7% 84 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.4% 204 / 228 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/eshu_css.h ↑ Summary include/eshu.h »
1 /*
2 * eshu_pod.h — POD (Plain Old Documentation) indentation scanner
3 *
4 * Normalizes indentation within POD sections:
5 * - Directives (=head1, =over, =item, etc.) stay at column 0
6 * - Text paragraphs stay at column 0
7 * - Verbatim/code blocks (lines starting with whitespace) are
8 * re-indented to a consistent level (one indent unit by default)
9 * - =over/=back nesting is NOT tracked for text indent (that's
10 * a formatter's job) but IS respected for code block context
11 */
12  
13 #ifndef ESHU_POD_H
14 #define ESHU_POD_H
15  
16 #include "eshu.h"
17  
18 /* ══════════════════════════════════════════════════════════════════
19 * Scanner context — persists across lines
20 * ══════════════════════════════════════════════════════════════════ */
21  
22 typedef struct {
23 int in_verbatim; /* currently in a code block? */
24 int verbatim_base; /* base indent of current block */
25 int over_depth; /* =over nesting depth */
26 eshu_config_t cfg;
27 } eshu_pod_ctx_t;
28  
29 104 static void eshu_pod_ctx_init(eshu_pod_ctx_t *ctx, const eshu_config_t *cfg) {
30 104 ctx->in_verbatim = 0;
31 104 ctx->verbatim_base = 0;
32 104 ctx->over_depth = 0;
33 104 ctx->cfg = *cfg;
34 104 }
35  
36 /* ══════════════════════════════════════════════════════════════════
37 * Detect POD directive lines (=head1, =over, =item, =cut, etc.)
38 * These are lines starting with '=' followed by a letter.
39 * ══════════════════════════════════════════════════════════════════ */
40  
41 1695 static int eshu_pod_is_directive(const char *p) {
42 1695 ▶ return (*p == '=' && isalpha((unsigned char)p[1]));
Branch: 2 branches
BranchTrueFalseTotal
return (*p == '=' && isalpha((unsigned char)p[1])); 627 1068 1695
return (*p == '=' && isalpha((unsigned char)p[1])); 627 0 627
43 }
44  
45 /* Case-insensitive prefix match for directive names */
46 1211 static int eshu_pod_directive_is(const char *p, const char *name, int nlen) {
47 int i;
48 1211 ▶ if (*p != '=') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '=') return 0; 0 1211 1211
49 1211 p++;
50 1562 ▶ for (i = 0; i < nlen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nlen; i++) { 1476 86 1562
51 1476 ▶ if (!p[i]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!p[i]) return 0; 0 1476 1476
52 1476 ▶ if (tolower((unsigned char)p[i]) != name[i]) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (tolower((unsigned char)p[i]) != name[i]) return 0; 1125 351 1476
53 }
54 /* must be followed by space, digit, or end of meaningful chars */
55 86 ▶ if (p[nlen] && isalpha((unsigned char)p[nlen])) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p[nlen] && isalpha((unsigned char)p[nlen])) return 0; 86 0 86
if (p[nlen] && isalpha((unsigned char)p[nlen])) return 0; 0 86 86
56 86 return 1;
57 }
58  
59 /* ══════════════════════════════════════════════════════════════════
60 * Find the minimum leading whitespace in a block of verbatim lines.
61 * Used for normalization: we strip this common prefix and re-indent.
62 * ══════════════════════════════════════════════════════════════════ */
63  
64 452 static int eshu_pod_measure_indent(const char *line_start, const char *eol) {
65 452 int n = 0;
66 452 const char *p = line_start;
67 1618 ▶ while (p < eol && (*p == ' ' || *p == '\t')) {
Branch: 3 branches
BranchTrueFalseTotal
while (p < eol && (*p == ' ' || *p == '\t')) { 1618 0 1618
while (p < eol && (*p == ' ' || *p == '\t')) { 817 801 1618
while (p < eol && (*p == ' ' || *p == '\t')) { 349 452 801
68 1166 ▶ if (*p == '\t')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\t') 349 817 1166
69 349 n += 4; /* count tab as 4 spaces for measurement */
70 else
71 817 n++;
72 1166 p++;
73 }
74 452 return n;
75 }
76  
77 /* ══════════════════════════════════════════════════════════════════
78 * Process a single POD line
79 * ══════════════════════════════════════════════════════════════════ */
80  
81 2690 static void eshu_pod_process_line(eshu_pod_ctx_t *ctx, eshu_buf_t *out,
82 const char *line_start, const char *eol) {
83 2690 const char *content = eshu_skip_leading_ws(line_start);
84 2690 int content_len = (int)(eol - content);
85 2690 int has_leading_ws = (content > line_start);
86  
87 /* ── Blank line ── */
88 2690 ▶ if (content >= eol) {
Branch: 1 branch
BranchTrueFalseTotal
if (content >= eol) { 995 1695 2690
89 995 ctx->in_verbatim = 0;
90 995 eshu_buf_putc(out, '\n');
91 995 return;
92 }
93  
94 /* ── Directive line (=something) — always column 0 ── */
95 1695 ▶ if (eshu_pod_is_directive(content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_pod_is_directive(content)) { 627 1068 1695
96 627 ctx->in_verbatim = 0;
97  
98 /* Track =over/=back depth */
99 627 ▶ if (eshu_pod_directive_is(content, "over", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_pod_directive_is(content, "over", 4)) { 43 584 627
100 43 ctx->over_depth++;
101 584 ▶ } else if (eshu_pod_directive_is(content, "back", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (eshu_pod_directive_is(content, "back", 4)) { 43 541 584
102 43 ctx->over_depth--;
103 43 ▶ if (ctx->over_depth < 0) ctx->over_depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->over_depth < 0) ctx->over_depth = 0; 0 43 43
104 }
105  
106 /* Emit at column 0 (strip any accidental leading whitespace) */
107 627 eshu_buf_write_trimmed(out, content, content_len);
108 627 eshu_buf_putc(out, '\n');
109 627 return;
110 }
111  
112 /* ── Verbatim/code block: line starts with whitespace ── */
113 1068 ▶ if (has_leading_ws) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_leading_ws) { 452 616 1068
114 452 int line_indent = eshu_pod_measure_indent(line_start, eol);
115  
116 452 ▶ if (!ctx->in_verbatim) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->in_verbatim) { 189 263 452
117 /* First line of a new verbatim block — record base indent */
118 189 ctx->in_verbatim = 1;
119 189 ctx->verbatim_base = line_indent;
120 }
121  
122 /* Compute relative indent beyond the block's base */
123 452 int extra = line_indent - ctx->verbatim_base;
124 452 ▶ if (extra < 0) extra = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (extra < 0) extra = 0; 0 452 452
125  
126 /* Emit: one base indent level + extra relative whitespace */
127 452 eshu_emit_indent(out, 1, &ctx->cfg);
128 {
129 int i;
130 862 ▶ for (i = 0; i < extra; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < extra; i++) 410 452 862
131 410 eshu_buf_putc(out, ' ');
132 }
133 452 eshu_buf_write_trimmed(out, content, content_len);
134 452 eshu_buf_putc(out, '\n');
135 452 return;
136 }
137  
138 /* ── Normal text paragraph: stays at column 0 ── */
139 616 ctx->in_verbatim = 0;
140 616 eshu_buf_write_trimmed(out, content, content_len);
141 616 eshu_buf_putc(out, '\n');
142 }
143  
144 /* ══════════════════════════════════════════════════════════════════
145 * Public API — indent a POD string
146 * ══════════════════════════════════════════════════════════════════ */
147  
148 104 static char * eshu_indent_pod(const char *src, size_t src_len,
149 const eshu_config_t *cfg, size_t *out_len) {
150 eshu_pod_ctx_t ctx;
151 eshu_buf_t out;
152 104 const char *p = src;
153 104 const char *end = src + src_len;
154 char *result;
155  
156 104 eshu_pod_ctx_init(&ctx, cfg);
157 104 eshu_buf_init(&out, src_len + 256);
158  
159 2794 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 2690 104 2794
160 2690 const char *eol = eshu_find_eol(p);
161 2690 eshu_pod_process_line(&ctx, &out, p, eol);
162 2690 ▶ p = (*eol == '\n') ? eol + 1 : eol;
Branch: 1 branch
BranchTrueFalseTotal
p = (*eol == '\n') ? eol + 1 : eol; 2690 0 2690
163 2690 ▶ if (p > end) p = end;
Branch: 1 branch
BranchTrueFalseTotal
if (p > end) p = end; 0 2690 2690
164 }
165  
166 /* NUL-terminate */
167 104 eshu_buf_putc(&out, '\0');
168 104 out.len--;
169  
170 104 *out_len = out.len;
171 104 result = out.data;
172 104 return result;
173 }
174  
175  
176 #include "eshu_hl_util.h"
177  
178 /* forward declaration — eshu_pl.h is included after eshu_pod.h */
179 static char *eshu_highlight_pl(const char *src, size_t src_len, size_t *out_len);
180  
181 /* ══════════════════════════════════════════════════════════════════
182 * POD highlighter (standalone POD input)
183 * Verbatim blocks (indented lines) are sub-highlighted as Perl.
184 * ══════════════════════════════════════════════════════════════════ */
185  
186 36 static char *eshu_highlight_pod(const char *src, size_t src_len, size_t *out_len) {
187 eshu_buf_t out;
188 eshu_buf_t vbuf; /* accumulates verbatim block lines */
189 36 const char *p = src;
190 36 const char *end = src + src_len;
191 36 const char *plain = p;
192 36 int at_bol = 1;
193 36 int in_verbatim = 0;
194  
195 36 eshu_buf_init(&out, src_len * 2 + 64);
196 36 eshu_buf_init(&vbuf, 256);
197  
198 /* flush non-verbatim plain text up to `upto` (HTML-escaped) */
199 #define POD_FLUSH_PLAIN(upto) do { \
200 eshu_hl_flush(&out, plain, (upto)); \
201 plain = (upto); \
202 } while(0)
203  
204 /* emit a =command span and advance plain */
205 #define POD_EMIT_CMD(ts, te) do { \
206 eshu_hl_flush(&out, plain, (ts)); \
207 eshu_hl_span(&out, "esh-d", (ts), (te)); \
208 plain = (te); \
209 } while(0)
210  
211 /* run accumulated verbatim block through the Perl highlighter */
212 #define POD_FLUSH_VERBATIM() do { \
213 if (vbuf.len > 0) { \
214 size_t _pl_len; \
215 char *_pl_out = eshu_highlight_pl(vbuf.data, vbuf.len, &_pl_len); \
216 if (_pl_out) { eshu_buf_write(&out, _pl_out, _pl_len); free(_pl_out); } \
217 vbuf.len = 0; \
218 } \
219 } while(0)
220  
221 383 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 347 36 383
222 /* POD command: '=word' at beginning of line */
223 347 ▶ if (at_bol && *p == '=') {
Branch: 2 branches
BranchTrueFalseTotal
if (at_bol && *p == '=') { 98 249 347
if (at_bol && *p == '=') { 46 52 98
224 46 const char *np = p + 1;
225 46 ▶ if (np < end && isalpha((unsigned char)*np)) {
Branch: 2 branches
BranchTrueFalseTotal
if (np < end && isalpha((unsigned char)*np)) { 46 0 46
if (np < end && isalpha((unsigned char)*np)) { 46 0 46
226 46 ▶ if (in_verbatim) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_verbatim) { 1 45 46
227 1 ▶ POD_FLUSH_VERBATIM();
Branch: 2 branches
BranchTrueFalseTotal
POD_FLUSH_VERBATIM(); 1 0 1
POD_FLUSH_VERBATIM(); 1 0 1
228 1 in_verbatim = 0;
229 1 plain = p;
230 }
231 46 const char *ts = p;
232 528 ▶ while (p < end && *p != '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '\n') p++; 528 0 528
while (p < end && *p != '\n') p++; 482 46 528
233 46 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 46 0 46
234 46 POD_EMIT_CMD(ts, p);
235 46 at_bol = 1;
236 46 continue;
237 }
238 }
239  
240 /* entering verbatim: first indented line */
241 301 ▶ if (at_bol && !in_verbatim && (*p == ' ' || *p == '\t')) {
Branch: 4 branches
BranchTrueFalseTotal
if (at_bol && !in_verbatim && (*p == ' ' || *p == '\t')) { 52 249 301
if (at_bol && !in_verbatim && (*p == ' ' || *p == '\t')) { 45 7 52
if (at_bol && !in_verbatim && (*p == ' ' || *p == '\t')) { 38 7 45
if (at_bol && !in_verbatim && (*p == ' ' || *p == '\t')) { 0 38 38
242 7 POD_FLUSH_PLAIN(p);
243 7 in_verbatim = 1;
244 }
245  
246 /* leaving verbatim: non-blank, non-indented line */
247 301 ▶ if (at_bol && in_verbatim && *p != ' ' && *p != '\t' && *p != '\n') {
Branch: 5 branches
BranchTrueFalseTotal
if (at_bol && in_verbatim && *p != ' ' && *p != '\t' && *p != '\n') { 52 249 301
if (at_bol && in_verbatim && *p != ' ' && *p != '\t' && *p != '\n') { 14 38 52
if (at_bol && in_verbatim && *p != ' ' && *p != '\t' && *p != '\n') { 3 11 14
if (at_bol && in_verbatim && *p != ' ' && *p != '\t' && *p != '\n') { 3 0 3
if (at_bol && in_verbatim && *p != ' ' && *p != '\t' && *p != '\n') { 1 2 3
248 1 ▶ POD_FLUSH_VERBATIM();
Branch: 2 branches
BranchTrueFalseTotal
POD_FLUSH_VERBATIM(); 1 0 1
POD_FLUSH_VERBATIM(); 1 0 1
249 1 in_verbatim = 0;
250 1 plain = p;
251 }
252  
253 301 ▶ if (in_verbatim) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_verbatim) { 13 288 301
254 /* collect line (or blank line) into vbuf */
255 13 const char *ts = p;
256 212 ▶ while (p < end && *p != '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '\n') p++; 212 0 212
while (p < end && *p != '\n') p++; 199 13 212
257 13 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 13 0 13
258 13 eshu_buf_write(&vbuf, ts, (size_t)(p - ts));
259 13 at_bol = 1;
260 13 continue;
261 }
262  
263 288 ▶ if (*p == '\n') { at_bol = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') { at_bol = 1; } 39 249 288
264 249 else { at_bol = 0; }
265 288 p++;
266 }
267  
268 36 ▶ if (in_verbatim)
Branch: 1 branch
BranchTrueFalseTotal
if (in_verbatim) 5 31 36
269 5 ▶ POD_FLUSH_VERBATIM();
Branch: 2 branches
BranchTrueFalseTotal
POD_FLUSH_VERBATIM(); 5 0 5
POD_FLUSH_VERBATIM(); 5 0 5
270 else
271 31 eshu_hl_flush(&out, plain, end);
272  
273 36 eshu_buf_free(&vbuf);
274 36 eshu_buf_putc(&out, '\0');
275 36 *out_len = out.len - 1;
276 36 return out.data;
277  
278 #undef POD_FLUSH_PLAIN
279 #undef POD_EMIT_CMD
280 #undef POD_FLUSH_VERBATIM
281 }
282  
283 #endif /* ESHU_POD_H */
« include/eshu_css.h ↑ Summary include/eshu.h »