diff --git a/README.md b/README.md index 667ecbf..220ee32 100644 --- a/README.md +++ b/README.md @@ -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 `` 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 `` element, where each line is a `` 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 `
` elements instead of using a `
`. - -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: ``` -
-
- int main() {
-     int a = 5;
-     return 0;
- }
-
-
-``` -If `table_mode` were `1`, then the output would have been: -```
diff --git a/c2html.c b/c2html.c index d087b8e..66a1c8a 100644 --- a/c2html.c +++ b/c2html.c @@ -36,6 +36,7 @@ #include #include #include +#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, "
\n" - "
\n", - class_prefix, class_prefix); - - if(table_mode) - buff_printf(&buff, "
\n" - " \n" + "
1"); + "
\n" + " \n" + " \n \n \n \n \n" - "
1", + 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, "
%d", lineno); - } - else { - lineno += tokens[i].len; - for(int j = 0; j < tokens[i].len; j += 1) - buff_printf(&buff, "
\n"); + for(int j = 0; j < tokens[i].len; j += 1) { + lineno += 1; + buff_printf(&buff, "
%d", 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, "%.*s", - 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, "", class_prefix); + buff_printf(&buff, "", prefix); print_escaped(&buff, str + tokens[i].off, tokens[i].len); buff_printf(&buff, ""); break; case T_VCHAR: - buff_printf(&buff, "", class_prefix); + buff_printf(&buff, "", prefix); print_escaped(&buff, str + tokens[i].off, tokens[i].len); buff_printf(&buff, ""); break; case T_VINT: buff_printf(&buff, "%.*s", - class_prefix, tokens[i].len, str + tokens[i].off); + prefix, tokens[i].len, str + tokens[i].off); break; case T_VFLT: buff_printf(&buff, "%.*s", - class_prefix, tokens[i].len, str + tokens[i].off); + prefix, tokens[i].len, str + tokens[i].off); break; case T_FDECLNAME: buff_printf(&buff, "%.*s", - 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, "%.*s", - 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, "%.*s", - 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, "", class_prefix); + buff_printf(&buff, "", prefix); print_escaped(&buff, str + line_off, line_len); buff_printf(&buff, ""); @@ -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, "
%d", lineno); - else - buff_printf(&buff, "
\n"); + buff_printf(&buff, "
%d", lineno); } break; } case T_OPERATOR: - buff_printf(&buff, "", class_prefix); + buff_printf(&buff, "", prefix); print_escaped(&buff, str + tokens[i].off, tokens[i].len); buff_printf(&buff, ""); break; case T_DIRECTIVE: - buff_printf(&buff, "", class_prefix); + buff_printf(&buff, "", prefix); print_escaped(&buff, str + tokens[i].off, tokens[i].len); buff_printf(&buff, ""); 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, - "
\n"); buff_printf(&buff, - "
\n" - ""); + "
\n" + " \n" + ""); char *res; if(buff.error == NULL) { diff --git a/c2html.h b/c2html.h index bf5d6e6..aaa7e2f 100644 --- a/c2html.h +++ b/c2html.h @@ -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); diff --git a/cli.c b/cli.c index 9b98687..b9c7b2a 100644 --- a/cli.c +++ b/cli.c @@ -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 The prefix of the HTML element's\n" " class names. The default is \"c2h-\"\n" - "\n" - " --no-table Use
elements to split lines\n" - " instead of using a \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); } \ No newline at end of file