hoedown/examples/smartypants.c

50 lines
1,012 B
C
Raw Normal View History

#include "html.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define READ_UNIT 1024
#define OUTPUT_UNIT 64
int
main(int argc, char **argv)
{
2013-09-20 06:09:52 +00:00
struct hoedown_buffer *ib, *ob;
FILE *in = stdin;
/* opening the file if given from the command line */
if (argc > 1) {
in = fopen(argv[1], "r");
if (!in) {
2013-09-21 13:29:41 +00:00
fprintf(stderr, "Unable to open input file \"%s\": %s\n", argv[1], strerror(errno));
return 1;
}
}
/* reading everything */
2013-09-20 06:09:52 +00:00
ib = hoedown_buffer_new(READ_UNIT);
while (!feof(in) && !ferror(in)) {
2013-09-20 06:09:52 +00:00
hoedown_buffer_grow(ib, ib->size + READ_UNIT);
ib->size += fread(ib->data + ib->size, 1, READ_UNIT, in);
}
if (in != stdin)
fclose(in);
2013-09-20 17:06:25 +00:00
/* performing SmartyPants parsing */
2013-09-20 06:09:52 +00:00
ob = hoedown_buffer_new(OUTPUT_UNIT);
2013-09-20 06:09:52 +00:00
hoedown_html_smartypants(ob, ib->data, ib->size);
/* writing the result to stdout */
(void)fwrite(ob->data, 1, ob->size, stdout);
/* cleanup */
2013-09-20 06:09:52 +00:00
hoedown_buffer_release(ib);
hoedown_buffer_release(ob);
2013-09-21 13:29:41 +00:00
return ferror(stdout);
}