Esempio n. 1
0
static Paragraph *
fencedcodeblock(ParagraphRoot *d, Line **ptr)
{
    Line *first, *r;
    Paragraph *ret;

    first = (*ptr);
    
    /* don't allow zero-length code fences
     */
    if ( (first->next == 0) || iscodefence(first->next, first->count, 0) )
	return 0;

    /* find the closing fence, discard the fences,
     * return a Paragraph with the contents
     */
    for ( r = first; r && r->next; r = r->next )
	if ( iscodefence(r->next, first->count, first->kind) ) {
	    (*ptr) = r->next->next;
	    ret = Pp(d, first->next, CODE);
      if (S(first->text) - first->count > 0) {
        char *lang_attr = T(first->text) + first->count;
        while ( *lang_attr != 0 && *lang_attr == ' ' ) lang_attr++;
        ret->lang = strdup(lang_attr);
      }
      else {
        ret->lang = 0;
      }
	    ___mkd_freeLine(first);
	    ___mkd_freeLine(r->next);
	    r->next = 0;
	    return ret;
	}
    return 0;
}
Esempio n. 2
0
static Paragraph *
fencedcodeblock(ParagraphRoot *d, Line **ptr)
{
    Line *first, *r;
    Paragraph *ret;

    first = (*ptr);
    
    /* don't allow zero-length code fences
     */
    if ( (first->next == 0) || iscodefence(first->next, first->count) )
	return 0;

    /* find the closing fence, discard the fences,
     * return a Paragraph with the contents
     */
    for ( r = first; r && r->next; r = r->next )
	if ( iscodefence(r->next, first->count) ) {
	    (*ptr) = r->next->next;
	    ret = Pp(d, first->next, CODE);
	    ___mkd_freeLine(first);
	    ___mkd_freeLine(r->next);
	    r->next = 0;
	    return ret;
	}
    return 0;
}
Esempio n. 3
0
/*
 * break a collection of markdown input into
 * blocks of lists, code, html, and text to
 * be marked up.
 */
static Paragraph *
compile(Line *ptr, int toplevel, MMIOT *f)
{
    ParagraphRoot d = { 0, 0 };
    Paragraph *p = 0;
    Line *r;
    int para = toplevel;
    int blocks = 0;
    int hdr_type, list_type, list_class, indent;

    ptr = consume(ptr, &para);

    while ( ptr ) {
	if ( iscode(ptr) ) {
	    p = Pp(&d, ptr, CODE);
	    
	    if ( f->flags & MKD_1_COMPAT) {
		/* HORRIBLE STANDARDS KLUDGE: the first line of every block
		 * has trailing whitespace trimmed off.
		 */
		___mkd_tidy(&p->text->text);
	    }
	    
	    ptr = codeblock(p);
	}
#if WITH_FENCED_CODE
	else if ( iscodefence(ptr,3,0) && (p=fencedcodeblock(&d, &ptr)) )
	    /* yay, it's already done */ ;
#endif
	else if ( ishr(ptr) ) {
	    p = Pp(&d, 0, HR);
	    r = ptr;
	    ptr = ptr->next;
	    ___mkd_freeLine(r);
	}
	else if ( list_class = islist(ptr, &indent, f->flags, &list_type) ) {
	    if ( list_class == DL ) {
		p = Pp(&d, ptr, DL);
		ptr = definition_block(p, indent, f, list_type);
	    }
	    else {
		p = Pp(&d, ptr, list_type);
		ptr = enumerated_block(p, indent, f, list_class);
	    }
	}
	else if ( isquote(ptr) ) {
	    p = Pp(&d, ptr, QUOTE);
	    ptr = quoteblock(p, f->flags);
	    p->down = compile(p->text, 1, f);
	    p->text = 0;
	}
	else if ( ishdr(ptr, &hdr_type) ) {
	    p = Pp(&d, ptr, HDR);
	    ptr = headerblock(p, hdr_type);
	}
	else {
	    p = Pp(&d, ptr, MARKUP);
	    ptr = textblock(p, toplevel, f->flags);
	    /* tables are a special kind of paragraph */
	    if ( actually_a_table(f, p->text) )
		p->typ = TABLE;
	}

	if ( (para||toplevel) && !p->align )
	    p->align = PARA;

	blocks++;
	para = toplevel || (blocks > 1);
	ptr = consume(ptr, &para);

	if ( para && !p->align )
	    p->align = PARA;

    }
    return T(d);
}