diff --git a/.gitignore b/.gitignore index aff0596..cbc8d7c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -c2h \ No newline at end of file +c2h +example \ No newline at end of file diff --git a/README.md b/README.md index ae5078c..2d64c3b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,69 @@ # c2html -A tool to add HTML syntax highlighting to C code. \ No newline at end of file +A tool to add HTML syntax highlighting to C code. + +# Installation and usage +c2html comes both as a C library and a command-line utility. + +The library only exports one function +```c +char *c2html(const char *str, long len, _Bool table_mode, + const char *class_prefix, const char **error); +``` +which, given a string of C code, returns the version highlighted using HTML tags. + +For example, lets consider the +```c +/* .. include stdlib.h, string.h and stdio.h .. */ +#include "c2html.h" + +int main() +{ + // Table mode refers to the structure of the output HTML. + // If table_mode is turned off, then the output lines are + // separated by
tags and there are no line numbers. + // Using table mode, then the lines are represented as rows + // of an HTML. The table has a first column with the line + // numbers and the second with their content. + _Bool table_mode = 0; + const char *prefix = NULL; + + char *c = + "int main() {\n" + " int a = 5;\n" + " return 0;\n" + "}\n"; + + char *html = c2html(c, strlen(c), table_mode, prefix, NULL); + printf("%s\n", html); + free(html); + return 0; +} +``` +will output: +``` +
+
+ int main() {
+     int a = 5;
+     return 0;
+ }
+
+
+``` +if `table_mode` were `1`, then the output would have been: +``` +
+
+ + + + + + +
1int main() {
2  int a = 5;
3  return 0;
4}
5
+
+
+``` +the color doesn't come with the generated HTML, you need to add it yourself using CSS. There's an example CSS style provided in `style.css`, inspired by the Sublime Text color theme I use. + +Since the generated class names of the HTML tags are pretty generic (`identifier`, `operator`, `comment`, ..), you can specify a prefix to be prepented to these names. \ No newline at end of file diff --git a/build.sh b/build.sh index 49beb58..e17610c 100755 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -gcc cli.c c2html.c -o c2h -Wall -Wextra -g \ No newline at end of file +gcc cli.c c2html.c -o c2h -Wall -Wextra \ No newline at end of file diff --git a/c2html.c b/c2html.c index 4c18a64..b5c8215 100644 --- a/c2html.c +++ b/c2html.c @@ -1,3 +1,34 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * +------------------------------------------------------------+ * + * | This is free and unencumbered software released into the | * + * | public domain. | * + * | | * + * | Anyone is free to copy, modify, publish, use, compile, | * + * | sell, or distribute this software, either in source code | * + * | form or as a compiled binary, for any purpose, commercial | * + * | or non-commercial, and by any means. | * + * | | * + * | In jurisdictions that recognize copyright laws, the author | * + * | or authors of this software dedicate any and all copyright | * + * | interest in the software to the public domain. We make | * + * | this dedication for the benefit of the public at large and | * + * | to the detriment of our heirs and successors. We intend | * + * | this dedication to be an overt act of relinquishment in | * + * | perpetuity of all present and future rights to this | * + * | software under copyright law. | * + * | | * + * | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | * + * | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | * + * | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | * + * | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | * + * | THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | * + * | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | * + * | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH | * + * | THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | * * + * +------------------------------------------------------------+--+ * + * | For more information, please refer to | * + * +---------------------------------------------------------------+ * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include @@ -352,7 +383,7 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref buff_init(&buff); long lineno = 1; - buff_printf(&buff, "\n" + buff_printf(&buff, "
\n" "
\n", class_prefix, class_prefix); @@ -364,6 +395,10 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref 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: @@ -460,9 +495,12 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref } if(table_mode) - buff_printf(&buff, "\n \n"); - buff_printf(&buff, "
\n" - "
"); + buff_printf(&buff, + "\n" + " \n"); + buff_printf(&buff, + " \n" + ""); char *res; if(buff.error == NULL) { diff --git a/example.c b/example.c new file mode 100644 index 0000000..c3e8c3e --- /dev/null +++ b/example.c @@ -0,0 +1,28 @@ +// Build with: gcc example.c c2html.c -o example -Wall -Wextra +#include +#include +#include +#include "c2html.h" + +int main() +{ + // Table mode refers to the structure of the output HTML. + // If table_mode is turned off, then the output lines are + // separated by
tags and there are no line numbers. + // Using table mode, then the lines are represented as rows + // of an HTML. The table has a first column with the line + // numbers and the second with their content. + _Bool table_mode = 0; + const char *prefix = NULL; + + char *c = + "int main() {\n" + " int a = 5;\n" + " return 0;\n" + "}\n"; + + char *html = c2html(c, strlen(c), table_mode, prefix, NULL); + printf("%s\n", html); + free(html); + return 0; +} \ No newline at end of file