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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
struct Tag {
char *search;
int hasChilds;
char *prefix, *suffix;
};
typedef unsigned int (*Parser)(const char *, const char *);
#define BUFFERSIZE 1024
#define ERRMALLOC eprint("malloc failed\n");
#define LENGTH(x) sizeof(x)/sizeof(x[0])
FILE *source;
unsigned int bsize = 0;
struct Tag lineprefix[] = {
{ " ", 0, "<pre>", "</pre>" },
{ "\t", 0, "<pre>", "</pre>" },
{ "> ", 1, "<blockquote>", "</blockquote>" },
};
struct Tag underline[] = {
{ "=", 1, "<h1>", "</h1>" },
{ "-", 1, "<h2>", "</h2>" },
};
struct Tag surround[] = {
{ "_", 1, "<b>", "</b>" },
{ "*", 1, "<i>", "</i>" },
};
char * replace[][2] = {
{ "\n---\n", "\n<hr />\n" },
{ "\n\n", "\n<br />\n" },
{ "<", "<" },
{ ">", ">" },
{ "&", "&" },
{ "\"", """ },
};
void eprint(const char *errstr, ...);
void process(const char *begin, const char *end, Parser parser);
unsigned int doreplace(const char *begin, const char *end);
unsigned int dolineprefix(const char *begin, const char *end);
unsigned int dosurround(const char *begin, const char *end);
unsigned int dounderline(const char *begin, const char *end);
unsigned int dolink(const char *begin, const char *end);
Parser parsers[] = { dounderline, dolineprefix, dosurround, dolink, doreplace };
void
eprint(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
unsigned int
dolink(const char *begin, const char *end) {
int img;
const char *desc, *link, *p;
unsigned int desclen, linklen;
if(*begin != '[' && strncmp(begin,") || p > end)
return 0;
desclen = p - desc;
link = p + 2;
if(!(p = strstr(link, ")")) || p > end)
return 0;
linklen = p - link;
if(img) {
fputs("<img src=\"",stdout);
process(link,link+linklen, doreplace);
fputs("\" alt=\"",stdout);
process(desc,desc+desclen, NULL);
fputs("\" />",stdout);
}
else {
fputs("<a href=\"",stdout);
process(link,link+linklen, doreplace);
fputs("\">",stdout);
process(desc,desc+desclen, NULL);
fputs("</a>",stdout);
}
return p + 1 - begin;
}
unsigned int
dosurround(const char *begin, const char *end) {
unsigned int i,l;
const char *p,*ps;
for(i = 0; i < LENGTH(surround); i++) {
l = strlen(surround[i].search);
if(end - begin < l || strncmp(begin,surround[i].search,l) != 0)
continue;
for(ps = surround[i].search; *ps == '\n'; ps++);
l = strlen(ps);
p = strstr(begin + strlen(surround[i].search), ps);
if(!p || p > end)
continue;
fputs(surround[i].prefix,stdout);
fwrite(begin + strlen(surround[i].search), sizeof(char), p - begin - l, stdout);
fputs(surround[i].suffix,stdout);
return p - begin + l;
}
return 0;
}
unsigned int
doreplace(const char *begin, const char *end) {
unsigned int i, l;
for(i = 0; i < LENGTH(replace); i++) {
l = strlen(replace[i][0]);
if(end - begin < l)
continue;
if(strncmp(replace[i][0],begin,l) == 0) {
fputs(replace[i][1], stdout);
return l;
}
}
return 0;
}
unsigned int
dolineprefix(const char *begin, const char *end) {
unsigned int i, j, l;
char *buffer;
const char *p;
if(*begin != '\n' && *begin != '\0')
return 0;
for(i = 0; i < LENGTH(lineprefix); i++) {
l = strlen(lineprefix[i].search);
if(end - begin+1 < l)
continue;
if(strncmp(lineprefix[i].search,begin+1,l))
continue;
if(!(buffer = malloc(end - begin+1)))
ERRMALLOC;
fputs(lineprefix[i].prefix, stdout);
for(p = begin, j = 0; p != end; p++, j++) {
buffer[j] = *p;
if(*p == '\n') {
if(strncmp(lineprefix[i].search,p+1,l) != 0)
break;
p += l;
}
}
if(lineprefix[i].hasChilds)
process(buffer,buffer+strlen(buffer), NULL);
else
fputs(buffer, stdout);
fputs(lineprefix[i].suffix, stdout);
free(buffer);
return p - begin;
}
return 0;
}
unsigned int
dounderline(const char *begin, const char *end) {
unsigned int i, j, l;
const char *p;
if(*begin != '\n' && *begin != '\0')
return 0;
for(p = begin+1,l = 0; p[l] != '\n' && p[l] && p+l != end; l++);
p += l+1;
if(l == 0)
return 0;
for(i = 0; i < LENGTH(underline); i++) {
for(j = 0; p[j] != '\n' && p[j] == underline[i].search[0] && p+j != end; j++);
if(j >= l) {
putchar('\n');
fputs(underline[i].prefix,stdout);
if(underline[i].hasChilds)
process(begin + 1, begin + l + 1, NULL);
else
fwrite(begin + 1, l, sizeof(char), stdout);
fputs(underline[i].suffix,stdout);
return j + l + 2;
}
}
return 0;
}
void
process(const char *begin, const char *end, Parser parser) {
const char *p;
int affected;
unsigned int i;
for(p = begin; *p && p != end;) {
affected = 0;
if(parser)
affected = parser(p, end);
else
for(i = 0; i < LENGTH(parsers) && affected == 0; i++)
affected = parsers[i](p, end);
if(affected == 0) {
putchar(*p);
p++;
}
else
p += affected;
}
}
int
main(int argc, char *argv[]) {
char *p, *buffer;
int s;
source = stdin;
if(argc > 1 && strcmp("-v", argv[1]) == 0)
eprint("markdown in C "VERSION" (C) Enno Boland\n");
else if(argc > 1 && strcmp("-h", argv[1]) == 0)
eprint("Usage markdown [file]\n");
else if (argc > 1 && strcmp("-", argv[1]) != 0 && !(source = fopen(argv[1],"r")))
eprint("Cannot open file `%s`\n",argv[1]);
if(!(buffer = malloc(BUFFERSIZE)))
ERRMALLOC;
bsize = BUFFERSIZE;
strcpy(buffer,"\n");
p = buffer+strlen(buffer);
while(s = fread(p, sizeof(char),BUFFERSIZE, source)) {
p += s;
*p = '\0';
if(BUFFERSIZE + strlen(buffer) > bsize) {
bsize += BUFFERSIZE;
if(!(buffer = realloc(buffer, bsize)))
ERRMALLOC;
}
}
process(buffer,buffer+strlen(buffer), NULL);
free(buffer);
}
|