10 typedef struct Fontdata Fontdata;
16 "lucidasans/unicode.7", 0, 0,
17 "lucidasans/unicode.8", 0, 0,
18 "lucidasans/unicode.10", 0, 0,
19 "lucidasans/unicode.13", 0, 0,
21 "lucidasans/italicunicode.7", 0, 0,
22 "lucidasans/italicunicode.8", 0, 0,
23 "lucidasans/italicunicode.10", 0, 0,
24 "lucidasans/italicunicode.13", 0, 0,
26 "lucidasans/boldunicode.7", 0, 0,
27 "lucidasans/boldunicode.8", 0, 0,
28 "lucidasans/boldunicode.10", 0, 0,
29 "lucidasans/boldunicode.13", 0, 0,
31 "lucidasans/typeunicode.7", 0, 0,
32 "pelm/unicode.8", 0, 0,
33 "lucidasans/typeunicode.12", 0, 0,
34 "lucidasans/typeunicode.16", 0, 0,
36 Fontdata *pl_whichfont(int f, int s){
39 assert(f >= 0 && f < 4);
40 assert(s >= 0 && s < 4);
42 if(fontlist[f][s].font==0){
43 snprint(name, sizeof(name), "/lib/font/bit/%s.font", fontlist[f][s].name);
44 fontlist[f][s].font=openfont(display, name);
45 if(fontlist[f][s].font==0) fontlist[f][s].font=font;
46 fontlist[f][s].space=stringwidth(fontlist[f][s].font, "0");
48 return &fontlist[f][s];
57 void pl_pushstate(Hglob *g, int t){
59 if(g->state==&g->stack[NSTACK]){
60 htmlerror(g->name, g->lineno, "stack overflow at <%s>", tag[t].name);
63 g->state[0]=g->state[-1];
66 void pl_linespace(Hglob *g){
67 plrtbitmap(&g->dst->text, 1000000, 0, linespace, 0, 0);
75 int strtolength(Hglob *g, int dir, char *str)
80 if(cistrstr(str, "px"))
82 if(cistrstr(str, "%"))
83 return floor(f*((dir==HORIZ) ? Dx(g->dst->text->r) : Dy(g->dst->text->r))/100);
84 if(cistrstr(str, "em")){
86 z = stringsize(g->dst->text->font, "M");
87 return floor(f*((dir==HORIZ) ? z.x : z.y));
92 void pl_htmloutput(Hglob *g, int nsp, char *s, Field *field){
96 if(g->state->tag==Tag_title
97 /* || g->state->tag==Tag_textarea */
98 || g->state->tag==Tag_select){
100 if(g->tp!=g->text && g->tp!=g->etext && g->tp[-1]!=' ')
102 while(g->tp!=g->etext && *s) *g->tp++=*s++;
103 if(g->state->tag==Tag_title) g->dst->changed=1;
108 f=pl_whichfont(g->state->font, g->state->size);
110 indent=g->state->margin;
113 indent+=g->state->indent;
119 if(g->state->image[0]==0 && g->state->link[0]==0 && g->state->name[0]==0 && field==0)
122 ap=mallocz(sizeof(Action), 1);
124 if(g->state->image[0])
125 ap->image = strdup(g->state->image);
126 if(g->state->link[0])
127 ap->link = strdup(g->state->link);
128 if(g->state->name[0])
129 ap->name = strdup(g->state->name);
130 ap->ismap=g->state->ismap;
131 ap->width=g->state->width;
132 ap->height=g->state->height;
137 if(indent<0) indent=0;
138 if(g->state->pre && s[0]=='\t'){
150 plrtstr(&g->dst->text, space, indent, f->font, strdup(s),
151 g->state->link[0] || g->state->image[0], ap);
158 * Buffered read, no translation
161 int pl_bread(Hglob *g){
164 if(g->hbufp==g->ehbuf){
165 n=read(g->hfd, g->hbuf, NHBUF);
168 snprint(err, sizeof(err), "%r reading %s", g->name);
169 pl_htmloutput(g, 1, err, 0);
178 if(c=='\n') g->lineno++;
182 * Read a character, translating \r\n, \n\r, \r and \n into \n
184 int pl_readc(Hglob *g){
205 void pl_putback(Hglob *g, int c){
206 if(g->npeekc==NPEEKC) htmlerror(g->name, g->lineno, "too much putback!");
207 else if(c!=EOF) g->peekc[g->npeekc++]=c;
209 int pl_nextc(Hglob *g){
213 char crune[UTFmax+1];
214 if(g->heof) return EOF;
215 if(g->npeekc!=0) return g->peekc[--g->npeekc];
223 if('a'<=c && c<='z' || 'A'<=c && c<='Z') return STAG;
227 if(c=='!' || 'a'<=c && c<='z' || 'A'<=c && c<='Z' || c=='?') return STAG;
230 if(c=='>') return ETAG;
232 for (n=1; n<=sizeof(crune); n++){
234 if(fullrune(crune, n)){
235 chartorune(&r, crune);
245 return c=='#' || 'a'<=c && c<='z' || 'A'<=c && c<='Z' || '0'<=c && c<='9';
248 * remove entity references, in place.
250 * This doesn't work if removing an entity reference can lengthen the string!
251 * Fortunately, this doesn't happen.
253 void pl_rmentities(Hglob *g, char *s){
261 && ((*s=='#' && strchr("0123456789Xx", s[1]))
262 || 'a'<=*s && *s<='z'
263 || 'A'<=*s && *s<='Z')){
265 while(entchar(*s)) s++;
269 if(strcmp(u, "lt") == 0)
271 else if(strcmp(u, "gt") == 0)
273 else if(strcmp(u, "quot") == 0)
275 else if(strcmp(u, "apos") == 0)
277 else if(strcmp(u, "amp") == 0)
291 * Skip over white space
293 char *pl_white(char *s){
294 while(*s==' ' || *s=='\t' || *s=='\n' || *s=='\r') s++;
298 * Skip over HTML word
300 char *pl_word(char *s){
301 if ('a'<=*s && *s<='z' || 'A'<=*s && *s<='Z') {
303 while('a'<=*s && *s<='z' || 'A'<=*s && *s<='Z' || '0'<=*s && *s<='9' || *s=='-' || *s=='.') s++;
308 * Skip to matching quote
310 char *pl_quote(char *s){
313 while(*s!=q && *s!='\0') s++;
316 void pl_dnl(char *s){
318 for(t=s;*s;s++) if(*s!='\r' && *s!='\n') *t++=*s;
321 void pl_tagparse(Hglob *g, char *str){
322 char *s, *t, *name, c;
327 if(str[0]=='!'){ /* test should be strncmp(str, "!--", 3)==0 */
332 if(str[0]=='/') str++;
335 if(*s!=' ' && *s!='\n' && *s!='\t' && *s!='\0'){
336 htmlerror(g->name, g->lineno, "bad tag name in %s", str);
340 if(*s!='\0') *s++='\0';
341 for(t=name;t!=s;t++) if('A'<=*t && *t<='Z') *t+='a'-'A';
343 * Binary search would be faster here
345 for(tagp=tag;tagp->name;tagp++) if(strcmp(name, tagp->name)==0) break;
347 if(g->tag==Tag_end) htmlerror(g->name, g->lineno, "no tag %s", name);
359 for(s=ap->name;*s;s++) if('A'<=*s && *s<='Z') *s+='a'-'A';
362 if(*s=='\'' || *s=='"'){
366 htmlerror(g->name, g->lineno,
367 "No terminating quote in rhs of attribute %s",
376 /* read up to white space or > */
378 while(*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0') s++;
379 if(*s!='\0') *s++='\0';
381 pl_rmentities(g, ap->value);
387 if(ap==&g->attr[NATTR-1])
388 htmlerror(g->name, g->lineno, "too many attributes!");
392 int pl_getcomment(Hglob *g){
394 if((c=pl_nextc(g))=='-' && (c=pl_nextc(g))=='-'){
395 /* <!-- eats everything until --> or EOF */
397 while((c=pl_nextc(g))!='-' && c!=EOF)
401 if((c=pl_nextc(g))=='-'){
402 while((c=pl_nextc(g))=='-')
404 if(c==ETAG || c==EOF)
409 /* <! eats everything until > or EOF */
410 while(c!=ETAG && c!=EOF)
414 htmlerror(g->name, g->lineno, "EOF in comment");
420 int lrunetochar(char *p, int v)
425 return runetochar(p, &r);
429 * Read a start or end tag -- the caller has read the initial <
431 int pl_gettag(Hglob *g){
435 if((c=pl_nextc(g))=='!' || c=='?')
436 return pl_getcomment(g);
438 while((c=pl_nextc(g))!=ETAG && c!=EOF)
439 if(tokp < &g->token[NTOKEN-UTFmax-1]) tokp += lrunetochar(tokp, c);
441 if(c==EOF) htmlerror(g->name, g->lineno, "EOF in tag");
442 pl_tagparse(g, g->token);
443 if(g->token[0]!='/') return TAG;
444 if(g->attr[0].name!=0)
445 htmlerror(g->name, g->lineno, "end tag should not have attributes");
449 * The next token is a tag, an end tag or a sequence of
450 * non-white characters.
451 * If inside <pre>, newlines are converted to <br> and spaces are preserved.
452 * Otherwise, spaces and newlines are noted and discarded.
454 int pl_gettoken(Hglob *g){
457 if(g->state->pre) switch(c=pl_nextc(g)){
458 case STAG: return pl_gettag(g);
459 case EOF: return EOF;
461 pl_tagparse(g, "br");
466 if(tokp < &g->token[NTOKEN-UTFmax-1]) tokp += lrunetochar(tokp, c);
469 while(c!='\t' && c!='\n' && c!=STAG && c!=EOF){
471 if(tokp < &g->token[NTOKEN-UTFmax-1]) tokp += lrunetochar(tokp, c);
475 pl_rmentities(g, g->token);
481 while((c=pl_nextc(g))==' ' || c=='\t' || c=='\n')
485 case STAG: return pl_gettag(g);
486 case EOF: return EOF;
491 if(tokp < &g->token[NTOKEN-UTFmax-1]) tokp += lrunetochar(tokp, c);
493 }while(c!=' ' && c!='\t' && c!='\n' && c!=STAG && c!=EOF);
495 pl_rmentities(g, g->token);
502 char *pl_getattr(Pair *attr, char *name){
503 for(;attr->name;attr++)
504 if(strcmp(attr->name, name)==0)
508 int pl_hasattr(Pair *attr, char *name){
509 for(;attr->name;attr++)
510 if(strcmp(attr->name, name)==0)
514 void plaintext(Hglob *g){
518 g->state->font=CWIDTH;
519 g->state->size=NORMAL;
520 elp=&line[NLINE-UTFmax-1];
525 if(c=='\n' || lp>=elp){
528 pl_htmloutput(g, 0, line, 0);
532 do *lp++=' '; while(lp<elp && utfnlen(line, lp-line)%8!=0);
535 lp += lrunetochar(lp, c);
540 pl_htmloutput(g, 0, line, 0);
543 void plrdplain(char *name, int fd, Www *dst){
546 g.state->tag=Tag_html;
547 g.state->font=CWIDTH;
548 g.state->size=NORMAL;
560 g.ehbuf=g.hbufp=g.hbuf;
568 g.etext=g.text+NTITLE-1;
571 strncpy(g.text, name, NTITLE);
575 void plrdhtml(char *name, int fd, Www *dst){
584 g.state->tag=Tag_html;
586 g.state->size=NORMAL;
600 g.ehbuf=g.hbufp=g.hbuf;
608 g.etext=g.text+NTITLE-1;
613 for(;;) switch(pl_gettoken(&g)){
615 switch(tag[g.tag].action){
617 for(sp=g.state;sp!=g.stack && sp->tag!=g.tag;--sp);
619 pl_pushstate(&g, g.tag);
621 for(;g.state!=sp;--g.state)
622 if(tag[g.state->tag].action!=OPTEND)
623 htmlerror(g.name, g.lineno,
624 "end tag </%s> missing",
625 tag[g.state->tag].name);
628 pl_pushstate(&g, g.tag);
631 if(str=pl_getattr(g.attr, "id")){
634 strncpy(swap, g.state->name, sizeof(swap));
635 strncpy(g.state->name, str, sizeof(g.state->name));
636 pl_htmloutput(&g, 0, "", 0);
637 strncpy(g.state->name, swap, sizeof(g.state->name));
641 htmlerror(g.name, g.lineno,
642 "unimplemented tag <%s>", tag[g.tag].name);
644 case Tag_end: /* unrecognized start tag */
649 if(str=pl_getattr(g.attr, "src"))
650 strncpy(g.state->image, str, sizeof(g.state->image));
651 g.state->ismap=pl_hasattr(g.attr, "ismap");
652 if(str=pl_getattr(g.attr, "width"))
653 g.state->width = strtolength(&g, HORIZ, str);
654 if(str=pl_getattr(g.attr, "height"))
655 g.state->height = strtolength(&g, VERT, str);
656 str=pl_getattr(g.attr, "alt");
658 if(g.state->image[0])
663 pl_htmloutput(&g, 0, str, 0);
682 if(g.state->table==1){
692 if(str=pl_getattr(g.attr, "href"))
693 strncpy(g.state->link, str, sizeof(g.state->link));
694 if(str=pl_getattr(g.attr, "name")){
695 strncpy(g.state->name, str, sizeof(g.state->name));
696 pl_htmloutput(&g, 0, "", 0);
700 pl_htmloutput(&g, 0, "FRAME: ", 0);
701 if(str=pl_getattr(g.attr, "src"))
702 strncpy(g.state->link, str, sizeof(g.state->link));
703 if(str=pl_getattr(g.attr, "name"))
704 strncpy(g.state->name, str, sizeof(g.state->name));
707 pl_htmloutput(&g, 0, str, 0);
717 g.state->size=NORMAL;
735 g.state->size=NORMAL;
748 g.state->font=ITALIC;
749 g.state->size=NORMAL;
752 g.state->font=CWIDTH;
753 g.state->size=NORMAL;
762 htmlerror(g.name, g.lineno, "<dfn> deprecated");
764 g.state->size=NORMAL;
768 g.state->size=NORMAL;
782 htmlerror(g.name, g.lineno, "<u> deprecated");
786 g.state->font=ITALIC;
791 g.state->size=ENORMOUS;
792 g.state->margin+=100;
798 g.state->size=ENORMOUS;
804 g.state->font=ITALIC;
805 g.state->size=ENORMOUS;
818 g.state->font=ITALIC;
831 plrtbitmap(&g.dst->text, 1000000, g.state->margin, hrule, 0, 0);
834 htmlerror(g.name, g.lineno, "<key> deprecated");
836 g.state->font=CWIDTH;
850 switch(g.state->tag){
852 htmlerror(g.name, g.lineno, "can't have <li> in <%s>",
853 tag[g.state->tag].name);
854 case Tag_dir: /* supposed to be multi-columns, can't do! */
860 snprint(buf, sizeof(buf), "%2d ", ++g.state->number);
861 pl_htmloutput(&g, 0, buf, 0);
867 plrtbitmap(&g.dst->text, 100000,
868 g.state->margin+g.state->indent, bullet, 0, 0);
879 htmlerror(g.name, g.lineno, "<%s> deprecated", tag[g.tag].name);
884 g.state->font=CWIDTH;
885 g.state->size=NORMAL;
889 g.state->font=CWIDTH;
890 g.state->size=NORMAL;
893 g.text=dst->title+strlen(dst->title);
895 g.etext=dst->title+NTITLE-1;
909 * ignore the content of these tags, eat tokens until we
910 * reach a matching endtag.
914 switch(pl_gettoken(&g)){
931 * If the end tag doesn't match the top, we try to uncover a match
934 if(g.state->tag!=g.tag){
936 for(sp=g.state;sp!=g.stack;--sp){
939 if(tag[g.state->tag].action!=OPTEND) tagerr++;
943 htmlerror(g.name, g.lineno,
944 "end tag mismatch <%s>...</%s>, ignored",
945 tag[g.state->tag].name, tag[g.tag].name);
949 htmlerror(g.name, g.lineno,
950 "end tag mismatch <%s>...</%s>, "
951 "intervening tags popped",
952 tag[g.state->tag].name, tag[g.tag].name);
956 else if(g.state==g.stack)
957 htmlerror(g.name, g.lineno, "end tag </%s> at stack bottom",
991 if(g.state->table==0)
1000 pl_htmloutput(&g, g.nsp, g.token, 0);
1003 for(;g.state!=g.stack;--g.state)
1004 if(tag[g.state->tag].action!=OPTEND)
1005 htmlerror(g.name, g.lineno,
1006 "missing </%s> at EOF", tag[g.state->tag].name);
1009 getpix(dst->text, dst);