123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /* markdown: a C implementation of John Gruber's Markdown markup language.
- *
- * Copyright (C) 2007 David L Parsons.
- * The redistribution terms are provided in the COPYRIGHT file that must
- * be distributed with this source code.
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <time.h>
- #include <ctype.h>
- #include "config.h"
- #include "cstring.h"
- #include "markdown.h"
- #include "amalloc.h"
- /* free a (single) line
- */
- void
- ___mkd_freeLine(Line *ptr)
- {
- DELETE(ptr->text);
- free(ptr);
- }
- /* free a list of lines
- */
- void
- ___mkd_freeLines(Line *p)
- {
- if (p->next)
- ___mkd_freeLines(p->next);
- ___mkd_freeLine(p);
- }
- /* bye bye paragraph.
- */
- void
- ___mkd_freeParagraph(Paragraph *p)
- {
- if (p->next)
- ___mkd_freeParagraph(p->next);
- if (p->down)
- ___mkd_freeParagraph(p->down);
- if (p->text)
- ___mkd_freeLines(p->text);
- if (p->ident)
- free(p->ident);
- if (p->lang)
- free(p->lang);
- free(p);
- }
- /* bye bye footnote.
- */
- void
- ___mkd_freefootnote(Footnote *f)
- {
- DELETE(f->tag);
- DELETE(f->link);
- DELETE(f->title);
- if ( f->text) ___mkd_freeParagraph(f->text);
- }
- /* bye bye footnotes.
- */
- void
- ___mkd_freefootnotes(MMIOT *f)
- {
- int i;
- if ( f->footnotes ) {
- for (i=0; i < S(f->footnotes->note); i++)
- ___mkd_freefootnote( &T(f->footnotes->note)[i] );
- DELETE(f->footnotes->note);
- free(f->footnotes);
- }
- }
- /* initialize a new MMIOT
- */
- void
- ___mkd_initmmiot(MMIOT *f, void *footnotes)
- {
- if ( f ) {
- memset(f, 0, sizeof *f);
- CREATE(f->in);
- CREATE(f->out);
- CREATE(f->Q);
- if ( footnotes )
- f->footnotes = footnotes;
- else {
- f->footnotes = malloc(sizeof f->footnotes[0]);
- CREATE(f->footnotes->note);
- }
- }
- }
- /* free the contents of a MMIOT, but leave the object alone.
- */
- void
- ___mkd_freemmiot(MMIOT *f, void *footnotes)
- {
- if ( f ) {
- DELETE(f->in);
- DELETE(f->out);
- DELETE(f->Q);
- if ( f->footnotes != footnotes )
- ___mkd_freefootnotes(f);
- memset(f, 0, sizeof *f);
- }
- }
- /* free lines up to an barrier.
- */
- void
- ___mkd_freeLineRange(Line *anchor, Line *stop)
- {
- Line *r = anchor->next;
- if ( r != stop ) {
- while ( r && (r->next != stop) )
- r = r->next;
- if ( r ) r->next = 0;
- ___mkd_freeLines(anchor->next);
- }
- anchor->next = 0;
- }
- /* clean up everything allocated in __mkd_compile()
- */
- void
- mkd_cleanup(Document *doc)
- {
- if ( doc && (doc->magic == VALID_DOCUMENT) ) {
- if ( doc->ctx ) {
- ___mkd_freemmiot(doc->ctx, 0);
- free(doc->ctx);
- }
- if ( doc->code) ___mkd_freeParagraph(doc->code);
- if ( doc->title) ___mkd_freeLine(doc->title);
- if ( doc->author) ___mkd_freeLine(doc->author);
- if ( doc->date) ___mkd_freeLine(doc->date);
- if ( T(doc->content) ) ___mkd_freeLines(T(doc->content));
- memset(doc, 0, sizeof doc[0]);
- free(doc);
- }
- }
|