src/highlighter.c

Fri, 26 Aug 2016 14:15:29 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 26 Aug 2016 14:15:29 +0200
changeset 53
5e47a26a16f0
parent 52
33ded421c512
child 54
b3f24e23bc25
permissions
-rw-r--r--

adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line

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
51
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
32 HighlighterData* new_highlighter_data() {
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
33 HighlighterData* hd = malloc(sizeof(HighlighterData));
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
34 if (hd == NULL) {
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
35 return NULL;
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
36 } else {
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
37 hd->multiline_comment = 0;
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
38 hd->primary_buffer = ucx_buffer_new(NULL, 256, UCX_BUFFER_AUTOEXTEND);
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
39 hd->secondary_buffer = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
40 return hd;
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
41 }
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
42 }
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
43
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
44 void free_highlighter_data(HighlighterData *hd) {
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
45 ucx_buffer_free(hd->primary_buffer);
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
46 ucx_buffer_free(hd->secondary_buffer);
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
47 free(hd);
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
48 }
f25ba6fd7a08 replaces stack buffers with UCX buffers
Mike Becker <universe@uap-core.de>
parents: 49
diff changeset
49
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
50 static void put_htmlescaped(UcxBuffer *dest, char c) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 if (c == '>') {
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
52 ucx_buffer_puts(dest, "&gt;");
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 } else if (c == '<') {
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
54 ucx_buffer_puts(dest, "&lt;");
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 } else {
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
56 ucx_buffer_putc(dest, c);
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 }
48
b2724c711203 highlighter now use the UcxBuffer API for writing to the destination buffer
Mike Becker <universe@uap-core.de>
parents: 47
diff changeset
58 }
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
60 static void put_htmlescapedstr(UcxBuffer *dest, sstr_t 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
61 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
62 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
63 }
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
66 static int check_keyword(sstr_t word, const char** keywords) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 for (int i = 0 ; keywords[i] ; i++) {
47
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
68 if (sstrcmp(word, sstr((char*)keywords[i])) == 0) {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 return 1;
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 0;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
75 static int check_capsonly(sstr_t word) {
47
c39ecbbca7c0 words (token) are now stored as sstr_t
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
76 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
77 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
78 && word.ptr[i] != '_') {
21
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 return 0;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 return 1;
537aec525835 structured source code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
84
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
85 /* Plaintext Highlighter */
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 void plain_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
88 while (*src && *src != '\n') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
89 put_htmlescaped(dest, *src);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
90 src++;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
91 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
92 ucx_buffer_putc(dest, '\n');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
93 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
94
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
95 /* C Highlighter */
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 static const char* ckeywords[] = {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
98 "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
99 "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
100 "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
101 "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
102 "while", NULL
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
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
105 void c_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
106 /* reset buffers without clearing them */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
107 hd->primary_buffer->size = hd->primary_buffer->pos = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
108 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
109
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
110 /* alias the buffers for better handling */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
111 UcxBuffer *wbuf = hd->primary_buffer;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
112 UcxBuffer *ifilebuf = hd->secondary_buffer;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
113
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
114 /* local information */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
115 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
116 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
117 char quote = '\0';
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
118 int isescaping = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
119
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
120 /* 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
121 if (hd->multiline_comment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
122 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
123 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
124 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
125
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
126 char c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
127 do {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
128 c = src[++sp];
53
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
129 if (!c) {
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
130 /* TODO: might cause problems if code file does not end with NL */
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
131 break;
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
132 }
52
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
133
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
134 /* comments */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
135 if (!isstring && c == '/') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
136 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
137 iscomment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
138 hd->multiline_comment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
139 ucx_buffer_puts(dest, "/</span>");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
140 continue;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
141 } 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
142 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
143 hd->multiline_comment = (src[sp+1] == '*');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
144 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
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
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
148 if (iscomment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
149 if (c == '\n') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
150 ucx_buffer_puts(dest, "</span>\n");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
151 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
152 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
153 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
154 } else if (isinclude) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
155 if (c == '<') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
156 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
157 "<span class=\"c2html-stdinclude\">&lt;");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
158 } else if (c == '\"') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
159 if (parseinclude) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
160 ucx_buffer_puts(dest, "\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
161 ucx_buffer_write(ifilebuf->space, 1, ifilebuf->size, dest);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
162 ucx_buffer_puts(dest, "\"</a>");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
163 parseinclude = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
164 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
165 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
166 "<a class=\"c2html-userinclude\" href=\"");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
167 ucx_buffer_putc(ifilebuf, '\"');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
168 parseinclude = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
169 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
170 } else if (c == '>') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
171 ucx_buffer_puts(dest, "&gt;</span>");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
172 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
173 if (parseinclude) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
174 ucx_buffer_putc(ifilebuf, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
175 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
176 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
177 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
178 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
179 /* strings */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
180 if (!isescaping && (c == '\'' || c == '\"')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
181 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
182 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
183 if (c == quote) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
184 isstring = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
185 ucx_buffer_puts(dest, "</span>");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
186 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
187 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
188 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
189 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
190 isstring = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
191 quote = c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
192 ucx_buffer_puts(dest, "<span class=\"c2html-string\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
193 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
194 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
195 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
196 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
197 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
198 } else if (!isalnum(c) && c!='_' && c!='#') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
199 /* write buffered word, if any */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
200 if (wbuf->size > 0) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
201 sstr_t word = sstrn(wbuf->space, wbuf->size);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
202 int closespan = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
203 sstr_t typesuffix = ST("_t");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
204 if (check_keyword(word, ckeywords)) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
205 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
206 "<span class=\"c2html-keyword\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
207 } else if (sstrsuffix(word, typesuffix)) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
208 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
209 "<span class=\"c2html-type\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
210 } else if (word.ptr[0] == '#') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
211 isinclude = !sstrcmp(word, S("#include"));
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
212 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
213 "<span class=\"c2html-directive\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
214 } else if (check_capsonly(word)) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
215 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
216 "<span class=\"c2html-macroconst\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
217 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
218 closespan = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
219 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
220 put_htmlescapedstr(dest, word);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
221 if (closespan) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
222 ucx_buffer_puts(dest, "</span>");
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 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
225 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
226
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
227 /* write current character */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
228 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
229 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
230 /* buffer the current word */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
231 ucx_buffer_putc(wbuf, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
232 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
233 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
234
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
235 isescaping = !isescaping & (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 } while (c != '\n');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
238 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
239
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
240 /* Java Highlighter */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
241
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
242 static const char* jkeywords[] = {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
243 "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
244 "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
245 "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
246 "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
247 "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
248 "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
249 "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
250 };
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
251
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
252 void java_highlighter(char *src, UcxBuffer *dest, HighlighterData *hd) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
253 /* reset buffers without clearing them */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
254 hd->primary_buffer->size = hd->primary_buffer->pos = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
255 hd->secondary_buffer->size = hd->secondary_buffer->pos = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
256
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
257 /* alias the buffers for better handling */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
258 UcxBuffer *wbuf = hd->primary_buffer;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
259
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
260 /* local information */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
261 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
262 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
263 char quote = '\0';
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
264 int isescaping = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
265
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
266 if (hd->multiline_comment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
267 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
268 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
269 }
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 char c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
272 do {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
273 c = src[++sp];
53
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
274 if (!c) {
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
275 /* TODO: might cause problems if code file does not end with NL */
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
276 break;
5e47a26a16f0 adds appendfile() function to main module + adds TODOs for source files which do not terminate with a blank line
Mike Becker <universe@uap-core.de>
parents: 52
diff changeset
277 }
52
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 /* comments */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
280 if (!isstring && c == '/') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
281 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
282 iscomment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
283 hd->multiline_comment = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
284 ucx_buffer_puts(dest, "/</span>");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
285 continue;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
286 } 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
287 iscomment = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
288 hd->multiline_comment = (src[sp+1] == '*');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
289 ucx_buffer_puts(dest, "<span class=\"c2html-comment\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
290 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
291 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
292
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
293 if (iscomment) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
294 if (c == '\n') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
295 ucx_buffer_puts(dest, "</span>\n");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
296 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
297 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
298 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
299 } else if (isimport) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
300 /* TODO: local imports */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
301 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
302 /* strings */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
303 if (!isescaping && (c == '\'' || c == '\"')) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
304 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
305 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
306 if (c == quote) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
307 isstring = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
308 ucx_buffer_puts(dest, "</span>");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
309 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
310 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
311 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
312 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
313 isstring = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
314 quote = c;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
315 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
316 "<span class=\"c2html-string\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
317 put_htmlescaped(dest, c);
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 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
320 if (isstring) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
321 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
322 } else if (!isalnum(c) && c!='_' && c!='@') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
323 /* write buffered word, if any */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
324 if (wbuf->size > 0) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
325 sstr_t word = sstrn(wbuf->space, wbuf->size);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
326 int closespan = 1;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
327 if (check_keyword(word, jkeywords)) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
328 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
329 "<span class=\"c2html-keyword\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
330 } else if (isupper(word.ptr[0])) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
331 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
332 "<span class=\"c2html-type\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
333 } else if (word.ptr[0] == '@') {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
334 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
335 "<span class=\"c2html-directive\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
336 } else if (check_capsonly(word)) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
337 ucx_buffer_puts(dest,
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
338 "<span class=\"c2html-macroconst\">");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
339 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
340 closespan = 0;
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
341 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
342 put_htmlescapedstr(dest, word);
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 if (closespan) {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
345 ucx_buffer_puts(dest, "</span>");
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
346 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
347 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
348 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
349
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
350 /* write current character */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
351 put_htmlescaped(dest, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
352 } else {
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
353 /* buffer the current word */
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
354 ucx_buffer_putc(wbuf, c);
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
355 }
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
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
358 isescaping = !isescaping & (c == '\\');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
359 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
360 } while (c != '\n');
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
361 }
33ded421c512 merges all highlighter functions into one highlighter module
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
362

mercurial