rmultimarkdown 4.7.1.1 → 6.2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +2 -2
- data/README.md +7 -9
- data/Rakefile +33 -18
- data/ext/Makefile +261 -0
- data/ext/extconf.rb +23 -3
- data/ext/mmd/aho-corasick.c +596 -0
- data/ext/mmd/aho-corasick.h +120 -0
- data/ext/mmd/beamer.c +344 -0
- data/ext/mmd/beamer.h +72 -0
- data/ext/mmd/char.c +156 -0
- data/ext/mmd/char.h +111 -0
- data/ext/mmd/char_lookup.c +212 -0
- data/ext/mmd/critic_markup.c +330 -0
- data/ext/mmd/critic_markup.h +94 -0
- data/ext/mmd/d_string.c +402 -0
- data/ext/mmd/epub.c +563 -0
- data/ext/mmd/epub.h +69 -0
- data/ext/mmd/fodt.c +2288 -0
- data/ext/mmd/fodt.h +81 -0
- data/ext/mmd/html.c +2460 -0
- data/ext/mmd/html.h +81 -0
- data/ext/mmd/i18n.h +170 -0
- data/ext/mmd/include/d_string.h +182 -0
- data/ext/mmd/include/libMultiMarkdown.h +548 -0
- data/ext/mmd/include/token.h +233 -0
- data/ext/mmd/latex.c +2435 -0
- data/ext/mmd/latex.h +83 -0
- data/ext/mmd/lexer.c +3001 -0
- data/ext/mmd/lexer.h +75 -0
- data/ext/mmd/memoir.c +138 -0
- data/ext/mmd/memoir.h +67 -0
- data/ext/mmd/miniz.c +7557 -0
- data/ext/mmd/miniz.h +1328 -0
- data/ext/mmd/mmd.c +2798 -0
- data/ext/mmd/mmd.h +120 -0
- data/ext/mmd/object_pool.c +141 -0
- data/ext/mmd/object_pool.h +101 -0
- data/ext/mmd/opendocument-content.c +2071 -0
- data/ext/mmd/opendocument-content.h +135 -0
- data/ext/mmd/opendocument.c +981 -0
- data/ext/mmd/opendocument.h +118 -0
- data/ext/mmd/parser.c +1760 -0
- data/ext/mmd/parser.h +39 -0
- data/{MultiMarkdown-4 → ext/mmd}/rng.c +90 -49
- data/ext/mmd/scanners.c +77512 -0
- data/ext/mmd/scanners.h +101 -0
- data/ext/mmd/stack.c +142 -0
- data/ext/mmd/stack.h +113 -0
- data/ext/mmd/textbundle.c +455 -0
- data/ext/mmd/textbundle.h +115 -0
- data/ext/mmd/token.c +773 -0
- data/ext/mmd/token_pairs.c +263 -0
- data/ext/mmd/token_pairs.h +123 -0
- data/ext/mmd/transclude.c +549 -0
- data/ext/mmd/transclude.h +87 -0
- data/ext/mmd/uthash.h +1074 -0
- data/ext/mmd/uuid.c +154 -0
- data/ext/mmd/uuid.h +77 -0
- data/ext/mmd/version.h +111 -0
- data/ext/mmd/writer.c +2652 -0
- data/ext/mmd/writer.h +260 -0
- data/ext/mmd/zip.c +210 -0
- data/ext/mmd/zip.h +120 -0
- data/ext/{multi_markdown.c → ruby_multi_markdown.c} +87 -18
- data/lib/multi_markdown.bundle +0 -0
- data/lib/multi_markdown.rb +5 -8
- data/lib/multi_markdown/version.rb +1 -1
- data/rmultimarkdown.gemspec +2 -2
- data/test/{extensions_test.rb.rb → extensions_test.rb} +10 -54
- data/test/multi_markdown_test.rb +13 -0
- metadata +67 -47
- data/MultiMarkdown-4/GLibFacade.c +0 -310
- data/MultiMarkdown-4/GLibFacade.h +0 -100
- data/MultiMarkdown-4/beamer.c +0 -182
- data/MultiMarkdown-4/beamer.h +0 -11
- data/MultiMarkdown-4/critic.c +0 -111
- data/MultiMarkdown-4/critic.h +0 -15
- data/MultiMarkdown-4/glib.h +0 -11
- data/MultiMarkdown-4/html.c +0 -1117
- data/MultiMarkdown-4/html.h +0 -14
- data/MultiMarkdown-4/latex.c +0 -1217
- data/MultiMarkdown-4/latex.h +0 -16
- data/MultiMarkdown-4/libMultiMarkdown.h +0 -177
- data/MultiMarkdown-4/lyx.c +0 -2265
- data/MultiMarkdown-4/lyx.h +0 -37
- data/MultiMarkdown-4/lyxbeamer.c +0 -265
- data/MultiMarkdown-4/lyxbeamer.h +0 -11
- data/MultiMarkdown-4/memoir.c +0 -80
- data/MultiMarkdown-4/memoir.h +0 -10
- data/MultiMarkdown-4/multimarkdown.c +0 -518
- data/MultiMarkdown-4/odf.c +0 -1222
- data/MultiMarkdown-4/odf.h +0 -18
- data/MultiMarkdown-4/opml.c +0 -189
- data/MultiMarkdown-4/opml.h +0 -15
- data/MultiMarkdown-4/parse_utilities.c +0 -884
- data/MultiMarkdown-4/parser.c +0 -16656
- data/MultiMarkdown-4/parser.h +0 -188
- data/MultiMarkdown-4/rtf.c +0 -665
- data/MultiMarkdown-4/rtf.h +0 -17
- data/MultiMarkdown-4/strtok.c +0 -56
- data/MultiMarkdown-4/strtok.h +0 -9
- data/MultiMarkdown-4/text.c +0 -53
- data/MultiMarkdown-4/text.h +0 -11
- data/MultiMarkdown-4/toc.c +0 -142
- data/MultiMarkdown-4/toc.h +0 -15
- data/MultiMarkdown-4/transclude.c +0 -307
- data/MultiMarkdown-4/transclude.h +0 -28
- data/MultiMarkdown-4/writer.c +0 -731
- data/MultiMarkdown-4/writer.h +0 -38
data/MultiMarkdown-4/odf.h
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
#ifndef ODF_PARSER_H
|
2
|
-
#define ODF_PARSER_H
|
3
|
-
|
4
|
-
#include "parser.h"
|
5
|
-
#include "html.h" /* Use some of the HTML routines */
|
6
|
-
#include "writer.h"
|
7
|
-
|
8
|
-
void begin_odf_output(GString *out, node* list, scratch_pad *scratch);
|
9
|
-
void print_odf_node_tree(GString *out, node *list, scratch_pad *scratch);
|
10
|
-
void print_odf_node(GString *out, node *n, scratch_pad *scratch);
|
11
|
-
void print_odf_section_and_children(GString *out, node *list, scratch_pad *scratch);
|
12
|
-
void end_odf_output(GString *out, node* list, scratch_pad *scratch);
|
13
|
-
void print_odf_string(GString *out, char *str);
|
14
|
-
void print_odf_code_string(GString *out, char *str);
|
15
|
-
void print_odf_header(GString *out);
|
16
|
-
void print_odf_footer(GString *out);
|
17
|
-
|
18
|
-
#endif
|
data/MultiMarkdown-4/opml.c
DELETED
@@ -1,189 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
|
3
|
-
opml.c -- OPML add-on to LaTeX writer
|
4
|
-
|
5
|
-
(c) 2013-2015 Fletcher T. Penney (http://fletcherpenney.net/).
|
6
|
-
|
7
|
-
This program is free software; you can redistribute it and/or modify
|
8
|
-
it under the terms of the GNU General Public License or the MIT
|
9
|
-
license. See LICENSE for details.
|
10
|
-
|
11
|
-
This program is distributed in the hope that it will be useful,
|
12
|
-
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
-
GNU General Public License for more details.
|
15
|
-
|
16
|
-
*/
|
17
|
-
|
18
|
-
#include "opml.h"
|
19
|
-
|
20
|
-
/* begin_opml_output -- handle the initial prefix, if any */
|
21
|
-
void begin_opml_output(GString *out, node* list, scratch_pad *scratch) {
|
22
|
-
#ifdef DEBUG_ON
|
23
|
-
fprintf(stderr, "begin_opml_output\n");
|
24
|
-
#endif
|
25
|
-
node *title;
|
26
|
-
|
27
|
-
g_string_append_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<opml version=\"1.0\">\n");
|
28
|
-
|
29
|
-
if (tree_contains_key(list, METAKEY)) {
|
30
|
-
title = metadata_for_key("title", list);
|
31
|
-
if (title != NULL) {
|
32
|
-
char *temp_str;
|
33
|
-
GString *temp = g_string_new("");
|
34
|
-
g_string_append_printf(out, "<head><title>");
|
35
|
-
print_raw_node_tree(temp, title->children);
|
36
|
-
temp_str = strdup(temp->str);
|
37
|
-
trim_trailing_whitespace(temp_str);
|
38
|
-
print_opml_string(out, temp_str);
|
39
|
-
g_string_append_printf(out, "</title></head>\n",temp_str);
|
40
|
-
free(temp_str);
|
41
|
-
g_string_free(temp, true);
|
42
|
-
}
|
43
|
-
}
|
44
|
-
g_string_append_printf(out, "<body>\n");
|
45
|
-
}
|
46
|
-
|
47
|
-
/* end_opml_output -- close the document */
|
48
|
-
void end_opml_output(GString *out, node* list, scratch_pad *scratch) {
|
49
|
-
#ifdef DEBUG_ON
|
50
|
-
fprintf(stderr, "end_opml_output\n");
|
51
|
-
#endif
|
52
|
-
if (tree_contains_key(list, METAKEY)) {
|
53
|
-
g_string_append_printf(out, "<outline text=\"Metadata\">\n");
|
54
|
-
print_opml_node_tree(out, list->children, scratch);
|
55
|
-
g_string_append_printf(out, "</outline>");
|
56
|
-
}
|
57
|
-
g_string_append_printf(out, "</body>\n</opml>");
|
58
|
-
}
|
59
|
-
|
60
|
-
/* print_opml_node_tree -- convert node tree to LaTeX */
|
61
|
-
void print_opml_node_tree(GString *out, node *list, scratch_pad *scratch) {
|
62
|
-
#ifdef DEBUG_ON
|
63
|
-
fprintf(stderr, "print_opml_node_tree\n");
|
64
|
-
#endif
|
65
|
-
int lev;
|
66
|
-
while (list != NULL) {
|
67
|
-
if (list->key == HEADINGSECTION) {
|
68
|
-
lev = list->children->key;
|
69
|
-
|
70
|
-
print_opml_section_and_children(out, list, scratch);
|
71
|
-
|
72
|
-
while ((list->next != NULL) && (list->next->key == HEADINGSECTION)
|
73
|
-
&& (list->next->children->key > lev)) {
|
74
|
-
list = list->next;
|
75
|
-
}
|
76
|
-
} else {
|
77
|
-
print_opml_node(out, list, scratch);
|
78
|
-
}
|
79
|
-
list = list->next;
|
80
|
-
}
|
81
|
-
}
|
82
|
-
|
83
|
-
/* print_opml_section_and_children -- we want to stay inside the outline structure */
|
84
|
-
void print_opml_section_and_children(GString *out, node *list, scratch_pad *scratch) {
|
85
|
-
#ifdef DEBUG_ON
|
86
|
-
fprintf(stderr, "print_opml_section_and_children: %d\n",list->key);
|
87
|
-
#endif
|
88
|
-
int lev = list->children->key;
|
89
|
-
|
90
|
-
/* print current section (parent) */
|
91
|
-
print_opml_node(out, list, scratch);
|
92
|
-
|
93
|
-
/* check for child nodes */
|
94
|
-
while ((list->next != NULL) && (list->next->key == HEADINGSECTION) && (list->next->children->key > lev)) {
|
95
|
-
/* next item is also a HEADINGSECTION and is a child */
|
96
|
-
if (list->next->children->key - lev == 1)
|
97
|
-
print_opml_section_and_children(out, list->next, scratch);
|
98
|
-
list = list->next;
|
99
|
-
}
|
100
|
-
g_string_append_printf(out, "</outline>\n");
|
101
|
-
}
|
102
|
-
|
103
|
-
/* print_opml_node -- convert given node to OPML and append */
|
104
|
-
void print_opml_node(GString *out, node *n, scratch_pad *scratch) {
|
105
|
-
#ifdef DEBUG_ON
|
106
|
-
fprintf(stderr, "print_opml_node: %d\n",n->key);
|
107
|
-
#endif
|
108
|
-
switch (n->key) {
|
109
|
-
case METADATA:
|
110
|
-
/* Metadata is present, so will need to be appended later */
|
111
|
-
break;
|
112
|
-
case METAKEY:
|
113
|
-
g_string_append_printf(out, "<outline text=\"");
|
114
|
-
print_opml_string(out, n->str);
|
115
|
-
g_string_append_printf(out, "\" _note=\"");
|
116
|
-
trim_trailing_newlines(n->children->str);
|
117
|
-
print_opml_string(out, n->children->str);
|
118
|
-
g_string_append_printf(out, "\"/>");
|
119
|
-
break;
|
120
|
-
case HEADINGSECTION:
|
121
|
-
/* Need to handle "nesting" properly */
|
122
|
-
g_string_append_printf(out, "<outline ");
|
123
|
-
|
124
|
-
/* Print header */
|
125
|
-
print_opml_node(out, n->children, scratch);
|
126
|
-
|
127
|
-
/* print remainder of paragraphs as note */
|
128
|
-
g_string_append_printf(out, " _note=\"");
|
129
|
-
print_opml_node_tree(out, n->children->next, scratch);
|
130
|
-
g_string_append_printf(out, "\">");
|
131
|
-
break;
|
132
|
-
case H1: case H2: case H3: case H4: case H5: case H6:
|
133
|
-
g_string_append_printf(out, "text=\"");
|
134
|
-
print_opml_string(out, n->str);
|
135
|
-
g_string_append_printf(out,"\"");
|
136
|
-
break;
|
137
|
-
case VERBATIM:
|
138
|
-
case VERBATIMFENCE:
|
139
|
-
print_opml_string(out, n->str);
|
140
|
-
break;
|
141
|
-
case SPACE:
|
142
|
-
print_opml_string(out, n->str);
|
143
|
-
break;
|
144
|
-
case STR:
|
145
|
-
print_opml_string(out, n->str);
|
146
|
-
break;
|
147
|
-
case LINEBREAK:
|
148
|
-
g_string_append_printf(out, " ");
|
149
|
-
break;
|
150
|
-
case PLAIN:
|
151
|
-
print_opml_node_tree(out, n->children, scratch);
|
152
|
-
if ((n->next != NULL) && (n->next->key == PLAIN)) {
|
153
|
-
g_string_append_printf(out, " ");
|
154
|
-
}
|
155
|
-
break;
|
156
|
-
default:
|
157
|
-
fprintf(stderr, "print_opml_node encountered unknown element key = %d\n", n->key);
|
158
|
-
exit(EXIT_FAILURE);
|
159
|
-
}
|
160
|
-
#ifdef DEBUG_ON
|
161
|
-
fprintf(stderr, "finish print_opml_node: %d\n", n->key);
|
162
|
-
#endif
|
163
|
-
}
|
164
|
-
|
165
|
-
/* print_opml_string - print string, escaping for OPML */
|
166
|
-
void print_opml_string(GString *out, char *str) {
|
167
|
-
while (*str != '\0') {
|
168
|
-
switch (*str) {
|
169
|
-
case '&':
|
170
|
-
g_string_append_printf(out, "&");
|
171
|
-
break;
|
172
|
-
case '<':
|
173
|
-
g_string_append_printf(out, "<");
|
174
|
-
break;
|
175
|
-
case '>':
|
176
|
-
g_string_append_printf(out, ">");
|
177
|
-
break;
|
178
|
-
case '"':
|
179
|
-
g_string_append_printf(out, """);
|
180
|
-
break;
|
181
|
-
case '\n': case '\r':
|
182
|
-
g_string_append_printf(out, " ");
|
183
|
-
break;
|
184
|
-
default:
|
185
|
-
g_string_append_c(out, *str);
|
186
|
-
}
|
187
|
-
str++;
|
188
|
-
}
|
189
|
-
}
|
data/MultiMarkdown-4/opml.h
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
#ifndef OPML_PARSER_H
|
2
|
-
#define OPML_PARSER_H
|
3
|
-
|
4
|
-
#include "parser.h"
|
5
|
-
#include "writer.h"
|
6
|
-
|
7
|
-
void begin_opml_output(GString *out, node* list, scratch_pad *scratch);
|
8
|
-
void print_opml_node_tree(GString *out, node *list, scratch_pad *scratch);
|
9
|
-
void print_opml_node(GString *out, node *n, scratch_pad *scratch);
|
10
|
-
void print_opml_section_and_children(GString *out, node *list, scratch_pad *scratch);
|
11
|
-
void end_opml_output(GString *out, node* list, scratch_pad *scratch);
|
12
|
-
void print_opml_string(GString *out, char *str);
|
13
|
-
|
14
|
-
|
15
|
-
#endif
|
@@ -1,884 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
|
3
|
-
parse_utilities.c -- miscellaneous support functions
|
4
|
-
|
5
|
-
(c) 2013-2015 Fletcher T. Penney (http://fletcherpenney.net/).
|
6
|
-
|
7
|
-
Derived from peg-multimarkdown, which was forked from peg-markdown,
|
8
|
-
which is (c) 2008 John MacFarlane (jgm at berkeley dot edu), and
|
9
|
-
licensed under GNU GPL or MIT.
|
10
|
-
|
11
|
-
This program is free software; you can redistribute it and/or modify
|
12
|
-
it under the terms of the GNU General Public License or the MIT
|
13
|
-
license. See LICENSE for details.
|
14
|
-
|
15
|
-
This program is distributed in the hope that it will be useful,
|
16
|
-
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
-
GNU General Public License for more details.
|
19
|
-
|
20
|
-
*/
|
21
|
-
|
22
|
-
#include "parser.h"
|
23
|
-
#include <libgen.h>
|
24
|
-
|
25
|
-
#pragma mark - Parse Tree
|
26
|
-
|
27
|
-
/* Create a new node in the parse tree */
|
28
|
-
node * mk_node(int key) {
|
29
|
-
node *result = (node *) malloc(sizeof(node));
|
30
|
-
result->key = key;
|
31
|
-
result->str = NULL;
|
32
|
-
result->children = NULL;
|
33
|
-
result->next = NULL;
|
34
|
-
result->link_data = NULL;
|
35
|
-
return result;
|
36
|
-
}
|
37
|
-
|
38
|
-
/* Create a new node in the parse tree, and store str */
|
39
|
-
node * mk_str(char *string) {
|
40
|
-
node *result = mk_node(STR);
|
41
|
-
assert(string != NULL);
|
42
|
-
result->str = strdup(string);
|
43
|
-
return result;
|
44
|
-
}
|
45
|
-
|
46
|
-
/* mk_str_from_list - merge list into a STR */
|
47
|
-
node * mk_str_from_list(node *list, bool extra_newline) {
|
48
|
-
node *result = mk_node(STR);
|
49
|
-
node *rev = reverse_list(list);
|
50
|
-
|
51
|
-
GString *c = concat_string_list(rev);
|
52
|
-
if (extra_newline)
|
53
|
-
g_string_append(c, "\n");
|
54
|
-
|
55
|
-
result->str = c->str;
|
56
|
-
|
57
|
-
g_string_free(c, false);
|
58
|
-
return result;
|
59
|
-
}
|
60
|
-
|
61
|
-
node * mk_link(node *text, char *label, char *source, char *title, node *attr) {
|
62
|
-
node *result = mk_node(LINK);
|
63
|
-
result->link_data = mk_link_data(label, source, title, attr);
|
64
|
-
result->children = text;
|
65
|
-
|
66
|
-
return result;
|
67
|
-
}
|
68
|
-
|
69
|
-
node * mk_autolink(char *text) {
|
70
|
-
char *label = label_from_string(text);
|
71
|
-
GString *anchor = g_string_new(label);
|
72
|
-
g_string_prepend(anchor, "#");
|
73
|
-
|
74
|
-
node *result = mk_node(LINK);
|
75
|
-
result->link_data = mk_link_data(label, anchor->str, NULL, NULL);
|
76
|
-
|
77
|
-
g_string_free(anchor, TRUE);
|
78
|
-
free(label);
|
79
|
-
return result;
|
80
|
-
}
|
81
|
-
|
82
|
-
/* concat_string_list - create string from STR's */
|
83
|
-
GString * concat_string_list(node *list) {
|
84
|
-
GString *result;
|
85
|
-
node *next;
|
86
|
-
result = g_string_new("");
|
87
|
-
while (list != NULL) {
|
88
|
-
assert(list->key == STR);
|
89
|
-
assert(list->str != NULL);
|
90
|
-
g_string_append(result, list->str);
|
91
|
-
next = list->next;
|
92
|
-
free_node(list);
|
93
|
-
list = next;
|
94
|
-
}
|
95
|
-
return result;
|
96
|
-
}
|
97
|
-
|
98
|
-
/* Create a node that is basically a parent for other elements */
|
99
|
-
node * mk_list(int key, node *list) {
|
100
|
-
node *result;
|
101
|
-
result = mk_node(key);
|
102
|
-
result->children = reverse_list(list);
|
103
|
-
return result;
|
104
|
-
}
|
105
|
-
|
106
|
-
/* Create a new node with position information */
|
107
|
-
node * mk_pos_node(int key, char *string, unsigned int start, unsigned int stop) {
|
108
|
-
node *result = mk_node(key);
|
109
|
-
if (string != NULL)
|
110
|
-
result->str = strdup(string);
|
111
|
-
|
112
|
-
return result;
|
113
|
-
}
|
114
|
-
|
115
|
-
/* Create a new string node with position information */
|
116
|
-
node * mk_pos_str(char *string, unsigned int start, unsigned int stop) {
|
117
|
-
node *result = mk_str(string);
|
118
|
-
|
119
|
-
return result;
|
120
|
-
}
|
121
|
-
|
122
|
-
/* Create a new list node with position information */
|
123
|
-
node * mk_pos_list(int key, node *list, unsigned int start, unsigned int stop) {
|
124
|
-
node *result = mk_list(key, list);
|
125
|
-
|
126
|
-
return result;
|
127
|
-
}
|
128
|
-
|
129
|
-
/* free just the current node and children*/
|
130
|
-
void free_node(node *n) {
|
131
|
-
if (n == NULL)
|
132
|
-
return;
|
133
|
-
|
134
|
-
if (n->str != NULL)
|
135
|
-
free(n->str);
|
136
|
-
n->str = NULL;
|
137
|
-
|
138
|
-
free_link_data(n->link_data);
|
139
|
-
n->link_data = NULL;
|
140
|
-
|
141
|
-
if (n->children != NULL) {
|
142
|
-
free_node_tree(n->children);
|
143
|
-
n->children = NULL;
|
144
|
-
}
|
145
|
-
n->next = NULL;
|
146
|
-
free(n);
|
147
|
-
}
|
148
|
-
|
149
|
-
/* free element and it's descendents/siblings */
|
150
|
-
void free_node_tree(node *n) {
|
151
|
-
node *next = NULL;
|
152
|
-
|
153
|
-
while (n != NULL) {
|
154
|
-
next = n->next;
|
155
|
-
free_node(n);
|
156
|
-
n = next;
|
157
|
-
}
|
158
|
-
}
|
159
|
-
|
160
|
-
/* print element list structure for testing */
|
161
|
-
void print_node_tree(node * n) {
|
162
|
-
while (n != NULL) {
|
163
|
-
fprintf(stderr,"node key: %d\n",n->key);
|
164
|
-
if (n->str != NULL)
|
165
|
-
fprintf(stderr,"node str: '%s'\n\n",n->str);
|
166
|
-
|
167
|
-
if (n->children != NULL) {
|
168
|
-
print_node_tree(n->children);
|
169
|
-
}
|
170
|
-
n = n->next;
|
171
|
-
}
|
172
|
-
}
|
173
|
-
|
174
|
-
/* cons -- add element to list (it goes to the beginning for performance reasons) */
|
175
|
-
node * cons(node *new, node *list) {
|
176
|
-
if (new != NULL) {
|
177
|
-
new->next = list;
|
178
|
-
return new;
|
179
|
-
}
|
180
|
-
return list;
|
181
|
-
}
|
182
|
-
|
183
|
-
/* reverse -- reverse a list to get it back into proper order */
|
184
|
-
node * reverse_list(node *list) {
|
185
|
-
node *new = NULL;
|
186
|
-
node *next = NULL;
|
187
|
-
#ifdef DEBUG_ON
|
188
|
-
if ((list != NULL) && (list->str != NULL))
|
189
|
-
fprintf(stderr, "reverse_list: '%s'\n",list->str);
|
190
|
-
#endif
|
191
|
-
|
192
|
-
while (list != NULL) {
|
193
|
-
next = list->next;
|
194
|
-
list->next = NULL;
|
195
|
-
new = cons(list, new);
|
196
|
-
list = next;
|
197
|
-
}
|
198
|
-
return new;
|
199
|
-
}
|
200
|
-
|
201
|
-
/* append_list -- add element to end of list; slower than cons */
|
202
|
-
void append_list(node *new, node *list) {
|
203
|
-
|
204
|
-
/* If we append to an empty list... */
|
205
|
-
if (list == NULL) {
|
206
|
-
list = new;
|
207
|
-
return;
|
208
|
-
}
|
209
|
-
|
210
|
-
if (new != NULL) {
|
211
|
-
node *step = list;
|
212
|
-
|
213
|
-
|
214
|
-
while (step->next != NULL) {
|
215
|
-
step = step->next;
|
216
|
-
}
|
217
|
-
/* new->next = NULL; // This could lose elements and prevents merging lists */
|
218
|
-
step->next = new;
|
219
|
-
}
|
220
|
-
}
|
221
|
-
|
222
|
-
#pragma mark - Parser Data
|
223
|
-
|
224
|
-
/* Create parser data - this is where you stash stuff to communicate
|
225
|
-
into and out of the parser */
|
226
|
-
parser_data * mk_parser_data(const char *charbuf, unsigned long extensions) {
|
227
|
-
clock_t start = clock();
|
228
|
-
|
229
|
-
parser_data *result = (parser_data *)malloc(sizeof(parser_data));
|
230
|
-
result->extensions = extensions;
|
231
|
-
result->charbuf = charbuf;
|
232
|
-
result->original = charbuf;
|
233
|
-
result->autolabels = NULL;
|
234
|
-
result->result = NULL;
|
235
|
-
|
236
|
-
result->parse_aborted = 0;
|
237
|
-
result->stop_time = start + 3 * CLOCKS_PER_SEC; /* 3 second timeout */
|
238
|
-
|
239
|
-
return result;
|
240
|
-
}
|
241
|
-
|
242
|
-
void free_parser_data(parser_data *data) {
|
243
|
-
free_node_tree(data->result);
|
244
|
-
free_node_tree(data->autolabels);
|
245
|
-
/* don't do this - it's owned by someone else -- free(data->original); */
|
246
|
-
data->original = NULL;
|
247
|
-
data->charbuf = NULL;
|
248
|
-
|
249
|
-
free(data);
|
250
|
-
}
|
251
|
-
|
252
|
-
/* mk_scratch_pad -- store stuff here while exporting the result tree */
|
253
|
-
void ran_start(long seed);
|
254
|
-
scratch_pad * mk_scratch_pad(unsigned long extensions) {
|
255
|
-
scratch_pad *result = (scratch_pad *)malloc(sizeof(scratch_pad));
|
256
|
-
result->extensions = extensions;
|
257
|
-
result->language = 0;
|
258
|
-
result->baseheaderlevel = 1;
|
259
|
-
result->printing_notes = 0;
|
260
|
-
result->notes = mk_node(KEY_COUNTER); /* Need empty need for trimming later */
|
261
|
-
result->used_notes = mk_node(KEY_COUNTER);
|
262
|
-
result->links = mk_node(KEY_COUNTER);
|
263
|
-
result->glossary = mk_node(KEY_COUNTER);
|
264
|
-
result->citations = mk_node(KEY_COUNTER);
|
265
|
-
result->abbreviations = mk_node(KEY_COUNTER);
|
266
|
-
result->result_tree = NULL;
|
267
|
-
result->padded = 2;
|
268
|
-
result->footnote_to_print = 0;
|
269
|
-
result->footnote_para_counter = 0;
|
270
|
-
result->max_footnote_num = 0;
|
271
|
-
result->obfuscate = 0;
|
272
|
-
result->no_latex_footnote = 0;
|
273
|
-
result->latex_footer = NULL;
|
274
|
-
result->odf_list_needs_end_p = FALSE;
|
275
|
-
result->odf_para_type = PARA;
|
276
|
-
result->cell_type = 0;
|
277
|
-
result->table_alignment = NULL;
|
278
|
-
result->table_column = 0;
|
279
|
-
|
280
|
-
if (extensions & EXT_RANDOM_FOOT) {
|
281
|
-
srand((int)time(NULL));
|
282
|
-
result->random_seed_base = rand() % 32000;
|
283
|
-
} else {
|
284
|
-
srand(1);
|
285
|
-
result->random_seed_base = 0;
|
286
|
-
}
|
287
|
-
ran_start(310952L);
|
288
|
-
|
289
|
-
result->lyx_para_type = PARA; /* CRC - Simple paragraph */
|
290
|
-
result->lyx_level = 0; /* CRC - out outside level */
|
291
|
-
result->no_lyx_footnote = 0; /* CRC */
|
292
|
-
result->lyx_number_headers = FALSE; /* CRC - default is not to number */
|
293
|
-
result->lyx_debug_nest = 0; /* CRC - initialize debug formatting */
|
294
|
-
result->lyx_debug_pad = g_string_new(""); /* CRC - initally, no indent */
|
295
|
-
result->lyx_definition_hit = TRUE; /* CRC - initialize to have hit it (closed) */
|
296
|
-
result->lyx_definition_open = FALSE; /* CRC - don't have an open definition */
|
297
|
-
result->lyx_in_frame = FALSE; /* CRC - not in a frame */
|
298
|
-
result->lyx_beamerbullet = FALSE; /* CRC - not in a beamer bullet */
|
299
|
-
result->lyx_debug_nest = 0; /* CRC - no nesting yet */
|
300
|
-
result->lyx_table_need_line = FALSE; /* CRC - No table yet */
|
301
|
-
result->lyx_table_total_rows = 0; /* CRC - No rows */
|
302
|
-
result->lyx_table_total_cols = 0; /* CRC - No Columns */
|
303
|
-
return result;
|
304
|
-
}
|
305
|
-
|
306
|
-
void free_scratch_pad(scratch_pad *scratch) {
|
307
|
-
#ifdef DEBUG_ON
|
308
|
-
fprintf(stderr, "free scratch pad\n");
|
309
|
-
#endif
|
310
|
-
|
311
|
-
free_node_tree(scratch->notes);
|
312
|
-
free_node_tree(scratch->used_notes);
|
313
|
-
free_node_tree(scratch->links);
|
314
|
-
free_node_tree(scratch->glossary);
|
315
|
-
free_node_tree(scratch->citations);
|
316
|
-
free_node_tree(scratch->abbreviations);
|
317
|
-
|
318
|
-
g_string_free(scratch->lyx_debug_pad, true); /* CRC - initally, no indent */
|
319
|
-
|
320
|
-
if (scratch->latex_footer != NULL)
|
321
|
-
free(scratch->latex_footer);
|
322
|
-
|
323
|
-
if (scratch->table_alignment != NULL)
|
324
|
-
free(scratch->table_alignment);
|
325
|
-
|
326
|
-
free (scratch);
|
327
|
-
#ifdef DEBUG_ON
|
328
|
-
fprintf(stderr, "finished freeing scratch\n");
|
329
|
-
#endif
|
330
|
-
}
|
331
|
-
|
332
|
-
link_data * mk_link_data(char *label, char *source, char *title, node *attr) {
|
333
|
-
link_data *result = (link_data *)malloc(sizeof(link_data));
|
334
|
-
if (label != NULL)
|
335
|
-
result->label = strdup(label);
|
336
|
-
else result->label = NULL;
|
337
|
-
if (source != NULL)
|
338
|
-
result->source = strdup(source);
|
339
|
-
else result->source = NULL;
|
340
|
-
if (title != NULL)
|
341
|
-
result->title = strdup(title);
|
342
|
-
else result->title = NULL;
|
343
|
-
|
344
|
-
result->attr = attr;
|
345
|
-
|
346
|
-
return result;
|
347
|
-
}
|
348
|
-
|
349
|
-
void free_link_data(link_data *l) {
|
350
|
-
if (l == NULL)
|
351
|
-
return;
|
352
|
-
|
353
|
-
free(l->label);
|
354
|
-
l->label = NULL;
|
355
|
-
free(l->source);
|
356
|
-
l->source = NULL;
|
357
|
-
free(l->title);
|
358
|
-
l->title = NULL;
|
359
|
-
free_node_tree(l->attr);
|
360
|
-
l->attr = NULL;
|
361
|
-
|
362
|
-
free(l);
|
363
|
-
}
|
364
|
-
|
365
|
-
/* Check if the specified extension is flagged */
|
366
|
-
bool extension(int ext, unsigned long extensions) {
|
367
|
-
return (extensions & ext);
|
368
|
-
}
|
369
|
-
|
370
|
-
/* label_from_string -- convert raw string into format suitable for use as label */
|
371
|
-
/* As of HTML 5, any character is valid, but no space. Must be HTML escaped.
|
372
|
-
But since we also use in LaTeX, ODF, will be a bit more strict.
|
373
|
-
It *looks* like UTF-8 characters are ok in ODF, and don't break LaTeX any more than
|
374
|
-
having those characters in the original string. Will still limit the ASCII characters
|
375
|
-
however, to avoid trouble.
|
376
|
-
|
377
|
-
NOTE: This is still a bit experimental, and will be removed if it breaks things.
|
378
|
-
*/
|
379
|
-
char *label_from_string(char *str) {
|
380
|
-
GString *out = g_string_new("");
|
381
|
-
char *label;
|
382
|
-
char *next_char;
|
383
|
-
|
384
|
-
while (*str != '\0') {
|
385
|
-
next_char = str;
|
386
|
-
next_char++;
|
387
|
-
/* Is this a multibyte character? */
|
388
|
-
if ((*next_char & 0xC0) == 0x80) {
|
389
|
-
g_string_append_c(out, *str);
|
390
|
-
while ((*next_char & 0xC0) == 0x80) {
|
391
|
-
str++;
|
392
|
-
/* fprintf(stderr, "multibyte\n"); */
|
393
|
-
g_string_append_c(out, *str);
|
394
|
-
next_char++;
|
395
|
-
}
|
396
|
-
}
|
397
|
-
|
398
|
-
/* can relax on following characters */
|
399
|
-
else if ((*str >= '0' && *str <= '9') || (*str >= 'A' && *str <= 'Z')
|
400
|
-
|| (*str >= 'a' && *str <= 'z') || (*str == '.') || (*str== '_')
|
401
|
-
|| (*str== '-') || (*str== ':'))
|
402
|
-
{
|
403
|
-
g_string_append_c(out, tolower(*str));
|
404
|
-
}
|
405
|
-
str++;
|
406
|
-
}
|
407
|
-
label = out->str;
|
408
|
-
g_string_free(out, false);
|
409
|
-
return label;
|
410
|
-
}
|
411
|
-
|
412
|
-
char *ascii_label_from_string(char *str) {
|
413
|
-
GString *out = g_string_new("");
|
414
|
-
char *label;
|
415
|
-
char *next_char;
|
416
|
-
|
417
|
-
while (*str != '\0') {
|
418
|
-
next_char = str;
|
419
|
-
next_char++;
|
420
|
-
/* Is this a multibyte character? */
|
421
|
-
if ((*next_char & 0xC0) == 0x80) {
|
422
|
-
while ((*next_char & 0xC0) == 0x80) {
|
423
|
-
str++;
|
424
|
-
/* fprintf(stderr, "multibyte\n"); */
|
425
|
-
next_char++;
|
426
|
-
}
|
427
|
-
}
|
428
|
-
|
429
|
-
/* can relax on following characters */
|
430
|
-
else if ((*str >= '0' && *str <= '9') || (*str >= 'A' && *str <= 'Z')
|
431
|
-
|| (*str >= 'a' && *str <= 'z') || (*str == '.') || (*str== '_')
|
432
|
-
|| (*str== '-') || (*str== ':'))
|
433
|
-
{
|
434
|
-
g_string_append_c(out, tolower(*str));
|
435
|
-
}
|
436
|
-
str++;
|
437
|
-
}
|
438
|
-
label = out->str;
|
439
|
-
g_string_free(out, false);
|
440
|
-
return label;
|
441
|
-
}
|
442
|
-
|
443
|
-
/* clean_string -- clean up whitespace */
|
444
|
-
char * clean_string(char *str) {
|
445
|
-
GString *out = g_string_new("");
|
446
|
-
char *clean;
|
447
|
-
bool block_whitespace = TRUE;
|
448
|
-
|
449
|
-
while (*str != '\0') {
|
450
|
-
if ((*str == '\t') || (*str == ' ') || (*str == '\n') || (*str == '\r')) {
|
451
|
-
if (!block_whitespace) {
|
452
|
-
g_string_append_c(out, ' ');
|
453
|
-
block_whitespace = TRUE;
|
454
|
-
}
|
455
|
-
} else {
|
456
|
-
g_string_append_c(out, *str);
|
457
|
-
block_whitespace = FALSE;
|
458
|
-
}
|
459
|
-
str++;
|
460
|
-
}
|
461
|
-
|
462
|
-
clean = out->str;
|
463
|
-
g_string_free(out, false);
|
464
|
-
return clean;
|
465
|
-
}
|
466
|
-
|
467
|
-
/* string_from_node_tree -- Returns a null-terminated string,
|
468
|
-
which must be freed after use. */
|
469
|
-
char * string_from_node_tree(node *n) {
|
470
|
-
char *result;
|
471
|
-
if (n == NULL)
|
472
|
-
return NULL;
|
473
|
-
|
474
|
-
GString *raw = g_string_new("");
|
475
|
-
print_raw_node_tree(raw, n);
|
476
|
-
|
477
|
-
result = raw->str;
|
478
|
-
g_string_free(raw, false);
|
479
|
-
|
480
|
-
return result;
|
481
|
-
}
|
482
|
-
|
483
|
-
/* label_from_node_tree -- Returns a null-terminated string,
|
484
|
-
which must be freed after use. */
|
485
|
-
char * label_from_node_tree(node *n) {
|
486
|
-
char *label;
|
487
|
-
if (n == NULL)
|
488
|
-
return NULL;
|
489
|
-
|
490
|
-
#ifdef DEBUG_ON
|
491
|
-
fprintf(stderr, "\n\nstart label from node_tree\n");
|
492
|
-
#endif
|
493
|
-
GString *raw = g_string_new("");
|
494
|
-
print_raw_node_tree(raw, n);
|
495
|
-
|
496
|
-
#ifdef DEBUG_ON
|
497
|
-
fprintf(stderr, "halfway('%s')\n",raw->str);
|
498
|
-
#endif
|
499
|
-
label = label_from_string(raw->str);
|
500
|
-
g_string_free(raw,true);
|
501
|
-
#ifdef DEBUG_ON
|
502
|
-
fprintf(stderr, "finish label from node_tree: '%s'\n",label);
|
503
|
-
#endif
|
504
|
-
return label;
|
505
|
-
}
|
506
|
-
|
507
|
-
/* ascii_label_from_node_tree -- Returns a null-terminated string,
|
508
|
-
which must be freed after use. */
|
509
|
-
char * ascii_label_from_node_tree(node *n) {
|
510
|
-
char *label;
|
511
|
-
if (n == NULL)
|
512
|
-
return NULL;
|
513
|
-
|
514
|
-
#ifdef DEBUG_ON
|
515
|
-
fprintf(stderr, "\n\nstart label from node_tree\n");
|
516
|
-
#endif
|
517
|
-
GString *raw = g_string_new("");
|
518
|
-
print_raw_node_tree(raw, n);
|
519
|
-
|
520
|
-
#ifdef DEBUG_ON
|
521
|
-
fprintf(stderr, "halfway('%s')\n",raw->str);
|
522
|
-
#endif
|
523
|
-
label = ascii_label_from_string(raw->str);
|
524
|
-
g_string_free(raw,true);
|
525
|
-
#ifdef DEBUG_ON
|
526
|
-
fprintf(stderr, "finish label from node_tree: '%s'\n",label);
|
527
|
-
#endif
|
528
|
-
return label;
|
529
|
-
}
|
530
|
-
|
531
|
-
/* label_from_node -- Returns a null-terminated string,
|
532
|
-
which must be freed after use. */
|
533
|
-
char *label_from_node(node *n) {
|
534
|
-
char *label;
|
535
|
-
char *label2;
|
536
|
-
if (n == NULL)
|
537
|
-
return NULL;
|
538
|
-
|
539
|
-
GString *raw = g_string_new("");
|
540
|
-
print_raw_node(raw, n);
|
541
|
-
label = label_from_string(raw->str);
|
542
|
-
label2 = strdup(label);
|
543
|
-
free(label);
|
544
|
-
g_string_free(raw,true);
|
545
|
-
return label2;
|
546
|
-
}
|
547
|
-
|
548
|
-
/* ascii_label_from_node -- Returns a null-terminated string,
|
549
|
-
which must be freed after use. */
|
550
|
-
char *ascii_label_from_node(node *n) {
|
551
|
-
char *label;
|
552
|
-
char *label2;
|
553
|
-
if (n == NULL)
|
554
|
-
return NULL;
|
555
|
-
|
556
|
-
GString *raw = g_string_new("");
|
557
|
-
print_raw_node(raw, n);
|
558
|
-
label = ascii_label_from_string(raw->str);
|
559
|
-
label2 = strdup(label);
|
560
|
-
free(label);
|
561
|
-
g_string_free(raw,true);
|
562
|
-
return label2;
|
563
|
-
}
|
564
|
-
|
565
|
-
/* print_raw_node - print an element as original text */
|
566
|
-
void print_raw_node(GString *out, node *n) {
|
567
|
-
if (n->str != NULL) {
|
568
|
-
#ifdef DEBUG_ON
|
569
|
-
fprintf(stderr, "print raw node %d: '%s'\n",n->key, n->str);
|
570
|
-
#endif
|
571
|
-
g_string_append_printf(out, "%s", n->str);
|
572
|
-
} else if (n->key == LINK) {
|
573
|
-
#ifdef DEBUG_ON
|
574
|
-
fprintf(stderr, "print raw node children from link\n");
|
575
|
-
#endif
|
576
|
-
/* this gets the text */
|
577
|
-
print_raw_node_tree(out, n->children);
|
578
|
-
/* need the label */
|
579
|
-
if ((n->link_data != NULL) && (n->link_data->label != NULL))
|
580
|
-
g_string_append_printf(out, "%s",n->link_data->label);
|
581
|
-
} else {
|
582
|
-
/* All others */
|
583
|
-
#ifdef DEBUG_ON
|
584
|
-
fprintf(stderr, "print raw node children from %d\n",n->key);
|
585
|
-
#endif
|
586
|
-
print_raw_node_tree(out, n->children);
|
587
|
-
}
|
588
|
-
#ifdef DEBUG_ON
|
589
|
-
fprintf(stderr, "finish print raw node %d: '%s'\n'%s'\n",n->key, n->str, out->str);
|
590
|
-
#endif
|
591
|
-
}
|
592
|
-
|
593
|
-
/* print_raw_node_tree - print a list of elements as original text */
|
594
|
-
void print_raw_node_tree(GString *out, node *n) {
|
595
|
-
#ifdef DEBUG_ON
|
596
|
-
if (n != NULL)
|
597
|
-
fprintf(stderr, "start print_raw_node_tree: '%d'\n",n->key);
|
598
|
-
#endif
|
599
|
-
while (n != NULL) {
|
600
|
-
print_raw_node(out, n);
|
601
|
-
n = n->next;
|
602
|
-
}
|
603
|
-
#ifdef DEBUG_ON
|
604
|
-
if (n != NULL)
|
605
|
-
fprintf(stderr, "finish print_raw_node_tree: '%d'\n",n->key);
|
606
|
-
#endif
|
607
|
-
}
|
608
|
-
|
609
|
-
/* preformat_text - allocate and copy text buffer while
|
610
|
-
* performing tab expansion. */
|
611
|
-
char * preformat_text(const char *text) {
|
612
|
-
GString *buf;
|
613
|
-
char next_char;
|
614
|
-
int charstotab;
|
615
|
-
char *out;
|
616
|
-
|
617
|
-
int len = 0;
|
618
|
-
|
619
|
-
buf = g_string_new("");
|
620
|
-
|
621
|
-
charstotab = TABSTOP;
|
622
|
-
while ((next_char = *text++) != '\0') {
|
623
|
-
switch (next_char) {
|
624
|
-
case '\t':
|
625
|
-
while (charstotab > 0)
|
626
|
-
g_string_append_c(buf, ' '), len++, charstotab--;
|
627
|
-
break;
|
628
|
-
case '\n':
|
629
|
-
g_string_append_c(buf, '\n'), len++, charstotab = TABSTOP;
|
630
|
-
break;
|
631
|
-
default:
|
632
|
-
g_string_append_c(buf, next_char), len++, charstotab--;
|
633
|
-
}
|
634
|
-
if (charstotab == 0)
|
635
|
-
charstotab = TABSTOP;
|
636
|
-
}
|
637
|
-
g_string_append_printf(buf, "\n\n");
|
638
|
-
out = buf->str;
|
639
|
-
g_string_free(buf,false);
|
640
|
-
return(out);
|
641
|
-
}
|
642
|
-
|
643
|
-
/* Don't let us get caught in "infinite" loop;
|
644
|
-
1 means we're ok
|
645
|
-
0 means we're stuck -- abort */
|
646
|
-
bool check_timeout(parser_data *data) {
|
647
|
-
/* Once we abort, keep aborting */
|
648
|
-
if (data->parse_aborted)
|
649
|
-
return 0;
|
650
|
-
if (clock() > data->stop_time) {
|
651
|
-
data->parse_aborted = 1;
|
652
|
-
return 0;
|
653
|
-
}
|
654
|
-
return 1;
|
655
|
-
}
|
656
|
-
|
657
|
-
/* determine whether a certain element is contained within a given list */
|
658
|
-
bool tree_contains_key(node *list, int key) {
|
659
|
-
node *step = NULL;
|
660
|
-
|
661
|
-
step = list;
|
662
|
-
while ( step != NULL ) {
|
663
|
-
if (step->key == key) {
|
664
|
-
return TRUE;
|
665
|
-
}
|
666
|
-
if (step->children != NULL) {
|
667
|
-
if (tree_contains_key(step->children, key)) {
|
668
|
-
return TRUE;
|
669
|
-
}
|
670
|
-
}
|
671
|
-
step = step->next;
|
672
|
-
}
|
673
|
-
return FALSE;
|
674
|
-
}
|
675
|
-
|
676
|
-
/* Count number of matches of type */
|
677
|
-
int tree_contains_key_count(node *list, int key) {
|
678
|
-
node *step = NULL;
|
679
|
-
int counter = 0;
|
680
|
-
|
681
|
-
step = list;
|
682
|
-
while ( step != NULL ) {
|
683
|
-
if (step->key == key) {
|
684
|
-
counter++;
|
685
|
-
}
|
686
|
-
if (step->children != NULL) {
|
687
|
-
counter += tree_contains_key_count(step->children, key);
|
688
|
-
}
|
689
|
-
step = step->next;
|
690
|
-
}
|
691
|
-
return counter;
|
692
|
-
}
|
693
|
-
|
694
|
-
/* list all metadata keys, if present */
|
695
|
-
char * metadata_keys(node *list) {
|
696
|
-
node *step = NULL;
|
697
|
-
step = list;
|
698
|
-
GString *out = g_string_new("");
|
699
|
-
char *temp;
|
700
|
-
|
701
|
-
while (step != NULL) {
|
702
|
-
if (step->key == METADATA) {
|
703
|
-
/* search METAKEY children */
|
704
|
-
step = step->children;
|
705
|
-
while ( step != NULL) {
|
706
|
-
temp = label_from_string(step->str);
|
707
|
-
g_string_append_printf(out,"%s\n",temp);
|
708
|
-
free(temp);
|
709
|
-
step = step->next;
|
710
|
-
}
|
711
|
-
|
712
|
-
temp = out->str;
|
713
|
-
g_string_free(out, false);
|
714
|
-
|
715
|
-
return temp;
|
716
|
-
}
|
717
|
-
step = step->next;
|
718
|
-
}
|
719
|
-
temp = out->str;
|
720
|
-
g_string_free(out, false);
|
721
|
-
|
722
|
-
return temp;
|
723
|
-
}
|
724
|
-
|
725
|
-
/* find specified metadata key, if present */
|
726
|
-
node * metadata_for_key(char *key, node *list) {
|
727
|
-
node *step = NULL;
|
728
|
-
step = list;
|
729
|
-
char *label;
|
730
|
-
char *temp;
|
731
|
-
|
732
|
-
label = label_from_string(key);
|
733
|
-
|
734
|
-
while (step != NULL) {
|
735
|
-
if (step->key == METADATA) {
|
736
|
-
/* search METAKEY children */
|
737
|
-
step = step->children;
|
738
|
-
while ( step != NULL) {
|
739
|
-
temp = label_from_string(step->str);
|
740
|
-
if (strcmp(temp, label) == 0) {
|
741
|
-
free(temp);
|
742
|
-
free(label);
|
743
|
-
return step;
|
744
|
-
}
|
745
|
-
free(temp);
|
746
|
-
step = step->next;
|
747
|
-
}
|
748
|
-
free(label);
|
749
|
-
return NULL;
|
750
|
-
}
|
751
|
-
step = step->next;
|
752
|
-
}
|
753
|
-
free(label);
|
754
|
-
return NULL;
|
755
|
-
}
|
756
|
-
|
757
|
-
/* metavalue_for_key -- return the string value for metadata */
|
758
|
-
char * metavalue_for_key(char *key, node *list) {
|
759
|
-
char *result;
|
760
|
-
|
761
|
-
list = metadata_for_key(key, list);
|
762
|
-
|
763
|
-
if (list == NULL)
|
764
|
-
return NULL;
|
765
|
-
|
766
|
-
result = strdup(list->children->str);
|
767
|
-
trim_trailing_whitespace(result);
|
768
|
-
|
769
|
-
return result;
|
770
|
-
}
|
771
|
-
|
772
|
-
/* Trim spaces at end of string */
|
773
|
-
void trim_trailing_whitespace(char *str) {
|
774
|
-
unsigned long l;
|
775
|
-
|
776
|
-
if (str == NULL)
|
777
|
-
return;
|
778
|
-
|
779
|
-
l = strlen(str);
|
780
|
-
|
781
|
-
if (l < 1)
|
782
|
-
return;
|
783
|
-
|
784
|
-
while ( (l > 0) && (( str[l - 1] == ' ' ) ||
|
785
|
-
( str[l - 1] == '\n' ) ||
|
786
|
-
( str[l - 1] == '\r' ) ||
|
787
|
-
( str[l - 1] == '\t' )) ) {
|
788
|
-
str[l - 1] = '\0';
|
789
|
-
l = strlen(str);
|
790
|
-
}
|
791
|
-
}
|
792
|
-
|
793
|
-
/* Trim spaces at end of string */
|
794
|
-
void trim_trailing_newlines(char *str) {
|
795
|
-
unsigned long l;
|
796
|
-
|
797
|
-
if (str == NULL)
|
798
|
-
return;
|
799
|
-
|
800
|
-
l = strlen(str);
|
801
|
-
|
802
|
-
if (l < 1)
|
803
|
-
return;
|
804
|
-
|
805
|
-
while ( (l > 0) && (( str[l - 1] == '\n' ) ||
|
806
|
-
( str[l - 1] == '\r' )) ) {
|
807
|
-
str[l - 1] = '\0';
|
808
|
-
l = strlen(str);
|
809
|
-
}
|
810
|
-
}
|
811
|
-
|
812
|
-
/* Return version */
|
813
|
-
char * mmd_version(void) {
|
814
|
-
char *result;
|
815
|
-
result = strdup(MMD_VERSION);
|
816
|
-
return result;
|
817
|
-
}
|
818
|
-
|
819
|
-
void debug_node(node *n) {
|
820
|
-
if (n != NULL) {
|
821
|
-
fprintf(stderr, "node (%d) '%s'\n",n->key, n->str);
|
822
|
-
if (n->children != NULL)
|
823
|
-
debug_node_tree(n->children);
|
824
|
-
}
|
825
|
-
}
|
826
|
-
|
827
|
-
void debug_node_tree(node *n) {
|
828
|
-
while (n != NULL) {
|
829
|
-
fprintf(stderr, "node (%d) '%s'\n",n->key, n->str);
|
830
|
-
if (n->children != NULL)
|
831
|
-
debug_node_tree(n->children);
|
832
|
-
n = n->next;
|
833
|
-
}
|
834
|
-
}
|
835
|
-
|
836
|
-
node * copy_node(node *n) {
|
837
|
-
if (n == NULL)
|
838
|
-
return NULL;
|
839
|
-
else {
|
840
|
-
node *m = (node *) malloc(sizeof(node));
|
841
|
-
|
842
|
-
*m = *n;
|
843
|
-
|
844
|
-
if (n->str != NULL)
|
845
|
-
m->str = strdup(n->str);
|
846
|
-
|
847
|
-
if (n->link_data != NULL) {
|
848
|
-
m->link_data = mk_link_data(n->link_data->label, n->link_data->source, n->link_data->title, copy_node_tree(n->link_data->attr));
|
849
|
-
}
|
850
|
-
|
851
|
-
if (n->children != NULL)
|
852
|
-
m->children = copy_node_tree(n->children);
|
853
|
-
|
854
|
-
return m;
|
855
|
-
}
|
856
|
-
}
|
857
|
-
|
858
|
-
node * copy_node_tree(node *n) {
|
859
|
-
if (n == NULL)
|
860
|
-
return NULL;
|
861
|
-
else {
|
862
|
-
node *m = copy_node(n);
|
863
|
-
|
864
|
-
if (n->next != NULL)
|
865
|
-
m->next = copy_node_tree(n->next);
|
866
|
-
|
867
|
-
return m;
|
868
|
-
}
|
869
|
-
}
|
870
|
-
|
871
|
-
char * my_strndup(const char * source, size_t n) {
|
872
|
-
size_t len = strlen(source);
|
873
|
-
char * result;
|
874
|
-
|
875
|
-
if (n < len)
|
876
|
-
len = n;
|
877
|
-
|
878
|
-
result = malloc(len + 1);
|
879
|
-
|
880
|
-
memcpy(result, source, len);
|
881
|
-
result[len] = '\0';
|
882
|
-
|
883
|
-
return result;
|
884
|
-
}
|