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.
# Index
* [Install](#install)
* [Supported platforms](#supported-platforms)
* [Install the library](#install-the-library)
* [Install the command-line interface](#install-the-command-line-interface)
* [Usage](#usage)
* [Using the command-line interface](#using-the-command-line-interface)
* [--no-table](#--no-table)
* [--style](#--style)
* [--prefix](#--prefix)
* [Using the library](#using-the-library)
* [License](#license)
1. [Install](#install)
1. [Supported platforms](#supported-platforms)
1. [Install the library](#install-the-library)
1. [Install the command-line interface](#install-the-command-line-interface)
1. [Usage](#usage)
1. [Using the command-line interface](#using-the-command-line-interface)
1. [--style](#--style)
1. [--prefix](#--prefix)
1. [Using the library](#using-the-library)
1. [License](#license)
# Install
@@ -41,14 +40,6 @@ You can highlight your C files by doing
```
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
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()
{
_Bool table_mode = 0;
const char *prefix = NULL;
char *c =
@@ -89,7 +79,7 @@ int main()
" return 0;\n"
"}\n";
char *html = c2html(c, strlen(c), table_mode, prefix, NULL);
char *html = c2html(c, strlen(c), prefix, NULL);
printf("%s\n", html);
free(html);
return 0;
@@ -97,17 +87,6 @@ int main()
```
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-inner">
<table>
+30 -45
View File
@@ -36,6 +36,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include "c2html.h"
typedef enum {
T_DONE = 256,
@@ -90,7 +91,7 @@ static bool iskword(const char *str, long len)
};
const int num_keywords = sizeof(keywords)/sizeof(keywords[0]);
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 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)
str = "";
if(len < 0)
len = strlen(str);
if(class_prefix == NULL)
class_prefix = "";
if(prefix == NULL)
prefix = "";
buff_t 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,
"<div class=\"%scode\">\n"
" <div class=\"%scode-inner\">\n",
class_prefix, class_prefix);
if(table_mode)
buff_printf(&buff, " <table>\n"
" <tr><td>1</td><td>");
" <div class=\"%scode-inner\">\n"
" <table>\n"
" <tr><td>1</td><td>",
prefix, prefix);
Token *tokens = tokenize(str, len);
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) {
case T_DONE:
@@ -513,15 +509,9 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
break;
case T_NEWL:
if(table_mode)
for(int j = 0; j < tokens[i].len; j += 1) {
lineno += 1;
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");
for(int j = 0; j < tokens[i].len; j += 1) {
lineno += 1;
buff_printf(&buff, "</td></tr>\n <tr><td>%d</td><td>", lineno);
}
break;
@@ -536,46 +526,46 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
case T_KWORD:
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);
break;
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);
buff_printf(&buff, "</span>");
break;
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);
buff_printf(&buff, "</span>");
break;
case T_VINT:
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;
case T_VFLT:
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;
case T_FDECLNAME:
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;
case T_FCALLNAME:
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;
case T_IDENTIFIER:
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;
case T_COMMENT:
@@ -589,7 +579,7 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
j += 1;
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);
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'.
lineno += 1;
if(table_mode)
buff_printf(&buff, "</td></tr>\n <tr><td>%d</td><td>", lineno);
else
buff_printf(&buff, "<br />\n");
buff_printf(&buff, "</td></tr>\n <tr><td>%d</td><td>", lineno);
}
break;
}
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);
buff_printf(&buff, "</span>");
break;
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);
buff_printf(&buff, "</span>");
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,
" </div>\n"
"</div>");
"</td></tr>\n"
" </table>\n"
" </div>\n"
"</div>");
char *res;
if(buff.error == NULL) {
+2 -2
View File
@@ -1,3 +1,3 @@
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);
+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,
const char *style_file, const char *prefix, bool notable)
const char *style_file, const char *prefix)
{
if(input_file == NULL || output_file == NULL) {
fprintf(stderr,
@@ -53,8 +53,7 @@ static int fileconv(const char *input_file, const char *output_file,
/* Convert it */
const char *error;
char *output = c2html(input, input_size,
!notable, prefix, &error);
char *output = c2html(input, input_size, prefix, &error);
if(output == NULL) {
fprintf(stderr, "Error: %s\n", error);
free(input);
@@ -125,11 +124,7 @@ static void print_help(FILE *fp, char *name) {
"\n"
" -p, --prefix <prefix> The prefix of the HTML element's\n"
" class names. The default is \"c2h-\"\n"
"\n"
" --no-table Use <br /> elements to split lines\n"
" instead of using a <table>\n"
"\n",
name);
"\n", name);
}
int main(int argc, char **argv)
@@ -144,7 +139,6 @@ int main(int argc, char **argv)
*output_file = NULL,
*style_file = NULL,
*prefix = NULL;
bool notable = 0;
for(int i = 1; i < argc; i += 1) {
if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
@@ -173,8 +167,6 @@ int main(int argc, char **argv)
return -1;
}
prefix = argv[i];
} else if(!strcmp(argv[i], "--no-table")) {
notable = 1;
} else if(!strcmp(argv[i], "--style")) {
i += 1;
if(i == argc || argv[i][0] == '-') {
@@ -189,5 +181,5 @@ int main(int argc, char **argv)
}
return fileconv(input_file, output_file,
style_file, prefix, notable);
style_file, prefix);
}