| 1 |
|
| /* |
| 2 |
|
| * Copyright (c) 2011, Vicent Marti |
| 3 |
|
| * |
| 4 |
|
| * Permission to use, copy, modify, and distribute this software for any |
| 5 |
|
| * purpose with or without fee is hereby granted, provided that the above |
| 6 |
|
| * copyright notice and this permission notice appear in all copies. |
| 7 |
|
| * |
| 8 |
|
| * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
|
| * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
|
| * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
|
| * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
|
| * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
|
| * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
|
| * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
|
| */ |
| 16 |
|
| |
| 17 |
|
| #include "buffer.h" |
| 18 |
|
| #include "html.h" |
| 19 |
|
| |
| 20 |
|
| #include <string.h> |
| 21 |
|
| #include <stdlib.h> |
| 22 |
|
| #include <stdio.h> |
| 23 |
|
| #include <ctype.h> |
| 24 |
|
| |
| 25 |
|
| struct smartypants_data { |
| 26 |
|
| int in_squote; |
| 27 |
|
| int in_dquote; |
| 28 |
|
| }; |
| 29 |
|
| |
| 30 |
|
| static size_t smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 31 |
|
| static size_t smartypants_cb__dquote(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 32 |
|
| static size_t smartypants_cb__amp(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 33 |
|
| static size_t smartypants_cb__period(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 34 |
|
| static size_t smartypants_cb__number(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 35 |
|
| static size_t smartypants_cb__dash(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 36 |
|
| static size_t smartypants_cb__parens(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 37 |
|
| static size_t smartypants_cb__squote(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 38 |
|
| static size_t smartypants_cb__backtick(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size); |
| 39 |
|
| |
| 40 |
|
| static size_t (*smartypants_cb_ptrs[]) |
| 41 |
|
| (struct buf *, struct smartypants_data *, char, const char *, size_t) = |
| 42 |
|
| { |
| 43 |
|
| NULL, /* 0 */ |
| 44 |
|
| smartypants_cb__dash, /* 1 */ |
| 45 |
|
| smartypants_cb__parens, /* 2 */ |
| 46 |
|
| smartypants_cb__squote, /* 3 */ |
| 47 |
|
| smartypants_cb__dquote, /* 4 */ |
| 48 |
|
| smartypants_cb__amp, /* 5 */ |
| 49 |
|
| smartypants_cb__period, /* 6 */ |
| 50 |
|
| smartypants_cb__number, /* 7 */ |
| 51 |
|
| smartypants_cb__ltag, /* 8 */ |
| 52 |
|
| smartypants_cb__backtick, /* 9 */ |
| 53 |
|
| }; |
| 54 |
|
| |
| 55 |
|
| static const char smartypants_cb_chars[] = { |
| 56 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 57 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 58 |
|
| 0, 0, 4, 0, 0, 0, 5, 3, 2, 0, 0, 0, 0, 1, 6, 0, |
| 59 |
|
| 0, 7, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, |
| 60 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 61 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 62 |
|
| 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 63 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 64 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 65 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 66 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 67 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 68 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 69 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 70 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 71 |
|
| 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 72 |
|
| }; |
| 73 |
|
| |
| 74 |
|
| static inline int |
| 75 |
10 |
| word_boundary(char c) |
| 76 |
|
| { |
| 77 |
10 |
▶ | return c == 0 || isspace(c) || ispunct(c); |
Branch: 3 branches
| Branch | True | False | Total |
| return c == 0 || isspace(c) || ispunct(c); |
10 |
0 |
10 |
| return c == 0 || isspace(c) || ispunct(c); |
5 |
5 |
10 |
| return c == 0 || isspace(c) || ispunct(c); |
5 |
0 |
5 |
|
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| static int |
| 81 |
8 |
| smartypants_quotes(struct buf *ob, char previous_char, char next_char, char quote, int *is_open) |
| 82 |
|
| { |
| 83 |
|
| char ent[8]; |
| 84 |
|
| |
| 85 |
8 |
▶ | if (*is_open && !word_boundary(next_char)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (*is_open && !word_boundary(next_char)) |
4 |
4 |
8 |
| if (*is_open && !word_boundary(next_char)) |
0 |
4 |
4 |
|
| 86 |
0 |
| return 0; |
| 87 |
|
| |
| 88 |
8 |
▶ | if (!(*is_open) && !word_boundary(previous_char)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(*is_open) && !word_boundary(previous_char)) |
4 |
4 |
8 |
| if (!(*is_open) && !word_boundary(previous_char)) |
0 |
4 |
4 |
|
| 89 |
0 |
| return 0; |
| 90 |
|
| |
| 91 |
8 |
▶ | snprintf(ent, sizeof(ent), "&%c%cquo;", (*is_open) ? 'r' : 'l', quote); |
Branch: 1 branch
| Branch | True | False | Total |
| snprintf(ent, sizeof(ent), "&%c%cquo;", (*is_open) ? 'r' : 'l', quote); |
4 |
4 |
8 |
|
| 92 |
8 |
| *is_open = !(*is_open); |
| 93 |
8 |
| bufputs(ob, ent); |
| 94 |
8 |
| return 1; |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| static size_t |
| 98 |
2 |
| smartypants_cb__squote(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 99 |
|
| { |
| 100 |
2 |
▶ | if (size >= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size >= 2) { |
2 |
0 |
2 |
|
| 101 |
2 |
| char t1 = tolower(text[1]); |
| 102 |
|
| |
| 103 |
2 |
▶ | if (t1 == '\'') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t1 == '\'') { |
0 |
2 |
2 |
|
| 104 |
0 |
▶ | if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote)) |
0 |
0 |
0 |
| if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote)) |
0 |
0 |
0 |
|
| 105 |
0 |
| return 1; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
2 |
▶ | if ((t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && |
Branch: 5 branches
| Branch | True | False | Total |
| if ((t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && |
2 |
0 |
2 |
| if ((t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && |
2 |
0 |
2 |
| if ((t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && |
2 |
0 |
2 |
| if ((t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && |
0 |
2 |
2 |
| if ((t1 == 's' || t1 == 't' || t1 == 'm' || t1 == 'd') && |
0 |
0 |
0 |
|
| 109 |
0 |
▶ | (size == 3 || word_boundary(text[2]))) { |
Branch: 1 branch
| Branch | True | False | Total |
| (size == 3 || word_boundary(text[2]))) { |
0 |
0 |
0 |
|
| 110 |
0 |
| BUFPUTSL(ob, "’"); |
| 111 |
0 |
| return 0; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
2 |
▶ | if (size >= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size >= 3) { |
2 |
0 |
2 |
|
| 115 |
2 |
| char t2 = tolower(text[2]); |
| 116 |
|
| |
| 117 |
2 |
▶ | if (((t1 == 'r' && t2 == 'e') || |
Branch: 3 branches
| Branch | True | False | Total |
| if (((t1 == 'r' && t2 == 'e') || |
0 |
2 |
2 |
| if (((t1 == 'r' && t2 == 'e') || |
0 |
0 |
0 |
| if (((t1 == 'r' && t2 == 'e') || |
0 |
2 |
2 |
|
| 118 |
2 |
▶ | (t1 == 'l' && t2 == 'l') || |
Branch: 2 branches
| Branch | True | False | Total |
| (t1 == 'l' && t2 == 'l') || |
0 |
0 |
0 |
| (t1 == 'l' && t2 == 'l') || |
0 |
2 |
2 |
|
| 119 |
0 |
▶ | (t1 == 'v' && t2 == 'e')) && |
Branch: 2 branches
| Branch | True | False | Total |
| (t1 == 'v' && t2 == 'e')) && |
0 |
0 |
0 |
| (t1 == 'v' && t2 == 'e')) && |
0 |
0 |
0 |
|
| 120 |
0 |
▶ | (size == 4 || word_boundary(text[3]))) { |
Branch: 1 branch
| Branch | True | False | Total |
| (size == 4 || word_boundary(text[3]))) { |
0 |
0 |
0 |
|
| 121 |
0 |
| BUFPUTSL(ob, "’"); |
| 122 |
0 |
| return 0; |
| 123 |
|
| } |
| 124 |
|
| } |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
2 |
▶ | if (smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 's', &smrt->in_squote)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 's', &smrt->in_squote)) |
2 |
0 |
2 |
| if (smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 's', &smrt->in_squote)) |
2 |
0 |
2 |
|
| 128 |
2 |
| return 0; |
| 129 |
|
| |
| 130 |
0 |
| bufputc(ob, text[0]); |
| 131 |
0 |
| return 0; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| static size_t |
| 135 |
0 |
| smartypants_cb__parens(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 136 |
|
| { |
| 137 |
0 |
▶ | if (size >= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size >= 3) { |
0 |
0 |
0 |
|
| 138 |
0 |
| char t1 = tolower(text[1]); |
| 139 |
0 |
| char t2 = tolower(text[2]); |
| 140 |
|
| |
| 141 |
0 |
▶ | if (t1 == 'c' && t2 == ')') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t1 == 'c' && t2 == ')') { |
0 |
0 |
0 |
| if (t1 == 'c' && t2 == ')') { |
0 |
0 |
0 |
|
| 142 |
0 |
| BUFPUTSL(ob, "©"); |
| 143 |
0 |
| return 2; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
0 |
▶ | if (t1 == 'r' && t2 == ')') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (t1 == 'r' && t2 == ')') { |
0 |
0 |
0 |
| if (t1 == 'r' && t2 == ')') { |
0 |
0 |
0 |
|
| 147 |
0 |
| BUFPUTSL(ob, "®"); |
| 148 |
0 |
| return 2; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
0 |
▶ | if (size >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')') { |
Branch: 4 branches
| Branch | True | False | Total |
| if (size >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')') { |
0 |
0 |
0 |
| if (size >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')') { |
0 |
0 |
0 |
| if (size >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')') { |
0 |
0 |
0 |
| if (size >= 4 && t1 == 't' && t2 == 'm' && text[3] == ')') { |
0 |
0 |
0 |
|
| 152 |
0 |
| BUFPUTSL(ob, "™"); |
| 153 |
0 |
| return 3; |
| 154 |
|
| } |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
0 |
| bufputc(ob, text[0]); |
| 158 |
0 |
| return 0; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
|
| static size_t |
| 162 |
3 |
| smartypants_cb__dash(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 163 |
|
| { |
| 164 |
3 |
▶ | if (size >= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size >= 2) { |
3 |
0 |
3 |
|
| 165 |
3 |
▶ | if (text[1] == '-') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (text[1] == '-') { |
2 |
1 |
3 |
|
| 166 |
2 |
| BUFPUTSL(ob, "—"); |
| 167 |
2 |
| return 1; |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
1 |
▶ | if (word_boundary(previous_char) && word_boundary(text[1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (word_boundary(previous_char) && word_boundary(text[1])) { |
1 |
0 |
1 |
| if (word_boundary(previous_char) && word_boundary(text[1])) { |
1 |
0 |
1 |
|
| 171 |
1 |
| BUFPUTSL(ob, "–"); |
| 172 |
1 |
| return 0; |
| 173 |
|
| } |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
0 |
| bufputc(ob, text[0]); |
| 177 |
0 |
| return 0; |
| 178 |
|
| } |
| 179 |
|
| |
| 180 |
|
| static size_t |
| 181 |
6 |
| smartypants_cb__amp(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 182 |
|
| { |
| 183 |
6 |
▶ | if (size >= 6 && memcmp(text, """, 6) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (size >= 6 && memcmp(text, """, 6) == 0) { |
6 |
0 |
6 |
| if (size >= 6 && memcmp(text, """, 6) == 0) { |
6 |
0 |
6 |
|
| 184 |
6 |
▶ | if (smartypants_quotes(ob, previous_char, size >= 7 ? text[6] : 0, 'd', &smrt->in_dquote)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (smartypants_quotes(ob, previous_char, size >= 7 ? text[6] : 0, 'd', &smrt->in_dquote)) |
6 |
0 |
6 |
| if (smartypants_quotes(ob, previous_char, size >= 7 ? text[6] : 0, 'd', &smrt->in_dquote)) |
6 |
0 |
6 |
|
| 185 |
6 |
| return 5; |
| 186 |
|
| } |
| 187 |
|
| |
| 188 |
0 |
▶ | if (size >= 4 && memcmp(text, "�", 4) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (size >= 4 && memcmp(text, "�", 4) == 0) |
0 |
0 |
0 |
| if (size >= 4 && memcmp(text, "�", 4) == 0) |
0 |
0 |
0 |
|
| 189 |
0 |
| return 3; |
| 190 |
|
| |
| 191 |
0 |
| bufputc(ob, '&'); |
| 192 |
0 |
| return 0; |
| 193 |
|
| } |
| 194 |
|
| |
| 195 |
|
| static size_t |
| 196 |
2 |
| smartypants_cb__period(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 197 |
|
| { |
| 198 |
2 |
▶ | if (size >= 3 && text[1] == '.' && text[2] == '.') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (size >= 3 && text[1] == '.' && text[2] == '.') { |
2 |
0 |
2 |
| if (size >= 3 && text[1] == '.' && text[2] == '.') { |
1 |
1 |
2 |
| if (size >= 3 && text[1] == '.' && text[2] == '.') { |
1 |
0 |
1 |
|
| 199 |
1 |
| BUFPUTSL(ob, "…"); |
| 200 |
1 |
| return 2; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
1 |
▶ | if (size >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (size >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.') { |
1 |
0 |
1 |
| if (size >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.') { |
0 |
1 |
1 |
| if (size >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.') { |
0 |
0 |
0 |
| if (size >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.') { |
0 |
0 |
0 |
| if (size >= 5 && text[1] == ' ' && text[2] == '.' && text[3] == ' ' && text[4] == '.') { |
0 |
0 |
0 |
|
| 204 |
0 |
| BUFPUTSL(ob, "…"); |
| 205 |
0 |
| return 4; |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
1 |
| bufputc(ob, text[0]); |
| 209 |
1 |
| return 0; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| static size_t |
| 213 |
0 |
| smartypants_cb__backtick(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 214 |
|
| { |
| 215 |
0 |
▶ | if (size >= 2 && text[1] == '`') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (size >= 2 && text[1] == '`') { |
0 |
0 |
0 |
| if (size >= 2 && text[1] == '`') { |
0 |
0 |
0 |
|
| 216 |
0 |
▶ | if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote)) |
0 |
0 |
0 |
| if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote)) |
0 |
0 |
0 |
|
| 217 |
0 |
| return 1; |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
0 |
| return 0; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| static size_t |
| 224 |
0 |
| smartypants_cb__number(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 225 |
|
| { |
| 226 |
0 |
▶ | if (word_boundary(previous_char) && size >= 3) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (word_boundary(previous_char) && size >= 3) { |
0 |
0 |
0 |
| if (word_boundary(previous_char) && size >= 3) { |
0 |
0 |
0 |
|
| 227 |
0 |
▶ | if (text[0] == '1' && text[1] == '/' && text[2] == '2') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (text[0] == '1' && text[1] == '/' && text[2] == '2') { |
0 |
0 |
0 |
| if (text[0] == '1' && text[1] == '/' && text[2] == '2') { |
0 |
0 |
0 |
| if (text[0] == '1' && text[1] == '/' && text[2] == '2') { |
0 |
0 |
0 |
|
| 228 |
0 |
▶ | if (size == 3 || word_boundary(text[3])) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (size == 3 || word_boundary(text[3])) { |
0 |
0 |
0 |
| if (size == 3 || word_boundary(text[3])) { |
0 |
0 |
0 |
|
| 229 |
0 |
| BUFPUTSL(ob, "½"); |
| 230 |
0 |
| return 2; |
| 231 |
|
| } |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
0 |
▶ | if (text[0] == '1' && text[1] == '/' && text[2] == '4') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (text[0] == '1' && text[1] == '/' && text[2] == '4') { |
0 |
0 |
0 |
| if (text[0] == '1' && text[1] == '/' && text[2] == '4') { |
0 |
0 |
0 |
| if (text[0] == '1' && text[1] == '/' && text[2] == '4') { |
0 |
0 |
0 |
|
| 235 |
0 |
▶ | if (size == 3 || word_boundary(text[3]) || |
Branch: 3 branches
| Branch | True | False | Total |
| if (size == 3 || word_boundary(text[3]) || |
0 |
0 |
0 |
| if (size == 3 || word_boundary(text[3]) || |
0 |
0 |
0 |
| if (size == 3 || word_boundary(text[3]) || |
0 |
0 |
0 |
|
| 236 |
0 |
▶ | (size >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h')) { |
Branch: 2 branches
| Branch | True | False | Total |
| (size >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h')) { |
0 |
0 |
0 |
| (size >= 5 && tolower(text[3]) == 't' && tolower(text[4]) == 'h')) { |
0 |
0 |
0 |
|
| 237 |
0 |
| BUFPUTSL(ob, "¼"); |
| 238 |
0 |
| return 2; |
| 239 |
|
| } |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
0 |
▶ | if (text[0] == '3' && text[1] == '/' && text[2] == '4') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (text[0] == '3' && text[1] == '/' && text[2] == '4') { |
0 |
0 |
0 |
| if (text[0] == '3' && text[1] == '/' && text[2] == '4') { |
0 |
0 |
0 |
| if (text[0] == '3' && text[1] == '/' && text[2] == '4') { |
0 |
0 |
0 |
|
| 243 |
0 |
▶ | if (size == 3 || word_boundary(text[3]) || |
Branch: 3 branches
| Branch | True | False | Total |
| if (size == 3 || word_boundary(text[3]) || |
0 |
0 |
0 |
| if (size == 3 || word_boundary(text[3]) || |
0 |
0 |
0 |
| if (size == 3 || word_boundary(text[3]) || |
0 |
0 |
0 |
|
| 244 |
0 |
▶ | (size >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's')) { |
Branch: 3 branches
| Branch | True | False | Total |
| (size >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's')) { |
0 |
0 |
0 |
| (size >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's')) { |
0 |
0 |
0 |
| (size >= 6 && tolower(text[3]) == 't' && tolower(text[4]) == 'h' && tolower(text[5]) == 's')) { |
0 |
0 |
0 |
|
| 245 |
0 |
| BUFPUTSL(ob, "¾"); |
| 246 |
0 |
| return 2; |
| 247 |
|
| } |
| 248 |
|
| } |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
0 |
| bufputc(ob, text[0]); |
| 252 |
0 |
| return 0; |
| 253 |
|
| } |
| 254 |
|
| |
| 255 |
|
| static size_t |
| 256 |
0 |
| smartypants_cb__dquote(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 257 |
|
| { |
| 258 |
0 |
▶ | if (!smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 'd', &smrt->in_dquote)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 'd', &smrt->in_dquote)) |
0 |
0 |
0 |
| if (!smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 'd', &smrt->in_dquote)) |
0 |
0 |
0 |
|
| 259 |
0 |
| BUFPUTSL(ob, """); |
| 260 |
|
| |
| 261 |
0 |
| return 0; |
| 262 |
|
| } |
| 263 |
|
| |
| 264 |
|
| static size_t |
| 265 |
10 |
| smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, char previous_char, const char *text, size_t size) |
| 266 |
|
| { |
| 267 |
10 |
| size_t i = 0; |
| 268 |
|
| |
| 269 |
43 |
▶ | while (i < size && text[i] != '>') |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < size && text[i] != '>') |
43 |
0 |
43 |
| while (i < size && text[i] != '>') |
33 |
10 |
43 |
|
| 270 |
33 |
| i++; |
| 271 |
|
| |
| 272 |
10 |
| bufput(ob, text, i + 1); |
| 273 |
10 |
| return i; |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
|
| #if 0 |
| 277 |
|
| static struct { |
| 278 |
|
| char c0; |
| 279 |
|
| const char *pattern; |
| 280 |
|
| const char *entity; |
| 281 |
|
| int skip; |
| 282 |
|
| } smartypants_subs[] = { |
| 283 |
|
| { '\'', "'s>", "’", 0 }, |
| 284 |
|
| { '\'', "'t>", "’", 0 }, |
| 285 |
|
| { '\'', "'re>", "’", 0 }, |
| 286 |
|
| { '\'', "'ll>", "’", 0 }, |
| 287 |
|
| { '\'', "'ve>", "’", 0 }, |
| 288 |
|
| { '\'', "'m>", "’", 0 }, |
| 289 |
|
| { '\'', "'d>", "’", 0 }, |
| 290 |
|
| { '-', "--", "—", 1 }, |
| 291 |
|
| { '-', "<->", "–", 0 }, |
| 292 |
|
| { '.', "...", "…", 2 }, |
| 293 |
|
| { '.', ". . .", "…", 4 }, |
| 294 |
|
| { '(', "(c)", "©", 2 }, |
| 295 |
|
| { '(', "(r)", "®", 2 }, |
| 296 |
|
| { '(', "(tm)", "™", 3 }, |
| 297 |
|
| { '3', "<3/4>", "¾", 2 }, |
| 298 |
|
| { '3', "<3/4ths>", "¾", 2 }, |
| 299 |
|
| { '1', "<1/2>", "½", 2 }, |
| 300 |
|
| { '1', "<1/4>", "¼", 2 }, |
| 301 |
|
| { '1', "<1/4th>", "¼", 2 }, |
| 302 |
|
| { '&', "�", 0, 3 }, |
| 303 |
|
| }; |
| 304 |
|
| #endif |
| 305 |
|
| |
| 306 |
|
| void |
| 307 |
1 |
| upshtml_smartypants(struct buf *ob, struct buf *text) |
| 308 |
|
| { |
| 309 |
|
| size_t i; |
| 310 |
1 |
| struct smartypants_data smrt = {0, 0}; |
| 311 |
|
| |
| 312 |
1 |
▶ | if (!text) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!text) |
0 |
1 |
1 |
|
| 313 |
0 |
| return; |
| 314 |
|
| |
| 315 |
1 |
| bufgrow(ob, text->size); |
| 316 |
|
| |
| 317 |
25 |
▶ | for (i = 0; i < text->size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < text->size; ++i) { |
24 |
1 |
25 |
|
| 318 |
|
| size_t org; |
| 319 |
24 |
| char action = 0; |
| 320 |
|
| |
| 321 |
24 |
| org = i; |
| 322 |
130 |
▶ | while (i < text->size && (action = smartypants_cb_chars[(unsigned char)text->data[i]]) == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < text->size && (action = smartypants_cb_chars[(unsigned char)text->data[i]]) == 0) |
129 |
1 |
130 |
| while (i < text->size && (action = smartypants_cb_chars[(unsigned char)text->data[i]]) == 0) |
106 |
23 |
129 |
|
| 323 |
106 |
| i++; |
| 324 |
|
| |
| 325 |
24 |
▶ | if (i > org) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i > org) |
20 |
4 |
24 |
|
| 326 |
20 |
| bufput(ob, text->data + org, i - org); |
| 327 |
|
| |
| 328 |
24 |
▶ | if (i < text->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < text->size) { |
23 |
1 |
24 |
|
| 329 |
23 |
| i += smartypants_cb_ptrs[(int)action] |
| 330 |
23 |
▶ | (ob, &smrt, i ? text->data[i - 1] : 0, text->data + i, text->size - i); |
Branch: 1 branch
| Branch | True | False | Total |
| (ob, &smrt, i ? text->data[i - 1] : 0, text->data + i, text->size - i); |
22 |
1 |
23 |
|
| 331 |
|
| } |
| 332 |
|
| } |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
|
| |