move the tests out of tree
[ctxt.git] / html.c
blob90b9cb9d44321b0da8d429c9bcaa7aeaa935e259
1 #include <stdlib.h>
2 #include <string.h>
3 #include "ctxt.h"
5 static void html_doc_beg(struct doc *doc)
7 doc_write(doc, "<body>\n");
10 static void html_doc_end(struct doc *doc)
12 doc_write(doc, "\n</body>\n");
15 static int digits(int n)
17 int d = 1;
18 while ((n /= 10))
19 d++;
20 return d;
23 static char *putint(char *s, int n)
25 int i;
26 int d = digits(n);
27 for (i = 0; i < d; i++) {
28 s[d - i - 1] = n % 10 + '0';
29 n /= 10;
31 s[d] = '\0';
32 return s + d;
35 static char *putstr(char *dst, char *src)
37 strcpy(dst, src);
38 return strchr(dst, '\0');
41 static void html_head_beg(struct doc *doc, int level)
43 char buf[64];
44 char *s = buf;
45 s = putstr(s, "<h");
46 s = putint(s, level + 1);
47 putstr(s, ">");
48 doc_write(doc, buf);
51 static void html_head_end(struct doc *doc, int level)
53 char buf[64];
54 char *s = buf;
55 s = putstr(s, "</h");
56 s = putint(s, level + 1);
57 putstr(s, ">");
58 doc_write(doc, buf);
61 static void html_par_beg(struct doc *doc)
63 doc_write(doc, "<p>\n");
66 static void html_par_end(struct doc *doc)
70 static void html_put(struct doc *doc, char *s)
72 doc_write(doc, s);
75 static void html_list_beg(struct doc *doc)
77 doc_write(doc, "<ul>\n");
80 static void html_list_end(struct doc *doc)
82 doc_write(doc, "</ul>\n");
85 static void html_item_beg(struct doc *doc)
87 doc_write(doc, "<li>");
90 static void html_item_end(struct doc *doc)
92 doc_write(doc, "</li>\n");
95 static void html_block_beg(struct doc *doc, char *beg, int block)
97 switch (block) {
98 case B_PRE:
99 doc_write(doc, "<pre>");
100 break;
101 case B_MATH:
102 doc_write(doc, "<p><i>");
103 break;
104 case B_RAW:
105 break;
109 static void html_block_end(struct doc *doc, char *end, int block)
111 switch (block) {
112 case B_PRE:
113 doc_write(doc, "</pre>");
114 break;
115 case B_MATH:
116 doc_write(doc, "</i>\n");
117 break;
118 case B_RAW:
119 break;
123 static void html_put_link(struct doc *doc, char *s)
125 char desc[1 << 10];
126 char *link = desc;
127 strcpy(desc, s);
128 while ((link = strchr(desc, ':'))) {
129 if (link[1] == ' ')
130 break;
131 link++;
133 if (link)
134 *link = '\0';
135 doc_write(doc, "<a href=\"");
136 doc_write(doc, link ? link + 2 : s);
137 doc_write(doc, "\">");
138 doc_write(doc, desc);
139 doc_write(doc, "</a>");
142 static void html_put_txt(struct doc *doc, char *s, int marker)
144 switch (marker) {
145 case M_EMPH:
146 doc_write(doc, "<b>");
147 doc_write(doc, s);
148 doc_write(doc, "</b>");
149 break;
150 case M_RAW:
151 doc_write(doc, s);
152 break;
153 case M_LINK:
154 html_put_link(doc, s);
155 break;
156 case M_FOOT:
157 doc_write(doc, "[");
158 doc_write(doc, s);
159 doc_write(doc, "]");
160 break;
161 default:
162 html_put(doc, s);
167 static void html_table_beg(struct doc *doc, int columns)
169 doc_write(doc, "<table>\n");
172 static void html_table_end(struct doc *doc)
174 doc_write(doc, "</table>\n");
177 static void html_row_beg(struct doc *doc)
179 doc_write(doc, "<tr>");
182 static void html_row_end(struct doc *doc)
184 doc_write(doc, "</tr>\n");
187 static void html_entry_beg(struct doc *doc)
189 doc_write(doc, "<td>");
192 static void html_entry_end(struct doc *doc)
194 doc_write(doc, "</td>");
197 struct fmt_ops html_ops = {
198 .doc_beg = html_doc_beg,
199 .doc_end = html_doc_end,
200 .head_beg = html_head_beg,
201 .head_end = html_head_end,
202 .par_beg = html_par_beg,
203 .par_end = html_par_end,
204 .list_beg = html_list_beg,
205 .list_end = html_list_end,
206 .item_beg = html_item_beg,
207 .item_end = html_item_end,
208 .table_beg = html_table_beg,
209 .table_end = html_table_end,
210 .row_beg = html_row_beg,
211 .row_end = html_row_end,
212 .entry_beg = html_entry_beg,
213 .entry_end = html_entry_end,
214 .block_beg = html_block_beg,
215 .block_end = html_block_end,
216 .put = html_put,
217 .put_txt = html_put_txt