File indexing completed on 2024-04-28 11:21:06

0001 /* markdown: a C implementation of John Gruber's Markdown markup language.
0002  *
0003  * Copyright (C) 2009 David L Parsons.
0004  * The redistribution terms are provided in the COPYRIGHT file that must
0005  * be distributed with this source code.
0006  */
0007 #include <stdio.h>
0008 #include <string.h>
0009 #include <stdarg.h>
0010 #include <stdlib.h>
0011 #include <time.h>
0012 #include <ctype.h>
0013 
0014 #include "config.h"
0015 
0016 #include "cstring.h"
0017 #include "markdown.h"
0018 #include "amalloc.h"
0019 
0020 
0021 /*
0022  * dump out stylesheet sections.
0023  */
0024 static void
0025 stylesheets(Paragraph *p, Cstring *f)
0026 {
0027     Line* q;
0028 
0029     for ( ; p ; p = p->next ) {
0030     if ( p->typ == STYLE ) {
0031         for ( q = p->text; q ; q = q->next ) {
0032         Cswrite(f, T(q->text), S(q->text));
0033         Csputc('\n', f);
0034         }
0035     }
0036     if ( p->down )
0037         stylesheets(p->down, f);
0038     }
0039 }
0040 
0041 
0042 /* dump any embedded styles to a string
0043  */
0044 int
0045 mkd_css(Document *d, char **res)
0046 {
0047     Cstring f;
0048     int size;
0049 
0050     if ( res && d && d->compiled ) {
0051     *res = 0;
0052     CREATE(f);
0053     RESERVE(f, 100);
0054     stylesheets(d->code, &f);
0055             
0056     if ( (size = S(f)) > 0 ) {
0057         /* null-terminate, then strdup() into a free()able memory
0058          * chunk
0059          */
0060         EXPAND(f) = 0;
0061         *res = strdup(T(f));
0062     }
0063     DELETE(f);
0064     return size;
0065     }
0066     return EOF;
0067 }
0068 
0069 
0070 /* dump any embedded styles to a file
0071  */
0072 int
0073 mkd_generatecss(Document *d, FILE *f)
0074 {
0075     char *res;
0076     int written;
0077     int size = mkd_css(d, &res);
0078 
0079     written = (size > 0) ? fwrite(res,1,size,f) : 0;
0080     
0081     if ( res )
0082     free(res);
0083     
0084     return (written == size) ? size : EOF;
0085 }