Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7a912929b0
Fetching contributors…

Cannot retrieve contributors at this time

file 185 lines (151 sloc) 3.744 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
* mkd2html: parse a markdown input file and generate a web page.
*
* usage: mkd2html [options] filename
* or mkd2html [options] < markdown > html
*
* options
* -css css-file
* -header line-to-add-to-<HEADER>
* -footer line-to-add-before-</BODY>
*
* example:
*
* mkd2html -cs /~orc/pages.css syntax
* ( read syntax OR syntax.text, write syntax.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 "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_BASENAME
# ifdef HAVE_LIBGEN_H
# include <libgen.h>
# else
# include <unistd.h>
# endif
#endif
#include <stdarg.h>

#include "mkdio.h"
#include "cstring.h"
#include "amalloc.h"

char *pgm = "mkd2html";

#ifndef HAVE_BASENAME
char *
basename(char *path)
{
    char *p;

    if (( p = strrchr(path, '/') ))
return 1+p;
    return path;
}
#endif

void
fail(char *why, ...)
{
    va_list ptr;

    va_start(ptr,why);
    fprintf(stderr, "%s: ", pgm);
    vfprintf(stderr, why, ptr);
    fputc('\n', stderr);
    va_end(ptr);
    exit(1);
}


void
main(argc, argv)
char **argv;
{
    char *h;
    char *source = 0, *dest = 0;
    MMIOT *mmiot;
    int i;
    FILE *input, *output;
    STRING(char*) css, headers, footers;


    CREATE(css);
    CREATE(headers);
    CREATE(footers);
    pgm = basename(argv[0]);

    while ( argc > 2 ) {
if ( strcmp(argv[1], "-css") == 0 ) {
EXPAND(css) = argv[2];
argc -= 2;
argv += 2;
}
else if ( strcmp(argv[1], "-header") == 0 ) {
EXPAND(headers) = argv[2];
argc -= 2;
argv += 2;
}
else if ( strcmp(argv[1], "-footer") == 0 ) {
EXPAND(footers) = argv[2];
argc -= 2;
argv += 2;
}
    }


    if ( argc > 1 ) {
char *p, *dot;

source = malloc(strlen(argv[1]) + 6);
dest = malloc(strlen(argv[1]) + 6);

if ( !(source && dest) )
fail("out of memory allocating name buffers");

strcpy(source, argv[1]);
if (( p = strrchr(source, '/') ))
p = source;
else
++p;

if ( (input = fopen(source, "r")) == 0 ) {
strcat(source, ".text");
if ( (input = fopen(source, "r")) == 0 )
fail("can't open either %s or %s", argv[1], source);
}
strcpy(dest, source);

if (( dot = strrchr(dest, '.') ))
*dot = 0;
strcat(dest, ".html");

if ( (output = fopen(dest, "w")) == 0 )
fail("can't write to %s", dest);
    }
    else {
input = stdin;
output = stdout;
    }

    if ( (mmiot = mkd_in(input, 0)) == 0 )
fail("can't read %s", source ? source : "stdin");

    if ( !mkd_compile(mmiot, 0) )
fail("couldn't compile input");


    h = mkd_doc_title(mmiot);

    /* print a header */

    fprintf(output,
"<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional //EN\">\n"
"<html>\n"
"<head>\n"
" <meta name=\"GENERATOR\" content=\"mkd2html %s\">\n", markdown_version);

    fprintf(output," <meta http-equiv=\"Content-Type\"\n"
" content=\"text/html; charset-us-ascii\">");

    for ( i=0; i < S(css); i++ )
fprintf(output, " <link rel=\"stylesheet\"\n"
" type=\"text/css\"\n"
" href=\"%s\" />\n", T(css)[i]);

    if ( h ) {
fprintf(output," <title>");
mkd_generateline(h, strlen(h), output, 0);
fprintf(output, "</title>\n");
    }
    for ( i=0; i < S(headers); i++ )
fprintf(output, " %s\n", T(headers)[i]);
    fprintf(output, "</head>\n"
"<body>\n");

    /* print the compiled body */

    mkd_generatehtml(mmiot, output);

    for ( i=0; i < S(footers); i++ )
fprintf(output, "%s\n", T(footers)[i]);
    
    fprintf(output, "</body>\n"
"</html>\n");
    
    mkd_cleanup(mmiot);
    exit(0);
}
Something went wrong with that request. Please try again.