mkdio.c 6.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * mkdio -- markdown front end input functions
 *
 * 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 <ctype.h>

#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"

typedef ANCHOR(Line) LineAnchor;

19

20 21
/* create a new blank Document
 */
22 23
Document*
__mkd_new_Document()
24 25 26 27
{
    Document *ret = calloc(sizeof(Document), 1);

    if ( ret ) {
28
	if ( ret->ctx = calloc(sizeof(MMIOT), 1) ) {
29 30 31 32 33 34 35 36 37
	    ret->magic = VALID_DOCUMENT;
	    return ret;
	}
	free(ret);
    }
    return 0;
}


38 39
/* add a line to the markdown input chain, expanding tabs and
 * noting the presence of special characters as we go.
40
 */
41 42
void
__mkd_enqueue(Document* a, Cstring *line)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
    Line *p = calloc(sizeof *p, 1);
    unsigned char c;
    int xp = 0;
    int           size = S(*line);
    unsigned char *str = (unsigned char*)T(*line);

    CREATE(p->text);
    ATTACH(a->content, p);

    while ( size-- ) {
	if ( (c = *str++) == '\t' ) {
	    /* expand tabs into ->tabstop spaces.  We use ->tabstop
	     * because the ENTIRE FREAKING COMPUTER WORLD uses editors
	     * that don't do ^T/^D, but instead use tabs for indentation,
	     * and, of course, set their tabs down to 4 spaces 
	     */
	    do {
		EXPAND(p->text) = ' ';
	    } while ( ++xp % a->tabstop );
	}
	else if ( c >= ' ' ) {
65 66
	    if ( c == '|' )
		p->flags |= PIPECHAR;
67 68 69 70 71 72 73 74 75 76 77 78
	    EXPAND(p->text) = c;
	    ++xp;
	}
    }
    EXPAND(p->text) = 0;
    S(p->text)--;
    p->dle = mkd_firstnonblank(p);
}


/* trim leading blanks from a header line
 */
79 80
void
__mkd_header_dle(Line *p)
81 82 83 84 85 86 87 88 89 90 91 92 93 94
{
    CLIP(p->text, 0, 1);
    p->dle = mkd_firstnonblank(p);
}


/* build a Document from any old input.
 */
typedef int (*getc_func)(void*);

Document *
populate(getc_func getc, void* ctx, int flags)
{
    Cstring line;
95
    Document *a = __mkd_new_Document();
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    int c;
    int pandoc = 0;

    if ( !a ) return 0;

    a->tabstop = (flags & MKD_TABSTOP) ? 4 : TABSTOP;

    CREATE(line);

    while ( (c = (*getc)(ctx)) != EOF ) {
	if ( c == '\n' ) {
	    if ( pandoc != EOF && pandoc < 3 ) {
		if ( S(line) && (T(line)[0] == '%') )
		    pandoc++;
		else
		    pandoc = EOF;
	    }
113
	    __mkd_enqueue(a, &line);
114 115 116 117 118 119 120
	    S(line) = 0;
	}
	else if ( isprint(c) || isspace(c) || (c & 0x80) )
	    EXPAND(line) = c;
    }

    if ( S(line) )
121
	__mkd_enqueue(a, &line);
122 123 124 125 126 127 128 129 130 131

    DELETE(line);

    if ( (pandoc == 3) && !(flags & (MKD_NOHEADER|MKD_STRICT)) ) {
	/* the first three lines started with %, so we have a header.
	 * clip the first three lines out of content and hang them
	 * off header.
	 */
	Line *headers = T(a->content);

132 133 134
	a->title = headers;             __mkd_header_dle(a->title);
	a->author= headers->next;       __mkd_header_dle(a->author);
	a->date  = headers->next->next; __mkd_header_dle(a->date);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

	T(a->content) = headers->next->next->next;
    }

    return a;
}


/* convert a file into a linked list
 */
Document *
mkd_in(FILE *f, DWORD flags)
{
    return populate((getc_func)fgetc, f, flags & INPUT_MASK);
}


/* return a single character out of a buffer
 */
154 155
int
__mkd_io_strget(struct string_stream *in)
156 157 158 159 160 161 162 163 164 165 166 167
{
    if ( !in->size ) return EOF;

    --(in->size);

    return *(in->data)++;
}


/* convert a block of text into a linked list
 */
Document *
168
mkd_string(const char *buf, int len, DWORD flags)
169
{
170
    struct string_stream about;
171 172 173 174

    about.data = buf;
    about.size = len;

175
    return populate((getc_func)__mkd_io_strget, &about, flags & INPUT_MASK);
176 177 178 179 180 181 182 183 184 185 186
}


/* write the html to a file (xmlified if necessary)
 */
