Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 439b9f67c9
Fetching contributors…

Cannot retrieve contributors at this time

file 100 lines (79 sloc) 2.188 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

/*
* github_flavoured -- implement the obnoxious "returns are hard newlines"
* feature in github flavoured markdown.
*
* Copyright (C) 2012 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"

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

Document *
gfm_populate(getc_func getc, void* ctx, int flags)
{
    Cstring line;
    Document *a = __mkd_new_Document();
    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;
}
            
            if (pandoc == EOF) {
EXPAND(line) = ' ';
EXPAND(line) = ' ';
}
__mkd_enqueue(a, &line);
S(line) = 0;
}
else if ( isprint(c) || isspace(c) || (c & 0x80) )
EXPAND(line) = c;
    }

    if ( S(line) )
__mkd_enqueue(a, &line);

    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);

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);

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

    return a;
}


/* convert a block of text into a linked list
*/
Document *
gfm_string(const char *buf, int len, DWORD flags)
{
    struct string_stream about;

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

    return gfm_populate((getc_func)__mkd_io_strget, &about, flags & INPUT_MASK);
}


/* convert a file into a linked list
*/
Document *
gfm_in(FILE *f, DWORD flags)
{
    return gfm_populate((getc_func)fgetc, f, flags & INPUT_MASK);
}
Something went wrong with that request. Please try again.