10 mkwtxt(int type, char *text)
14 w = emalloc(sizeof(*w));
17 setmalloctag(w, getcallerpc(&type));
22 * turn runs of whitespace into single spaces,
23 * eliminate whitespace at beginning and end.
26 strcondense(char *s, int cutbegin)
55 * turn runs of Wplain into single Wplain.
58 wcondense(Wpage *wtxt)
64 strcondense(w->text, 1);
66 if(w->type != Wplain || w->next==nil
67 || w->next->type != Wplain){
72 w->text = erealloc(w->text, strlen(w->text)+1+strlen(w->next->text)+1);
74 strcat(w->text, w->next->text);
85 * Parse a link, without the brackets.
93 for(q=s; *q && *q != '|'; q++)
97 w = mkwtxt(Wlink, estrdup(strcondense(s, 1)));
101 w = mkwtxt(Wlink, estrdup(strcondense(s, 1)));
102 w->url = estrdup(strcondense(q+1, 1));
104 setmalloctag(w, getcallerpc(&s));
109 * Parse Wplains, inserting Wlink nodes where appropriate.
117 for(w=wtxt; w; w=nw){
119 if(w->type != Wplain)
123 for(q=p; *q && *q != '['; q++)
127 for(r=q; *r && *r != ']'; r++)
134 w->text = estrdup(w->text);
135 w->next = mklink(q+1);
137 w->next = mkwtxt(Wplain, estrdup(r+1));
142 assert(w->next == nw);
150 return ('a' <= c && c <= 'z')
151 || ('A' <= c && c <= 'Z')
152 || ('0' <= c && c <= '9')
153 || c=='_' || c=='-' || c=='.' || c=='/'
154 || (c < 0); /* UTF */
158 findmanref(char *p, char **beginp, char **endp)
165 for(; q[0] && (q[0] != '(' || !isdigit(q[1]) || q[2] != ')'); q++)
169 for(r=q; r>p && ismanchar(r[-1]); r--)
176 w = mkwtxt(Wman, estrdup(r));
179 w->section = q[1]-'0';
181 setmalloctag(w, getcallerpc(&p));
188 * Parse Wplains, looking for man page references.
189 * This should be done by using a plumb(6)-style
190 * control file rather than hard-coding things here.
198 for(w=wtxt; w; w=nw){
200 if(w->type != Wplain)
203 if((mw = findmanref(w->text, &q, &r)) == nil)
208 w->next = mkwtxt(Wplain, estrdup(r));
212 assert(w->next == nw);
217 static int isheading(char *p) {
224 else if(islowerrune(r))
231 Brdpage(char *(*rdline)(void*,int), void *b)
240 while((p = rdline(b, '\n')) != nil){
242 p = strcondense(p, 1);
246 *pw = mkwtxt(Wpara, nil);
254 *pw = mkwtxt(Wbullet, nil);
256 *pw = mkwtxt(Wplain, estrdup(p+1));
260 *pw = mkwtxt(Wpre, estrdup(p[1]==' '?p+2:p+1));
264 for(c = p; *c != '\0'; c++) {
272 *pw = mkwtxt(Whr, nil);
279 *pw = mkwtxt(Wheading, estrdup(p));
283 *pw = mkwtxt(Wplain, estrdup(p));
289 werrstr("empty page");
295 setmalloctag(w, getcallerpc(&rdline));
309 print("heading '%s'\n", w->text);
315 print("link '%s' '%s'\n", w->text, w->url);
318 print("man %d %s\n", w->section, w->text);
321 print("plain '%s'\n", w->text);
327 print("pre '%s'\n", w->text);