src/c2html.c

Mon, 24 Apr 2023 21:01:41 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 24 Apr 2023 21:01:41 +0200
changeset 67
5da2cb5aea6b
parent 66
1b12cf799fee
parent 61
47a5fc33590a
child 70
60cecca5e484
permissions
-rw-r--r--

merge upstream changes

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@1 34
universe@66 35 size_t c2html_format(
universe@66 36 CxList const *lines,
universe@66 37 void *outbuf,
universe@66 38 cx_write_func wfnc,
universe@66 39 c2html_highlighter_func highlighter,
universe@66 40 int showln
universe@66 41 ) {
universe@66 42 /* total written bytes */
universe@66 43 size_t written = 0;
universe@66 44
universe@66 45 /* compute width of line numbering */
universe@66 46 int lnw = 0;
universe@66 47 if (showln) {
universe@66 48 size_t no_lines = cxListSize(lines);
universe@66 49 for (size_t p = 1; p < no_lines; p *= 10) lnw++;
universe@53 50 }
universe@53 51
universe@66 52 /* start code formatting */
universe@66 53 written += wfnc("<div class=\"c2html-code\">\n", 1, 26, outbuf);
universe@39 54
universe@44 55 /* process lines */
universe@66 56 int lineno = 0;
universe@66 57 c2html_highlighter_data hd;
universe@66 58 hd.multiline_comment = 0;
universe@66 59 cxBufferInit(&hd.primary_buffer, NULL, 256, NULL, CX_BUFFER_AUTO_EXTEND);
universe@66 60 cxBufferInit(&hd.secondary_buffer, NULL, 32, NULL, CX_BUFFER_AUTO_EXTEND);
universe@66 61 CxBuffer out_line;
universe@66 62 cxBufferInit(&out_line, NULL, 128, NULL, CX_BUFFER_AUTO_EXTEND);
universe@66 63
universe@66 64 CxIterator in_lines = cxListIterator(lines);
universe@66 65 cx_foreach(char*, in_line, in_lines) {
universe@44 66 /* increase line number and clean line buffer */
universe@39 67 lineno++;
universe@66 68 cxBufferClear(&out_line);
universe@66 69
universe@44 70 /* write line number */
universe@66 71 if (showln) {
universe@66 72 cx_bprintf(&out_line, "<a class=\"c2html-lineno\" name=\"l%d\" "
universe@66 73 "href=\"#l%d\">%*d </a>",
universe@66 74 lineno, lineno, lnw, lineno);
universe@44 75 }
universe@66 76
universe@45 77 /* process code line */
universe@66 78 highlighter(in_line, &out_line, &hd);
universe@66 79
universe@50 80 /* write code line */
universe@66 81 written += wfnc(out_line.space, 1, out_line.size, outbuf);
olaf@24 82 }
universe@66 83
universe@66 84 /* end code formatting */
universe@66 85 written += wfnc("</div>\n", 1, 7, outbuf);
universe@66 86
universe@44 87 /* cleanup and return */
universe@66 88 cxBufferDestroy(&hd.primary_buffer);
universe@66 89 cxBufferDestroy(&hd.secondary_buffer);
universe@66 90 cxBufferDestroy(&out_line);
universe@66 91
universe@55 92 return written;
olaf@24 93 }
olaf@24 94
universe@66 95 size_t c2html_bformat(
universe@66 96 char const *inputbuffer,
universe@66 97 size_t inputbuflen,
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@66 104 size_t est_cap = 16 + inputbuflen / 40;
universe@42 105
universe@66 106 /* create the line pointer array */
universe@66 107 CxList *lines = cxArrayListCreateSimple(CX_STORE_POINTERS, est_cap);
universe@66 108 cxListAdd(lines, inputbuffer);
universe@66 109 for (size_t i = 1; i < inputbuflen; i++) {
universe@66 110 if (inputbuffer[i] == '\n' && i + 1 < inputbuflen) {
universe@66 111 cxListAdd(lines, inputbuffer + i + 1);
universe@57 112 }
universe@57 113 }
universe@66 114
universe@66 115 /* invoke the other function */
universe@66 116 size_t n = c2html_format(lines, outbuf, wfnc, highlighter, showln);
universe@66 117
universe@66 118 /* cleanup and return */
universe@66 119 cxListDestroy(lines);
universe@57 120 return n;
universe@66 121 }

mercurial