add example and readme
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
c2h
|
c2h
|
||||||
|
example
|
||||||
@@ -1,2 +1,69 @@
|
|||||||
# c2html
|
# c2html
|
||||||
A tool to add HTML syntax highlighting to C code.
|
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 <br /> 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:
|
||||||
|
```
|
||||||
|
<div class="code">
|
||||||
|
<div class="code-inner">
|
||||||
|
<span class="kword kword-int">int</span> <span class="identifier fdeclname">main</span>() {<br />
|
||||||
|
    <span class="kword kword-int">int</span> <span class="identifier">a</span> <span class="operator">=</span> <span class="val-int">5</span>;<br />
|
||||||
|
    <span class="kword kword-return">return</span> <span class="val-int">0</span>;<br />
|
||||||
|
}<br />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
if `table_mode` were `1`, then the output would have been:
|
||||||
|
```
|
||||||
|
<div class="code">
|
||||||
|
<div class="code-inner">
|
||||||
|
<table>
|
||||||
|
<tr><td>1</td><td><span class="kword kword-int">int</span> <span class="identifier fdeclname">main</span>() {</td></tr>
|
||||||
|
<tr><td>2</td><td>  <span class="kword kword-int">int</span> <span class="identifier">a</span> <span class="operator">=</span> <span class="val-int">5</span>;</td></tr>
|
||||||
|
<tr><td>3</td><td>  <span class="kword kword-return">return</span> <span class="val-int">0</span>;</td></tr>
|
||||||
|
<tr><td>4</td><td>}</td></tr>
|
||||||
|
<tr><td>5</td><td></td></tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
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.
|
||||||
@@ -1 +1 @@
|
|||||||
gcc cli.c c2html.c -o c2h -Wall -Wextra -g
|
gcc cli.c c2html.c -o c2h -Wall -Wextra
|
||||||
@@ -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 <http://unlicense.org/> | *
|
||||||
|
* +---------------------------------------------------------------+ *
|
||||||
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -352,7 +383,7 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
|
|||||||
buff_init(&buff);
|
buff_init(&buff);
|
||||||
long lineno = 1;
|
long lineno = 1;
|
||||||
|
|
||||||
buff_printf(&buff, "\n"
|
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);
|
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);
|
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:
|
||||||
@@ -460,8 +495,11 @@ char *c2html(const char *str, long len, _Bool table_mode, const char *class_pref
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(table_mode)
|
if(table_mode)
|
||||||
buff_printf(&buff, "</td></tr>\n </table>\n");
|
buff_printf(&buff,
|
||||||
buff_printf(&buff, " </div>\n"
|
"</td></tr>\n"
|
||||||
|
" </table>\n");
|
||||||
|
buff_printf(&buff,
|
||||||
|
" </div>\n"
|
||||||
"</div>");
|
"</div>");
|
||||||
|
|
||||||
char *res;
|
char *res;
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// Build with: gcc example.c c2html.c -o example -Wall -Wextra
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <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 <br /> 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user