2 * google code wiki to html converter.
3 * https://code.google.com/p/support/wiki/WikiSyntax
32 return cistrncmp(pos, s, n) == 0;
45 look(char *s, char *e)
54 for(p = pos; p <= e; p++)
55 if(cistrncmp(p, s, n) == 0)
63 while(pos < epos && (*pos == ' ' || *pos == '\t'))
70 while(s > pos && strchr("\t ", s[-1]) != nil)
83 if(write(1, buf, n) != n)
84 sysfatal("write: %r");
89 output(char *s, int n)
95 r = HUNK - (opos - buf);
110 output(s, strlen(s));
118 for(p = pos; p < e; p++)
120 output(pos, p - pos);
123 } else if(*p == '>'){
124 output(pos, p - pos);
127 } else if(*p == '&'){
128 output(pos, p - pos);
132 output(pos, p - pos);
149 tag(char *term, char *tag)
155 if(e = look(term, nil)){
157 string("<"); string(tag); string(">");
159 string("</"); string(tag); string(">");
172 for(s = "======"; *s; s++)
178 e = look("=", look("\n", nil));
185 output("0123456"+n, 1);
186 string("><a name=\"");
190 if((*pos >= 'a' && *pos <= 'z')
191 || (*pos >= 'A' && *pos <= 'Z')
192 || (*pos >= '0' && *pos <= '9')
193 || (strchr("!#$%()_+,-./{|}~:;=?@[\\]^_`", *pos) != 0))
195 else if(*pos == ' ' || *pos == '\t')
205 else if(*pos == '\'')
215 output("0123456"+n, 1);
227 if(e != nil && s >= e)
229 if(*s == 0 || strchr("<>[] \t\r\n", *s) != nil)
234 if(cistrncmp(s-4, ".png", 4)
235 && cistrncmp(s-4, ".jpg", 4)
236 && cistrncmp(s-4, ".gif", 4)){
237 string("<a href=\"");
241 if(e != nil && pos < e)
249 string("<img src=\"");
265 if(got("\n") || got("\r\n"))
267 if(got("\n") || got("\r\n")){
269 while(got("\n") || got("\r\n"))
285 if(intable && look("||", look("\n", nil)) == nil){
291 if((indent < inlist) || (indent < inquote))
297 if(got("*") || got("#")){
310 string("</li></ul>");
312 string("</li></ol>");
317 } else if(indent > inquote){
318 string("<blockquote>");
323 string("</blockquote>");
330 if((pos = look("\n", nil)) == nil)
346 if(s = look("`", nil)){
354 if(s = look(">", nil)){
356 output(pos, s - pos);
361 if(s = look("]", nil)){
367 else if(tag("*", "b") ||
371 tag("~~", "strike")){
374 if(s = look("}}}", nil)){
389 if(s = look("||", look("\n", nil))){
392 case 0: string("<table>");
394 case 1: string("<tr>");
405 else if(match("http://"))
407 else if(match("https://"))
409 else if(match("ftp://"))
421 fprint(2, "usage: %s [ file ]\n", argv0);
426 main(int argc, char *argv[])
435 if(argc != 0 && argc != 1)
439 if((n = open(*argv, OREAD)) < 0)
440 sysfatal("open %s: %r", *argv);
445 buf = opos = sbrk(HUNK);
446 pos = epos = buf + HUNK;
448 if(brk(epos + HUNK + 8) < 0)
450 if((n = read(0, epos, HUNK)) < 0)
451 sysfatal("read: %r");
456 if(epos > pos && epos[-1] != '\n')