1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "config.h"
#include "cmark.h"
#include "node.h"
#include "utf8.h"
#include "buffer.h"
#include "chunk.h"
void escape_with_smart(cmark_strbuf *buf,
cmark_node *node,
void (*escape)(cmark_strbuf *, const unsigned char *, int),
const char *left_double_quote,
const char *right_double_quote,
const char *left_single_quote,
const char *right_single_quote,
const char *em_dash,
const char *en_dash,
const char *ellipses)
{
int32_t c = 0;
int32_t after_char = 0;
int32_t before_char = 0;
int len;
bool left_flanking, right_flanking;
int lastout = 0;
int i = 0;
cmark_chunk lit = node->as.literal;
// set before_char based on previous text node if there is one:
if (node->prev) {
if (node->prev->type == CMARK_NODE_TEXT) {
// walk back to the beginning of the UTF_8 sequence:
i = node->prev->as.literal.len - 1;
while (i > 0 && node->prev->as.literal.data[i] >> 6 == 2) {
i -= 1;
}
len = utf8proc_iterate(node->prev->as.literal.data + i,
node->prev->as.literal.len - i,
&before_char);
if (len == -1) {
before_char = 10;
}
} else if (node->prev->type == CMARK_NODE_SOFTBREAK ||
node->prev->type == CMARK_NODE_LINEBREAK) {
before_char = 10;
} else {
before_char = 65;
}
} else {
before_char = 10;
}
while (i < lit.len) {
len = utf8proc_iterate(lit.data + i, lit.len - i, &c);
i += len;
// replace with efficient lookup table:
if (!(c == 34 || c == 39 || c == 45 || c == 46)) {
before_char = c;
continue;
}
(*escape)(buf, lit.data + lastout, i - len - lastout);
if (c == 34 || c == 39) {
if (i >= lit.len) {
if (node->next) {
if (node->next->type == CMARK_NODE_TEXT) {
utf8proc_iterate(node->next->as.literal.data,
node->next->as.literal.len,
&after_char);
} else if (node->next->type == CMARK_NODE_SOFTBREAK ||
node->next->type == CMARK_NODE_LINEBREAK) {
after_char = 10;
} else {
after_char = 65;
}
} else {
after_char = 10;
}
} else {
utf8proc_iterate(lit.data + i, lit.len - i, &after_char);
}
left_flanking = !utf8proc_is_space(after_char) &&
!(utf8proc_is_punctuation(after_char) &&
!utf8proc_is_space(before_char) &&
!utf8proc_is_punctuation(before_char));
right_flanking = !utf8proc_is_space(before_char) &&
!(utf8proc_is_punctuation(before_char) &&
!utf8proc_is_space(after_char) &&
!utf8proc_is_punctuation(after_char));
}
switch (c) {
case 34: // "
if (right_flanking) {
cmark_strbuf_puts(buf, right_double_quote);
} else {
cmark_strbuf_puts(buf, left_double_quote);
}
break;
case 39: // '
if (left_flanking && !right_flanking) {
cmark_strbuf_puts(buf, left_single_quote);
} else {
cmark_strbuf_puts(buf, right_single_quote);
}
break;
case 45: // -
if (i < lit.len && lit.data[i] == '-') {
if (lit.data[i + 1] == '-') {
cmark_strbuf_puts(buf, em_dash);
i += 2;
} else {
cmark_strbuf_puts(buf, en_dash);
i += 1;
}
} else {
cmark_strbuf_putc(buf, c);
}
break;
case 46: // .
if (i < lit.len - 1 && lit.data[i] == '.' &&
lit.data[i + 1] == '.') {
cmark_strbuf_puts(buf, ellipses);
i += 2;
} else {
cmark_strbuf_putc(buf, c);
}
break;
default:
cmark_strbuf_putc(buf, c);
}
lastout = i;
}
(*escape)(buf, node->as.literal.data + lastout, lit.len - lastout);
}
|