int
mkd_generatehtml(Document *p, FILE *output)
{
    char *doc;
    int szdoc;

187 188 189 190 191 192 193
    DO_OR_DIE( szdoc = mkd_document(p,&doc) );
    if ( p->ctx->flags & MKD_CDATA )
	DO_OR_DIE( mkd_generatexml(doc, szdoc, output) );
    else if ( fwrite(doc, szdoc, 1, output) != 1 )
	return EOF;
    DO_OR_DIE( putc('\n', output) );
    return 0;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}


/* convert some markdown text to html
 */
int
markdown(Document *document, FILE *out, int flags)
{
    if ( mkd_compile(document, flags) ) {
	mkd_generatehtml(document, out);
	mkd_cleanup(document);
	return 0;
    }
    return -1;
}


/* write out a Cstring, mangled into a form suitable for `<a href=` or `<a id=`
 */
void
214
mkd_string_to_anchor(char *s, int len, mkd_sta_function_t outchar,
215 216
				       void *out, int labelformat,
				       DWORD flags)
217
{
218
    static const unsigned char hexchars[] = "0123456789abcdef";
219 220 221 222 223 224
    unsigned char c;

    int i, size;
    char *line;

    size = mkd_line(s, len, &line, IS_LABEL);
225 226 227 228

    if ( !(flags & MKD_URLENCODEDANCHOR)
	 && labelformat
	 && (size>0) && !isalpha(line[0]) )
229 230 231 232 233 234
	(*outchar)('L',out);
    for ( i=0; i < size ; i++ ) {
	c = line[i];
	if ( labelformat ) {
	    if ( isalnum(c) || (c == '_') || (c == ':') || (c == '-') || (c == '.' ) )
		(*outchar)(c, out);
235
	    else if ( flags & MKD_URLENCODEDANCHOR ) {
236 237 238 239
		(*outchar)('%', out);
		(*outchar)(hexchars[c >> 4 & 0xf], out);
		(*outchar)(hexchars[c      & 0xf], out);
	    }
240
	    else
241
		(*outchar)('.', out);
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	}
	else
	    (*outchar)(c,out);
    }
	
    if (line)
	free(line);
}


/*  ___mkd_reparse() a line
 */
static void
mkd_parse_line(char *bfr, int size, MMIOT *f, int flags)
{
    ___mkd_initmmiot(f, 0);
    f->flags = flags & USER_FLAGS;
259
    ___mkd_reparse(bfr, size, 0, f, 0);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    ___mkd_emblock(f);
}


/* ___mkd_reparse() a line, returning it in malloc()ed memory
 */
int
mkd_line(char *bfr, int size, char **res, DWORD flags)
{
    MMIOT f;
    int len;
    
    mkd_parse_line(bfr, size, &f, flags);

    if ( len = S(f.out) ) {
	/* kludge alert;  we know that T(f.out) is malloced memory,
	 * so we can just steal it away.   This is awful -- there
	 * should be an opaque method that transparently moves 
	 * the pointer out of the embedded Cstring.
	 */
	EXPAND(f.out) = 0;
	*res = T(f.out);
	T(f.out) = 0;
	S(f.out) = ALLOCATED(f.out) = 0;
    }
    else {
	 *res = 0;
	 len = EOF;
     }
    ___mkd_freemmiot(&f, 0);
    return len;
}


/* ___mkd_reparse() a line, writing it to a FILE
 */
int
mkd_generateline(char *bfr, int size, FILE *output, DWORD flags)
{
    MMIOT f;
300
    int status;
301 302 303

    mkd_parse_line(bfr, size, &f, flags);
    if ( flags & MKD_CDATA )
304
	status = mkd_generatexml(T(f.out), S(f.out), output) != EOF;
305
    else
306
	status = fwrite(T(f.out), S(f.out), 1, output) == S(f.out);
307 308

    ___mkd_freemmiot(&f, 0);
309
    return status ? 0 : EOF;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
}


/* set the url display callback
 */
void
mkd_e_url(Document *f, mkd_callback_t edit)
{
    if ( f )
	f->cb.e_url = edit;
}


/* set the url options callback
 */
void
mkd_e_flags(Document *f, mkd_callback_t edit)
{
    if ( f )
	f->cb.e_flags = edit;
}


/* set the url display/options deallocator
 */
void
mkd_e_free(Document *f, mkd_free_t dealloc)
{
    if ( f )
	f->cb.e_free = dealloc;
}


/* set the url display/options context data field
 */
void
mkd_e_data(Document *f, void *data)
{
    if ( f )
	f->cb.e_data = data;
}
351 352 353 354 355 356 357 358 359 360


/* set the href prefix for markdown extra style footnotes
 */
void
mkd_ref_prefix(Document *f, char *data)
{
    if ( f )
	f->ref_prefix = data;
}