Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Better support of entities and '&'

The "entity" renderer callback is no longer called when a lone '&' is
encountered, only when a valid entity ("&#?[A-Za-z0-9]+;") is recognized.
Otherwise '&' is considered as an inactive character and handed over to
"normal_text" callback along with other inactive charaters.

In the example renderers rndr_entity has therefore been replaced by NULL,
because the only treatment of entities is to copy them verbatim into the
output (without the escaping rndr_normal_text does).
  • Loading branch information...
commit 92aac3a2469e417a9128659289e90590ec760085 1 parent 410e83d
Natacha Porté authored
Showing with 20 additions and 25 deletions.
  1. +14 −12 markdown.c
  2. +6 −13 renderers.c
View
26 markdown.c
@@ -218,7 +218,7 @@ tag_length(char *data, size_t size, enum mkd_autolink *autolink) {
static void
parse_inline(struct buf *ob, struct render *rndr, char *data, size_t size) {
size_t i = 0, end = 0;
- char_trigger action;
+ char_trigger action = 0;
struct buf work = { 0, 0, 0, 0, 0 };
while (i < size) {
@@ -237,11 +237,11 @@ parse_inline(struct buf *ob, struct render *rndr, char *data, size_t size) {
/* calling the trigger */
end = action(ob, rndr, data + i, i, size - i);
- if (!end) { /* no action from the callback */
- bufputc(ob, data[i]);
- i += 1; }
- else i += end;
- end = i; } }
+ if (!end) /* no action from the callback */
+ end = i + 1;
+ else {
+ i += end;
+ end = i; } } }
/* find_emph_char • looks for the next emph char, skipping other constructs */
@@ -484,7 +484,7 @@ char_codespan(struct buf *ob, struct render *rndr,
static size_t
char_escape(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
- struct buf work = { 0, 0, 0, 0 };
+ struct buf work = { 0, 0, 0, 0, 0 };
if (size > 1) {
if (rndr->make.normal_text) {
work.data = data + 1;
@@ -512,10 +512,12 @@ char_entity(struct buf *ob, struct render *rndr,
end += 1; }
else {
/* lone '&' */
- end = 1; }
- work.data = data;
- work.size = end;
- rndr->make.entity(ob, &work, rndr->make.opaque);
+ return 0; }
+ if (rndr->make.entity) {
+ work.data = data;
+ work.size = end;
+ rndr->make.entity(ob, &work, rndr->make.opaque); }
+ else bufput(ob, data, end);
return end; }
@@ -1455,11 +1457,11 @@ markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer) {
= char_emphasis;
if (rndr.make.codespan) rndr.active_char['`'] = char_codespan;
if (rndr.make.linebreak) rndr.active_char['\n'] = char_linebreak;
- if (rndr.make.entity) rndr.active_char['&'] = char_entity;
if (rndr.make.image || rndr.make.link)
rndr.active_char['['] = char_link;
rndr.active_char['<'] = char_langle_tag;
rndr.active_char['\\'] = char_escape;
+ rndr.active_char['&'] = char_entity;
/* first pass: looking for references, copying everything else */
beg = 0;
View
19 renderers.c
@@ -94,13 +94,6 @@ rndr_double_emphasis(struct buf *ob, struct buf *text, char c, void *opaque) {
BUFPUTSL(ob, "</strong>");
return 1; }
-static void
-rndr_entity(struct buf *ob, struct buf *entity, void *opaque) {
- /* called with "&" when matching something that isn't an entity */
- if (!entity || entity->size < 1)
- BUFPUTSL(ob, "&amp;");
- else bufput(ob, entity->data, entity->size); }
-
static int
rndr_emphasis(struct buf *ob, struct buf *text, char c, void *opaque) {
if (!text || !text->size) return 0;
@@ -235,7 +228,7 @@ const struct mkd_renderer mkd_html = {
rndr_raw_inline,
rndr_triple_emphasis,
- rndr_entity,
+ NULL,
rndr_normal_text,
"*_",
@@ -294,7 +287,7 @@ const struct mkd_renderer mkd_xhtml = {
rndr_raw_inline,
rndr_triple_emphasis,
- rndr_entity,
+ NULL,
rndr_normal_text,
"*_",
@@ -434,7 +427,7 @@ const struct mkd_renderer discount_html = {
rndr_raw_inline,
rndr_triple_emphasis,
- rndr_entity,
+ NULL,
rndr_normal_text,
"*_",
@@ -459,7 +452,7 @@ const struct mkd_renderer discount_xhtml = {
rndr_raw_inline,
rndr_triple_emphasis,
- rndr_entity,
+ NULL,
rndr_normal_text,
"*_",
@@ -564,7 +557,7 @@ const struct mkd_renderer nat_html = {
rndr_raw_inline,
nat_triple_emphasis,
- rndr_entity,
+ NULL,
rndr_normal_text,
"*_-+|",
@@ -589,7 +582,7 @@ const struct mkd_renderer nat_xhtml = {
rndr_raw_inline,
nat_triple_emphasis,
- rndr_entity,
+ NULL,
rndr_normal_text,
"*_-+|",
Please sign in to comment.
Something went wrong with that request. Please try again.