-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmd.c
222 lines (178 loc) · 7.23 KB
/
tmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define ASCII_MAX 0x80
int result_len(char* text, char** substitutions_begin, char** substitutions_end, bool format)
{
int result_len = 0;
bool status[ASCII_MAX] = {0};
for (; *text; text++)
{
if (*text == '\\') // skip past escaped chars
{
text++;
result_len++;
continue;
}
if (*text == '^' || *text == '|')
{
while (text[0] == text[1]) text++;
char escape = text[0];
char control = text[1];
int ctrl_i = ASCII_MAX * (escape == '^' ? 1 : 2) + control;
if (format) result_len += strlen(substitutions_begin[ctrl_i] ?
substitutions_begin[ctrl_i] :
substitutions_end[escape]);
if (control != '\\' &&
!substitutions_end[control] &&
substitutions_begin[ctrl_i])
text++;
continue;
}
if (substitutions_begin[*text])
status[*text] = !status[*text];
char* substitution = status[*text] ?
substitutions_begin[*text] :
substitutions_end[*text];
if (format && substitution)
result_len += strlen(substitution);
else if (!substitution) result_len++;
}
return result_len + 1;
}
void init_substitutions(char** substitutions_begin, char** substitutions_end)
{
substitutions_begin['#'] = "\e[1m"; // bold
substitutions_end['#'] = "\e[22m";
substitutions_begin['~'] = "\e[2m"; // dim
substitutions_end['~'] = "\e[22m";
substitutions_begin['*'] = "\e[3m"; // italic
substitutions_end['*'] = "\e[23m";
substitutions_begin['_'] = "\e[4m"; // underline
substitutions_end['_'] = "\e[24m";
substitutions_begin['@'] = "\e[5m"; // blink
substitutions_end['@'] = "\e[25m";
substitutions_begin['$'] = "\e[7m"; // inverted
substitutions_end['$'] = "\e[27m";
substitutions_begin['`'] = "\e[8m"; // hidden
substitutions_end['`'] = "\e[28m";
substitutions_begin['%'] = "\e[9m"; // strikethrough
substitutions_end['%'] = "\e[29m";
// special end sequences
substitutions_end['^'] = "\e[39m"; // font color
substitutions_end['|'] = "\e[49m"; // background
// text colors
substitutions_begin[ASCII_MAX + 'l'] = "\e[30m"; // black
substitutions_begin[ASCII_MAX + 'r'] = "\e[31m"; // red
substitutions_begin[ASCII_MAX + 'g'] = "\e[32m"; // green
substitutions_begin[ASCII_MAX + 'y'] = "\e[33m"; // yellow
substitutions_begin[ASCII_MAX + 'b'] = "\e[34m"; // blue
substitutions_begin[ASCII_MAX + 'm'] = "\e[35m"; // magenta
substitutions_begin[ASCII_MAX + 'c'] = "\e[36m"; // cyan
substitutions_begin[ASCII_MAX + 'L'] = "\e[97m"; // white
substitutions_begin[ASCII_MAX + 'R'] = "\e[91m"; // bright red
substitutions_begin[ASCII_MAX + 'G'] = "\e[92m"; // bright green
substitutions_begin[ASCII_MAX + 'Y'] = "\e[93m"; // bright yellow
substitutions_begin[ASCII_MAX + 'B'] = "\e[94m"; // bright blue
substitutions_begin[ASCII_MAX + 'M'] = "\e[95m"; // bright magenta
substitutions_begin[ASCII_MAX + 'C'] = "\e[96m"; // bright cyan
// backgrounds
substitutions_begin[ASCII_MAX * 2 + 'l'] = "\e[40m"; // black
substitutions_begin[ASCII_MAX * 2 + 'r'] = "\e[41m"; // red
substitutions_begin[ASCII_MAX * 2 + 'g'] = "\e[42m"; // green
substitutions_begin[ASCII_MAX * 2 + 'y'] = "\e[43m"; // yellow
substitutions_begin[ASCII_MAX * 2 + 'b'] = "\e[44m"; // blue
substitutions_begin[ASCII_MAX * 2 + 'm'] = "\e[45m"; // magenta
substitutions_begin[ASCII_MAX * 2 + 'c'] = "\e[46m"; // cyan
substitutions_begin[ASCII_MAX * 2 + 'L'] = "\e[107m"; // white
substitutions_begin[ASCII_MAX * 2 + 'R'] = "\e[101m"; // bright red
substitutions_begin[ASCII_MAX * 2 + 'G'] = "\e[102m"; // bright green
substitutions_begin[ASCII_MAX * 2 + 'Y'] = "\e[103m"; // bright yellow
substitutions_begin[ASCII_MAX * 2 + 'B'] = "\e[104m"; // bright blue
substitutions_begin[ASCII_MAX * 2 + 'M'] = "\e[105m"; // bright magenta
substitutions_begin[ASCII_MAX * 2 + 'C'] = "\e[106m"; // bright cyan
}
// EXTERNAL
char* escape_all(char* text) // escapes all instances of special characters in text
{
char* substitutions_begin[ASCII_MAX * 3] = {0}; // unused in this function
char* substitutions_end[ASCII_MAX] = {0};
init_substitutions(substitutions_begin, substitutions_end);
int result_len = 1; // start with 1 for the null pointer
char* text_p = text;
while (*text_p)
{
result_len++;
if (substitutions_end[*text_p]) result_len++;
text_p++;
}
char* result = calloc(result_len, sizeof(char));
text_p = text; // reset text pointer
char* result_p = result;
while (*text_p)
{
if (*text_p == '\\' || substitutions_end[*text_p]) *result_p++ = '\\';
*result_p++ = *text_p++;
}
return result;
}
char* substitute_escapes(char* text, bool format)
{
for (char* text_i = text; *text_i; text_i++)
{
if ((unsigned char) *text_i >= ASCII_MAX)
{
char* ERROR = "\e[1m\e[31mError:\e[39m\e[22m"; // bold and red 'Error:' text
fprintf(stderr, "%s text is not ASCII encoded.\n", ERROR);
return NULL;
}
}
bool status[ASCII_MAX] = {0}; // tell whether text is currently in special state
char* substitutions_begin[ASCII_MAX * 3] = {0};
char* substitutions_end[ASCII_MAX] = {0};
init_substitutions(substitutions_begin, substitutions_end);
char* result = calloc(result_len(text, substitutions_begin, substitutions_end, format), sizeof(char));
for (char* result_i = result; *text; text++)
{
if (*text == '\\') // provide '\' as an escape character
{
*result_i++ = *++text;
continue;
}
if (*text == '^' || *text == '|')
{
while (text[0] == text[1]) text++;
char escape = text[0];
char control = text[1];
int ctrl_i = ASCII_MAX * (escape == '^' ? 1 : 2) + control;
char* substitution = substitutions_begin[ctrl_i] ?
substitutions_begin[ctrl_i] :
substitutions_end[escape];
if (format) while (*substitution)
*result_i++ = *substitution++;
if (control != '\\' &&
!substitutions_end[control] &&
substitutions_begin[ctrl_i])
text++;
continue;
}
if (substitutions_begin[*text]) // if char is special char, change status
status[*text] = !status[*text];
char* substitution = status[*text] ?
substitutions_begin[*text] :
substitutions_end[*text];
if (format && substitution)
while (*substitution)
*result_i++ = *substitution++;
else if (!substitution) *result_i++ = *text;
}
return result;
}
void format_and_print(char* text, bool format)
{
char* formatted_text = substitute_escapes(text, format);
if (!formatted_text) return;
printf("%s", formatted_text);
free(formatted_text);
}