Пример #1
0
/* 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;
}
/* 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(char *text, int extensions, int output_format) {
    element *result;
    element *references;
    element *notes;
    element *labels;
    GString *formatted_text;
    GString *out;
    out = g_string_new("");

    formatted_text = preformat_text(text);

    if (output_format == OPML_FORMAT) {
        result = parse_markdown_for_opml(formatted_text->str, extensions);
    } else {
        references = parse_references(formatted_text->str, extensions);
        notes = parse_notes(formatted_text->str, extensions, references);
        labels = parse_labels(formatted_text->str, extensions, references, notes);
        result = parse_markdown_with_metadata(formatted_text->str, extensions, references, notes, labels);

        result = process_raw_blocks(result, extensions, references, notes, labels);
    }

    g_string_free(formatted_text, TRUE);

    print_element_list(out, result, output_format, extensions);

    free_element_list(result);

    if (output_format != OPML_FORMAT) {
        free_element_list(references);
        free_element_list(labels);
    }
    return out;
}
Пример #3
0
/* 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(char *text, int extensions, int output_format) {
    element *result;
    element *references;
    element *notes;
    element *labels;
    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);
    labels = parse_labels(formatted_text->str, extensions, references, notes);

    if (output_format == OPML_FORMAT) {
        result = parse_markdown_for_opml(formatted_text->str, extensions);
    } else {
        result = parse_markdown_with_metadata(formatted_text->str, extensions, references, notes, labels);
        result = process_raw_blocks(result, extensions, references, notes, labels);
    }

    g_string_free(formatted_text, TRUE);

    if (result == NULL) {
        /* The parsing was aborted */
        g_string_append(out,"MultiMarkdown was unable to parse this file.");
    } else {
        print_element_list(out, result, output_format, extensions);
    }
    free_element_list(result);

    free_element_list(references);
    free_element_list(labels);

    return out;
}
Пример #4
0
/* 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;
}