Skip to content

Commit

Permalink
Backport Upskirt changes: HTML by default
Browse files Browse the repository at this point in the history
Enable XHTML rendering again with the `:xhtml` flag.
  • Loading branch information
vmg committed May 6, 2011
1 parent 913561f commit ecf6c85
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 77 deletions.
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ task :gather => 'upskirt/src/markdown.h' do |t|
FileList[
'upskirt/src/{markdown,buffer,array}.h',
'upskirt/src/{markdown,buffer,array}.c',
'upskirt/render/xhtml.c',
'upskirt/render/xhtml_smartypants.c',
'upskirt/render/xhtml.h',
'upskirt/render/html.c',
'upskirt/render/html_smartypants.c',
'upskirt/render/html.h',
]
cp files, 'ext/redcarpet/',
:preserve => true,
Expand Down
84 changes: 46 additions & 38 deletions ext/redcarpet/xhtml.c → ext/redcarpet/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@
*/

#include "markdown.h"
#include "xhtml.h"
#include "html.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

struct xhtml_renderopt {
struct html_renderopt {
struct {
int header_count;
int current_level;
} toc_data;

unsigned int flags;
const char *close_tag;
};

static inline void
Expand Down Expand Up @@ -104,12 +105,12 @@ is_html_tag(struct buf *tag, const char *tagname)
static int
rndr_autolink(struct buf *ob, struct buf *link, enum mkd_autolink type, void *opaque)
{
struct xhtml_renderopt *options = opaque;
struct html_renderopt *options = opaque;

if (!link || !link->size)
return 0;

if ((options->flags & XHTML_SAFELINK) != 0 &&
if ((options->flags & HTML_SAFELINK) != 0 &&
!is_safe_link(link->data, link->size) &&
type != MKDA_EMAIL)
return 0;
Expand Down Expand Up @@ -272,12 +273,12 @@ rndr_emphasis(struct buf *ob, struct buf *text, void *opaque)
static void
rndr_header(struct buf *ob, struct buf *text, int level, void *opaque)
{
struct xhtml_renderopt *options = opaque;
struct html_renderopt *options = opaque;

if (ob->size)
bufputc(ob, '\n');

if (options->flags & XHTML_TOC) {
if (options->flags & HTML_TOC) {
bufprintf(ob, "<a name=\"toc_%d\"></a>", options->toc_data.header_count++);
}

Expand All @@ -289,9 +290,9 @@ rndr_header(struct buf *ob, struct buf *text, int level, void *opaque)
static int
rndr_link(struct buf *ob, struct buf *link, struct buf *title, struct buf *content, void *opaque)
{
struct xhtml_renderopt *options = opaque;
struct html_renderopt *options = opaque;

if ((options->flags & XHTML_SAFELINK) != 0 && !is_safe_link(link->data, link->size))
if ((options->flags & HTML_SAFELINK) != 0 && !is_safe_link(link->data, link->size))
return 0;

BUFPUTSL(ob, "<a href=\"");
Expand Down Expand Up @@ -328,7 +329,7 @@ rndr_listitem(struct buf *ob, struct buf *text, int flags, void *opaque)
static void
rndr_paragraph(struct buf *ob, struct buf *text, void *opaque)
{
struct xhtml_renderopt *options = opaque;
struct html_renderopt *options = opaque;
size_t i = 0;

if (ob->size) bufputc(ob, '\n');
Expand All @@ -342,7 +343,7 @@ rndr_paragraph(struct buf *ob, struct buf *text, void *opaque)
return;

BUFPUTSL(ob, "<p>");
if (options->flags & XHTML_HARD_WRAP) {
if (options->flags & HTML_HARD_WRAP) {
size_t org;
while (i < text->size) {
org = i;
Expand All @@ -355,7 +356,8 @@ rndr_paragraph(struct buf *ob, struct buf *text, void *opaque)
if (i >= text->size)
break;

BUFPUTSL(ob, "<br/>\n");
BUFPUTSL(ob, "<br");
bufputs(ob, options->close_tag);
i++;
}
} else {
Expand Down Expand Up @@ -389,21 +391,19 @@ rndr_triple_emphasis(struct buf *ob, struct buf *text, void *opaque)
return 1;
}


/**********************
* XHTML 1.0 RENDERER *
**********************/

static void
rndr_hrule(struct buf *ob, void *opaque)
{
struct html_renderopt *options = opaque;
if (ob->size) bufputc(ob, '\n');
BUFPUTSL(ob, "<hr />\n");
BUFPUTSL(ob, "<hr");
bufputs(ob, options->close_tag);
}

static int
rndr_image(struct buf *ob, struct buf *link, struct buf *title, struct buf *alt, void *opaque)
{
struct html_renderopt *options = opaque;
if (!link || !link->size) return 0;
BUFPUTSL(ob, "<img src=\"");
attr_escape(ob, link->data, link->size);
Expand All @@ -413,32 +413,36 @@ rndr_image(struct buf *ob, struct buf *link, struct buf *title, struct buf *alt,
if (title && title->size) {
BUFPUTSL(ob, "\" title=\"");
attr_escape(ob, title->data, title->size); }
BUFPUTSL(ob, "\" />");

bufputc(ob, '"');
bufputs(ob, options->close_tag);
return 1;
}

static int
rndr_linebreak(struct buf *ob, void *opaque)
{
BUFPUTSL(ob, "<br />\n");
struct html_renderopt *options = opaque;
BUFPUTSL(ob, "<br");
bufputs(ob, options->close_tag);
return 1;
}

static int
rndr_raw_html(struct buf *ob, struct buf *text, void *opaque)
{
struct xhtml_renderopt *options = opaque;
struct html_renderopt *options = opaque;

if ((options->flags & XHTML_SKIP_HTML) != 0)
if ((options->flags & HTML_SKIP_HTML) != 0)
return 1;

if ((options->flags & XHTML_SKIP_STYLE) != 0 && is_html_tag(text, "style"))
if ((options->flags & HTML_SKIP_STYLE) != 0 && is_html_tag(text, "style"))
return 1;

if ((options->flags & XHTML_SKIP_LINKS) != 0 && is_html_tag(text, "a"))
if ((options->flags & HTML_SKIP_LINKS) != 0 && is_html_tag(text, "a"))
return 1;

if ((options->flags & XHTML_SKIP_IMAGES) != 0 && is_html_tag(text, "img"))
if ((options->flags & HTML_SKIP_IMAGES) != 0 && is_html_tag(text, "img"))
return 1;

bufput(ob, text->data, text->size);
Expand Down Expand Up @@ -505,7 +509,7 @@ rndr_normal_text(struct buf *ob, struct buf *text, void *opaque)
static void
toc_header(struct buf *ob, struct buf *text, int level, void *opaque)
{
struct xhtml_renderopt *options = opaque;
struct html_renderopt *options = opaque;

if (level > options->toc_data.current_level) {
if (level > 1)
Expand All @@ -530,7 +534,7 @@ toc_header(struct buf *ob, struct buf *text, int level, void *opaque)
static void
toc_finalize(struct buf *ob, void *opaque)
{
struct xhtml_renderopt *options = opaque;
struct html_renderopt *options = opaque;

while (options->toc_data.current_level > 1) {
BUFPUTSL(ob, "</ul></li>\n");
Expand All @@ -542,7 +546,7 @@ toc_finalize(struct buf *ob, void *opaque)
}

void
ups_toc_renderer(struct mkd_renderer *renderer)
upshtml_toc_renderer(struct mkd_renderer *renderer)
{
static const struct mkd_renderer toc_render = {
NULL,
Expand Down Expand Up @@ -577,17 +581,20 @@ ups_toc_renderer(struct mkd_renderer *renderer)
NULL
};

struct xhtml_renderopt *options;
options = calloc(1, sizeof(struct xhtml_renderopt));
options->flags = XHTML_TOC;
struct html_renderopt *options;
options = calloc(1, sizeof(struct html_renderopt));
options->flags = HTML_TOC;

memcpy(renderer, &toc_render, sizeof(struct mkd_renderer));
renderer->opaque = options;
}

void
ups_xhtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags)
upshtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags)
{
static const char *xhtml_close = "/>\n";
static const char *html_close = ">\n";

static const struct mkd_renderer renderer_default = {
rndr_blockcode,
rndr_blockquote,
Expand Down Expand Up @@ -621,30 +628,31 @@ ups_xhtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags)
NULL
};

struct xhtml_renderopt *options;
options = calloc(1, sizeof(struct xhtml_renderopt));
struct html_renderopt *options;
options = calloc(1, sizeof(struct html_renderopt));
options->flags = render_flags;
options->close_tag = (render_flags & HTML_USE_XHTML) ? xhtml_close : html_close;

memcpy(renderer, &renderer_default, sizeof(struct mkd_renderer));
renderer->opaque = options;

if (render_flags & XHTML_SKIP_IMAGES)
if (render_flags & HTML_SKIP_IMAGES)
renderer->image = NULL;

if (render_flags & XHTML_SKIP_LINKS) {
if (render_flags & HTML_SKIP_LINKS) {
renderer->link = NULL;
renderer->autolink = NULL;
}

if (render_flags & XHTML_SKIP_HTML)
if (render_flags & HTML_SKIP_HTML)
renderer->blockhtml = NULL;

if (render_flags & XHTML_GITHUB_BLOCKCODE)
if (render_flags & HTML_GITHUB_BLOCKCODE)
renderer->blockcode = rndr_blockcode_github;
}

void
ups_free_renderer(struct mkd_renderer *renderer)
upshtml_free_renderer(struct mkd_renderer *renderer)
{
free(renderer->opaque);
}
Expand Down
31 changes: 16 additions & 15 deletions ext/redcarpet/xhtml.h → ext/redcarpet/html.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,35 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef UPSKIRT_XHTML_H
#define UPSKIRT_XHTML_H
#ifndef UPSKIRT_HTML_H
#define UPSKIRT_HTML_H

#include "markdown.h"

typedef enum {
XHTML_SKIP_HTML = (1 << 0),
XHTML_SKIP_STYLE = (1 << 1),
XHTML_SKIP_IMAGES = (1 << 2),
XHTML_SKIP_LINKS = (1 << 3),
XHTML_EXPAND_TABS = (1 << 5),
XHTML_SAFELINK = (1 << 7),
XHTML_TOC = (1 << 8),
XHTML_HARD_WRAP = (1 << 9),
XHTML_GITHUB_BLOCKCODE = (1 << 10),
HTML_SKIP_HTML = (1 << 0),
HTML_SKIP_STYLE = (1 << 1),
HTML_SKIP_IMAGES = (1 << 2),
HTML_SKIP_LINKS = (1 << 3),
HTML_EXPAND_TABS = (1 << 5),
HTML_SAFELINK = (1 << 7),
HTML_TOC = (1 << 8),
HTML_HARD_WRAP = (1 << 9),
HTML_GITHUB_BLOCKCODE = (1 << 10),
HTML_USE_XHTML = (1 << 11),
} render_mode;

extern void
ups_xhtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags);
upshtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags);

extern void
ups_toc_renderer(struct mkd_renderer *renderer);
upshtml_toc_renderer(struct mkd_renderer *renderer);

extern void
ups_free_renderer(struct mkd_renderer *renderer);
upshtml_free_renderer(struct mkd_renderer *renderer);

extern void
ups_xhtml_smartypants(struct buf *ob, struct buf *text);
upshtml_smartypants(struct buf *ob, struct buf *text);

#endif

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#include "buffer.h"
#include "xhtml.h"
#include "html.h"

#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -304,7 +304,7 @@ static struct {
#endif

void
ups_xhtml_smartypants(struct buf *ob, struct buf *text)
upshtml_smartypants(struct buf *ob, struct buf *text)
{
size_t i;
struct smartypants_data smrt = {0, 0};
Expand Down
Loading

0 comments on commit ecf6c85

Please sign in to comment.