removed the --no-table option

This commit is contained in:
cozis
2022-05-08 01:26:31 +02:00
parent 124814f763
commit 79ecfad232
4 changed files with 47 additions and 91 deletions
+11 -32
View File
@@ -4,17 +4,16 @@ A tool to add HTML syntax highlighting to C code.
Basically you give `c2html` some C code as input and it classifies all the keywords, identifiers etc using `<span>` elements, associating them with the appropriate class names. By applying the `style.css` stylesheet to the generated output, you get the highliting. If you prefer, you can write your own style. Basically you give `c2html` some C code as input and it classifies all the keywords, identifiers etc using `<span>` elements, associating them with the appropriate class names. By applying the `style.css` stylesheet to the generated output, you get the highliting. If you prefer, you can write your own style.
# Index # Index
* [Install](#install) 1. [Install](#install)
* [Supported platforms](#supported-platforms) 1. [Supported platforms](#supported-platforms)
* [Install the library](#install-the-library) 1. [Install the library](#install-the-library)
* [Install the command-line interface](#install-the-command-line-interface) 1. [Install the command-line interface](#install-the-command-line-interface)
* [Usage](#usage) 1. [Usage](#usage)
* [Using the command-line interface](#using-the-command-line-interface) 1. [Using the command-line interface](#using-the-command-line-interface)
* [--no-table](#--no-table) 1. [--style](#--style)
* [--style](#--style) 1. [--prefix](#--prefix)
* [--prefix](#--prefix) 1. [Using the library](#using-the-library)
* [Using the library](#using-the-library) 1. [License](#license)
* [License](#license)
# Install # Install
@@ -41,14 +40,6 @@ You can highlight your C files by doing
``` ```
This command will generate the highlighted C code. This command will generate the highlighted C code.
### --no-table
Normally, `c2html` will generate html using a `<table>` element, where each line is a `<tr>` element. This makes the output kind of big. By using the `--no-table` option, it's possible to generate a more lightweight output where lines are split using `<br/>` elements instead of using a `<table>`.
You'd use it like this;
```sh
./c2html --input file.c --output file.html --no-table
```
### --style ### --style
The HTML comes with no styling. If you want to apply a CSS to it, you can provide to `c2html` a style file using the `--style` option followed by the name of the file. The HTML comes with no styling. If you want to apply a CSS to it, you can provide to `c2html` a style file using the `--style` option followed by the name of the file.
@@ -80,7 +71,6 @@ For example, consider the following C code:
int main() int main()
{ {
_Bool table_mode = 0;
const char *prefix = NULL; const char *prefix = NULL;
char *c = char *c =
@@ -89,7 +79,7 @@ int main()
" return 0;\n" " return 0;\n"
"}\n"; "}\n";
char *html = c2html(c, strlen(c), table_mode, prefix, NULL); char *html = c2html(c, strlen(c), prefix, NULL);
printf("%s\n", html); printf("%s\n", html);
free(html); free(html);
return 0; return 0;
@@ -97,17 +87,6 @@ int main()
``` ```
when executed, the output will be: when executed, the output will be:
``` ```
<div class="c2h-code">
<div class="c2h-code-inner">
<span class="c2h-kword c2h-kword-int">int</span> <span class="c2h-identifier c2h-fdeclname">main</span>() {<br />
&emsp;&emsp;&emsp;&emsp;<span class="c2h-kword c2h-kword-int">int</span> <span class="c2h-identifier">a</span> <span class="c2h-operator">=</span> <span class="c2h-val-int">5</span>;<br />
&emsp;&emsp;&emsp;&emsp;<span class="c2h-kword c2h-kword-return">return</span> <span class="c2h-val-int">0</span>;<br />
}<br />
</div>
</div>
```
If `table_mode` were `1`, then the output would have been:
```
<div class="c2h-code"> <div class="c2h-code">
<div class="c2h-code-inner"> <div class="c2h-code-inner">
<table> <table>
+30 -45
View File
@@ -36,6 +36,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include "c2html.h"
typedef enum { typedef enum {
T_DONE = 256, T_DONE = 256,
@@ -90,7 +91,7 @@ static bool iskword(const char *str, long len)
}; };
const int num_keywords = sizeof(keywords)/sizeof(keywords[0]); const int num_keywords = sizeof(keywords)/sizeof(keywords[0]);
for(int i = 0; i < num_keywords; i += 1) for(int i = 0; i < num_keywords; i += 1)
if(strlen(keywords[i]) == len && !strncmp(keywords[i], str, len)) if((int) strlen(keywords[i]) == len && !strncmp(keywords[i], str, len))
return 1; return 1;
return 0; return 0;
} }
@@ -475,16 +476,17 @@ static void print_escaped(buff_t *buff, const char *str, long len)
} }
} }
char *c2html(const char *str, long len, _Bool table_mode, const char *class_prefix, const char **error) { char *c2html(const char *str, long len,
const char *prefix, const char **error)
{
if(str == NULL) if(str == NULL)
str = ""; str = "";
if(len < 0) if(len < 0)
len = strlen(str); len = strlen(str);
if(class_prefix == NULL) if(prefix == NULL)
class_prefix = ""; prefix = "";
buff_t buff; buff_t buff;
buff_init(&buff); buff_init(&buff);
@@ -492,20 +494,14 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
buff_printf(&buff, buff_printf(&buff,
"<div class=\"%scode\">\n" "<div class=\"%scode\">\n"
" <div class=\"%scode-inner\">\n", " <div class=\"%scode-inner\">\n"
class_prefix, class_prefix); " <table>\n"
" <tr><td>1</td><td>",
if(table_mode) prefix, prefix);
buff_printf(&buff, " <table>\n"
" <tr><td>1</td><td>");
Token *tokens = tokenize(str, len); Token *tokens = tokenize(str, len);
for(int i = 0; tokens[i].kind != T_DONE; i += 1) { for(int i = 0; tokens[i].kind != T_DONE; i += 1) {
if(!table_mode)
if(i == 0 || tokens[i-1].kind == T_NEWL)
buff_printf(&buff, " ");
switch(tokens[i].kind) { switch(tokens[i].kind) {
case T_DONE: case T_DONE:
@@ -513,15 +509,9 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
break; break;
case T_NEWL: case T_NEWL:
if(table_mode) for(int j = 0; j < tokens[i].len; j += 1) {
for(int j = 0; j < tokens[i].len; j += 1) { lineno += 1;
lineno += 1; buff_printf(&buff, "</td></tr>\n <tr><td>%d</td><td>", lineno);
buff_printf(&buff, "</td></tr>\n <tr><td>%d</td><td>", lineno);
}
else {
lineno += tokens[i].len;
for(int j = 0; j < tokens[i].len; j += 1)
buff_printf(&buff, "<br />\n");
} }
break; break;
@@ -536,46 +526,46 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
case T_KWORD: case T_KWORD:
buff_printf(&buff, "<span class=\"%skword %skword-%.*s\">%.*s</span>", buff_printf(&buff, "<span class=\"%skword %skword-%.*s\">%.*s</span>",
class_prefix, class_prefix, prefix, prefix,
tokens[i].len, str + tokens[i].off, tokens[i].len, str + tokens[i].off,
tokens[i].len, str + tokens[i].off); tokens[i].len, str + tokens[i].off);
break; break;
case T_VSTR: case T_VSTR:
buff_printf(&buff, "<span class=\"%sval-str\">", class_prefix); buff_printf(&buff, "<span class=\"%sval-str\">", prefix);
print_escaped(&buff, str + tokens[i].off, tokens[i].len); print_escaped(&buff, str + tokens[i].off, tokens[i].len);
buff_printf(&buff, "</span>"); buff_printf(&buff, "</span>");
break; break;
case T_VCHAR: case T_VCHAR:
buff_printf(&buff, "<span class=\"%sval-char\">", class_prefix); buff_printf(&buff, "<span class=\"%sval-char\">", prefix);
print_escaped(&buff, str + tokens[i].off, tokens[i].len); print_escaped(&buff, str + tokens[i].off, tokens[i].len);
buff_printf(&buff, "</span>"); buff_printf(&buff, "</span>");
break; break;
case T_VINT: case T_VINT:
buff_printf(&buff, "<span class=\"%sval-int\">%.*s</span>", buff_printf(&buff, "<span class=\"%sval-int\">%.*s</span>",
class_prefix, tokens[i].len, str + tokens[i].off); prefix, tokens[i].len, str + tokens[i].off);
break; break;
case T_VFLT: case T_VFLT:
buff_printf(&buff, "<span class=\"%sval-flt\">%.*s</span>", buff_printf(&buff, "<span class=\"%sval-flt\">%.*s</span>",
class_prefix, tokens[i].len, str + tokens[i].off); prefix, tokens[i].len, str + tokens[i].off);
break; break;
case T_FDECLNAME: case T_FDECLNAME:
buff_printf(&buff, "<span class=\"%sidentifier %sfdeclname\">%.*s</span>", buff_printf(&buff, "<span class=\"%sidentifier %sfdeclname\">%.*s</span>",
class_prefix, class_prefix, tokens[i].len, str + tokens[i].off); prefix, prefix, tokens[i].len, str + tokens[i].off);
break; break;
case T_FCALLNAME: case T_FCALLNAME:
buff_printf(&buff, "<span class=\"%sidentifier %sfcallname\">%.*s</span>", buff_printf(&buff, "<span class=\"%sidentifier %sfcallname\">%.*s</span>",
class_prefix, class_prefix, tokens[i].len, str + tokens[i].off); prefix, prefix, tokens[i].len, str + tokens[i].off);
break; break;
case T_IDENTIFIER: case T_IDENTIFIER:
buff_printf(&buff, "<span class=\"%sidentifier\">%.*s</span>", buff_printf(&buff, "<span class=\"%sidentifier\">%.*s</span>",
class_prefix, tokens[i].len, str + tokens[i].off); prefix, tokens[i].len, str + tokens[i].off);
break; break;
case T_COMMENT: case T_COMMENT:
@@ -589,7 +579,7 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
j += 1; j += 1;
long line_len = j - line_off; long line_len = j - line_off;
buff_printf(&buff, "<span class=\"%scomment\">", class_prefix); buff_printf(&buff, "<span class=\"%scomment\">", prefix);
print_escaped(&buff, str + line_off, line_len); print_escaped(&buff, str + line_off, line_len);
buff_printf(&buff, "</span>"); buff_printf(&buff, "</span>");
@@ -599,22 +589,19 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
j += 1; // Skip the '\n'. j += 1; // Skip the '\n'.
lineno += 1; lineno += 1;
if(table_mode) buff_printf(&buff, "</td></tr>\n <tr><td>%d</td><td>", lineno);
buff_printf(&buff, "</td></tr>\n <tr><td>%d</td><td>", lineno);
else
buff_printf(&buff, "<br />\n");
} }
break; break;
} }
case T_OPERATOR: case T_OPERATOR:
buff_printf(&buff, "<span class=\"%soperator\">", class_prefix); buff_printf(&buff, "<span class=\"%soperator\">", prefix);
print_escaped(&buff, str + tokens[i].off, tokens[i].len); print_escaped(&buff, str + tokens[i].off, tokens[i].len);
buff_printf(&buff, "</span>"); buff_printf(&buff, "</span>");
break; break;
case T_DIRECTIVE: case T_DIRECTIVE:
buff_printf(&buff, "<span class=\"%sdirective\">", class_prefix); buff_printf(&buff, "<span class=\"%sdirective\">", prefix);
print_escaped(&buff, str + tokens[i].off, tokens[i].len); print_escaped(&buff, str + tokens[i].off, tokens[i].len);
buff_printf(&buff, "</span>"); buff_printf(&buff, "</span>");
break; break;
@@ -625,13 +612,11 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
} }
} }
if(table_mode)
buff_printf(&buff,
"</td></tr>\n"
" </table>\n");
buff_printf(&buff, buff_printf(&buff,
" </div>\n" "</td></tr>\n"
"</div>"); " </table>\n"
" </div>\n"
"</div>");
char *res; char *res;
if(buff.error == NULL) { if(buff.error == NULL) {
+2 -2
View File
@@ -1,3 +1,3 @@
char *c2html(const char *str, long len, _Bool table_mode, char *c2html(const char *str, long len,
const char *class_prefix, const char **error); const char *prefix, const char **error);
+4 -12
View File
@@ -31,7 +31,7 @@ static char *load_file(const char *file, long *size)
} }
static int fileconv(const char *input_file, const char *output_file, static int fileconv(const char *input_file, const char *output_file,
const char *style_file, const char *prefix, bool notable) const char *style_file, const char *prefix)
{ {
if(input_file == NULL || output_file == NULL) { if(input_file == NULL || output_file == NULL) {
fprintf(stderr, fprintf(stderr,
@@ -53,8 +53,7 @@ static int fileconv(const char *input_file, const char *output_file,
/* Convert it */ /* Convert it */
const char *error; const char *error;
char *output = c2html(input, input_size, char *output = c2html(input, input_size, prefix, &error);
!notable, prefix, &error);
if(output == NULL) { if(output == NULL) {
fprintf(stderr, "Error: %s\n", error); fprintf(stderr, "Error: %s\n", error);
free(input); free(input);
@@ -125,11 +124,7 @@ static void print_help(FILE *fp, char *name) {
"\n" "\n"
" -p, --prefix <prefix> The prefix of the HTML element's\n" " -p, --prefix <prefix> The prefix of the HTML element's\n"
" class names. The default is \"c2h-\"\n" " class names. The default is \"c2h-\"\n"
"\n" "\n", name);
" --no-table Use <br /> elements to split lines\n"
" instead of using a <table>\n"
"\n",
name);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@@ -144,7 +139,6 @@ int main(int argc, char **argv)
*output_file = NULL, *output_file = NULL,
*style_file = NULL, *style_file = NULL,
*prefix = NULL; *prefix = NULL;
bool notable = 0;
for(int i = 1; i < argc; i += 1) { for(int i = 1; i < argc; i += 1) {
if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
@@ -173,8 +167,6 @@ int main(int argc, char **argv)
return -1; return -1;
} }
prefix = argv[i]; prefix = argv[i];
} else if(!strcmp(argv[i], "--no-table")) {
notable = 1;
} else if(!strcmp(argv[i], "--style")) { } else if(!strcmp(argv[i], "--style")) {
i += 1; i += 1;
if(i == argc || argv[i][0] == '-') { if(i == argc || argv[i][0] == '-') {
@@ -189,5 +181,5 @@ int main(int argc, char **argv)
} }
return fileconv(input_file, output_file, return fileconv(input_file, output_file,
style_file, prefix, notable); style_file, prefix);
} }