toc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * toc -- spit out a table of contents based on header blocks
  3. *
  4. * Copyright (C) 2008 Jjgod Jiang, David L Parsons
  5. * portions Copyright (C) 2011 Stefano D'Angelo
  6. * The redistribution terms are provided in the COPYRIGHT file that must
  7. * be distributed with this source code.
  8. */
  9. #include "config.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include "cstring.h"
  14. #include "markdown.h"
  15. #include "amalloc.h"
  16. /* import from Csio.c */
  17. extern void Csreparse(Cstring *, char *, int, mkd_flag_t*);
  18. /* write an header index
  19. */
  20. int
  21. mkd_toc(Document *p, char **doc)
  22. {
  23. Paragraph *tp, *srcp;
  24. int last_hnumber = 0;
  25. Cstring res;
  26. int size;
  27. int first = 1;
  28. #if HAVE_NAMED_INITIALIZERS
  29. static mkd_flag_t islabel = { { [IS_LABEL] = 1 } };
  30. #else
  31. mkd_flag_t islabel;
  32. mkd_init_flags(&islabel);
  33. set_mkd_flag(&islabel, IS_LABEL);
  34. #endif
  35. if ( !(doc && p && p->ctx) ) return -1;
  36. *doc = 0;
  37. if ( ! is_flag_set(&p->ctx->flags, MKD_TOC) ) return 0;
  38. CREATE(res);
  39. RESERVE(res, 100);
  40. for ( tp = p->code; tp ; tp = tp->next ) {
  41. if ( tp->typ == SOURCE ) {
  42. for ( srcp = tp->down; srcp; srcp = srcp->next ) {
  43. if ( (srcp->typ == HDR) && srcp->text ) {
  44. while ( last_hnumber > srcp->hnumber ) {
  45. if ( (last_hnumber - srcp->hnumber) > 1 )
  46. Csprintf(&res, "\n");
  47. Csprintf(&res, "</li>\n%*s</ul>\n%*s",
  48. last_hnumber-1, "", last_hnumber-1, "");
  49. --last_hnumber;
  50. }
  51. if ( last_hnumber == srcp->hnumber )
  52. Csprintf(&res, "</li>\n");
  53. else if ( (srcp->hnumber > last_hnumber) && !first )
  54. Csprintf(&res, "\n");
  55. while ( srcp->hnumber > last_hnumber ) {
  56. Csprintf(&res, "%*s<ul>\n", last_hnumber, "");
  57. if ( (srcp->hnumber - last_hnumber) > 1 )
  58. Csprintf(&res, "%*s<li>\n", last_hnumber+1, "");
  59. ++last_hnumber;
  60. }
  61. Csprintf(&res, "%*s<li><a href=\"#", srcp->hnumber, "");
  62. mkd_string_to_anchor(T(srcp->text->text),
  63. S(srcp->text->text),
  64. (mkd_sta_function_t)Csputc,
  65. &res,1,p->ctx);
  66. Csprintf(&res, "\">");
  67. Csreparse(&res, T(srcp->text->text),
  68. S(srcp->text->text), &islabel);
  69. Csprintf(&res, "</a>");
  70. first = 0;
  71. }
  72. }
  73. }
  74. }
  75. while ( last_hnumber > 0 ) {
  76. --last_hnumber;
  77. Csprintf(&res, "</li>\n%*s</ul>\n%*s",
  78. last_hnumber, "", last_hnumber, "");
  79. }
  80. if ( (size = S(res)) > 0 ) {
  81. /* null-terminate & strdup into a free()able memory chunk
  82. */
  83. EXPAND(res) = 0;
  84. *doc = strdup(T(res));
  85. }
  86. DELETE(res);
  87. return size;
  88. }
  89. /* write an header index
  90. */
  91. int
  92. mkd_generatetoc(Document *p, FILE *out)
  93. {
  94. char *buf = 0;
  95. int sz = mkd_toc(p, &buf);
  96. int ret = EOF;
  97. if ( sz > 0 )
  98. ret = fwrite(buf, 1, sz, out);
  99. if ( buf ) free(buf);
  100. return (ret == sz) ? ret : EOF;
  101. }