Repository navigation
Expand file tree
/
Copy pathbb2html.c
More file actions
412 lines (367 loc) · 9.71 KB
/
Copy pathbb2html.c
File metadata and controls
412 lines (367 loc) · 9.71 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* bb2html.c
* Converts the compiled BBCode intermediate language to HTML.
*
* @author Nathan Campos <nathan@innoveworkshop.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "atom.h"
#include "shims.h"
/* Private functions. */
void print_tag(const atom_t *atom, int close);
void print_tag_arg(const atom_t *tag, const atom_t *arg);
void print_tag_group(const atom_group_t *atoms);
void print_image_tag_group(const atom_group_t *atoms);
int atom_maybe_paragraph(const atom_t *atom);
/**
* Application's main entry point.
*
* @param argc Number of command line arguments passed.
* @param argv Array of command line arguments.
*
* @return Return code.
*/
int main(int argc, char **argv) {
char line[LINE_MAX];
atom_group_t *atomsgrp;
atom_t *atom;
int ret;
int newlines;
int inpara;
FILE *stream;
/* Take a memory snapshot for memory leak detection. */
mem_snapshot_start();
/* Initialize variables. */
ret = 0;
newlines = 1;
inpara = 0;
atom = NULL;
atomsgrp = NULL;
stream = stdin;
/* Check if we should read from a file. */
if (argc > 1) {
stream = fopen(argv[1], "r");
if (stream == NULL) {
fprintf(stderr, "error: failed to open file '%s' for reading.\n",
argv[1]);
return 1;
}
}
/* Go through input line-by-line. */
while (fgets(line, sizeof(line), stream)) {
char *buf;
/* Removing newline from the end of the string. */
buf = line + strlen(line) - 1;
if (*buf == '\n')
*buf = '\0';
/* Allocate a new atom if needed. */
if (atom == NULL)
atom = atom_new(-1);
/* Are we parsing a new atom or continuing from a previous one? */
if (ret == 0) {
/* Parse atom from line. */
ret = atom_parse_line(atom, line);
} else {
/* Do we need to keep parsing the last line? */
ret = atom_parse_continue(atom, line);
}
/* Should we stop everything or continue parsing? */
if (ret == -1) {
break;
} else if (ret >= 1) {
continue;
}
/* Handle the creation of paragraphs. */
if (atom_is_type(atom, ATOM_LINEBREAK)) {
/* Are we closing a paragraph? */
if (inpara) {
printf("</p>");
inpara = 0;
}
newlines++;
putchar('\n');
} else {
/* Are we starting a new paragraph? */
if (newlines && atom_maybe_paragraph(atom)) {
inpara = newlines > 1;
if (inpara)
printf("<p>");
}
newlines = 0;
}
/* Deal with tags that may span more than a single atom. */
switch (atom->type) {
case ATOM_TAG_OPEN:
/* Check if this is a tag that doesn't need to be grouped. */
if (atomsgrp || (!atom_args_maybe(atom) && (*atom->value != '*')))
goto handle_atom;
atomsgrp = atom_group_new();
atom_group_append(atomsgrp, atom);
atom = NULL;
break;
case ATOM_TAG_ARG:
/* Check if we have a valid tag group to append this argument to. */
if (atomsgrp == NULL) {
fprintf(stderr, "error: atom argument found without group\n");
goto finish;
}
/* Append the argument to the current tag group. */
atom_group_append(atomsgrp, atom);
atom = NULL;
break;
default:
handle_atom:
/* Handle previous atom group first if one exists. */
if (atomsgrp) {
atom_t *first = *atomsgrp->group;
if (atom_is_type(first, ATOM_TAG_OPEN)) {
/* Check if we are dealing with a list item. */
if (*first->value == '*') {
/* Add atoms to the group until we reach a linebreak. */
if (!atom_is_type(atom, ATOM_LINEBREAK)) {
atom_group_append(atomsgrp, atom);
atom = NULL;
break;
}
/* Convert linebreak into tag close for list item. */
atom->type = ATOM_TAG_CLOSE;
free(atom->value);
atom->value = strdup("*");
} else if (atom_is_value(first, "IMG")) {
/* Append atoms until the tag closes. */
atom_group_append(atomsgrp, atom);
if ((atom->type == ATOM_TAG_CLOSE) &&
!strcmp(atom->value, first->value)) {
/* Ensure that we ignore the last tag close. */
atom = NULL;
} else {
/* Move to the next atom of the group. */
atom = NULL;
break;
}
}
}
/* Print out the entire tag group and move on. */
print_tag_group(atomsgrp);
atom_group_free(atomsgrp);
atomsgrp = NULL;
}
/* Print the atom and move on. */
if (atom) {
if (!atom_is_type(atom, ATOM_LINEBREAK))
print_tag(atom, 1);
atom_free(atom);
atom = NULL;
}
break;
}
}
finish:
/* Close the file stream if needed. */
if (stream != stdin) {
fclose(stream);
stream = NULL;
}
/* Free resources. */
atom_group_free(atomsgrp);
atomsgrp = NULL;
atom_free(atom);
atom = NULL;
/* Check for memory leaks. */
mem_snapshot_report();
return ret;
}
/**
* Prints out an HTML tag associated with an atom.
*
* @param atom Atom object to be converted to HTML.
* @param close Should we put the final '>' of the tag?
*/
void print_tag(const atom_t *atom, int close) {
if (atom_is_type(atom, ATOM_RAW) || atom_is_type(atom, ATOM_PREFMT)) {
/* Regular text and preformatted sections. */
printf("%s", atom->value);
} else if (atom_is_type(atom, ATOM_LINEBREAK)) {
/* Linebreak */
printf("<br>\n");
} else if (atom->type >= ATOM_TAG_OPEN) {
/* Actual tag for us to deal with. */
const char *v;
if (atom_is_type(atom, ATOM_TAG_ARG)) {
fprintf(stderr, "error: Trying to print a tag argument atom.\n");
return;
}
/* Print the tag. */
v = atom->value;
putchar('<');
if (atom_is_type(atom, ATOM_TAG_CLOSE))
putchar('/');
if (!strcmp(v, "B")) {
putchar('b');
} else if (!strcmp(v, "I")) {
putchar('i');
} else if (!strcmp(v, "U")) {
putchar('u');
} else if (!strcmp(v, "S")) {
putchar('s');
} else if (!strcmp(v, "PRE")) {
printf("pre");
} else if (!strcmp(v, "H1")) {
printf("h1");
} else if (!strcmp(v, "H2")) {
printf("h2");
} else if (!strcmp(v, "H3")) {
printf("h3");
} else if (!strcmp(v, "H4")) {
printf("h4");
} else if (!strcmp(v, "H5")) {
printf("h5");
} else if (!strcmp(v, "H6")) {
printf("h6");
} else if (!strcmp(v, "URL")) {
putchar('a');
} else if (!strcmp(v, "IMG")) {
printf("img");
} else if (!strcmp(v, "CODE")) {
if (atom_is_type(atom, ATOM_TAG_OPEN)) {
printf("pre><code");
} else {
printf("code></pre");
}
} else if (!strcmp(v, "QUOTE")) {
printf("blockquote");
} else if (!strcmp(v, "COLOR")) {
printf("span");
} else if (!strcmp(v, "TABLE")) {
printf("table");
} else if (!strcmp(v, "*")) {
printf("li");
} else if (!strcmp(v, "LIST")) {
printf("ul");
} else if (!strcmp(v, "OLIST")) {
printf("ol");
} else if (!strcmp(v, "TR")) {
printf("tr");
} else if (!strcmp(v, "TH")) {
printf("th");
} else if (!strcmp(v, "TD")) {
printf("td");
} else {
fprintf(stderr, "warning: unknown tag %s\n", v);
printf("%s", v);
}
/* Close the tag brace. */
if (close)
putchar('>');
}
}
/**
* Prints an HTML tag's argument.
*
* @param tag Tag atom that the argument is part of.
* @param arg Argument atom to be printed out.
*/
void print_tag_arg(const atom_t *tag, const atom_t *arg) {
const char *v, *a;
/* Shortcut tag and argument values. */
v = tag->value;
a = arg->value;
/* Print out the argument. */
putchar(' ');
if (!strcmp(v, "URL")) {
printf("href=\"%s\"", a);
} else if (!strcmp(v, "IMG")) {
printf("src=\"%s\"", a);
} else if (!strcmp(v, "CODE")) {
printf("class=\"lang-%s\"", a);
} else if (!strcmp(v, "QUOTE")) {
printf("author=\"%s\"", a);
} else if (!strcmp(v, "COLOR")) {
printf("style=\"color: %s;\"", a);
} else {
fprintf(stderr, "warning: unknown tag argument %s for tag %s.\n", a, v);
}
}
/**
* Prints out an HTML tag associated with an atom group.
*
* @param atoms Atom group object to be converted to HTML.
*/
void print_tag_group(const atom_group_t *atoms) {
int i;
/* Deal with the special case of image tags. */
if (atom_is_value(atoms->group[0], "IMG")) {
print_image_tag_group(atoms);
return;
}
for (i = 0; i < atoms->len; i++) {
/* Print tag and its possible argument. */
if (atom_is_type(atoms->group[i], ATOM_TAG_OPEN)) {
int j = 0;
print_tag(atoms->group[i], 0);
while ((i + j + 1) < atoms->len) {
if (atoms->group[i + j + 1]->type == ATOM_TAG_ARG) {
print_tag_arg(atoms->group[i], atoms->group[i + j + 1]);
j++;
} else {
/* Advance past arguments that have already been printed. */
if (j > 0) {
i += j;
j = 0;
}
break;
}
}
putchar('>');
/* Advance past single argument conditions. */
if (j > 0)
i += j;
} else {
print_tag(atoms->group[i], 1);
}
}
}
/**
* Prints out an HTML img tag associated with an atom group.
*
* @param atoms Atom group object to be converted to an HTML img tag.
*/
void print_image_tag_group(const atom_group_t *atoms) {
int i;
/* Open the tag and add the source attribute. */
print_tag(atoms->group[0], 0);
print_tag_arg(atoms->group[0], atoms->group[1]);
/* Print the alt text. */
if (atoms->len > 3) {
printf(" alt=\"");
for (i = 2; i < (atoms->len - 1); i++)
print_tag(atoms->group[i], 1);
putchar('"');
}
/* Close the tag. */
printf(" />");
}
/**
* Checks if the atom may be the start of a paragraph.
*
* @param atom Atom to be checked.
*
* @return TRUE if the atom type or value may be a valid start of a paragraph.
* FALSE if it's an atom that may never be a start of a paragraph.
*/
int atom_maybe_paragraph(const atom_t *atom) {
const char *v;
/* If it's text, it's always a paragraph. */
if (atom_is_type(atom, ATOM_RAW)) {
return 1;
} else if (!atom_is_type(atom, ATOM_TAG_OPEN)) {
return 0;
}
/* Check for specific tags that may be at the start of a paragraph. */
v = atom->value;
return !strcmp(v, "B") || !strcmp(v, "I") || !strcmp(v, "U") ||
!strcmp(v, "S") || !strcmp(v, "URL") || !strcmp(v, "COLOR");
}