Files
discount/mkd2html.c
T
david parsons 3fc49328f3 Rework the published interface.
1. Officially expose mkd_compile(), mkd_cleanup(),
       mkd_generatehtml().
    2. Internally use the new Document structure, which
       hold the markup and (optionally) header information.
    3. Support ``--enable-<foo>`` during configure.sh
       * change --with-dl-tag to --enable-dl-tag.
       * add --enable-pandoc-header to support pandoc headers.
2008-01-10 12:22:39 -08:00

105 lines
2.1 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 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;
{
Cstring *h;
MMIOT frame;
int i;
Document *input;
if ( (input = mkd_in(stdin)) == 0 )
fail("can't read input");
if ( !mkd_compile(input, stdout, 0, &frame) )
fail("couldn't compile input");
if ( input->headers )
h = &(input->headers->text);
else
h = findh1(input->code);
/* 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(input, &frame);
mkd_cleanup(input, &frame);
i++;
while ( i < argc )
puts(argv[i++]);
puts("</BODY>");
puts("</HTML>");
exit(0);
}