Sun, 11 Jun 2023 15:21:44 +0200
fix incorrect highlighting when input file starts with line break
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "c2html.h"
32 #include <cx/array_list.h>
33 #include <cx/printf.h>
34 #include <string.h>
36 size_t c2html_format(
37 CxList const *lines,
38 void *outbuf,
39 cx_write_func wfnc,
40 c2html_highlighter_func highlighter,
41 int showln
42 ) {
43 /* total written bytes */
44 size_t written = 0;
46 /* compute width of line numbering */
47 int lnw = 0;
48 if (showln) {
49 size_t no_lines = cxListSize(lines);
50 for (size_t p = 1; p < no_lines; p *= 10) lnw++;
51 }
53 /* start code formatting */
54 written += wfnc("<div class=\"c2html-code\">\n", 1, 26, outbuf);
56 /* process lines */
57 int lineno = 0;
58 c2html_highlighter_data hd;
59 hd.multiline_comment = 0;
60 cxBufferInit(&hd.primary_buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND);
61 cxBufferInit(&hd.secondary_buffer, NULL, 32, NULL, CX_BUFFER_AUTO_EXTEND);
62 CxBuffer out_line;
63 cxBufferInit(&out_line, NULL, 128, NULL, CX_BUFFER_AUTO_EXTEND);
65 CxIterator in_lines = cxListIterator(lines);
66 cx_foreach(char*, in_line, in_lines) {
67 /* increase line number and clean line buffer */
68 lineno++;
69 cxBufferClear(&out_line);
71 /* write line number */
72 if (showln) {
73 cx_bprintf(&out_line, "<a class=\"c2html-lineno\" name=\"l%d\" "
74 "href=\"#l%d\">%*d </a>",
75 lineno, lineno, lnw, lineno);
76 }
78 /* process code line */
79 highlighter(in_line, &out_line, &hd);
81 /* write code line */
82 written += wfnc(out_line.space, 1, out_line.size, outbuf);
83 }
85 /* end code formatting */
86 written += wfnc("</div>\n", 1, 7, outbuf);
88 /* cleanup and return */
89 cxBufferDestroy(&hd.primary_buffer);
90 cxBufferDestroy(&hd.secondary_buffer);
91 cxBufferDestroy(&out_line);
93 return written;
94 }
96 size_t c2html_textformat(
97 char const *inputbuffer,
98 void *outbuf,
99 cx_write_func wfnc,
100 c2html_highlighter_func highlighter,
101 int showln
102 ) {
103 /* a rough estimate for the number of lines */
104 size_t inputbuflen = strlen(inputbuffer);
105 size_t est_cap = 16 + inputbuflen / 40;
107 /* create the line pointer array */
108 CxList *lines = cxArrayListCreateSimple(CX_STORE_POINTERS, est_cap);
109 cxListAdd(lines, inputbuffer);
110 for (size_t i = 0; i < inputbuflen; i++) {
111 if (inputbuffer[i] == '\n' && i + 1 < inputbuflen) {
112 cxListAdd(lines, inputbuffer + i + 1);
113 }
114 }
116 /* invoke the other function */
117 size_t n = c2html_format(lines, outbuf, wfnc, highlighter, showln);
119 /* cleanup and return */
120 cxListDestroy(lines);
121 return n;
122 }