Debian Codegraph @

ext/css.c

raw symbols SCIP
1 /* markdown: a C implementation of John Gruber's Markdown markup language.
2 *
3 * Copyright (C) 2009 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
14 #include "config.h"
15
16 #include "cstring.h"
17 #include "markdown.h"
18 #include "amalloc.h"
19
20
21 /*
22 * dump out stylesheet sections.
23 */
24 static void
25 stylesheets(Paragraph *p, Cstring *f)
26 {
27 Line* q;
28
29 for ( ; p ; p = p->next ) {
30 if ( p->typ == STYLE ) {
31 for ( q = p->text; q ; q = q->next ) {
32 Cswrite(f, T(q->text), S(q->text));
33 Csputc('\n', f);
34 }
35 }
36 if ( p->down )
37 stylesheets(p->down, f);
38 }
39 }
40
41
42 /* dump any embedded styles to a string
43 */
44 int
45 mkd_css(Document *d, char **res)
46 {
47 Cstring f;
48 int size;
49
50 if ( res && d && d->compiled ) {
51 *res = 0;
52 CREATE(f);
53 RESERVE(f, 100);
54 stylesheets(d->code, &f);
55
56 if ( (size = S(f)) > 0 ) {
57 /* null-terminate, then strdup() into a free()able memory
58 * chunk
59 */
60 EXPAND(f) = 0;
61 *res = strdup(T(f));
62 }
63 DELETE(f);
64 return size;
65 }
66 return EOF;
67 }
68
69
70 /* dump any embedded styles to a file
71 */
72 int
73 mkd_generatecss(Document *d, FILE *f)
74 {
75 char *res;
76 int written;
77 int size = mkd_css(d, &res);
78
79 written = (size > 0) ? fwrite(res,1,size,f) : 0;
80
81 if ( res )
82 free(res);
83
84 return (written == size) ? size : EOF;
85 }