src/c2html.c

Sun, 11 Jun 2023 15:21:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 11 Jun 2023 15:21:44 +0200
changeset 71
d5af9261231d
parent 70
60cecca5e484
permissions
-rw-r--r--

fix incorrect highlighting when input file starts with line break

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

mercurial