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')
205 output("0123456"+n, 1);
217 if(e != nil && s >= e)
219 if(*s == 0 || strchr("<>[] \t\r\n", *s) != nil)
224 if(cistrncmp(s-4, ".png", 4)
225 && cistrncmp(s-4, ".jpg", 4)
226 && cistrncmp(s-4, ".gif", 4)){
227 string("<a href=\"");
231 if(e != nil && pos < e)
239 string("<img src=\"");
255 if(got("\n") || got("\r\n"))
257 if(got("\n") || got("\r\n")){
259 while(got("\n") || got("\r\n"))
275 if(intable && look("||", look("\n", nil)) == nil){
281 if((indent < inlist) || (indent < inquote))
287 if(got("*") || got("#")){
300 string("</li></ul>");
302 string("</li></ol>");
307 } else if(indent > inquote){
308 string("<blockquote>");
313 string("</blockquote>");
320 if((pos = look("\n", nil)) == nil)
336 if(s = look("`", nil)){
344 if(s = look(">", nil)){
346 output(pos, s - pos);
351 if(s = look("]", nil)){
357 else if(tag("*", "b") ||
361 tag("~~", "strike")){
364 if(s = look("}}}", nil)){
379 if(s = look("||", look("\n", nil))){
382 case 0: string("<table>");
384 case 1: string("<tr>");
395 else if(match("http://"))
397 else if(match("https://"))
399 else if(match("ftp://"))
411 fprint(2, "usage: %s [ file ]\n", argv0);
416 main(int argc, char *argv[])
425 if(argc != 0 && argc != 1)
429 if((n = open(*argv, OREAD)) < 0)
430 sysfatal("open %s: %r", *argv);
435 buf = opos = sbrk(HUNK);
436 pos = epos = buf + HUNK;
438 if(brk(epos + HUNK + 8) < 0)
440 if((n = read(0, epos, HUNK)) < 0)
441 sysfatal("read: %r");
446 if(epos > pos && epos[-1] != '\n')