src/highlighter.c

Thu, 11 Jul 2024 20:14:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 11 Jul 2024 20:14:34 +0200
changeset 83
75ae588389d4
parent 80
0f1c55d2a90a
permissions
-rw-r--r--

clean up tag v3.1

21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
35
abaf2489c549 adjusted copyright info
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
4 * Copyright 2016 Mike Becker. All rights reserved.
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
30 #include "highlighter.h"
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
55
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 54
diff changeset
32 #include <string.h>
bf54085ce341 adds appropriate public API
Mike Becker <universe@uap-core.de>
parents: 54
diff changeset
33 #include <ctype.h>
51
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
34
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
35 #include <cx/string.h>
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
36 #include <cx/printf.h>
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
37
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
38 static void put_htmlescaped(CxBuffer *dest, char c) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 if (c == '>') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
40 cxBufferPutString(dest, "&gt;");
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 } else if (c == '<') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
42 cxBufferPutString(dest, "&lt;");
80
0f1c55d2a90a hotfix: ampersand not escaped in html output
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
43 } else if (c == '&') {
0f1c55d2a90a hotfix: ampersand not escaped in html output
Mike Becker <universe@uap-core.de>
parents: 76
diff changeset
44 cxBufferPutString(dest, "&amp;");
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
45 } else if (c) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
46 cxBufferPut(dest, c);
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 }
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
48 }
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
50 static void put_htmlescapedstr(CxBuffer *dest, cxstring s) {
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
51 for (int i = 0 ; i < s.length ; i++) {
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
52 put_htmlescaped(dest, s.ptr[i]);
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
53 }
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
56 static int check_keyword(cxstring word, const char** keywords) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 for (int i = 0 ; keywords[i] ; i++) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
58 if (cx_strcmp(word, cx_str(keywords[i])) == 0) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 return 1;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 return 0;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
65 static int check_capsonly(cxstring word) {
47
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
66 for (size_t i = 0 ; i < word.length ; i++) {
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
67 if (!isupper(word.ptr[i]) && !isdigit(word.ptr[i])
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
68 && word.ptr[i] != '_') {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 return 0;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 return 1;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
74
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
75 /* Plaintext Highlighter */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
76
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
77 void c2html_plain_highlighter(char const *src, CxBuffer *dest,
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
78 c2html_highlighter_data *hd) {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
79 while (*src && *src != '\n') {
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
80 if (*src != '\r') {
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
81 put_htmlescaped(dest, *src);
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
82 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
83 src++;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
84 }
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
85 cxBufferPut(dest, '\n');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
86 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
87
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
88 /* C Highlighter */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
89
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
90 static const char* ckeywords[] = {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
91 "auto", "break", "case", "char", "const", "continue", "default", "do",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
92 "double", "else", "enum", "extern", "float", "for", "goto", "if", "int",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
93 "long", "register", "return", "short", "signed", "sizeof", "static",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
94 "struct", "switch", "typedef", "union", "unsigned", "void", "volatile",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
95 "while", NULL
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
96 };
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
97
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
98 void c2html_c_highlighter(char const *src, CxBuffer *dest,
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
99 c2html_highlighter_data *hd) {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
100 /* reset buffers without clearing them */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
101 hd->primary_buffer.size = hd->primary_buffer.pos = 0;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
102 hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
103
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
104 /* alias the buffers for better handling */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
105 CxBuffer *wbuf = &hd->primary_buffer;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
106 CxBuffer *ifilebuf = &hd->secondary_buffer;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
107
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
108 /* local information */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
109 size_t sp = (size_t)-1;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
110
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
111 int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
112 char quote = '\0';
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
113 int isescaping = 0;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
114
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
115 int continuation_enabled = 0;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
116 const char* current_highlight = NULL;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
117
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
118 /* define convenience macros */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
119 #define start_span(cl) \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
120 current_highlight = cl; \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
121 cx_bprintf(dest, "<span class=\"c2html-%s\">", current_highlight)
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
122 #define stop_span \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
123 current_highlight = NULL;\
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
124 cxBufferPutString(dest, "</span>")
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
125
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
126 /* continue a multi line comment highlighting */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
127 if (hd->multiline_comment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
128 iscomment = 1;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
129 start_span("comment");
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
130 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
131
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
132 /* continue highlighting in case of line continuation */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
133 if (hd->continue_highlight) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
134 start_span(hd->continue_highlight);
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
135 isinclude = hd->continuation_info & 0x1;
76
44c7423d6ce2 hotfix for multiple subsequent line continuations
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
136 isstring = (hd->continuation_info & 0x2) >> 1;
44c7423d6ce2 hotfix for multiple subsequent line continuations
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
137 iscomment = (hd->continuation_info & 0x4) >> 2;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
138 if (hd->continuation_info & 0x10) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
139 quote = '\'';
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
140 } else if (hd->continuation_info & 0x20) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
141 quote = '\"';
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
142 }
76
44c7423d6ce2 hotfix for multiple subsequent line continuations
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
143 hd->continue_highlight = NULL;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
144 hd->continuation_info = 0;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
145 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
146
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
147 char c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
148 do {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
149 c = src[++sp];
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
150 if (c == '\r') continue;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
151
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
152 /* line continuation */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
153 if (c == '\\') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
154 /* currently do not support continuations in user includes */
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
155 // TODO: also support user includes
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
156 if (!parseinclude) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
157 continuation_enabled = 1;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
158 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
159 } else if (continuation_enabled) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
160 if (!isspace(c)) {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
161 continuation_enabled = 0;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
162 } else if (c == '\n') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
163 cxBufferPut(dest, '\n');
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
164 hd->continue_highlight = current_highlight;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
165 hd->continuation_info = \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
166 isinclude | \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
167 (isstring << 1) | \
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
168 (iscomment << 2);
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
169 if (quote == '\'') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
170 hd->continuation_info |= 0x10;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
171 } else if (quote == '\"') {
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
172 hd->continuation_info |= 0x20;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
173 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
174 stop_span;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
175 continue;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
176 }
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
177 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
178
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
179 /* comments */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
180 if (!isstring && c == '/') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
181 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
182 iscomment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
183 hd->multiline_comment = 0;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
184 cxBufferPut(dest, '/');
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
185 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
186 continue;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
187 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
188 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
189 hd->multiline_comment = (src[sp+1] == '*');
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
190 start_span("comment");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
191 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
192 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
193
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
194 if (iscomment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
195 if (c == '\n') {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
196 stop_span;
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
197 cxBufferPut(dest, '\n');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
198 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
199 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
200 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
201 } else if (isinclude) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
202 if (c == '<') {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
203 start_span("stdinclude");
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
204 cxBufferPutString(dest, "&lt;");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
205 } else if (c == '\"') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
206 if (parseinclude) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
207 cxBufferPutString(dest, "\">");
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
208 cxBufferWrite(ifilebuf->space, 1, ifilebuf->size, dest);
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
209 cxBufferPutString(dest, "\"</a>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
210 parseinclude = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
211 } else {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
212 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
213 "<a class=\"c2html-userinclude\" href=\"");
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
214 cxBufferPut(ifilebuf, '\"');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
215 parseinclude = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
216 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
217 } else if (c == '>') {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
218 cxBufferPutString(dest, "&gt;");
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
219 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
220 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
221 if (parseinclude) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
222 cxBufferPut(ifilebuf, c);
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
223 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
224 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
225 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
226 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
227 /* strings */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
228 if (!isescaping && (c == '\'' || c == '\"')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
229 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
230 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
231 if (c == quote) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
232 isstring = 0;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
233 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
234 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
235 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
236 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
237 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
238 isstring = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
239 quote = c;
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
240 start_span("string");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
241 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
242 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
243 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
244 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
245 put_htmlescaped(dest, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
246 } else if (isalnum(c) || c == '_' || c == '#') {
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
247 /* buffer the current word */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
248 cxBufferPut(wbuf, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
249 } else {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
250 /* write buffered word, if any */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
251 if (wbuf->size > 0) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
252 cxstring word = cx_strn(wbuf->space, wbuf->size);
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
253 int closespan = 1;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
254 cxstring typesuffix = CX_STR("_t");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
255 if (check_keyword(word, ckeywords)) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
256 start_span("keyword");
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
257 } else if (cx_strsuffix(word, typesuffix)) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
258 start_span("type");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
259 } else if (word.ptr[0] == '#') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
260 isinclude = !cx_strcmp(word, CX_STR("#include"));
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
261 start_span("directive");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
262 } else if (check_capsonly(word)) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
263 start_span("macroconst");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
264 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
265 closespan = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
266 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
267 put_htmlescapedstr(dest, word);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
268 if (closespan) {
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
269 stop_span;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
270 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
271 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
272 wbuf->pos = wbuf->size = 0; /* reset word buffer */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
273
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
274 /* write current character */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
275 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
276 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
277 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
278
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
279 isescaping = !isescaping & (c == '\\');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
280 }
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
281 } while (c && c != '\n');
75
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
282
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
283 #undef start_span
c72b250866ab add basic support for line continuations in C highlighter
Mike Becker <universe@uap-core.de>
parents: 67
diff changeset
284 #undef stop_span
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
285 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
286
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
287 /* Java Highlighter */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
288
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
289 static const char* jkeywords[] = {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
290 "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
291 "package", "synchronized", "boolean", "do", "if", "private", "this",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
292 "break", "double", "implements", "protected", "throw", "byte", "else",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
293 "import", "public", "throws", "case", "enum", "instanceof", "return",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
294 "transient", "catch", "extends", "int", "short", "try", "char", "final",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
295 "interface", "static", "void", "class", "finally", "long", "strictfp",
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
296 "volatile", "const", "float", "native", "super", "while", NULL
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
297 };
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
298
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
299 void c2html_java_highlighter(char const *src, CxBuffer *dest,
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
300 c2html_highlighter_data *hd) {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
301 /* reset buffers without clearing them */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
302 hd->primary_buffer.size = hd->primary_buffer.pos = 0;
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
303 hd->secondary_buffer.size = hd->secondary_buffer.pos = 0;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
304
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
305 /* alias the buffers for better handling */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
306 CxBuffer *wbuf = &hd->primary_buffer;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
307
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
308 /* local information */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
309 size_t sp = (size_t)-1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
310 int isstring = 0, iscomment = 0, isimport = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
311 char quote = '\0';
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
312 int isescaping = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
313
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
314 if (hd->multiline_comment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
315 iscomment = 1;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
316 cxBufferPutString(dest, "<span class=\"c2html-comment\">");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
317 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
318
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
319 char c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
320 do {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
321 c = src[++sp];
57
eba880c1705c improves API and adds functions for strings
Mike Becker <universe@uap-core.de>
parents: 55
diff changeset
322 if (c == '\r') continue;
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
323
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
324 /* comments */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
325 if (!isstring && c == '/') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
326 if (hd->multiline_comment && sp > 0 && src[sp-1] == '*') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
327 iscomment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
328 hd->multiline_comment = 0;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
329 cxBufferPutString(dest, "/</span>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
330 continue;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
331 } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
332 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
333 hd->multiline_comment = (src[sp+1] == '*');
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
334 cxBufferPutString(dest, "<span class=\"c2html-comment\">");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
335 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
336 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
337
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
338 if (iscomment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
339 if (c == '\n') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
340 cxBufferPutString(dest, "</span>\n");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
341 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
342 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
343 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
344 } else if (isimport) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
345 /* TODO: local imports */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
346 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
347 /* strings */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
348 if (!isescaping && (c == '\'' || c == '\"')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
349 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
350 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
351 if (c == quote) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
352 isstring = 0;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
353 cxBufferPutString(dest, "</span>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
354 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
355 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
356 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
357 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
358 isstring = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
359 quote = c;
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
360 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
361 "<span class=\"c2html-string\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
362 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
363 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
364 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
365 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
366 put_htmlescaped(dest, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
367 } else if (isalnum(c) || c == '_' || c == '@') {
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
368 /* buffer the current word */
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
369 cxBufferPut(wbuf, c);
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
370 } else {
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
371 /* write buffered word, if any */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
372 if (wbuf->size > 0) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
373 cxstring word = cx_strn(wbuf->space, wbuf->size);
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
374 int closespan = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
375 if (check_keyword(word, jkeywords)) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
376 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
377 "<span class=\"c2html-keyword\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
378 } else if (isupper(word.ptr[0])) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
379 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
380 "<span class=\"c2html-type\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
381 } else if (word.ptr[0] == '@') {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
382 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
383 "<span class=\"c2html-directive\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
384 } else if (check_capsonly(word)) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
385 cxBufferPutString(dest,
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
386 "<span class=\"c2html-macroconst\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
387 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
388 closespan = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
389 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
390 put_htmlescapedstr(dest, word);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
391
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
392 if (closespan) {
66
1b12cf799fee upgrade to ucx 3.0
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
393 cxBufferPutString(dest, "</span>");
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
394 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
395 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
396 wbuf->pos = wbuf->size = 0; /* reset buffer */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
397
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
398 /* write current character */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
399 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
400 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
401 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
402
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
403 isescaping = !isescaping & (c == '\\');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
404 }
54
b3f24e23bc25 highlighter can now handle files which do not end with a blank line
Mike Becker <universe@uap-core.de>
parents: 53
diff changeset
405 } while (c && c != '\n');
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
406 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
407

mercurial