/* process_raw_blocks - traverses an element list, replacing any RAW elements with * the result of parsing them as markdown text, and recursing into the children * of parent elements. The result should be a tree of elements without any RAWs. */ static element * process_raw_blocks(element *input, int extensions, element *references, element *notes, element *labels) { element *current = NULL; element *last_child = NULL; char *contents; current = input; while (current != NULL) { if (current->key == RAW) { /* \001 is used to indicate boundaries between nested lists when there * is no blank line. We split the string by \001 and parse * each chunk separately. */ contents = strtok(current->contents.str, "\001"); current->key = LIST; current->children = parse_markdown(contents, extensions, references, notes, labels); last_child = current->children; while ((contents = strtok(NULL, "\001"))) { while (last_child->next != NULL) last_child = last_child->next; last_child->next = parse_markdown(contents, extensions, references, notes, labels); } free(current->contents.str); current->contents.str = NULL; } if (current->children != NULL) current->children = process_raw_blocks(current->children, extensions, references, notes, labels); current = current->next; } return input; }
static int markdown_compile(Value *vret, Value *v, RefNode *node) { Ref *r = Value_vp(*v); Markdown *md = Value_ptr(r->v[INDEX_MARKDOWN_MD]); if (!parse_markdown(md, Value_cstr(v[1]))) { return FALSE; } if (!link_markdown(r)) { return FALSE; } return TRUE; }
/* markdown_to_gstring - convert markdown text to the output format specified. * Returns a GString, which must be freed after use using g_string_free(). */ GString * markdown_to_g_string(const char *text, int extensions, int output_format) { element *result; element *references; element *notes; GString *formatted_text; GString *out; out = g_string_new(""); formatted_text = preformat_text(text); references = parse_references(formatted_text->str, extensions); notes = parse_notes(formatted_text->str, extensions, references); result = parse_markdown(formatted_text->str, extensions, references, notes); result = process_raw_blocks(result, extensions, references, notes); g_string_free(formatted_text, TRUE); print_element_list(out, result, output_format, extensions); free_element_list(result); free_element_list(references); return out; }