Exemplo n.º 1
0
Arquivo: main.c Projeto: txdv/cmark
static void
render_html(test_batch_runner *runner)
{
	char *html;

	static const char markdown[] =
		"foo *bar*\n"
		"\n"
		"paragraph 2\n";
	cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1,
		CMARK_OPT_DEFAULT);

	cmark_node *paragraph = cmark_node_first_child(doc);
	html = cmark_render_html(paragraph, CMARK_OPT_DEFAULT);
	STR_EQ(runner, html, "<p>foo <em>bar</em></p>\n",
	       "render single paragraph");
	free(html);

	cmark_node *string = cmark_node_first_child(paragraph);
	html = cmark_render_html(string, CMARK_OPT_DEFAULT);
	STR_EQ(runner, html, "foo ", "render single inline");
	free(html);

	cmark_node *emph = cmark_node_next(string);
	html = cmark_render_html(emph, CMARK_OPT_DEFAULT);
	STR_EQ(runner, html, "<em>bar</em>", "render inline with children");
	free(html);

	cmark_node_free(doc);
}
Exemplo n.º 2
0
char*
CFCPerlPod_md_to_pod(const char *md, CFCClass *klass, int header_level) {
    int options = CMARK_OPT_SMART
                  | CMARK_OPT_VALIDATE_UTF8;
    cmark_node *doc = cmark_parse_document(md, strlen(md), options);
    char *pod = S_node_to_pod(doc, klass, header_level);
    cmark_node_free(doc);

    return pod;
}
Exemplo n.º 3
0
char*
CFCPerlPod_md_to_pod(CFCPerlPod *self, CFCClass *klass, const char *md) {
    (void)self; // unused

    cmark_node *doc = cmark_parse_document(md, strlen(md));
    char *pod = S_nodes_to_pod(klass, doc);
    cmark_node_free(doc);

    return pod;
}
Exemplo n.º 4
0
char *cmark_markdown_to_html(const char *text, size_t len, int options) {
  cmark_node *doc;
  char *result;

  doc = cmark_parse_document(text, len, options);

  result = cmark_render_html(doc, options, NULL);
  cmark_node_free(doc);

  return result;
}
Exemplo n.º 5
0
char *cmark_markdown_to_html(const char *text, int len)
{
	cmark_node *doc;
	char *result;

	doc = cmark_parse_document(text, len, CMARK_OPT_DEFAULT);

	result = cmark_render_html(doc, CMARK_OPT_DEFAULT);
	cmark_node_free(doc);

	return result;
}
Exemplo n.º 6
0
unsigned char *cmark_markdown_to_html(unsigned char *text, int len)
{
	node_block *blocks;
	unsigned char *result;

	blocks = cmark_parse_document(text, len);

	result = cmark_render_html(blocks);
	cmark_free_blocks(blocks);

	return result;
}
Exemplo n.º 7
0
unsigned char *cmark_markdown_to_html(unsigned char *text, int len)
{
	node_block *blocks;
	strbuf htmlbuf = GH_BUF_INIT;
	unsigned char *result;

	blocks = cmark_parse_document(text, len);

	cmark_render_html(&htmlbuf, blocks);
	cmark_free_blocks(blocks);

	result = strbuf_detach(&htmlbuf);
	strbuf_free(&htmlbuf);

	return result;
}
Exemplo n.º 8
0
Arquivo: main.c Projeto: txdv/cmark
static void
iterator(test_batch_runner *runner) {
	cmark_node *doc = cmark_parse_document("> a *b*\n\nc", 10, CMARK_OPT_DEFAULT);
	int parnodes = 0;
	cmark_event_type ev_type;
	cmark_iter *iter = cmark_iter_new(doc);
	cmark_node *cur;

	while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
		cur = cmark_iter_get_node(iter);
		if (cur->type == CMARK_NODE_PARAGRAPH &&
		    ev_type == CMARK_EVENT_ENTER) {
			parnodes += 1;
		}
	}
	INT_EQ(runner, parnodes, 2, "iterate correctly counts paragraphs");

	cmark_iter_free(iter);
	cmark_node_free(doc);
}
Exemplo n.º 9
0
char*
CFCPerlPod_md_doc_to_pod(const char *module, const char *md) {
    int options = CMARK_OPT_SMART
                  | CMARK_OPT_VALIDATE_UTF8;
    cmark_node *doc = cmark_parse_document(md, strlen(md), options);
    cmark_node *maybe_header = cmark_node_first_child(doc);
    char *name;
    char *desc;

    if (maybe_header
        && cmark_node_get_type(maybe_header) == CMARK_NODE_HEADER
       ) {
        cmark_node *header_child = cmark_node_first_child(maybe_header);
        char *short_desc = S_nodes_to_pod(header_child, NULL, 1);
        name = CFCUtil_sprintf("%s - %s", module, short_desc);
        FREEMEM(short_desc);

        cmark_node *remaining = cmark_node_next(maybe_header);
        desc = S_nodes_to_pod(remaining, NULL, 1);
    }
    else {
        // No header found.
        name = CFCUtil_strdup(module);
        desc = S_node_to_pod(doc, NULL, 1);
    }

    const char *pattern =
        "=head1 NAME\n"
        "\n"
        "%s\n"
        "\n"
        "=head1 DESCRIPTION\n"
        "\n"
        "%s";
    char *retval = CFCUtil_sprintf(pattern, name, desc);

    FREEMEM(name);
    FREEMEM(desc);
    cmark_node_free(doc);
    return retval;
}
Exemplo n.º 10
0
Arquivo: main.c Projeto: txdv/cmark
static void
iterator_delete(test_batch_runner *runner) {
	static const char md[] =
		"a *b* c\n"
		"\n"
		"* item1\n"
		"* item2\n"
		"\n"
		"a `b` c\n"
		"\n"
		"* item1\n"
		"* item2\n";
	cmark_node *doc  = cmark_parse_document(md, sizeof(md) - 1,
						CMARK_OPT_DEFAULT);
	cmark_iter *iter = cmark_iter_new(doc);
	cmark_event_type ev_type;

	while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
		cmark_node *node = cmark_iter_get_node(iter);
		// Delete list, emph, and code nodes.
		if ((ev_type == CMARK_EVENT_EXIT &&
		     node->type == CMARK_NODE_LIST) ||
		    (ev_type == CMARK_EVENT_EXIT &&
		     node->type == CMARK_NODE_EMPH) ||
		    (ev_type == CMARK_EVENT_ENTER &&
		     node->type == CMARK_NODE_CODE)) {
			cmark_node_free(node);
		}
	}

	char *html = cmark_render_html(doc, CMARK_OPT_DEFAULT);
	static const char expected[] =
		"<p>a  c</p>\n"
		"<p>a  c</p>\n";
	STR_EQ(runner, html, expected, "iterate and delete nodes");

	free(html);
	cmark_iter_free(iter);
	cmark_node_free(doc);
}
Exemplo n.º 11
0
Arquivo: main.c Projeto: txdv/cmark
static void
accessors(test_batch_runner *runner)
{
	static const char markdown[] =
		"## Header\n"
		"\n"
		"* Item 1\n"
		"* Item 2\n"
		"\n"
		"2. Item 1\n"
		"\n"
		"3. Item 2\n"
		"\n"
		"\n"
		"    code\n"
		"\n"
		"``` lang\n"
		"fenced\n"
		"```\n"
		"\n"
		"<div>html</div>\n"
		"\n"
		"[link](url 'title')\n";

	cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT);

	// Getters

	cmark_node *header = cmark_node_first_child(doc);
	INT_EQ(runner, cmark_node_get_header_level(header), 2,
	       "get_header_level");

	cmark_node *bullet_list = cmark_node_next(header);
	INT_EQ(runner, cmark_node_get_list_type(bullet_list),
	       CMARK_BULLET_LIST, "get_list_type bullet");
	INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1,
	       "get_list_tight tight");

	cmark_node *ordered_list = cmark_node_next(bullet_list);
	INT_EQ(runner, cmark_node_get_list_type(ordered_list),
	       CMARK_ORDERED_LIST, "get_list_type ordered");
	INT_EQ(runner, cmark_node_get_list_delim(ordered_list),
	       CMARK_PERIOD_DELIM, "get_list_delim ordered");
	INT_EQ(runner, cmark_node_get_list_start(ordered_list), 2,
	       "get_list_start");
	INT_EQ(runner, cmark_node_get_list_tight(ordered_list), 0,
	       "get_list_tight loose");

	cmark_node *code = cmark_node_next(ordered_list);
	STR_EQ(runner, cmark_node_get_literal(code), "code\n",
	       "get_literal indented code");

	cmark_node *fenced = cmark_node_next(code);
	STR_EQ(runner, cmark_node_get_literal(fenced), "fenced\n",
	       "get_literal fenced code");
	STR_EQ(runner, cmark_node_get_fence_info(fenced), "lang",
	       "get_fence_info");

	cmark_node *html = cmark_node_next(fenced);
	STR_EQ(runner, cmark_node_get_literal(html),
	       "<div>html</div>\n", "get_literal html");

	cmark_node *paragraph = cmark_node_next(html);
	INT_EQ(runner, cmark_node_get_start_line(paragraph), 19,
	       "get_start_line");
	INT_EQ(runner, cmark_node_get_start_column(paragraph), 1,
	       "get_start_column");
	INT_EQ(runner, cmark_node_get_end_line(paragraph), 19,
	       "get_end_line");

	cmark_node *link = cmark_node_first_child(paragraph);
	STR_EQ(runner, cmark_node_get_url(link), "url",
	       "get_url");
	STR_EQ(runner, cmark_node_get_title(link), "title",
	       "get_title");

	cmark_node *string = cmark_node_first_child(link);
	STR_EQ(runner, cmark_node_get_literal(string), "link",
	       "get_literal string");

	// Setters

	OK(runner, cmark_node_set_header_level(header, 3),
	   "set_header_level");

	OK(runner, cmark_node_set_list_type(bullet_list, CMARK_ORDERED_LIST),
	   "set_list_type ordered");
	OK(runner, cmark_node_set_list_delim(bullet_list, CMARK_PAREN_DELIM),
	   "set_list_delim paren");
	OK(runner, cmark_node_set_list_start(bullet_list, 3),
	   "set_list_start");
	OK(runner, cmark_node_set_list_tight(bullet_list, 0),
	   "set_list_tight loose");

	OK(runner, cmark_node_set_list_type(ordered_list, CMARK_BULLET_LIST),
	   "set_list_type bullet");
	OK(runner, cmark_node_set_list_tight(ordered_list, 1),
	   "set_list_tight tight");

	OK(runner, cmark_node_set_literal(code, "CODE\n"),
	   "set_literal indented code");

	OK(runner, cmark_node_set_literal(fenced, "FENCED\n"),
	   "set_literal fenced code");
	OK(runner, cmark_node_set_fence_info(fenced, "LANG"),
	   "set_fence_info");

	OK(runner, cmark_node_set_literal(html, "<div>HTML</div>\n"),
	   "set_literal html");

	OK(runner, cmark_node_set_url(link, "URL"),
	   "set_url");
	OK(runner, cmark_node_set_title(link, "TITLE"),
	   "set_title");

	OK(runner, cmark_node_set_literal(string, "LINK"),
	   "set_literal string");

	char *rendered_html = cmark_render_html(doc, CMARK_OPT_DEFAULT);
	static const char expected_html[] =
		"<h3>Header</h3>\n"
		"<ol start=\"3\">\n"
		"<li>\n"
		"<p>Item 1</p>\n"
		"</li>\n"
		"<li>\n"
		"<p>Item 2</p>\n"
		"</li>\n"
		"</ol>\n"
		"<ul>\n"
		"<li>Item 1</li>\n"
		"<li>Item 2</li>\n"
		"</ul>\n"
		"<pre><code>CODE\n"
		"</code></pre>\n"
		"<pre><code class=\"language-LANG\">FENCED\n"
		"</code></pre>\n"
		"<div>HTML</div>\n"
		"<p><a href=\"URL\" title=\"TITLE\">LINK</a></p>\n";
	STR_EQ(runner, rendered_html, expected_html, "setters work");
	free(rendered_html);

	// Getter errors

	INT_EQ(runner, cmark_node_get_header_level(bullet_list), 0,
	       "get_header_level error");
	INT_EQ(runner, cmark_node_get_list_type(header), CMARK_NO_LIST,
	       "get_list_type error");
	INT_EQ(runner, cmark_node_get_list_start(code), 0,
	       "get_list_start error");
	INT_EQ(runner, cmark_node_get_list_tight(fenced), 0,
	       "get_list_tight error");
	OK(runner, cmark_node_get_literal(ordered_list) == NULL,
	   "get_literal error");
	OK(runner, cmark_node_get_fence_info(paragraph) == NULL,
	   "get_fence_info error");
	OK(runner, cmark_node_get_url(html) == NULL,
	   "get_url error");
	OK(runner, cmark_node_get_title(header) == NULL,
	   "get_title error");

	// Setter errors

	OK(runner, !cmark_node_set_header_level(bullet_list, 3),
	   "set_header_level error");
	OK(runner, !cmark_node_set_list_type(header, CMARK_ORDERED_LIST),
	   "set_list_type error");
	OK(runner, !cmark_node_set_list_start(code, 3),
	   "set_list_start error");
	OK(runner, !cmark_node_set_list_tight(fenced, 0),
	   "set_list_tight error");
	OK(runner, !cmark_node_set_literal(ordered_list, "content\n"),
	   "set_literal error");
	OK(runner, !cmark_node_set_fence_info(paragraph, "lang"),
	   "set_fence_info error");
	OK(runner, !cmark_node_set_url(html, "url"),
	   "set_url error");
	OK(runner, !cmark_node_set_title(header, "title"),
	   "set_title error");

	OK(runner, !cmark_node_set_header_level(header, 0),
	   "set_header_level too small");
	OK(runner, !cmark_node_set_header_level(header, 7),
	   "set_header_level too large");
	OK(runner, !cmark_node_set_list_type(bullet_list, CMARK_NO_LIST),
	   "set_list_type invalid");
	OK(runner, !cmark_node_set_list_start(bullet_list, -1),
	   "set_list_start negative");

	cmark_node_free(doc);
}