tohtml.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <String.h>
  5. #include <thread.h>
  6. #include "wiki.h"
  7. /*
  8. * Get HTML and text templates from underlying file system.
  9. * Caches them, which means changes don't take effect for
  10. * up to Tcache seconds after they are made.
  11. *
  12. * If the files are deleted, we keep returning the last
  13. * known copy.
  14. */
  15. enum {
  16. WAIT = 60
  17. };
  18. static char *name[2*Ntemplate] = {
  19. [Tpage] "page.html",
  20. [Tedit] "edit.html",
  21. [Tdiff] "diff.html",
  22. [Thistory] "history.html",
  23. [Tnew] "new.html",
  24. [Toldpage] "oldpage.html",
  25. [Twerror] "werror.html",
  26. [Ntemplate+Tpage] "page.txt",
  27. [Ntemplate+Tdiff] "diff.txt",
  28. [Ntemplate+Thistory] "history.txt",
  29. [Ntemplate+Toldpage] "oldpage.txt",
  30. [Ntemplate+Twerror] "werror.txt",
  31. };
  32. static struct {
  33. RWLock;
  34. String *s;
  35. ulong t;
  36. Qid qid;
  37. } cache[2*Ntemplate];
  38. static void
  39. cacheinit(void)
  40. {
  41. int i;
  42. static int x;
  43. static Lock l;
  44. if(x)
  45. return;
  46. lock(&l);
  47. if(x){
  48. unlock(&l);
  49. return;
  50. }
  51. for(i=0; i<2*Ntemplate; i++)
  52. if(name[i])
  53. cache[i].s = s_copy("");
  54. x = 1;
  55. unlock(&l);
  56. }
  57. static String*
  58. gettemplate(int type)
  59. {
  60. int n;
  61. Biobuf *b;
  62. Dir *d;
  63. String *s, *ns;
  64. if(name[type]==nil)
  65. return nil;
  66. cacheinit();
  67. rlock(&cache[type]);
  68. if(0 && cache[type].t+Tcache >= time(0)){
  69. s = s_incref(cache[type].s);
  70. runlock(&cache[type]);
  71. return s;
  72. }
  73. runlock(&cache[type]);
  74. // d = nil;
  75. wlock(&cache[type]);
  76. if(0 && cache[type].t+Tcache >= time(0) || (d = wdirstat(name[type])) == nil)
  77. goto Return;
  78. if(0 && d->qid.vers == cache[type].qid.vers && d->qid.path == cache[type].qid.path){
  79. cache[type].t = time(0);
  80. goto Return;
  81. }
  82. if((b = wBopen(name[type], OREAD)) == nil)
  83. goto Return;
  84. ns = s_reset(nil);
  85. do
  86. n = s_read(b, ns, Bsize);
  87. while(n > 0);
  88. Bterm(b);
  89. if(n < 0)
  90. goto Return;
  91. s_free(cache[type].s);
  92. cache[type].s = ns;
  93. cache[type].qid = d->qid;
  94. cache[type].t = time(0);
  95. Return:
  96. free(d);
  97. s = s_incref(cache[type].s);
  98. wunlock(&cache[type]);
  99. return s;
  100. }
  101. /*
  102. * Write wiki document in HTML.
  103. */
  104. static String*
  105. s_escappend(String *s, char *p, int pre)
  106. {
  107. char *q;
  108. while(q = strpbrk(p, pre ? "<>&" : " <>&")){
  109. s = s_nappend(s, p, q-p);
  110. switch(*q){
  111. case '<':
  112. s = s_append(s, "&lt;");
  113. break;
  114. case '>':
  115. s = s_append(s, "&gt;");
  116. break;
  117. case '&':
  118. s = s_append(s, "&amp;");
  119. break;
  120. case ' ':
  121. s = s_append(s, "\n");
  122. }
  123. p = q+1;
  124. }
  125. s = s_append(s, p);
  126. return s;
  127. }
  128. static char*
  129. mkurl(char *s, int ty)
  130. {
  131. char *p, *q;
  132. if(strncmp(s, "http:", 5)==0
  133. || strncmp(s, "https:", 6)==0
  134. || strncmp(s, "#", 1)==0
  135. || strncmp(s, "ftp:", 4)==0
  136. || strncmp(s, "mailto:", 7)==0
  137. || strncmp(s, "telnet:", 7)==0
  138. || strncmp(s, "file:", 5)==0)
  139. return estrdup(s);
  140. if(strchr(s, ' ')==nil && strchr(s, '@')!=nil){
  141. p = emalloc(strlen(s)+8);
  142. strcpy(p, "mailto:");
  143. strcat(p, s);
  144. return p;
  145. }
  146. if(ty == Toldpage)
  147. p = smprint("../../%s", s);
  148. else
  149. p = smprint("../%s", s);
  150. for(q=p; *q; q++)
  151. if(*q==' ')
  152. *q = '_';
  153. return p;
  154. }
  155. int okayinlist[Nwtxt] =
  156. {
  157. [Wbullet] 1,
  158. [Wlink] 1,
  159. [Wman] 1,
  160. [Wplain] 1,
  161. };
  162. int okayinpre[Nwtxt] =
  163. {
  164. [Wlink] 1,
  165. [Wman] 1,
  166. [Wpre] 1,
  167. };
  168. int okayinpara[Nwtxt] =
  169. {
  170. [Wpara] 1,
  171. [Wlink] 1,
  172. [Wman] 1,
  173. [Wplain] 1,
  174. };
  175. char*
  176. nospaces(char *s)
  177. {
  178. char *q;
  179. s = strdup(s);
  180. if(s == nil)
  181. return nil;
  182. for(q=s; *q; q++)
  183. if(*q == ' ')
  184. *q = '_';
  185. return s;
  186. }
  187. String*
  188. pagehtml(String *s, Wpage *wtxt, int ty)
  189. {
  190. char *p, tmp[40];
  191. int inlist, inpara, inpre, t, tnext;
  192. Wpage *w;
  193. inlist = 0;
  194. inpre = 0;
  195. inpara = 0;
  196. for(w=wtxt; w; w=w->next){
  197. t = w->type;
  198. tnext = Whr;
  199. if(w->next)
  200. tnext = w->next->type;
  201. if(inlist && !okayinlist[t]){
  202. inlist = 0;
  203. s = s_append(s, "\n</li>\n</ul>\n");
  204. }
  205. if(inpre && !okayinpre[t]){
  206. inpre = 0;
  207. s = s_append(s, "</pre>\n");
  208. }
  209. switch(t){
  210. case Wheading:
  211. p = nospaces(w->text);
  212. s = s_appendlist(s,
  213. "\n<a name=\"", p, "\" /><h3>",
  214. w->text, "</h3>\n", nil);
  215. free(p);
  216. break;
  217. case Wpara:
  218. if(inpara){
  219. s = s_append(s, "\n</p>\n");
  220. inpara = 0;
  221. }
  222. if(okayinpara[tnext]){
  223. s = s_append(s, "\n<p class='para'>\n");
  224. inpara = 1;
  225. }
  226. break;
  227. case Wbullet:
  228. if(!inlist){
  229. inlist = 1;
  230. s = s_append(s, "\n<ul>\n");
  231. }else
  232. s = s_append(s, "\n</li>\n");
  233. s = s_append(s, "\n<li>\n");
  234. break;
  235. case Wlink:
  236. if(w->url == nil)
  237. p = mkurl(w->text, ty);
  238. else
  239. p = w->url;
  240. s = s_appendlist(s, "<a href=\"", p, "\">", nil);
  241. s = s_escappend(s, w->text, 0);
  242. s = s_append(s, "</a>");
  243. if(w->url == nil)
  244. free(p);
  245. break;
  246. case Wman:
  247. sprint(tmp, "%d", w->section);
  248. s = s_appendlist(s,
  249. "<a href=\"http://plan9.bell-labs.com/magic/man2html/",
  250. tmp, "/", w->text, "\"><i>", w->text, "</i>(",
  251. tmp, ")</a>", nil);
  252. break;
  253. case Wpre:
  254. if(!inpre){
  255. inpre = 1;
  256. s = s_append(s, "\n<pre>\n");
  257. }
  258. s = s_escappend(s, w->text, 1);
  259. s = s_append(s, "\n");
  260. break;
  261. case Whr:
  262. s = s_append(s, "<hr />");
  263. break;
  264. case Wplain:
  265. s = s_escappend(s, w->text, 0);
  266. break;
  267. }
  268. }
  269. if(inlist)
  270. s = s_append(s, "\n</li>\n</ul>\n");
  271. if(inpre)
  272. s = s_append(s, "</pre>\n");
  273. if(inpara)
  274. s = s_append(s, "\n</p>\n");
  275. return s;
  276. }
  277. static String*
  278. copythru(String *s, char **newp, int *nlinep, int l)
  279. {
  280. char *oq, *q, *r;
  281. int ol;
  282. q = *newp;
  283. oq = q;
  284. ol = *nlinep;
  285. while(ol < l){
  286. if(r = strchr(q, '\n'))
  287. q = r+1;
  288. else{
  289. q += strlen(q);
  290. break;
  291. }
  292. ol++;
  293. }
  294. if(*nlinep < l)
  295. *nlinep = l;
  296. *newp = q;
  297. return s_nappend(s, oq, q-oq);
  298. }
  299. static int
  300. dodiff(char *f1, char *f2)
  301. {
  302. int p[2];
  303. if(pipe(p) < 0){
  304. return -1;
  305. }
  306. switch(fork()){
  307. case -1:
  308. return -1;
  309. case 0:
  310. close(p[0]);
  311. dup(p[1], 1);
  312. execl("/bin/diff", "diff", f1, f2, nil);
  313. _exits(nil);
  314. }
  315. close(p[1]);
  316. return p[0];
  317. }
  318. /* print document i grayed out, with only diffs relative to j in black */
  319. static String*
  320. s_diff(String *s, Whist *h, int i, int j)
  321. {
  322. char *p, *q, *pnew;
  323. int fdiff, fd1, fd2, n1, n2;
  324. Biobuf b;
  325. char fn1[40], fn2[40];
  326. String *new, *old;
  327. int nline;
  328. if(j < 0)
  329. return pagehtml(s, h->doc[i].wtxt, Tpage);
  330. strcpy(fn1, "/tmp/wiki.XXXXXX");
  331. strcpy(fn2, "/tmp/wiki.XXXXXX");
  332. if((fd1 = opentemp(fn1)) < 0 || (fd2 = opentemp(fn2)) < 0){
  333. close(fd1);
  334. s = s_append(s, "\nopentemp failed; sorry\n");
  335. return s;
  336. }
  337. new = pagehtml(s_reset(nil), h->doc[i].wtxt, Tpage);
  338. old = pagehtml(s_reset(nil), h->doc[j].wtxt, Tpage);
  339. write(fd1, s_to_c(new), s_len(new));
  340. write(fd2, s_to_c(old), s_len(old));
  341. fdiff = dodiff(fn2, fn1);
  342. if(fdiff < 0)
  343. s = s_append(s, "\ndiff failed; sorry\n");
  344. else{
  345. nline = 0;
  346. pnew = s_to_c(new);
  347. Binit(&b, fdiff, OREAD);
  348. while(p = Brdline(&b, '\n')){
  349. if(p[0]=='<' || p[0]=='>' || p[0]=='-')
  350. continue;
  351. p[Blinelen(&b)-1] = '\0';
  352. if((q = strpbrk(p, "acd")) == nil)
  353. continue;
  354. n1 = atoi(q+1);
  355. if(q = strchr(q, ','))
  356. n2 = atoi(q+1);
  357. else
  358. n2 = n1;
  359. switch(*q){
  360. case 'a':
  361. case 'c':
  362. s = s_append(s, "<span class='old_text'>");
  363. s = copythru(s, &pnew, &nline, n1-1);
  364. s = s_append(s, "</span><span class='new_text'>");
  365. s = copythru(s, &pnew, &nline, n2);
  366. s = s_append(s, "</span>");
  367. break;
  368. }
  369. }
  370. close(fdiff);
  371. s = s_append(s, "<span class='old_text'>");
  372. s = s_append(s, pnew);
  373. s = s_append(s, "</span>");
  374. }
  375. s_free(new);
  376. s_free(old);
  377. close(fd1);
  378. close(fd2);
  379. return s;
  380. }
  381. static String*
  382. diffhtml(String *s, Whist *h)
  383. {
  384. int i;
  385. char tmp[50];
  386. char *atime;
  387. for(i=h->ndoc-1; i>=0; i--){
  388. s = s_append(s, "<hr /><div class='diff_head'>\n");
  389. if(i==h->current)
  390. sprint(tmp, "index.html");
  391. else
  392. sprint(tmp, "%lud", h->doc[i].time);
  393. atime = ctime(h->doc[i].time);
  394. atime[strlen(atime)-1] = '\0';
  395. s = s_appendlist(s,
  396. "<a href=\"", tmp, "\">",
  397. atime, "</a>", nil);
  398. if(h->doc[i].author)
  399. s = s_appendlist(s, ", ", h->doc[i].author, nil);
  400. if(h->doc[i].conflict)
  401. s = s_append(s, ", conflicting write");
  402. s = s_append(s, "\n");
  403. if(h->doc[i].comment)
  404. s = s_appendlist(s, "<br /><i>", h->doc[i].comment, "</i>\n", nil);
  405. s = s_append(s, "</div><hr />");
  406. s = s_diff(s, h, i, i-1);
  407. }
  408. s = s_append(s, "<hr>");
  409. return s;
  410. }
  411. static String*
  412. historyhtml(String *s, Whist *h)
  413. {
  414. int i;
  415. char tmp[40];
  416. char *atime;
  417. s = s_append(s, "<ul>\n");
  418. for(i=h->ndoc-1; i>=0; i--){
  419. if(i==h->current)
  420. sprint(tmp, "index.html");
  421. else
  422. sprint(tmp, "%lud", h->doc[i].time);
  423. atime = ctime(h->doc[i].time);
  424. atime[strlen(atime)-1] = '\0';
  425. s = s_appendlist(s,
  426. "<li><a href=\"", tmp, "\">",
  427. atime, "</a>", nil);
  428. if(h->doc[i].author)
  429. s = s_appendlist(s, ", ", h->doc[i].author, nil);
  430. if(h->doc[i].conflict)
  431. s = s_append(s, ", conflicting write");
  432. s = s_append(s, "\n");
  433. if(h->doc[i].comment)
  434. s = s_appendlist(s, "<br><i>", h->doc[i].comment, "</i>\n", nil);
  435. }
  436. s = s_append(s, "</ul>");
  437. return s;
  438. }
  439. String*
  440. tohtml(Whist *h, Wdoc *d, int ty)
  441. {
  442. char *atime;
  443. char *p, *q, ver[40];
  444. int nsub;
  445. Sub sub[3];
  446. String *s, *t;
  447. t = gettemplate(ty);
  448. if(p = strstr(s_to_c(t), "PAGE"))
  449. q = p+4;
  450. else{
  451. p = s_to_c(t)+s_len(t);
  452. q = nil;
  453. }
  454. nsub = 0;
  455. if(h){
  456. sub[nsub] = (Sub){ "TITLE", h->title };
  457. nsub++;
  458. }
  459. if(d){
  460. sprint(ver, "%lud", d->time);
  461. sub[nsub] = (Sub){ "VERSION", ver };
  462. nsub++;
  463. atime = ctime(d->time);
  464. atime[strlen(atime)-1] = '\0';
  465. sub[nsub] = (Sub){ "DATE", atime };
  466. nsub++;
  467. }
  468. s = s_reset(nil);
  469. s = s_appendsub(s, s_to_c(t), p-s_to_c(t), sub, nsub);
  470. switch(ty){
  471. case Tpage:
  472. case Toldpage:
  473. s = pagehtml(s, d->wtxt, ty);
  474. break;
  475. case Tedit:
  476. s = pagetext(s, d->wtxt, 0);
  477. break;
  478. case Tdiff:
  479. s = diffhtml(s, h);
  480. break;
  481. case Thistory:
  482. s = historyhtml(s, h);
  483. break;
  484. case Tnew:
  485. case Twerror:
  486. break;
  487. }
  488. if(q)
  489. s = s_appendsub(s, q, strlen(q), sub, nsub);
  490. s_free(t);
  491. return s;
  492. }
  493. enum {
  494. LINELEN = 70,
  495. };
  496. static String*
  497. s_appendbrk(String *s, char *p, char *prefix, int dosharp)
  498. {
  499. char *e, *w, *x;
  500. int first, l;
  501. Rune r;
  502. first = 1;
  503. while(*p){
  504. s = s_append(s, p);
  505. e = strrchr(s_to_c(s), '\n');
  506. if(e == nil)
  507. e = s_to_c(s);
  508. else
  509. e++;
  510. if(utflen(e) <= LINELEN)
  511. break;
  512. x = e; l=LINELEN;
  513. while(l--)
  514. x+=chartorune(&r, x);
  515. x = strchr(x, ' ');
  516. if(x){
  517. *x = '\0';
  518. w = strrchr(e, ' ');
  519. *x = ' ';
  520. }else
  521. w = strrchr(e, ' ');
  522. if(w-s_to_c(s) < strlen(prefix))
  523. break;
  524. x = estrdup(w+1);
  525. *w = '\0';
  526. s->ptr = w;
  527. s_append(s, "\n");
  528. if(dosharp)
  529. s_append(s, "#");
  530. s_append(s, prefix);
  531. if(!first)
  532. free(p);
  533. first = 0;
  534. p = x;
  535. }
  536. if(!first)
  537. free(p);
  538. return s;
  539. }
  540. static void
  541. s_endline(String *s, int dosharp)
  542. {
  543. if(dosharp){
  544. if(s->ptr == s->base+1 && s->ptr[-1] == '#')
  545. return;
  546. if(s->ptr > s->base+1 && s->ptr[-1] == '#' && s->ptr[-2] == '\n')
  547. return;
  548. s_append(s, "\n#");
  549. }else{
  550. if(s->ptr > s->base+1 && s->ptr[-1] == '\n')
  551. return;
  552. s_append(s, "\n");
  553. }
  554. }
  555. String*
  556. pagetext(String *s, Wpage *page, int dosharp)
  557. {
  558. int inlist, inpara;
  559. char *prefix, *sharp, tmp[40];
  560. String *t;
  561. Wpage *w;
  562. inlist = 0;
  563. inpara = 0;
  564. prefix = "";
  565. sharp = dosharp ? "#" : "";
  566. s = s_append(s, sharp);
  567. for(w=page; w; w=w->next){
  568. switch(w->type){
  569. case Wheading:
  570. if(inlist){
  571. prefix = "";
  572. inlist = 0;
  573. }
  574. s_endline(s, dosharp);
  575. if(!inpara){
  576. inpara = 1;
  577. s = s_appendlist(s, "\n", sharp, nil);
  578. }
  579. s = s_appendlist(s, w->text, "\n", sharp, "\n", sharp, nil);
  580. break;
  581. case Wpara:
  582. s_endline(s, dosharp);
  583. if(inlist){
  584. prefix = "";
  585. inlist = 0;
  586. }
  587. if(!inpara){
  588. inpara = 1;
  589. s = s_appendlist(s, "\n", sharp, nil);
  590. }
  591. break;
  592. case Wbullet:
  593. s_endline(s, dosharp);
  594. if(!inlist)
  595. inlist = 1;
  596. if(inpara)
  597. inpara = 0;
  598. s = s_append(s, " *\t");
  599. prefix = "\t";
  600. break;
  601. case Wlink:
  602. if(inpara)
  603. inpara = 0;
  604. t = s_append(s_copy("["), w->text);
  605. if(w->url == nil)
  606. t = s_append(t, "]");
  607. else{
  608. t = s_append(t, " | ");
  609. t = s_append(t, w->url);
  610. t = s_append(t, "]");
  611. }
  612. s = s_appendbrk(s, s_to_c(t), prefix, dosharp);
  613. s_free(t);
  614. break;
  615. case Wman:
  616. if(inpara)
  617. inpara = 0;
  618. s = s_appendbrk(s, w->text, prefix, dosharp);
  619. sprint(tmp, "(%d)", w->section);
  620. s = s_appendbrk(s, tmp, prefix, dosharp);
  621. break;
  622. case Wpre:
  623. if(inlist){
  624. prefix = "";
  625. inlist = 0;
  626. }
  627. if(inpara)
  628. inpara = 0;
  629. s_endline(s, dosharp);
  630. s = s_appendlist(s, "! ", w->text, "\n", sharp, nil);
  631. break;
  632. case Whr:
  633. s_endline(s, dosharp);
  634. s = s_appendlist(s, "------------------------------------------------------ \n", sharp, nil);
  635. break;
  636. case Wplain:
  637. if(inpara)
  638. inpara = 0;
  639. s = s_appendbrk(s, w->text, prefix, dosharp);
  640. break;
  641. }
  642. }
  643. s_endline(s, dosharp);
  644. s->ptr--;
  645. *s->ptr = '\0';
  646. return s;
  647. }
  648. static String*
  649. historytext(String *s, Whist *h)
  650. {
  651. int i;
  652. char tmp[40];
  653. char *atime;
  654. for(i=h->ndoc-1; i>=0; i--){
  655. if(i==h->current)
  656. sprint(tmp, "[current]");
  657. else
  658. sprint(tmp, "[%lud/]", h->doc[i].time);
  659. atime = ctime(h->doc[i].time);
  660. atime[strlen(atime)-1] = '\0';
  661. s = s_appendlist(s, " * ", tmp, " ", atime, nil);
  662. if(h->doc[i].author)
  663. s = s_appendlist(s, ", ", h->doc[i].author, nil);
  664. if(h->doc[i].conflict)
  665. s = s_append(s, ", conflicting write");
  666. s = s_append(s, "\n");
  667. if(h->doc[i].comment)
  668. s = s_appendlist(s, "<i>", h->doc[i].comment, "</i>\n", nil);
  669. }
  670. return s;
  671. }
  672. String*
  673. totext(Whist *h, Wdoc *d, int ty)
  674. {
  675. char *atime;
  676. char *p, *q, ver[40];
  677. int nsub;
  678. Sub sub[3];
  679. String *s, *t;
  680. t = gettemplate(Ntemplate+ty);
  681. if(p = strstr(s_to_c(t), "PAGE"))
  682. q = p+4;
  683. else{
  684. p = s_to_c(t)+s_len(t);
  685. q = nil;
  686. }
  687. nsub = 0;
  688. if(h){
  689. sub[nsub] = (Sub){ "TITLE", h->title };
  690. nsub++;
  691. }
  692. if(d){
  693. sprint(ver, "%lud", d->time);
  694. sub[nsub] = (Sub){ "VERSION", ver };
  695. nsub++;
  696. atime = ctime(d->time);
  697. atime[strlen(atime)-1] = '\0';
  698. sub[nsub] = (Sub){ "DATE", atime };
  699. nsub++;
  700. }
  701. s = s_reset(nil);
  702. s = s_appendsub(s, s_to_c(t), p-s_to_c(t), sub, nsub);
  703. switch(ty){
  704. case Tpage:
  705. case Toldpage:
  706. s = pagetext(s, d->wtxt, 0);
  707. break;
  708. case Thistory:
  709. s = historytext(s, h);
  710. break;
  711. case Tnew:
  712. case Twerror:
  713. break;
  714. }
  715. if(q)
  716. s = s_appendsub(s, q, strlen(q), sub, nsub);
  717. s_free(t);
  718. return s;
  719. }
  720. String*
  721. doctext(String *s, Wdoc *d)
  722. {
  723. char tmp[40];
  724. sprint(tmp, "D%lud", d->time);
  725. s = s_append(s, tmp);
  726. if(d->comment){
  727. s = s_append(s, "\nC");
  728. s = s_append(s, d->comment);
  729. }
  730. if(d->author){
  731. s = s_append(s, "\nA");
  732. s = s_append(s, d->author);
  733. }
  734. if(d->conflict)
  735. s = s_append(s, "\nX");
  736. s = s_append(s, "\n");
  737. s = pagetext(s, d->wtxt, 1);
  738. return s;
  739. }