cleaned up the cli
This commit is contained in:
@@ -66,4 +66,4 @@ if `table_mode` were `1`, then the output would have been:
|
|||||||
```
|
```
|
||||||
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.
|
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.
|
Since the generated class names of the HTML tags are pretty generic (`identifier`, `operator`, `comment`, ..) they may conflict with your own CSS code. To avoid this problem you can specify a prefix to be prepented to these names. The command-line interface `c2h` uses the `c2h-` previx.
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -29,45 +30,119 @@ static char *load_file(const char *file, long *size)
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
static int fileconv(const char *input_file, const char *output_file,
|
||||||
|
const char *style_file, const char *prefix, bool notable)
|
||||||
{
|
{
|
||||||
if(argc < 3) {
|
if(input_file == NULL || output_file == NULL) {
|
||||||
fprintf(stderr, "ERROR: Missing input and output file\n");
|
fprintf(stderr,
|
||||||
return -1;
|
"Error: You must specify both input and output "
|
||||||
}
|
"files using the --input and --output options\n");
|
||||||
char *src_file = argv[1];
|
|
||||||
char *dst_file = argv[2];
|
|
||||||
|
|
||||||
long src_size;
|
|
||||||
char *src_data = load_file(src_file, &src_size);
|
|
||||||
|
|
||||||
if(src_data == NULL) {
|
|
||||||
fprintf(stderr, "ERROR: Failed to open \"%s\"\n", src_file);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_Bool table_mode = 0;
|
/* Load the input string */
|
||||||
const char *class_prefix = "c2h-";
|
long input_size;
|
||||||
|
char *input = load_file(input_file, &input_size);
|
||||||
|
if(input == NULL) {
|
||||||
|
fprintf(stderr, "Error: Failed to open file %s\n", input_file);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Convert it */
|
||||||
const char *error;
|
const char *error;
|
||||||
char *dst_data = c2html(src_data, src_size, table_mode, class_prefix, &error);
|
char *output = c2html(input, input_size,
|
||||||
|
!notable, prefix, &error);
|
||||||
if(dst_data == NULL)
|
if(output == NULL) {
|
||||||
fprintf(stderr, "ERROR: %s\n", error);
|
fprintf(stderr, "Error: %s\n", error);
|
||||||
else {
|
free(input);
|
||||||
|
return -1;
|
||||||
FILE *fp = fopen(dst_file, "wb");
|
|
||||||
if(fp == NULL)
|
|
||||||
fprintf(stderr, "ERROR: Failed to open \"%s\"\n", dst_file);
|
|
||||||
else {
|
|
||||||
fwrite(dst_data, 1, strlen(dst_data), fp);
|
|
||||||
fclose(fp);
|
|
||||||
fprintf(stderr, "OK\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
free(dst_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(src_data);
|
/* Open the output file */
|
||||||
|
FILE *fp = fopen(output_file, "wb");
|
||||||
|
if(fp == NULL) {
|
||||||
|
fprintf(stderr, "ERROR: Failed to write to file %s\n",
|
||||||
|
output_file);
|
||||||
|
free(output);
|
||||||
|
free(input);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(style_file != NULL) {
|
||||||
|
char *style_data = load_file(style_file, NULL);
|
||||||
|
if(style_data == NULL) {
|
||||||
|
fprintf(stderr, "Error: Failed to open file %s\n",
|
||||||
|
style_file);
|
||||||
|
free(output);
|
||||||
|
free(input);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
fputs("<style>", fp);
|
||||||
|
fputs(style_data, fp);
|
||||||
|
fputs("</style>", fp);
|
||||||
|
free(style_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ..and write the converted string to it */
|
||||||
|
fwrite(output, 1, strlen(output), fp);
|
||||||
|
fclose(fp);
|
||||||
|
fprintf(stderr, "OK\n");
|
||||||
|
|
||||||
|
/* All done! :^) */
|
||||||
|
free(output);
|
||||||
|
free(input);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
|
||||||
|
/* Parse command-line arguments */
|
||||||
|
char *input_file = NULL,
|
||||||
|
*output_file = NULL,
|
||||||
|
*style_file = NULL,
|
||||||
|
*prefix = NULL;
|
||||||
|
bool notable = 0;
|
||||||
|
bool http = 0;
|
||||||
|
for(int i = 1; i < argc; i += 1) {
|
||||||
|
if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--input")) {
|
||||||
|
i += 1;
|
||||||
|
if(i == argc || argv[i][0] == '-') {
|
||||||
|
fprintf(stderr, "Error: Missing argument after %s\n", argv[i-1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
input_file = argv[i];
|
||||||
|
} else if(!strcmp(argv[i], "-o") || !strcmp(argv[i], "--output")) {
|
||||||
|
i += 1;
|
||||||
|
if(i == argc || argv[i][0] == '-') {
|
||||||
|
fprintf(stderr, "Error: Missing argument after %s\n", argv[i-1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
output_file = argv[i];
|
||||||
|
} else if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--prefix")) {
|
||||||
|
i += 1;
|
||||||
|
if(i == argc || argv[i][0] == '-') {
|
||||||
|
fprintf(stderr, "Error: Missing argument after %s\n", argv[i-1]);
|
||||||
|
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] == '-') {
|
||||||
|
fprintf(stderr, "Error: Missing argument after %s\n", argv[i-1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
style_file = argv[i];
|
||||||
|
} else if(!strcmp(argv[i], "--http")) {
|
||||||
|
http = 1;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Error: Unknown option %s\n", argv[i]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileconv(input_file, output_file,
|
||||||
|
style_file, prefix, notable);
|
||||||
|
}
|
||||||
@@ -22,7 +22,11 @@ int main()
|
|||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
char *html = c2html(c, strlen(c), table_mode, prefix, NULL);
|
char *html = c2html(c, strlen(c), table_mode, prefix, NULL);
|
||||||
|
|
||||||
|
/* .. check for errors .. */
|
||||||
|
|
||||||
printf("%s\n", html);
|
printf("%s\n", html);
|
||||||
|
|
||||||
free(html);
|
free(html);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,18 @@
|
|||||||
|
|
||||||
|
div.c2h-main {
|
||||||
|
}
|
||||||
|
|
||||||
div.c2h-code {
|
div.c2h-code {
|
||||||
max-height: 80vh;
|
max-height: 600px;
|
||||||
margin: 50px;
|
|
||||||
|
padding: 10px;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
color: white;
|
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
|
|
||||||
|
color: white;
|
||||||
background: hsl(210, 15%, 22%);
|
background: hsl(210, 15%, 22%);
|
||||||
padding: 10px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
div.c2h-code-inner::-webkit-scrollbar {
|
div.c2h-code-inner::-webkit-scrollbar {
|
||||||
@@ -21,6 +26,7 @@
|
|||||||
|
|
||||||
div.c2h-code-inner {
|
div.c2h-code-inner {
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
|
height: inherit;
|
||||||
max-height: inherit;
|
max-height: inherit;
|
||||||
scrollbar-color: hsla(210, 13%, 40%, 0.7) transparent;
|
scrollbar-color: hsla(210, 13%, 40%, 0.7) transparent;
|
||||||
}
|
}
|
||||||
@@ -32,6 +38,7 @@
|
|||||||
|
|
||||||
div.c2h-code table td {
|
div.c2h-code table td {
|
||||||
color: white;
|
color: white;
|
||||||
|
vertical-align: top;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.c2h-code table td:first-child {
|
div.c2h-code table td:first-child {
|
||||||
|
|||||||
Reference in New Issue
Block a user