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.

157 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. free(p);
  48. }
  49. /* bye bye footnote.
  50. */
  51. void
  52. ___mkd_freefootnote(Footnote *f)
  53. {
  54. DELETE(f->tag);
  55. DELETE(f->link);
  56. DELETE(f->title);
  57. }
  58. /* bye bye footnotes.
  59. */
  60. void
  61. ___mkd_freefootnotes(MMIOT *f)
  62. {
  63. int i;
  64. if ( f->footnotes ) {
  65. for (i=0; i < S(*f->footnotes); i++)
  66. ___mkd_freefootnote( &T(*f->footnotes)[i] );
  67. DELETE(*f->footnotes);
  68. free(f->footnotes);
  69. }
  70. }
  71. /* initialize a new MMIOT
  72. */
  73. void
  74. ___mkd_initmmiot(MMIOT *f, void *footnotes)
  75. {
  76. if ( f ) {
  77. memset(f, 0, sizeof *f);
  78. CREATE(f->in);
  79. CREATE(f->out);
  80. CREATE(f->Q);
  81. if ( footnotes )
  82. f->footnotes = footnotes;
  83. else {
  84. f->footnotes = malloc(sizeof f->footnotes[0]);
  85. CREATE(*f->footnotes);
  86. }
  87. }
  88. }
  89. /* free the contents of a MMIOT, but leave the object alone.
  90. */
  91. void
  92. ___mkd_freemmiot(MMIOT *f, void *footnotes)
  93. {
  94. if ( f ) {
  95. DELETE(f->in);
  96. DELETE(f->out);
  97. DELETE(f->Q);
  98. if ( f->footnotes != footnotes )
  99. ___mkd_freefootnotes(f);
  100. memset(f, 0, sizeof *f);
  101. }
  102. }
  103. /* free lines up to an barrier.
  104. */
  105. void
  106. ___mkd_freeLineRange(Line *anchor, Line *stop)
  107. {
  108. Line *r = anchor->next;
  109. if ( r != stop ) {
  110. while ( r && (r->next != stop) )
  111. r = r->next;
  112. if ( r ) r->next = 0;
  113. ___mkd_freeLines(anchor->next);
  114. }
  115. anchor->next = 0;
  116. }
  117. /* clean up everything allocated in __mkd_compile()
  118. */
  119. void
  120. mkd_cleanup(Document *doc)
  121. {
  122. if ( doc && (doc->magic == VALID_DOCUMENT) ) {
  123. if ( doc->ctx ) {
  124. ___mkd_freemmiot(doc->ctx, 0);
  125. free(doc->ctx);
  126. }
  127. if ( doc->code) ___mkd_freeParagraph(doc->code);
  128. if ( doc->title) ___mkd_freeLine(doc->title);
  129. if ( doc->author) ___mkd_freeLine(doc->author);
  130. if ( doc->date) ___mkd_freeLine(doc->date);
  131. if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
  132. memset(doc, 0, sizeof doc[0]);
  133. free(doc);
  134. }
  135. }