tohtml.c 14 KB

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