summaryrefslogtreecommitdiff
path: root/src/stmd.h
blob: 21a86b041829f22d7a37c6fa6ac4f2890f970bfc (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef _STDMD_H_
#define _STDMD_H_

#include <stdbool.h>
#include <stdio.h>
#include "buffer.h"
#include "chunk.h"
#include "uthash.h"

#define VERSION "0.1"
#define CODE_INDENT 4

struct node_inl {
	enum {
		INL_STRING,
		INL_SOFTBREAK,
		INL_LINEBREAK,
		INL_CODE,
		INL_RAW_HTML,
		INL_EMPH,
		INL_STRONG,
		INL_LINK,
		INL_IMAGE
	} tag;
	union {
		chunk literal;
		struct node_inl *inlines;
		struct {
			struct node_inl *label;
			unsigned char *url;
			unsigned char *title;
		} linkable;
	} content;
	struct node_inl *next;
};

typedef struct node_inl node_inl;

struct reference {
  unsigned char *label;
  unsigned char *url;
  unsigned char *title;
  UT_hash_handle  hh; // used by uthash
};

typedef struct reference reference;

// Types for blocks

struct ListData {
	enum {
		bullet,
		ordered
	}  list_type;
	int               marker_offset;
	int               padding;
	int               start;
	enum {
		period,
		parens
	} delimiter;
	unsigned char     bullet_char;
	bool              tight;
};

struct FencedCodeData {
	int               fence_length;
	int               fence_offset;
	char              fence_char;
	strbuf            info;
};

struct node_block {
	enum {
		BLOCK_DOCUMENT,
		BLOCK_BQUOTE,
		BLOCK_LIST,
		BLOCK_LIST_ITEM,
		BLOCK_FENCED_CODE,
		BLOCK_INDENTED_CODE,
		BLOCK_HTML,
		BLOCK_PARAGRAPH,
		BLOCK_ATX_HEADER,
		BLOCK_SETEXT_HEADER,
		BLOCK_HRULE,
		BLOCK_REFERENCE_DEF
	} tag;
	int start_line;
	int start_column;
	int end_line;
	bool open;
	bool last_line_blank;
	struct node_block* children;
	struct node_block* last_child;
	struct node_block* parent;
	struct node_block* top;
	strbuf string_content;
	node_inl* inline_content;

	union  {
		struct ListData list;
		struct FencedCodeData code;
		struct {
			int level;
		} header;
		struct {
			reference** refmap;
		} document;
	} as;

	struct node_block *next;
	struct node_block *prev;
};

typedef struct node_block node_block;

node_inl* parse_inlines(strbuf *input, reference** refmap);
void free_inlines(node_inl* e);

int parse_reference(strbuf *input, reference** refmap);
void free_reference(reference *ref);
void free_reference_map(reference **refmap);

void add_reference(reference** refmap, reference* ref);
void unescape_buffer(strbuf *buf);

extern node_block* make_document();
extern node_block* add_child(node_block* parent,
                        int block_type, int start_line, int start_column);
void free_blocks(node_block* e);

extern node_block *stmd_parse_document(const unsigned char *buffer, size_t len);
extern node_block *stmd_parse_file(FILE *f);

void print_inlines(node_inl* ils, int indent);
void print_blocks(node_block* blk, int indent);

void blocks_to_html(strbuf *html, node_block *b, bool tight);
void inlines_to_html(strbuf *html, node_inl *b);

#endif