diff options
| author | 2022-09-27 17:41:56 +0200 | |
|---|---|---|
| committer | 2022-09-27 17:41:56 +0200 | |
| commit | 5784ddecb493802846695d8044d78448409de340 (patch) | |
| tree | edd81fc7f0dce916a2fb864134541b6407ae3b8e /html.c | |
| parent | Backport from xs. (diff) | |
| download | snac2-5784ddecb493802846695d8044d78448409de340.tar.gz snac2-5784ddecb493802846695d8044d78448409de340.tar.xz snac2-5784ddecb493802846695d8044d78448409de340.zip | |
Rewrite not_really_markdown() with xs_regex_split().
Diffstat (limited to 'html.c')
| -rw-r--r-- | html.c | 94 |
1 files changed, 39 insertions, 55 deletions
| @@ -16,69 +16,53 @@ d_char *not_really_markdown(char *content, d_char **f_content) | |||
| 16 | int in_blq = 0; | 16 | int in_blq = 0; |
| 17 | xs *list; | 17 | xs *list; |
| 18 | char *p, *v; | 18 | char *p, *v; |
| 19 | xs *wrk = xs_dup(content); | 19 | xs *wrk = xs_str_new(NULL); |
| 20 | 20 | ||
| 21 | /* global changes */ | ||
| 22 | { | 21 | { |
| 23 | /* backticks */ | 22 | /* split by special markup */ |
| 24 | xs *ml = xs_regex_match(wrk, "`[^`]+`"); | 23 | xs *sm = xs_regex_split(content, |
| 25 | p = ml; | 24 | "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)"); |
| 25 | int n = 0; | ||
| 26 | 26 | ||
| 27 | p = sm; | ||
| 27 | while (xs_list_iter(&p, &v)) { | 28 | while (xs_list_iter(&p, &v)) { |
| 28 | xs *s1 = xs_crop(xs_dup(v), 1, -1); | 29 | if ((n & 0x1)) { |
| 29 | xs *s2 = xs_fmt("<code>%s</code>", s1); | 30 | /* markup */ |
| 30 | 31 | if (xs_startswith(v, "`")) { | |
| 31 | wrk = xs_replace_i(wrk, v, s2); | 32 | xs *s1 = xs_crop(xs_dup(v), 1, -1); |
| 32 | } | 33 | xs *s2 = xs_fmt("<code>%s</code>", s1); |
| 33 | } | 34 | wrk = xs_str_cat(wrk, s2); |
| 34 | 35 | } | |
| 35 | { | 36 | else |
| 36 | /* double asterisks */ | 37 | if (xs_startswith(v, "**")) { |
| 37 | xs *ml = xs_regex_match(wrk, "\\*\\*[^\\*]+\\*\\*"); | 38 | xs *s1 = xs_crop(xs_dup(v), 2, -2); |
| 38 | p = ml; | 39 | xs *s2 = xs_fmt("<b>%s</b>", s1); |
| 39 | 40 | wrk = xs_str_cat(wrk, s2); | |
| 40 | while (xs_list_iter(&p, &v)) { | 41 | } |
| 41 | xs *s1 = xs_crop(xs_dup(v), 2, -2); | 42 | else |
| 42 | xs *s2 = xs_fmt("<b>%s</b>", s1); | 43 | if (xs_startswith(v, "*")) { |
| 43 | 44 | xs *s1 = xs_crop(xs_dup(v), 1, -1); | |
| 44 | wrk = xs_replace_i(wrk, v, s2); | 45 | xs *s2 = xs_fmt("<i>%s</i>", s1); |
| 45 | } | 46 | wrk = xs_str_cat(wrk, s2); |
| 46 | } | 47 | } |
| 47 | 48 | else | |
| 48 | { | 49 | if (xs_startswith(v, "http")) { |
| 49 | /* single asterisks */ | 50 | xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v); |
| 50 | xs *ml = xs_regex_match(wrk, "\\*[^\\*]+\\*"); | 51 | wrk = xs_str_cat(wrk, s1); |
| 51 | p = ml; | 52 | } |
| 52 | 53 | else | |
| 53 | while (xs_list_iter(&p, &v)) { | 54 | /* what the hell is this */ |
| 54 | xs *s1 = xs_crop(xs_dup(v), 1, -1); | 55 | wrk = xs_str_cat(wrk, v); |
| 55 | xs *s2 = xs_fmt("<i>%s</i>", s1); | ||
| 56 | |||
| 57 | wrk = xs_replace_i(wrk, v, s2); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | #if 0 | ||
| 62 | { | ||
| 63 | /* urls */ | ||
| 64 | xs *done = xs_list_new(); | ||
| 65 | xs *ml = xs_regex_matchall(wrk, "https?:/" "/[^ ]+"); | ||
| 66 | p = ml; | ||
| 67 | |||
| 68 | while (xs_list_iter(&p, &v)) { | ||
| 69 | if (xs_list_in(done, v) == -1) { | ||
| 70 | xs *s2 = xs_fmt("<a href=\"%s\">%s</a>", v, v); | ||
| 71 | wrk = xs_replace_i(wrk, v, s2); | ||
| 72 | |||
| 73 | /* keep track of already done replaces */ | ||
| 74 | done = xs_list_append(done, v); | ||
| 75 | } | 56 | } |
| 57 | else | ||
| 58 | /* surrounded text, copy directly */ | ||
| 59 | wrk = xs_str_cat(wrk, v); | ||
| 60 | |||
| 61 | n++; | ||
| 76 | } | 62 | } |
| 77 | } | 63 | } |
| 78 | #endif | ||
| 79 | |||
| 80 | /* now work on lines */ | ||
| 81 | 64 | ||
| 65 | /* now work by lines */ | ||
| 82 | p = list = xs_split(wrk, "\n"); | 66 | p = list = xs_split(wrk, "\n"); |
| 83 | 67 | ||
| 84 | s = xs_str_new(NULL); | 68 | s = xs_str_new(NULL); |