Text::Markdown::Discount
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
2.7 KiB

  1. /* markdown: a C implementation of John Gruber's Markdown markup language.
  2. *
  3. * Copyright (C) 2007 David L Parsons.
  4. * The redistribution terms are provided in the COPYRIGHT file that must
  5. * be distributed with this source code.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <ctype.h>
  13. #include "config.h"
  14. #include "cstring.h"
  15. #include "markdown.h"
  16. #include "amalloc.h"
  17. /* free a (single) line
  18. */
  19. void
  20. ___mkd_freeLine(Line *ptr)
  21. {
  22. DELETE(ptr->text);
  23. free(ptr);
  24. }
  25. /* free a list of lines
  26. */
  27. void
  28. ___mkd_freeLines(Line *p)
  29. {
  30. if (p->next)
  31. ___mkd_freeLines(p->next);
  32. ___mkd_freeLine(p);
  33. }
  34. /* bye bye paragraph.
  35. */
  36. void
  37. ___mkd_freeParagraph(Paragraph *p)
  38. {
  39. if (p->next)
  40. ___mkd_freeParagraph(p->next);
  41. if (p->down)
  42. ___mkd_freeParagraph(p->down);
  43. if (p->text)
  44. ___mkd_freeLines(p->text);
  45. if (p->ident)
  46. free(p->ident);
  47. if (p->lang)
  48. free(p->lang);
  49. free(p);
  50. }
  51. /* bye bye footnote.
  52. */
  53. void
  54. ___mkd_freefootnote(Footnote *f)
  55. {
  56. DELETE(f->tag);
  57. DELETE(f->link);
  58. DELETE(f->title);
  59. }
  60. /* bye bye footnotes.
  61. */
  62. void
  63. ___mkd_freefootnotes(MMIOT *f)
  64. {
  65. int i;
  66. if ( f->footnotes ) {
  67. for (i=0; i < S(*f->footnotes); i++)
  68. ___mkd_freefootnote( &T(*f->footnotes)[i] );
  69. DELETE(*f->footnotes);
  70. free(f->footnotes);
  71. }
  72. }
  73. /* initialize a new MMIOT
  74. */
  75. void
  76. ___mkd_initmmiot(MMIOT *f, void *footnotes)
  77. {
  78. if ( f ) {
  79. memset(f, 0, sizeof *f);
  80. CREATE(f->in);
  81. CREATE(f->out);
  82. CREATE(f->Q);
  83. if ( footnotes )
  84. f->footnotes = footnotes;
  85. else {
  86. f->footnotes = malloc(sizeof f->footnotes[0]);
  87. CREATE(*f->footnotes);
  88. }
  89. }
  90. }
  91. /* free the contents of a MMIOT, but leave the object alone.
  92. */
  93. void
  94. ___mkd_freemmiot(MMIOT *f, void *footnotes)
  95. {
  96. if ( f ) {
  97. DELETE(f->in);
  98. DELETE(f->out);
  99. DELETE(f->Q);
  100. if ( f->footnotes != footnotes )
  101. ___mkd_freefootnotes(f);
  102. memset(f, 0, sizeof *f);
  103. }
  104. }
  105. /* free lines up to an barrier.
  106. */
  107. void
  108. ___mkd_freeLineRange(Line *anchor, Line *stop)
  109. {
  110. Line *r = anchor->next;
  111. if ( r != stop ) {
  112. while ( r && (r->next != stop) )
  113. r = r->next;
  114. if ( r ) r->next = 0;
  115. ___mkd_freeLines(anchor->next);
  116. }
  117. anchor->next = 0;
  118. }
  119. /* clean up everything allocated in __mkd_compile()
  120. */
  121. void
  122. mkd_cleanup(Document *doc)
  123. {
  124. if ( doc && (doc->magic == VALID_DOCUMENT) ) {
  125. if ( doc->ctx ) {
  126. ___mkd_freemmiot(doc->ctx, 0);
  127. free(doc->ctx);
  128. }
  129. if ( doc->code) ___mkd_freeParagraph(doc->code);
  130. if ( doc->title) ___mkd_freeLine(doc->title);
  131. if ( doc->author) ___mkd_freeLine(doc->author);
  132. if ( doc->date) ___mkd_freeLine(doc->date);
  133. if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
  134. memset(doc, 0, sizeof doc[0]);
  135. free(doc);
  136. }
  137. }