summaryrefslogtreecommitdiff
path: root/format.c
diff options
context:
space:
mode:
authorGravatar default2022-11-13 09:12:20 +0100
committerGravatar default2022-11-13 09:12:20 +0100
commitbb7bcc674caa0d5d6f0b0918c34c88cca97f1a80 (patch)
tree41474f97e8123f4a43169dcf24b06d10e13b1b1e /format.c
parentSome formatting refactoring. (diff)
downloadsnac2-bb7bcc674caa0d5d6f0b0918c34c88cca97f1a80.tar.gz
snac2-bb7bcc674caa0d5d6f0b0918c34c88cca97f1a80.tar.xz
snac2-bb7bcc674caa0d5d6f0b0918c34c88cca97f1a80.zip
More formatting tweaks.
Diffstat (limited to 'format.c')
-rw-r--r--format.c112
1 files changed, 60 insertions, 52 deletions
diff --git a/format.c b/format.c
index 13f0840..c38352e 100644
--- a/format.c
+++ b/format.c
@@ -34,71 +34,73 @@ struct {
34}; 34};
35 35
36 36
37d_char *not_really_markdown(char *content) 37static d_char *format_line(const char *line)
38/* formats a content using some Markdown rules */ 38/* formats a line */
39{ 39{
40 d_char *s = NULL; 40 d_char *s = xs_str_new(NULL);
41 int in_pre = 0;
42 int in_blq = 0;
43 xs *list;
44 char *p, *v; 41 char *p, *v;
45 xs *wrk = xs_str_new(NULL);
46 42
47 /* some preparation to avoid writing very kludgy code */ 43 /* split by markup */
48 xs *p_content = xs_replace(content, "```", "@pre@"); 44 xs *sm = xs_regex_split(line,
45 "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
46 int n = 0;
49 47
50 { 48 p = sm;
51 /* split by special markup */ 49 while (xs_list_iter(&p, &v)) {
52 xs *sm = xs_regex_split(p_content, 50 if ((n & 0x1)) {
53 "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)"); 51 /* markup */
54 int n = 0; 52 if (xs_startswith(v, "`")) {
55 53 xs *s1 = xs_crop(xs_dup(v), 1, -1);
56 p = sm; 54 xs *s2 = xs_fmt("<code>%s</code>", s1);
57 while (xs_list_iter(&p, &v)) { 55 s = xs_str_cat(s, s2);
58 if ((n & 0x1)) {
59 /* markup */
60 if (xs_startswith(v, "`")) {
61 xs *s1 = xs_crop(xs_dup(v), 1, -1);
62 xs *s2 = xs_fmt("<code>%s</code>", s1);
63 wrk = xs_str_cat(wrk, s2);
64 }
65 else
66 if (xs_startswith(v, "**")) {
67 xs *s1 = xs_crop(xs_dup(v), 2, -2);
68 xs *s2 = xs_fmt("<b>%s</b>", s1);
69 wrk = xs_str_cat(wrk, s2);
70 }
71 else
72 if (xs_startswith(v, "*")) {
73 xs *s1 = xs_crop(xs_dup(v), 1, -1);
74 xs *s2 = xs_fmt("<i>%s</i>", s1);
75 wrk = xs_str_cat(wrk, s2);
76 }
77 else
78 if (xs_startswith(v, "http")) {
79 xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v, v);
80 wrk = xs_str_cat(wrk, s1);
81 }
82 else
83 wrk = xs_str_cat(wrk, v);
84 } 56 }
85 else 57 else
86 /* surrounded text, copy directly */ 58 if (xs_startswith(v, "**")) {
87 wrk = xs_str_cat(wrk, v); 59 xs *s1 = xs_crop(xs_dup(v), 2, -2);
88 60 xs *s2 = xs_fmt("<b>%s</b>", s1);
89 n++; 61 s = xs_str_cat(s, s2);
62 }
63 else
64 if (xs_startswith(v, "*")) {
65 xs *s1 = xs_crop(xs_dup(v), 1, -1);
66 xs *s2 = xs_fmt("<i>%s</i>", s1);
67 s = xs_str_cat(s, s2);
68 }
69 else
70 if (xs_startswith(v, "http")) {
71 xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v, v);
72 s = xs_str_cat(s, s1);
73 }
74 else
75 s = xs_str_cat(s, v);
90 } 76 }
77 else
78 /* surrounded text, copy directly */
79 s = xs_str_cat(s, v);
80
81 n++;
91 } 82 }
92 83
93 /* now work by lines */ 84 return s;
94 p = list = xs_split(wrk, "\n"); 85}
86
87
88d_char *not_really_markdown(char *content)
89/* formats a content using some Markdown rules */
90{
91 d_char *s = xs_str_new(NULL);
92 int in_pre = 0;
93 int in_blq = 0;
94 xs *list;
95 char *p, *v;
95 96
96 s = xs_str_new(NULL); 97 /* work by lines */
98 p = list = xs_split(content, "\n");
97 99
98 while (xs_list_iter(&p, &v)) { 100 while (xs_list_iter(&p, &v)) {
99 xs *ss = xs_strip(xs_dup(v)); 101 xs *ss = NULL;
100 102
101 if (xs_startswith(ss, "@pre@")) { 103 if (strcmp(v, "```") == 0) {
102 if (!in_pre) 104 if (!in_pre)
103 s = xs_str_cat(s, "<pre>"); 105 s = xs_str_cat(s, "<pre>");
104 else 106 else
@@ -108,6 +110,11 @@ d_char *not_really_markdown(char *content)
108 continue; 110 continue;
109 } 111 }
110 112
113 if (in_pre)
114 ss = xs_dup(v);
115 else
116 ss = xs_strip(format_line(v));
117
111 if (xs_startswith(ss, ">")) { 118 if (xs_startswith(ss, ">")) {
112 /* delete the > and subsequent spaces */ 119 /* delete the > and subsequent spaces */
113 ss = xs_strip(xs_crop(ss, 1, 0)); 120 ss = xs_strip(xs_crop(ss, 1, 0));
@@ -138,6 +145,7 @@ d_char *not_really_markdown(char *content)
138 s = xs_str_cat(s, "</pre>"); 145 s = xs_str_cat(s, "</pre>");
139 146
140 /* some beauty fixes */ 147 /* some beauty fixes */
148 s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
141 s = xs_replace_i(s, "</blockquote><br>", "</blockquote>"); 149 s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
142 s = xs_replace_i(s, "</pre><br>", "</pre>"); 150 s = xs_replace_i(s, "</pre><br>", "</pre>");
143 151