From c28af79329264a7cf331a1b1c414919e4ed9e9f9 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 2 Sep 2014 13:37:34 +0200 Subject: It buiiiilds --- src/html/html.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 src/html/html.c (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c new file mode 100644 index 0000000..2f160ca --- /dev/null +++ b/src/html/html.c @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include + +#include "stmd.h" +#include "debug.h" +#include "scanners.h" +#include "html/houdini.h" + +// Functions to convert block and inline lists to HTML strings. + +static void escape_html(gh_buf *dest, const unsigned char *source, int length) +{ + if (length < 0) + length = strlen((char *)source); + + houdini_escape_html0(dest, source, (size_t)length, 0); +} + +static void escape_href(gh_buf *dest, const unsigned char *source, int length) +{ + if (length < 0) + length = strlen((char *)source); + + houdini_escape_href(dest, source, (size_t)length); +} + +static inline void cr(gh_buf *html) +{ + if (html->size && html->ptr[html->size - 1] != '\n') + gh_buf_putc(html, '\n'); +} + +// Convert a block list to HTML. Returns 0 on success, and sets result. +void blocks_to_html(gh_buf *html, block *b, bool tight) +{ + struct ListData *data; + + while(b != NULL) { + switch(b->tag) { + case document: + blocks_to_html(html, b->children, false); + break; + + case paragraph: + if (tight) { + inlines_to_html(html, b->inline_content); + } else { + cr(html); + gh_buf_puts(html, "

"); + inlines_to_html(html, b->inline_content); + gh_buf_puts(html, "

"); + cr(html); + } + break; + + case block_quote: + cr(html); + gh_buf_puts(html, "
"); + blocks_to_html(html, b->children, false); + gh_buf_puts(html, "
"); + cr(html); + break; + + case list_item: + cr(html); + gh_buf_puts(html, "
  • "); + blocks_to_html(html, b->children, tight); + gh_buf_trim(html); + gh_buf_puts(html, "
  • "); + cr(html); + break; + + case list: + // make sure a list starts at the beginning of the line: + cr(html); + data = &(b->attributes.list_data); + + if (data->start > 1) { + gh_buf_printf(html, "<%s start=\"%d\">\n", + data->list_type == bullet ? "ul" : "ol", + data->start); + } else { + gh_buf_puts(html, data->list_type == bullet ? "
      \n" : "
        \n"); + } + + blocks_to_html(html, b->children, data->tight); + gh_buf_puts(html, data->list_type == bullet ? "
    " : ""); + cr(html); + break; + + case atx_header: + case setext_header: + cr(html); + gh_buf_printf(html, "", b->attributes.header_level); + inlines_to_html(html, b->inline_content); + gh_buf_printf(html, "", b->attributes.header_level); + cr(html); + break; + + case indented_code: + case fenced_code: + /* TODO: fenced code lang attributes */ + cr(html); + gh_buf_puts(html, "
    ");
    +				escape_html(html, b->string_content.ptr, b->string_content.size);
    +				gh_buf_puts(html, "
    "); + cr(html); + break; + + case html_block: + gh_buf_put(html, b->string_content.ptr, b->string_content.size); + break; + + case hrule: + gh_buf_puts(html, "
    "); + cr(html); + break; + + case reference_def: + break; + + default: + assert(false); + } + + b = b->next; + } +} + +// Convert an inline list to HTML. Returns 0 on success, and sets result. +void inlines_to_html(gh_buf *html, inl* ils) +{ + gh_buf scrap = GH_BUF_INIT; + + while(ils != NULL) { + switch(ils->tag) { + case INL_STRING: + escape_html(html, ils->content.literal.data, ils->content.literal.len); + break; + + case INL_LINEBREAK: + gh_buf_puts(html, "
    \n"); + break; + + case INL_SOFTBREAK: + gh_buf_putc(html, '\n'); + break; + + case INL_CODE: + gh_buf_puts(html, ""); + escape_html(html, ils->content.literal.data, ils->content.literal.len); + gh_buf_puts(html, ""); + break; + + case INL_RAW_HTML: + case INL_ENTITY: + gh_buf_put(html, + ils->content.literal.data, + ils->content.literal.len); + break; + + case INL_LINK: + gh_buf_puts(html, "content.linkable.url, -1); + + if (ils->content.linkable.title) { + gh_buf_puts(html, "\" title=\""); + escape_html(html, ils->content.linkable.title, -1); + } + + gh_buf_puts(html, "\">"); + inlines_to_html(html, ils->content.inlines); + gh_buf_puts(html, ""); + break; + + case INL_IMAGE: + gh_buf_puts(html, "content.linkable.url, -1); + + inlines_to_html(&scrap, ils->content.inlines); + if (scrap.size) { + gh_buf_puts(html, "\" alt=\""); + escape_html(html, scrap.ptr, scrap.size); + } + gh_buf_clear(&scrap); + + if (ils->content.linkable.title) { + gh_buf_puts(html, "\" title=\""); + escape_html(html, ils->content.linkable.title, -1); + } + + gh_buf_puts(html, "\"/>"); + break; + + case INL_STRONG: + gh_buf_puts(html, ""); + inlines_to_html(html, ils->content.inlines); + gh_buf_puts(html, ""); + break; + + case INL_EMPH: + gh_buf_puts(html, ""); + inlines_to_html(html, ils->content.inlines); + gh_buf_puts(html, ""); + break; + } + ils = ils->next; + } +} -- cgit v1.2.3 From a7314deae649646f1f7ce5ede972641b5b62538c Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 3 Sep 2014 03:40:23 +0200 Subject: 338/103 --- src/html/html.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 2f160ca..27ffe58 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -68,7 +68,7 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) cr(html); gh_buf_puts(html, "
  • "); blocks_to_html(html, b->children, tight); - gh_buf_trim(html); + gh_buf_trim(html); /* TODO: rtrim */ gh_buf_puts(html, "
  • "); cr(html); break; @@ -106,7 +106,7 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) cr(html); gh_buf_puts(html, "
    ");
     				escape_html(html, b->string_content.ptr, b->string_content.size);
    -				gh_buf_puts(html, "
    "); + gh_buf_puts(html, ""); cr(html); break; -- cgit v1.2.3 From bb6d7c4a394e61574f5f32db60da5c5f5a5e5002 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Wed, 3 Sep 2014 03:54:11 +0200 Subject: 342/99 --- src/html/html.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 27ffe58..3bd5df0 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -102,9 +102,26 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) case indented_code: case fenced_code: - /* TODO: fenced code lang attributes */ cr(html); - gh_buf_puts(html, "
    ");
    +
    +				gh_buf_puts(html, "tag == fenced_code) {
    +					gh_buf *info = &b->attributes.fenced_code_data.info;
    +
    +					if (gh_buf_len(info) > 0) {
    +						int first_tag = gh_buf_strchr(info, ' ', 0);
    +						if (first_tag < 0)
    +							first_tag = gh_buf_len(info);
    +
    +
    +						gh_buf_puts(html, " class=\"");
    +						escape_html(html, info->ptr, first_tag);
    +						gh_buf_putc(html, '"');
    +					}
    +				}
    +
    +				gh_buf_puts(html, ">");
     				escape_html(html, b->string_content.ptr, b->string_content.size);
     				gh_buf_puts(html, "
    "); cr(html); -- cgit v1.2.3 From 28be4a59c940bd55ed4fef668091d52638925c3c Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 15:55:27 +0200 Subject: 379/62 --- src/html/html.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 3bd5df0..2a65a63 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -6,7 +6,6 @@ #include "stmd.h" #include "debug.h" -#include "scanners.h" #include "html/houdini.h" // Functions to convert block and inline lists to HTML strings. -- cgit v1.2.3 From 45c1d9fadb3e8aab4a01bb27a4e2ece379902d1a Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 17:26:11 +0200 Subject: 426/15 --- src/html/html.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 2a65a63..cdccf2a 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -50,17 +50,15 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) cr(html); gh_buf_puts(html, "

    "); inlines_to_html(html, b->inline_content); - gh_buf_puts(html, "

    "); - cr(html); + gh_buf_puts(html, "

    \n"); } break; case block_quote: cr(html); - gh_buf_puts(html, "
    "); + gh_buf_puts(html, "
    \n"); blocks_to_html(html, b->children, false); - gh_buf_puts(html, "
    "); - cr(html); + gh_buf_puts(html, "
    \n"); break; case list_item: @@ -68,8 +66,7 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) gh_buf_puts(html, "
  • "); blocks_to_html(html, b->children, tight); gh_buf_trim(html); /* TODO: rtrim */ - gh_buf_puts(html, "
  • "); - cr(html); + gh_buf_puts(html, "\n"); break; case list: @@ -87,7 +84,7 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) blocks_to_html(html, b->children, data->tight); gh_buf_puts(html, data->list_type == bullet ? "" : ""); - cr(html); + gh_buf_putc(html, '\n'); break; case atx_header: @@ -95,8 +92,7 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) cr(html); gh_buf_printf(html, "", b->attributes.header_level); inlines_to_html(html, b->inline_content); - gh_buf_printf(html, "", b->attributes.header_level); - cr(html); + gh_buf_printf(html, "\n", b->attributes.header_level); break; case indented_code: @@ -122,8 +118,7 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) gh_buf_puts(html, ">"); escape_html(html, b->string_content.ptr, b->string_content.size); - gh_buf_puts(html, ""); - cr(html); + gh_buf_puts(html, "
    \n"); break; case html_block: @@ -131,8 +126,7 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) break; case hrule: - gh_buf_puts(html, "
    "); - cr(html); + gh_buf_puts(html, "
    \n"); break; case reference_def: -- cgit v1.2.3 From 9830d3a05a374a0d05676301bd4065917b59ad53 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 17:42:12 +0200 Subject: 430/11 --- src/html/html.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index cdccf2a..913a602 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -191,10 +191,9 @@ void inlines_to_html(gh_buf *html, inl* ils) escape_href(html, ils->content.linkable.url, -1); inlines_to_html(&scrap, ils->content.inlines); - if (scrap.size) { - gh_buf_puts(html, "\" alt=\""); + gh_buf_puts(html, "\" alt=\""); + if (scrap.size) escape_html(html, scrap.ptr, scrap.size); - } gh_buf_clear(&scrap); if (ils->content.linkable.title) { -- cgit v1.2.3 From d8f44f1e4f0bd944ab43e6434a1579d670ed66cf Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 17:49:13 +0200 Subject: 433/8 --- src/html/html.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 913a602..41b8fda 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -174,7 +174,8 @@ void inlines_to_html(gh_buf *html, inl* ils) case INL_LINK: gh_buf_puts(html, "content.linkable.url, -1); + if (ils->content.linkable.url) + escape_href(html, ils->content.linkable.url, -1); if (ils->content.linkable.title) { gh_buf_puts(html, "\" title=\""); @@ -188,7 +189,8 @@ void inlines_to_html(gh_buf *html, inl* ils) case INL_IMAGE: gh_buf_puts(html, "content.linkable.url, -1); + if (ils->content.linkable.url) + escape_href(html, ils->content.linkable.url, -1); inlines_to_html(&scrap, ils->content.inlines); gh_buf_puts(html, "\" alt=\""); -- cgit v1.2.3 From 543c2c94d71adee42c7bd2f8027d75c87ed8120d Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 18:38:14 +0200 Subject: Rename to strbuf --- src/html/html.c | 98 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 41b8fda..a9356dd 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -10,7 +10,7 @@ // Functions to convert block and inline lists to HTML strings. -static void escape_html(gh_buf *dest, const unsigned char *source, int length) +static void escape_html(strbuf *dest, const unsigned char *source, int length) { if (length < 0) length = strlen((char *)source); @@ -18,7 +18,7 @@ static void escape_html(gh_buf *dest, const unsigned char *source, int length) houdini_escape_html0(dest, source, (size_t)length, 0); } -static void escape_href(gh_buf *dest, const unsigned char *source, int length) +static void escape_href(strbuf *dest, const unsigned char *source, int length) { if (length < 0) length = strlen((char *)source); @@ -26,14 +26,14 @@ static void escape_href(gh_buf *dest, const unsigned char *source, int length) houdini_escape_href(dest, source, (size_t)length); } -static inline void cr(gh_buf *html) +static inline void cr(strbuf *html) { if (html->size && html->ptr[html->size - 1] != '\n') - gh_buf_putc(html, '\n'); + strbuf_putc(html, '\n'); } // Convert a block list to HTML. Returns 0 on success, and sets result. -void blocks_to_html(gh_buf *html, block *b, bool tight) +void blocks_to_html(strbuf *html, block *b, bool tight) { struct ListData *data; @@ -48,25 +48,25 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) inlines_to_html(html, b->inline_content); } else { cr(html); - gh_buf_puts(html, "

    "); + strbuf_puts(html, "

    "); inlines_to_html(html, b->inline_content); - gh_buf_puts(html, "

    \n"); + strbuf_puts(html, "

    \n"); } break; case block_quote: cr(html); - gh_buf_puts(html, "
    \n"); + strbuf_puts(html, "
    \n"); blocks_to_html(html, b->children, false); - gh_buf_puts(html, "
    \n"); + strbuf_puts(html, "
    \n"); break; case list_item: cr(html); - gh_buf_puts(html, "
  • "); + strbuf_puts(html, "
  • "); blocks_to_html(html, b->children, tight); - gh_buf_trim(html); /* TODO: rtrim */ - gh_buf_puts(html, "
  • \n"); + strbuf_trim(html); /* TODO: rtrim */ + strbuf_puts(html, "\n"); break; case list: @@ -75,58 +75,58 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) data = &(b->attributes.list_data); if (data->start > 1) { - gh_buf_printf(html, "<%s start=\"%d\">\n", + strbuf_printf(html, "<%s start=\"%d\">\n", data->list_type == bullet ? "ul" : "ol", data->start); } else { - gh_buf_puts(html, data->list_type == bullet ? "
      \n" : "
        \n"); + strbuf_puts(html, data->list_type == bullet ? "
          \n" : "
            \n"); } blocks_to_html(html, b->children, data->tight); - gh_buf_puts(html, data->list_type == bullet ? "
        " : "
      "); - gh_buf_putc(html, '\n'); + strbuf_puts(html, data->list_type == bullet ? "
    " : ""); + strbuf_putc(html, '\n'); break; case atx_header: case setext_header: cr(html); - gh_buf_printf(html, "", b->attributes.header_level); + strbuf_printf(html, "", b->attributes.header_level); inlines_to_html(html, b->inline_content); - gh_buf_printf(html, "\n", b->attributes.header_level); + strbuf_printf(html, "\n", b->attributes.header_level); break; case indented_code: case fenced_code: cr(html); - gh_buf_puts(html, "tag == fenced_code) { - gh_buf *info = &b->attributes.fenced_code_data.info; + strbuf *info = &b->attributes.fenced_code_data.info; - if (gh_buf_len(info) > 0) { - int first_tag = gh_buf_strchr(info, ' ', 0); + if (strbuf_len(info) > 0) { + int first_tag = strbuf_strchr(info, ' ', 0); if (first_tag < 0) - first_tag = gh_buf_len(info); + first_tag = strbuf_len(info); - gh_buf_puts(html, " class=\""); + strbuf_puts(html, " class=\""); escape_html(html, info->ptr, first_tag); - gh_buf_putc(html, '"'); + strbuf_putc(html, '"'); } } - gh_buf_puts(html, ">"); + strbuf_puts(html, ">"); escape_html(html, b->string_content.ptr, b->string_content.size); - gh_buf_puts(html, "\n"); + strbuf_puts(html, "\n"); break; case html_block: - gh_buf_put(html, b->string_content.ptr, b->string_content.size); + strbuf_put(html, b->string_content.ptr, b->string_content.size); break; case hrule: - gh_buf_puts(html, "
    \n"); + strbuf_puts(html, "
    \n"); break; case reference_def: @@ -141,9 +141,9 @@ void blocks_to_html(gh_buf *html, block *b, bool tight) } // Convert an inline list to HTML. Returns 0 on success, and sets result. -void inlines_to_html(gh_buf *html, inl* ils) +void inlines_to_html(strbuf *html, inl* ils) { - gh_buf scrap = GH_BUF_INIT; + strbuf scrap = GH_BUF_INIT; while(ils != NULL) { switch(ils->tag) { @@ -152,70 +152,70 @@ void inlines_to_html(gh_buf *html, inl* ils) break; case INL_LINEBREAK: - gh_buf_puts(html, "
    \n"); + strbuf_puts(html, "
    \n"); break; case INL_SOFTBREAK: - gh_buf_putc(html, '\n'); + strbuf_putc(html, '\n'); break; case INL_CODE: - gh_buf_puts(html, ""); + strbuf_puts(html, ""); escape_html(html, ils->content.literal.data, ils->content.literal.len); - gh_buf_puts(html, ""); + strbuf_puts(html, ""); break; case INL_RAW_HTML: case INL_ENTITY: - gh_buf_put(html, + strbuf_put(html, ils->content.literal.data, ils->content.literal.len); break; case INL_LINK: - gh_buf_puts(html, "
    content.linkable.url) escape_href(html, ils->content.linkable.url, -1); if (ils->content.linkable.title) { - gh_buf_puts(html, "\" title=\""); + strbuf_puts(html, "\" title=\""); escape_html(html, ils->content.linkable.title, -1); } - gh_buf_puts(html, "\">"); + strbuf_puts(html, "\">"); inlines_to_html(html, ils->content.inlines); - gh_buf_puts(html, ""); + strbuf_puts(html, ""); break; case INL_IMAGE: - gh_buf_puts(html, "content.linkable.url) escape_href(html, ils->content.linkable.url, -1); inlines_to_html(&scrap, ils->content.inlines); - gh_buf_puts(html, "\" alt=\""); + strbuf_puts(html, "\" alt=\""); if (scrap.size) escape_html(html, scrap.ptr, scrap.size); - gh_buf_clear(&scrap); + strbuf_clear(&scrap); if (ils->content.linkable.title) { - gh_buf_puts(html, "\" title=\""); + strbuf_puts(html, "\" title=\""); escape_html(html, ils->content.linkable.title, -1); } - gh_buf_puts(html, "\"/>"); + strbuf_puts(html, "\"/>"); break; case INL_STRONG: - gh_buf_puts(html, ""); + strbuf_puts(html, ""); inlines_to_html(html, ils->content.inlines); - gh_buf_puts(html, ""); + strbuf_puts(html, ""); break; case INL_EMPH: - gh_buf_puts(html, ""); + strbuf_puts(html, ""); inlines_to_html(html, ils->content.inlines); - gh_buf_puts(html, ""); + strbuf_puts(html, ""); break; } ils = ils->next; -- cgit v1.2.3 From 647b15968c95ec268d6d728eea73756c7ba648a8 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 18:42:49 +0200 Subject: Rename inl --- src/html/html.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index a9356dd..53521b8 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -141,7 +141,7 @@ void blocks_to_html(strbuf *html, block *b, bool tight) } // Convert an inline list to HTML. Returns 0 on success, and sets result. -void inlines_to_html(strbuf *html, inl* ils) +void inlines_to_html(strbuf *html, struct inl* ils) { strbuf scrap = GH_BUF_INIT; -- cgit v1.2.3 From 9e4855365b920c2a80b0f1ab6937280f0b504334 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 18:45:44 +0200 Subject: Rename `inl` --- src/html/html.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 53521b8..a7bb21a 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -141,7 +141,7 @@ void blocks_to_html(strbuf *html, block *b, bool tight) } // Convert an inline list to HTML. Returns 0 on success, and sets result. -void inlines_to_html(strbuf *html, struct inl* ils) +void inlines_to_html(strbuf *html, node_inl* ils) { strbuf scrap = GH_BUF_INIT; -- cgit v1.2.3 From 19ba82d7a30bd999a25fc303a8516056880abc9d Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 18:49:33 +0200 Subject: Rename node_block --- src/html/html.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index a7bb21a..6041fde 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -8,7 +8,7 @@ #include "debug.h" #include "html/houdini.h" -// Functions to convert block and inline lists to HTML strings. +// Functions to convert node_block and inline lists to HTML strings. static void escape_html(strbuf *dest, const unsigned char *source, int length) { @@ -32,8 +32,8 @@ static inline void cr(strbuf *html) strbuf_putc(html, '\n'); } -// Convert a block list to HTML. Returns 0 on success, and sets result. -void blocks_to_html(strbuf *html, block *b, bool tight) +// Convert a node_block list to HTML. Returns 0 on success, and sets result. +void blocks_to_html(strbuf *html, node_block *b, bool tight) { struct ListData *data; -- cgit v1.2.3 From 806ff17755c90579afc68914b251b80e2f8c4b77 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Thu, 4 Sep 2014 18:56:52 +0200 Subject: Rename block literals --- src/html/html.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 6041fde..758ec80 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -39,11 +39,11 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) while(b != NULL) { switch(b->tag) { - case document: + case BLOCK_DOCUMENT: blocks_to_html(html, b->children, false); break; - case paragraph: + case BLOCK_PARAGRAPH: if (tight) { inlines_to_html(html, b->inline_content); } else { @@ -54,14 +54,14 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) } break; - case block_quote: + case BLOCK_BQUOTE: cr(html); strbuf_puts(html, "
    \n"); blocks_to_html(html, b->children, false); strbuf_puts(html, "
    \n"); break; - case list_item: + case BLOCK_LIST_ITEM: cr(html); strbuf_puts(html, "
  • "); blocks_to_html(html, b->children, tight); @@ -69,7 +69,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) strbuf_puts(html, "
  • \n"); break; - case list: + case BLOCK_LIST: // make sure a list starts at the beginning of the line: cr(html); data = &(b->attributes.list_data); @@ -87,21 +87,21 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) strbuf_putc(html, '\n'); break; - case atx_header: - case setext_header: + case BLOCK_ATX_HEADER: + case BLOCK_SETEXT_HEADER: cr(html); strbuf_printf(html, "", b->attributes.header_level); inlines_to_html(html, b->inline_content); strbuf_printf(html, "\n", b->attributes.header_level); break; - case indented_code: - case fenced_code: + case BLOCK_INDENTED_CODE: + case BLOCK_FENCED_CODE: cr(html); strbuf_puts(html, "tag == fenced_code) { + if (b->tag == BLOCK_FENCED_CODE) { strbuf *info = &b->attributes.fenced_code_data.info; if (strbuf_len(info) > 0) { @@ -121,15 +121,15 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) strbuf_puts(html, "\n"); break; - case html_block: + case BLOCK_HTML: strbuf_put(html, b->string_content.ptr, b->string_content.size); break; - case hrule: + case BLOCK_HRULE: strbuf_puts(html, "
    \n"); break; - case reference_def: + case BLOCK_REFERENCE_DEF: break; default: -- cgit v1.2.3 From 61e3e606e64221eaa5cf3d83dc598d5a42818d10 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Sat, 6 Sep 2014 20:48:05 +0200 Subject: UTF8-aware detabbing and entity handling --- src/html/html.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 758ec80..595dfcd 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -166,7 +166,6 @@ void inlines_to_html(strbuf *html, node_inl* ils) break; case INL_RAW_HTML: - case INL_ENTITY: strbuf_put(html, ils->content.literal.data, ils->content.literal.len); -- cgit v1.2.3 From 7426f9ae60272a19bd4611b8579647118033a1e6 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Sun, 7 Sep 2014 22:48:33 +0200 Subject: Abstract the Block union --- src/html/html.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 595dfcd..129335f 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -72,7 +72,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) case BLOCK_LIST: // make sure a list starts at the beginning of the line: cr(html); - data = &(b->attributes.list_data); + data = &(b->as.list); if (data->start > 1) { strbuf_printf(html, "<%s start=\"%d\">\n", @@ -90,9 +90,9 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) case BLOCK_ATX_HEADER: case BLOCK_SETEXT_HEADER: cr(html); - strbuf_printf(html, "", b->attributes.header_level); + strbuf_printf(html, "", b->as.header.level); inlines_to_html(html, b->inline_content); - strbuf_printf(html, "\n", b->attributes.header_level); + strbuf_printf(html, "\n", b->as.header.level); break; case BLOCK_INDENTED_CODE: @@ -102,7 +102,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) strbuf_puts(html, "tag == BLOCK_FENCED_CODE) { - strbuf *info = &b->attributes.fenced_code_data.info; + strbuf *info = &b->as.code.info; if (strbuf_len(info) > 0) { int first_tag = strbuf_strchr(info, ' ', 0); -- cgit v1.2.3 From 2c06fa95fd3059a099bbe403beaf62f2e033f5b7 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 9 Sep 2014 03:42:05 +0200 Subject: Fix the class attribute for code fences --- src/html/html.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 129335f..74f6791 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -99,7 +99,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) case BLOCK_FENCED_CODE: cr(html); - strbuf_puts(html, "tag == BLOCK_FENCED_CODE) { strbuf *info = &b->as.code.info; @@ -109,14 +109,13 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) if (first_tag < 0) first_tag = strbuf_len(info); - - strbuf_puts(html, " class=\""); + strbuf_puts(html, " class=\"lang-"); escape_html(html, info->ptr, first_tag); strbuf_putc(html, '"'); } } - strbuf_puts(html, ">"); + strbuf_putc(html, '>'); escape_html(html, b->string_content.ptr, b->string_content.size); strbuf_puts(html, "\n"); break; -- cgit v1.2.3 From d21ef7b5db11075e038e60732682dfd8a5cf6a13 Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 9 Sep 2014 03:42:46 +0200 Subject: Oops --- src/html/html.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 74f6791..b48b10b 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -109,7 +109,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) if (first_tag < 0) first_tag = strbuf_len(info); - strbuf_puts(html, " class=\"lang-"); + strbuf_puts(html, " class=\"language-"); escape_html(html, info->ptr, first_tag); strbuf_putc(html, '"'); } -- cgit v1.2.3 From 118e3d3c39242225baa876319cdbfbb1adadc77b Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Mon, 15 Sep 2014 15:28:49 +0200 Subject: Cleanup external APIs --- src/html/html.c | 163 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 84 insertions(+), 79 deletions(-) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index b48b10b..6f3bc76 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -32,8 +32,89 @@ static inline void cr(strbuf *html) strbuf_putc(html, '\n'); } +// Convert an inline list to HTML. Returns 0 on success, and sets result. +static void inlines_to_html(strbuf *html, node_inl* ils) +{ + strbuf scrap = GH_BUF_INIT; + + while(ils != NULL) { + switch(ils->tag) { + case INL_STRING: + escape_html(html, ils->content.literal.data, ils->content.literal.len); + break; + + case INL_LINEBREAK: + strbuf_puts(html, "
    \n"); + break; + + case INL_SOFTBREAK: + strbuf_putc(html, '\n'); + break; + + case INL_CODE: + strbuf_puts(html, ""); + escape_html(html, ils->content.literal.data, ils->content.literal.len); + strbuf_puts(html, ""); + break; + + case INL_RAW_HTML: + strbuf_put(html, + ils->content.literal.data, + ils->content.literal.len); + break; + + case INL_LINK: + strbuf_puts(html, "content.linkable.url) + escape_href(html, ils->content.linkable.url, -1); + + if (ils->content.linkable.title) { + strbuf_puts(html, "\" title=\""); + escape_html(html, ils->content.linkable.title, -1); + } + + strbuf_puts(html, "\">"); + inlines_to_html(html, ils->content.inlines); + strbuf_puts(html, ""); + break; + + case INL_IMAGE: + strbuf_puts(html, "content.linkable.url) + escape_href(html, ils->content.linkable.url, -1); + + inlines_to_html(&scrap, ils->content.inlines); + strbuf_puts(html, "\" alt=\""); + if (scrap.size) + escape_html(html, scrap.ptr, scrap.size); + strbuf_clear(&scrap); + + if (ils->content.linkable.title) { + strbuf_puts(html, "\" title=\""); + escape_html(html, ils->content.linkable.title, -1); + } + + strbuf_puts(html, "\"/>"); + break; + + case INL_STRONG: + strbuf_puts(html, ""); + inlines_to_html(html, ils->content.inlines); + strbuf_puts(html, ""); + break; + + case INL_EMPH: + strbuf_puts(html, ""); + inlines_to_html(html, ils->content.inlines); + strbuf_puts(html, ""); + break; + } + ils = ils->next; + } +} + // Convert a node_block list to HTML. Returns 0 on success, and sets result. -void blocks_to_html(strbuf *html, node_block *b, bool tight) +static void blocks_to_html(strbuf *html, node_block *b, bool tight) { struct ListData *data; @@ -139,83 +220,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight) } } -// Convert an inline list to HTML. Returns 0 on success, and sets result. -void inlines_to_html(strbuf *html, node_inl* ils) +void stmd_render_html(strbuf *html, node_block *root) { - strbuf scrap = GH_BUF_INIT; - - while(ils != NULL) { - switch(ils->tag) { - case INL_STRING: - escape_html(html, ils->content.literal.data, ils->content.literal.len); - break; - - case INL_LINEBREAK: - strbuf_puts(html, "
    \n"); - break; - - case INL_SOFTBREAK: - strbuf_putc(html, '\n'); - break; - - case INL_CODE: - strbuf_puts(html, ""); - escape_html(html, ils->content.literal.data, ils->content.literal.len); - strbuf_puts(html, ""); - break; - - case INL_RAW_HTML: - strbuf_put(html, - ils->content.literal.data, - ils->content.literal.len); - break; - - case INL_LINK: - strbuf_puts(html, "content.linkable.url) - escape_href(html, ils->content.linkable.url, -1); - - if (ils->content.linkable.title) { - strbuf_puts(html, "\" title=\""); - escape_html(html, ils->content.linkable.title, -1); - } - - strbuf_puts(html, "\">"); - inlines_to_html(html, ils->content.inlines); - strbuf_puts(html, ""); - break; - - case INL_IMAGE: - strbuf_puts(html, "content.linkable.url) - escape_href(html, ils->content.linkable.url, -1); - - inlines_to_html(&scrap, ils->content.inlines); - strbuf_puts(html, "\" alt=\""); - if (scrap.size) - escape_html(html, scrap.ptr, scrap.size); - strbuf_clear(&scrap); - - if (ils->content.linkable.title) { - strbuf_puts(html, "\" title=\""); - escape_html(html, ils->content.linkable.title, -1); - } - - strbuf_puts(html, "\"/>"); - break; - - case INL_STRONG: - strbuf_puts(html, ""); - inlines_to_html(html, ils->content.inlines); - strbuf_puts(html, ""); - break; - - case INL_EMPH: - strbuf_puts(html, ""); - inlines_to_html(html, ils->content.inlines); - strbuf_puts(html, ""); - break; - } - ils = ils->next; - } + blocks_to_html(html, root, false); } -- cgit v1.2.3 From 507d8d3a09f6704e8c1f21e5a5df2e4e014e6779 Mon Sep 17 00:00:00 2001 From: Jordan Milne Date: Thu, 18 Sep 2014 09:26:05 -0300 Subject: Fix memory leak when rendering images as HTML --- src/html/html.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/html/html.c') diff --git a/src/html/html.c b/src/html/html.c index 6f3bc76..ab6fc35 100644 --- a/src/html/html.c +++ b/src/html/html.c @@ -111,6 +111,8 @@ static void inlines_to_html(strbuf *html, node_inl* ils) } ils = ils->next; } + + strbuf_free(&scrap); } // Convert a node_block list to HTML. Returns 0 on success, and sets result. -- cgit v1.2.3