File: css.c

package info (click to toggle)
discount 2.2.2-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 856 kB
  • sloc: ansic: 4,891; pascal: 871; sh: 223; makefile: 129
file content (87 lines) | stat: -rw-r--r-- 1,642 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* markdown: a C implementation of John Gruber's Markdown markup language.
 *
 * Copyright (C) 2009 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"


/*
 * dump out stylesheet sections.
 */
static void
stylesheets(Paragraph *p, Cstring *f)
{
    Line* q;

    for ( ; p ; p = p->next ) {
	if ( p->typ == STYLE ) {
	    for ( q = p->text; q ; q = q->next ) {
		Cswrite(f, T(q->text), S(q->text));
		Csputc('\n', f);
	    }
	}
	if ( p->down )
	    stylesheets(p->down, f);
    }
}


/* dump any embedded styles to a string
 */
int
mkd_css(Document *d, char **res)
{
    Cstring f;
    int size;

    if ( res && d && d->compiled ) {
	*res = 0;
	CREATE(f);
	RESERVE(f, 100);
	stylesheets(d->code, &f);
			
	if ( (size = S(f)) > 0 ) {
	    EXPAND(f) = 0;
			/* HACK ALERT! HACK ALERT! HACK ALERT! */
	    *res = T(f);/* we know that a T(Cstring) is a character pointer */
			/* so we can simply pick it up and carry it away, */
			/* leaving the husk of the Ctring on the stack */
			/* END HACK ALERT */
	}
	else
	    DELETE(f);
	return size;
    }
    return EOF;
}


/* dump any embedded styles to a file
 */
int
mkd_generatecss(Document *d, FILE *f)
{
    char *res;
    int written;
    int size = mkd_css(d, &res);

    written = (size > 0) ? fwrite(res,1,size,f) : 0;
    
    if ( res )
	free(res);
    
    return (written == size) ? size : EOF;
}