ctxt: introduce an escape character to prevent unwanted markups
[ctxt.git] / html.c
blob34359776ab44e5737f26fbf87ff0632bd2bebeb2
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "ctxt.h"
6 static void html_doc_beg(struct doc *doc)
8 doc_write(doc, "<body>\n");
11 static void html_doc_end(struct doc *doc)
13 doc_write(doc, "\n</body>\n");
16 static int digits(int n)
18 int d = 1;
19 while ((n /= 10))
20 d++;
21 return d;
24 static char *putint(char *s, int n)
26 int i;
27 int d = digits(n);
28 for (i = 0; i < d; i++) {
29 s[d - i - 1] = n % 10 + '0';
30 n /= 10;
32 s[d] = '\0';
33 return s + d;
36 static char *putstr(char *dst, char *src)
38 strcpy(dst, src);
39 return strchr(dst, '\0');
42 static void html_head_beg(struct doc *doc, int level)
44 char buf[64];
45 char *s = buf;
46 s = putstr(s, "<h");
47 s = putint(s, level + 1);
48 putstr(s, ">");
49 doc_write(doc, buf);
52 static void html_head_end(struct doc *doc, int level)
54 char buf[64];
55 char *s = buf;
56 s = putstr(s, "</h");
57 s = putint(s, level + 1);
58 putstr(s, ">");
59 doc_write(doc, buf);
62 static void html_par_beg(struct doc *doc)
64 doc_write(doc, "<p>\n");
67 static void html_par_end(struct doc *doc)
71 static void html_put(struct doc *doc, char *s)
73 doc_write(doc, s);
76 static void html_list_beg(struct doc *doc, int mark)
78 doc_write(doc, "<ul>\n");
81 static void html_list_end(struct doc *doc)
83 doc_write(doc, "</ul>\n");
86 static void html_item_beg(struct doc *doc, char *head)
88 doc_write(doc, "<li>");
91 static void html_item_end(struct doc *doc)
93 doc_write(doc, "</li>\n");
96 static void html_block_beg(struct doc *doc, char *beg)
98 doc_write(doc, beg ? beg : "<pre>");
99 doc_write(doc, "\n");
102 static void html_block_end(struct doc *doc, char *end)
104 doc_write(doc, end ? end : "</pre>");
105 doc_write(doc, "\n");
108 static void html_put_link(struct doc *doc, char *d, char *s)
110 char desc[1 << 10];
111 char *link = desc;
112 strcpy(desc, s);
113 while ((link = strchr(link, ':'))) {
114 if (link[1] == ' ')
115 break;
116 link++;
118 if (link)
119 *link = '\0';
120 sprintf(d, "<a href=\"%s\">%s</a>", link ? link + 2 : s, desc);
123 static void html_put_txt(struct doc *doc, char *s, char *m)
125 char b[1024];
126 switch (m[0]) {
127 case '*':
128 sprintf(b, "<b>%s</b>", s);
129 break;
130 case '/':
131 sprintf(b, "<i>%s</i>", s);
132 break;
133 case '%':
134 strcpy(b, s);
135 break;
136 case '|':
137 html_put_link(doc, b, s);
138 break;
139 default:
140 sprintf(b, "%c%s%c", m[0], s, m[1]);
142 html_put(doc, b);
145 static void html_table_beg(struct doc *doc, int columns)
147 doc_write(doc, "<table>\n");
150 static void html_table_end(struct doc *doc)
152 doc_write(doc, "</table>\n");
155 static void html_row_beg(struct doc *doc)
157 doc_write(doc, "<tr>");
160 static void html_row_end(struct doc *doc)
162 doc_write(doc, "</tr>\n");
165 static void html_entry_beg(struct doc *doc)
167 doc_write(doc, "<td>");
170 static void html_entry_end(struct doc *doc)
172 doc_write(doc, "</td>");
175 struct fmt_ops html_ops = {
176 .doc_beg = html_doc_beg,
177 .doc_end = html_doc_end,
178 .head_beg = html_head_beg,
179 .head_end = html_head_end,
180 .par_beg = html_par_beg,
181 .par_end = html_par_end,
182 .list_beg = html_list_beg,
183 .list_end = html_list_end,
184 .item_beg = html_item_beg,
185 .item_end = html_item_end,
186 .table_beg = html_table_beg,
187 .table_end = html_table_end,
188 .row_beg = html_row_beg,
189 .row_end = html_row_end,
190 .entry_beg = html_entry_beg,
191 .entry_end = html_entry_end,
192 .block_beg = html_block_beg,
193 .block_end = html_block_end,
194 .put = html_put,
195 .put_txt = html_put_txt