major cleanup and improved block handling
[ctxt.git] / latex.c
blob1749468376ac03a85469dda907dec59dd92e671d
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "ctxt.h"
6 static void latex_doc_beg(struct doc *doc)
8 doc_write(doc, "\\begin{document}\n");
11 static void latex_doc_end(struct doc *doc)
13 doc_write(doc, "\\end{document}\n");
16 static void latex_head_beg(struct doc *doc, int level)
18 switch (level) {
19 case 0:
20 doc_write(doc, "\\section*{");
21 break;
22 case 1:
23 doc_write(doc, "\\subsection*{");
24 break;
25 default:
26 doc_write(doc, "\\subsection*{");
30 static void latex_head_end(struct doc *doc, int level)
32 doc_write(doc, "}\n");
35 static void latex_par_beg(struct doc *doc)
39 static void latex_par_end(struct doc *doc)
43 static void latex_put(struct doc *doc, char *s)
45 doc_write(doc, s);
48 static void latex_list_beg(struct doc *doc)
50 doc_write(doc, "\\begin{itemize}\n");
53 static void latex_list_end(struct doc *doc)
55 doc_write(doc, "\\end{itemize}\n");
58 static void latex_item_beg(struct doc *doc)
60 doc_write(doc, "\\item ");
63 static void latex_item_end(struct doc *doc)
65 doc_write(doc, "");
68 static void latex_block_beg(struct doc *doc, char *beg)
70 doc_write(doc, beg ? beg : "\\begin{verbatim}");
71 doc_write(doc, "\n");
74 static void latex_block_end(struct doc *doc, char *end)
76 doc_write(doc, end ? end : "\\end{verbatim}");
77 doc_write(doc, "\n");
80 static void latex_put_txt(struct doc *doc, char *s, char *m)
82 char b[1024];
83 switch(m[0]) {
84 case '*':
85 sprintf(b, "\\textbf{%s}", s);
86 break;
87 case '/':
88 sprintf(b, "\\emph{%s}", s);
89 break;
90 case '%':
91 strcpy(b, s);
92 break;
93 case '|':
94 sprintf(b, "\\cite{%s}", s);
95 break;
96 case '[':
97 sprintf(b, "\\footnote{%s}", s);
98 break;
99 default:
100 sprintf(b, "%c%s%c", m[0], s, m[1]);
102 latex_put(doc, b);
105 static void latex_table_beg(struct doc *doc, int columns)
107 int i;
108 doc_write(doc, "\\begin{tabular}");
109 if (columns) {
110 doc_write(doc, "{|");
111 for (i = 0; i < columns; i++)
112 doc_write(doc, "l|");
113 doc_write(doc, "}\n");
117 static void latex_table_end(struct doc *doc)
119 doc_write(doc, "\\hline\n");
120 doc_write(doc, "\\end{tabular}\n");
123 /* a hack to identify the first entry in each row */
124 static int entcol;
126 static void latex_row_beg(struct doc *doc)
128 entcol = 0;
129 doc_write(doc, "\\hline\n");
132 static void latex_row_end(struct doc *doc)
134 doc_write(doc, "\t\\\\\n");
137 static void latex_entry_beg(struct doc *doc)
139 if (entcol++)
140 doc_write(doc, "\t& ");
143 static void latex_entry_end(struct doc *doc)
147 struct fmt_ops latex_ops = {
148 .doc_beg = latex_doc_beg,
149 .doc_end = latex_doc_end,
150 .head_beg = latex_head_beg,
151 .head_end = latex_head_end,
152 .par_beg = latex_par_beg,
153 .par_end = latex_par_end,
154 .list_beg = latex_list_beg,
155 .list_end = latex_list_end,
156 .item_beg = latex_item_beg,
157 .item_end = latex_item_end,
158 .table_beg = latex_table_beg,
159 .table_end = latex_table_end,
160 .row_beg = latex_row_beg,
161 .row_end = latex_row_end,
162 .entry_beg = latex_entry_beg,
163 .entry_end = latex_entry_end,
164 .block_beg = latex_block_beg,
165 .block_end = latex_block_end,
166 .put = latex_put,
167 .put_txt = latex_put_txt