summaryrefslogtreecommitdiff
path: root/src/smart.c
blob: cb67448e3b7a9607794d8546056adcc75f747515 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#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"

static const char SMART_PUNCT_TABLE[] = {
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

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)
{
	char c;
	int32_t after_char = 0;
	int32_t before_char = 0;
	bool left_flanking, right_flanking;
	int lastout = 0;
	int i = 0, j = 0;
	cmark_chunk lit = node->as.literal;
	int len;

	while (i < lit.len) {
		c = lit.data[i];
		i++;
		if (SMART_PUNCT_TABLE[(int)c] == 0) {
			continue;
		}

		if (i - 1 - lastout > 0) {
			(*escape)(buf, lit.data + lastout, i - 1 - lastout);
		}

		if (c == 34 || c == 39) {
			if (i == 1) {
                                // set before_char based on previous text node if there is one:
				if (node->prev) {
					if (node->prev->type == CMARK_NODE_TEXT) {

						// walk to the beginning of the UTF_8 sequence:
						j = node->prev->as.literal.len - 1;
						while (j > 0 &&
						       node->prev->as.literal.data[j] >> 6 == 2) {
							j--;
						}
						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;
				}
			} else {
				j = i - 2;
				// walk back to the beginning of the UTF_8 sequence:
				while (j > 0 && lit.data[j] >> 6 == 2) {
					j--;
				}
				utf8proc_iterate(lit.data + j, lit.len - j, &before_char);
			}

			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 '"':
			if (right_flanking) {
				cmark_strbuf_puts(buf, right_double_quote);
			} else {
				cmark_strbuf_puts(buf, left_double_quote);
			}
			break;
		case '\'':
			if (left_flanking && !right_flanking) {
				cmark_strbuf_puts(buf, left_single_quote);
			} else {
				cmark_strbuf_puts(buf, right_single_quote);
			}
			break;
		case '-':
			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 '.':
			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);

}