void emitword(Bytes *b, Rune *r, int nr) { char *s; int space; if(nr == 0) return; s = smprint("%.*S", nr, r); space = b->n > 0 && !isspace(b->b[b->n-1]) && (!newitextitem || !closingpunct(*s)); if(col > 0 && col+space+nr > width){ growbytes(b, "\n", 1); space = 0; col = 0; } if(space && col > 0){ growbytes(b, " ", 1); col++; } growbytes(b, s, strlen(s)); col += nr; free(s); inword = 0; newitextitem = 0; }
void renderrunes(Bytes *b, Rune *r) { int i, n; n = runestrlen(r); for(i=0; i<n; i++){ switch(r[i]){ case '\n': if(inword) emitword(b, r+wordi, i-wordi); col = 0; if(b->n == 0) break; /* don't start with blank lines */ if(b->n<2 || b->b[b->n-1]!='\n' || b->b[b->n-2]!='\n') growbytes(b, "\n", 1); break; case ' ': if(inword) emitword(b, r+wordi, i-wordi); break; default: if(!inword) wordi = i; inword = 1; break; } } if(inword) emitword(b, r+wordi, i-wordi); }
char* loadhtml(int fd) { URLwin *u; Bytes *b; int n; char buf[4096]; u = emalloc(sizeof(URLwin)); u->infd = fd; u->outfd = 1; u->url = estrdup(url); u->type = TextHtml; b = emalloc(sizeof(Bytes)); while((n = read(fd, buf, sizeof buf)) > 0) growbytes(b, buf, n); if(b->b == nil) return nil; /* empty file */ rendertext(u, b); freeurlwin(u); return nil; }