-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd2latex.c
210 lines (203 loc) · 4.04 KB
/
md2latex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include "md4c/src/md4c.h"
#define print(str) write(1, (str), strlen((str)))
#define printl(str, limit) write(1, (str), (limit))
int enter_block(MD_BLOCKTYPE t, void *details, void *userdata) {
switch (t) {
case MD_BLOCK_DOC:
print("\\begin{document}\n");
break;
case MD_BLOCK_QUOTE:
print("\\begin{displayquote}\n");
break;
case MD_BLOCK_CODE:
print("\\begin{verbatim}\n");
break;
case MD_BLOCK_UL:
print("\\begin{itemize}\n");
break;
case MD_BLOCK_OL:
print("\\begin{enumerate}\n");
break;
case MD_BLOCK_LI:
print("\\item\t");
break;
case MD_BLOCK_HR:
print("\\hrulefill\n");
break;
case MD_BLOCK_H:
switch (((MD_BLOCK_H_DETAIL *)details)->level) {
case 1:
print("\\part*{");
break;
case 2:
print("\\section{");
break;
case 3:
print("\\subsection{");
break;
case 4:
print("\\subsubsection{");
break;
case 5:
print("\\paragraph{");
break;
case 6:
default:
print("\\subparagraph{");
}
break;
case MD_BLOCK_P:
print("\n");
break;
default:
print("");
}
return 0;
}
int leave_block(MD_BLOCKTYPE t, void *details, void *userdata) {
switch (t) {
case MD_BLOCK_DOC:
print("\\end{document}\n");
break;
case MD_BLOCK_QUOTE:
print("\\end{displayquote}\n");
break;
case MD_BLOCK_CODE:
print("\\end{verbatim}\n");
break;
case MD_BLOCK_UL:
print("\\end{itemize}\n");
break;
case MD_BLOCK_OL:
print("\\end{enumerate}\n");
break;
case MD_BLOCK_LI:
print("\n");
break;
case MD_BLOCK_HR:
break;
case MD_BLOCK_H:
print("}");
break;
case MD_BLOCK_P:
print("\n");
break;
default:
print("");
}
return 0;
}
int enter_span(MD_SPANTYPE t, void *details, void *userdata) {
switch (t) {
case MD_SPAN_EM:
print("\\emph{");
break;
case MD_SPAN_STRONG:
print("\\textbf{");
break;
case MD_SPAN_A:
print("\\href{");
struct MD_SPAN_A_DETAIL *d_a = (struct MD_SPAN_A_DETAIL *)details;
printl(d_a->href.text, d_a->href.size);
print("}{");
break;
case MD_SPAN_IMG:
print("\\begin{figure}[h]\n");
print("\\includegraphics[width=\\linewidth]{");
struct MD_SPAN_IMG_DETAIL *d_img = (struct MD_SPAN_IMG_DETAIL *)details;
printl(d_img->src.text, d_img->src.size);
print("}\n");
break;
case MD_SPAN_CODE:
print("\\verb!");
break;
case MD_SPAN_DEL:
print("");
break;
case MD_SPAN_LATEXMATH:
case MD_SPAN_LATEXMATH_DISPLAY:
print("$");
break;
case MD_SPAN_WIKILINK:
print("");
break;
case MD_SPAN_U:
print("\\underline{");
break;
}
return 0;
}
int leave_span(MD_SPANTYPE t, void *details, void *userdata) {
switch (t) {
case MD_SPAN_CODE:
print("!");
break;
case MD_SPAN_LATEXMATH:
case MD_SPAN_LATEXMATH_DISPLAY:
print("$");
break;
case MD_SPAN_IMG:
print("\n\\end{figure}\n");
break;
default:
print("}");
}
return 0;
}
static inline int text(MD_TEXTTYPE t, const MD_CHAR *str, MD_SIZE size, void *userdata) {
switch (t) {
case MD_TEXT_NORMAL:
printl(str, size);
break;
case MD_TEXT_NULLCHAR:
print("<?>");
break;
case MD_TEXT_BR:
printl(str, size);
break;
case MD_TEXT_ENTITY:
printl(str, size);
break;
case MD_TEXT_CODE:
printl(str, size);
break;
case MD_TEXT_HTML:
break;
case MD_TEXT_LATEXMATH:
printl(str, size);
break;
default:
break;
}
return 0;
}
int main(int argc, char **argv) {
int fd = argc > 1 ? open(argv[1], O_RDONLY) : -1;
if (fd == -1)
return 1;
int len = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
MD_PARSER p = {
0,
0x1060,
enter_block,
leave_block,
enter_span,
leave_span,
text,
NULL,
NULL};
char *input = malloc(sizeof(char) * len);
read(fd, input, len);
close(fd);
print("\\documentclass{article}\n");
print("\\usepackage{graphicx}\n");
print("\\usepackage{hyperref}\n");
// print("\\usepackage[normalem]{ulem}\n");
md_parse(input, strlen(input), &p, NULL);
free(input);
}