Files
discount/mkd2html.c
T
david parsons b73546a9bf 1. Write a sample program that uses some of the so-far-unpublished internal
interfaces to generate a complete web page.

2. Add the paragraph flow testcase to version control.
2008-01-08 22:35:16 -08:00

110 lines
2.3 KiB
C

/*
* mkd2html: parse a markdown input file and generate a web page.
* the first h1 found will be the title of the web page.
*
* usage: mkd2html [header-fields] / [signature fields] < markdown > html
*
* example:
*
* mkd2html ' <STYLE type="text/css">@import url(pages.css); </STYLE>' /
* '<HR>' '<P ALIGN=RIGHT>--generated by mk2html</P>' < text > html
*
*/
/*
* Copyright (C) 2007 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include "config.h"
#include "markdown.h"
#include "cstring.h"
extern Paragraph *__mkd_compile(Line*, FILE*, int, MMIOT*);
extern void __mkd_cleanup(Paragraph *, MMIOT*);
extern void __mkd_generatehtml(Paragraph *, MMIOT *);
extern Line *mkd_in(FILE *);
extern int mkd_text(char *,int, FILE*, int);
extern char version[];
Cstring *
findh1(Paragraph *p)
{
Cstring *ret;
if ( p->typ == HDR && p->hnumber == 1 )
return &(p->text->text);
if ( p->next && (ret = findh1(p->next)) )
return ret;
if ( p->down && (ret = findh1(p->down)) )
return ret;
return 0;
}
void
fail(char *why)
{
fprintf(stderr, "md2html: %s\n", why);
exit(1);
}
void
main(argc, argv)
char **argv;
{
Paragraph *tree;
Cstring *h;
MMIOT frame;
int i;
Line *input = mkd_in(stdin);
if ( !input ) fail("can't read input");
tree = __mkd_compile(input, stdout, 0, &frame);
if ( !tree ) fail("couldn't compile input");
h = findh1(tree);
/* print a header */
puts("<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional //EN\">");
puts("<HTML>");
puts("<HEAD>");
printf(" <meta name=\"GENERATOR\" content=\"md2html\" %s>\n", version);
puts(" <meta http-equiv=\"Content-Type\"");
puts(" content=\"text/html; charset-us-ascii\">");
if ( h ) {
printf(" <title>");
mkd_text(T(*h), S(*h), stdout, 0);
puts("</title>");
}
for (i=1; (i < argc) && (strcmp(argv[i], "/") != 0); i++)
printf(" %s\n", argv[i]);
puts("</HEAD>");
puts("<BODY>");
/* print the compiled body */
__mkd_generatehtml(tree, &frame);
__mkd_cleanup(tree, &frame);
i++;
while ( i < argc )
puts(argv[i++]);
puts("</BODY>");
puts("</HTML>");
exit(0);
}