Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 159 additions & 47 deletions cpp/parser/GumboNormalizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ static bool is_br_node(GumboNode *node) {
return n && strcmp(n, "br") == 0;
}

static bool is_code_block_node(GumboNode *node) {
char buf[64];
const char *n = get_tag_name(node, buf, sizeof(buf));
return n && (strcmp(n, "pre") == 0 || strcmp(n, "codeblock") == 0);
}

static bool is_block_producing(GumboNode *node) {
char buf[64];
const char *n = get_tag_name(node, buf, sizeof(buf));
Expand Down Expand Up @@ -680,6 +686,18 @@ static bool has_styled_span_descendant(GumboNode *node) {
static void walk_node(GumboNode *node, buffer_t *out,
const custom_style_t *inherited);

/** Receives one line of a code block, with its open inline tags closed.
* has_content is false for an empty line. */
typedef void (*code_line_fn)(buffer_t *line, bool has_content, void *ctx);

static void walk_children(GumboNode *node, buffer_t *out,
const custom_style_t *inherited);
static bool has_code_line_break(const char *html, size_t len);
static void split_code_lines(const char *html, size_t len,
code_line_fn emit_line, void *ctx);
static void emit_code_lines(const char *html, size_t len, buffer_t *out,
GumboElement *align_el);

/* ------------------------------------------------------------------ */
/* Blockquote content flattening */
/* ------------------------------------------------------------------ */
Expand Down Expand Up @@ -758,9 +776,30 @@ static void flatten_bq_node(GumboNode *node, buffer_t *ib, buffer_t *out,
}
return;
}
// A code block can't be nested in a blockquote, so its lines become separate
// paragraphs. Its inline content is collected whole first (as for a
// top-level code block), so both newlines and <br>s split it.
if (is_code_block_node(node) && is_purely_inline(node)) {
flush_inline_p(ib, out, NULL);
walk_children(node, ib, inherited);
if (has_code_line_break(ib->data, ib->len)) {
emit_code_lines(ib->data, ib->len, out, &node->v.element);
buffer_clear(ib);
} else {
flush_inline_p(ib, out, &node->v.element);
}
return;
}
if (is_block_producing(node) || is_blockquote_node(node)) {
flush_inline_p(ib, out, NULL);
flatten_bq_children(node, ib, out, inherited);
// A code block can't be nested in a blockquote, so its lines become
// separate paragraphs.
if (is_code_block_node(node) && has_code_line_break(ib->data, ib->len)) {
Comment thread
szydlovsky marked this conversation as resolved.
Resolved
emit_code_lines(ib->data, ib->len, out, &node->v.element);
buffer_clear(ib);
return;
}
// The flattened block becomes a <p>; carry over its text-align (if any).
flush_inline_p(ib, out, &node->v.element);
return;
Expand Down Expand Up @@ -803,6 +842,19 @@ static void flush_li_buffer(buffer_t *ib, buffer_t *out, li_ctx_t *ctx) {
ctx->has_emitted = true;
}

typedef struct {
buffer_t *out;
li_ctx_t *li;
} li_code_line_ctx_t;

/** Emits a code line as its own <li>. Empty lines are dropped, as with <br>. */
static void emit_li_code_line(buffer_t *line, bool has_content, void *ctx) {
if (!has_content)
return;
li_code_line_ctx_t *c = (li_code_line_ctx_t *)ctx;
flush_li_buffer(line, c->out, c->li);
}

static void flatten_li_children(GumboNode *node, buffer_t *ib, buffer_t *out,
li_ctx_t *ctx) {
if (!is_element(node))
Expand Down Expand Up @@ -847,9 +899,33 @@ static void flatten_li_node(GumboNode *node, buffer_t *ib, buffer_t *out,
flush_li_buffer(ib, out, ctx);
return;
}
// A code block can't be nested in a list item, so its lines become separate
// items, like lines split by <br>. Its inline content is collected whole
// first (as for a top-level code block), so both newlines and <br>s split
// it.
if (is_code_block_node(node) && is_purely_inline(node)) {
flush_li_buffer(ib, out, ctx);
walk_children(node, ib, ctx->inherited);
Comment thread
szydlovsky marked this conversation as resolved.
Resolved
if (has_code_line_break(ib->data, ib->len)) {
li_code_line_ctx_t line_ctx = {out, ctx};
split_code_lines(ib->data, ib->len, emit_li_code_line, &line_ctx);
buffer_clear(ib);
} else {
flush_li_buffer(ib, out, ctx);
}
return;
}
if (is_block_producing(node) || is_blockquote_node(node)) {
flush_li_buffer(ib, out, ctx);
flatten_li_children(node, ib, out, ctx);
// A code block can't be nested in a list item, so its lines become
// separate items, like lines split by <br>.
if (is_code_block_node(node) && has_code_line_break(ib->data, ib->len)) {
li_code_line_ctx_t line_ctx = {out, ctx};
split_code_lines(ib->data, ib->len, emit_li_code_line, &line_ctx);
buffer_clear(ib);
return;
}
flush_li_buffer(ib, out, ctx);
return;
}
Expand Down Expand Up @@ -940,8 +1016,6 @@ static void walk_children(GumboNode *node, buffer_t *out,
/* Code block lines */
/* ------------------------------------------------------------------ */

#define CODE_LINE_MAX_OPEN_TAGS 32

/** True for a `<br>`, `<br/>` or `<br ...>` tag. */
static bool is_br_tag(const char *tag, size_t tag_len) {
return tag_len >= 4 && strncmp(tag, "<br", 3) == 0 &&
Expand Down Expand Up @@ -970,51 +1044,57 @@ static bool has_code_line_break(const char *html, size_t len) {
return false;
}

/** Closes the open inline tags, emits the line as a <p> (or <br> when it has
* no content) and reopens the tags for the next line. */
static void flush_code_line(buffer_t *line, buffer_t *out, const char *html,
const size_t *open_start, const size_t *open_len,
int depth, bool has_content) {
for (int d = depth - 1; d >= 0; d--) {
const char *tag = html + open_start[d];
/** Inline tags open at the current point of a code block, as offsets into its
* HTML. Grows as needed, so any nesting depth is closed and reopened. */
typedef struct {
size_t *start;
size_t *len;
int depth;
int cap;
} open_tags_t;

static void open_tags_push(open_tags_t *t, size_t start, size_t len) {
if (t->depth == t->cap) {
t->cap = t->cap ? t->cap * 2 : 8;
t->start = (size_t *)realloc(t->start, (size_t)t->cap * sizeof(size_t));
t->len = (size_t *)realloc(t->len, (size_t)t->cap * sizeof(size_t));
Comment thread
szydlovsky marked this conversation as resolved.
Resolved
}
t->start[t->depth] = start;
t->len[t->depth] = len;
t->depth++;
}

/** Closes the open inline tags, hands the line to emit_line and reopens the
* tags for the next line. */
static void flush_code_line(buffer_t *line, const char *html,
const open_tags_t *open, bool has_content,
code_line_fn emit_line, void *ctx) {
for (int d = open->depth - 1; d >= 0; d--) {
const char *tag = html + open->start[d];
size_t name_len = 1;
while (name_len < open_len[d] && tag[name_len] != ' ' &&
while (name_len < open->len[d] && tag[name_len] != ' ' &&
tag[name_len] != '>')
name_len++;
buffer_append_str(line, "</");
buffer_append(line, tag + 1, name_len - 1);
buffer_append_str(line, ">");
}
if (has_content) {
buffer_append_str(out, "<p>");
buffer_append(out, line->data, line->len);
buffer_append_str(out, "</p>");
} else {
buffer_append_str(out, "<br>");
}
emit_line(line, has_content, ctx);
buffer_clear(line);
for (int d = 0; d < depth; d++)
buffer_append(line, html + open_start[d], open_len[d]);
for (int d = 0; d < open->depth; d++)
buffer_append(line, html + open->start[d], open->len[d]);
}

/**
* Emits normalized inline HTML of a code block (from <pre> or <codeblock>) as
* one <p> per line, since line breaks in code are significant. A line break is
* a newline in text or a <br>. Inline tags open across a break are closed
* before it and reopened after it. An empty line becomes <br>, and a trailing
* empty line (`a\n</pre>`) is dropped, as browsers don't render it either.
* Splits normalized inline HTML of a code block (from <pre> or <codeblock>)
* into lines, since line breaks in code are significant. A line break is a
* newline in text or a <br>. Inline tags open across a break are closed before
* it and reopened after it. A trailing empty line (`a\n</pre>`) is dropped, as
* browsers don't render it either.
*/
static void emit_code_lines(const char *html, size_t len, buffer_t *out) {
if (!has_code_line_break(html, len)) {
buffer_append_str(out, "<p>");
buffer_append(out, html, len);
buffer_append_str(out, "</p>");
return;
}

size_t open_start[CODE_LINE_MAX_OPEN_TAGS];
size_t open_len[CODE_LINE_MAX_OPEN_TAGS];
int depth = 0;
static void split_code_lines(const char *html, size_t len,
code_line_fn emit_line, void *ctx) {
open_tags_t open = {NULL, NULL, 0, 0};
buffer_t line = buffer_create(64);
bool has_content = false;

Expand All @@ -1025,28 +1105,24 @@ static void emit_code_lines(const char *html, size_t len, buffer_t *out) {
size_t tag_len = end ? (size_t)(end - (html + i)) + 1 : len - i;
const char *tag = html + i;
if (is_br_tag(tag, tag_len)) {
flush_code_line(&line, out, html, open_start, open_len, depth,
has_content);
flush_code_line(&line, html, &open, has_content, emit_line, ctx);
has_content = false;
} else {
buffer_append(&line, tag, tag_len);
if (tag_len > 1 && tag[1] == '/') {
if (depth > 0)
depth--;
if (open.depth > 0)
open.depth--;
} else if (tag_len > 1 && tag[tag_len - 2] == '/') {
has_content = true; /* self-closing, e.g. <img /> */
} else if (depth < CODE_LINE_MAX_OPEN_TAGS) {
open_start[depth] = i;
open_len[depth] = tag_len;
depth++;
} else {
open_tags_push(&open, i, tag_len);
}
}
i += tag_len;
continue;
}
if (html[i] == '\n') {
flush_code_line(&line, out, html, open_start, open_len, depth,
has_content);
flush_code_line(&line, html, &open, has_content, emit_line, ctx);
has_content = false;
} else {
buffer_append(&line, &html[i], 1);
Expand All @@ -1055,8 +1131,44 @@ static void emit_code_lines(const char *html, size_t len, buffer_t *out) {
i++;
}
if (has_content)
flush_code_line(&line, out, html, open_start, open_len, depth, true);
flush_code_line(&line, html, &open, true, emit_line, ctx);
free(line.data);
free(open.start);
free(open.len);
}

typedef struct {
buffer_t *out;
GumboElement *align_el; /* text-align source for each <p>; may be NULL */
} code_p_ctx_t;

/** Emits a code line as a <p>, or an empty one as <br>. */
static void emit_code_line_p(buffer_t *line, bool has_content, void *ctx) {
code_p_ctx_t *c = (code_p_ctx_t *)ctx;
if (has_content) {
buffer_append_str(c->out, "<p");
if (c->align_el)
emit_alignment(c->align_el, "p", c->out);
buffer_append_str(c->out, ">");
buffer_append(c->out, line->data, line->len);
buffer_append_str(c->out, "</p>");
} else {
buffer_append_str(c->out, "<br>");
}
}

/** Emits a code block's inline HTML as one <p> per line (see
* split_code_lines). An empty line becomes <br>. Each <p> gets the
* text-align of align_el, if given. */
static void emit_code_lines(const char *html, size_t len, buffer_t *out,
GumboElement *align_el) {
code_p_ctx_t ctx = {out, align_el};
if (!has_code_line_break(html, len)) {
buffer_t line = {(char *)html, len, len};
emit_code_line_p(&line, true, &ctx);
return;
}
split_code_lines(html, len, emit_code_line_p, &ctx);
}

/* ------------------------------------------------------------------ */
Expand Down Expand Up @@ -1306,7 +1418,7 @@ static void walk_node(GumboNode *node, buffer_t *out,
if (is_purely_inline(node)) {
buffer_t code = buffer_create(64);
walk_children(node, &code, inherited);
emit_code_lines(code.data, code.len, out);
emit_code_lines(code.data, code.len, out, NULL);
free(code.data);
} else {
walk_children(node, out, inherited);
Expand Down
Loading
Loading