forked from fletcher/MultiMarkdown-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transclude.c
213 lines (163 loc) · 5.4 KB
/
transclude.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
transclude.c -- miscellaneous support functions
(c) 2013 Fletcher T. Penney (http://fletcherpenney.net/).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License or the MIT
license. See LICENSE for details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "transclude.h"
#include "parser.h"
/* Combine directory and filename to create a full path */
char * path_from_dir_base(char *dir, char *base) {
#if defined(__WIN32)
char sep = '\\';
#else
char sep = '/';
#endif
GString *path = NULL;
char *result;
if ((base != NULL) && (base[0] == sep)) {
path = g_string_new(base);
} else {
path = g_string_new(dir);
/* Ensure that folder ends in "/" */
if (!(path->str[strlen(path->str)-1] == sep) ) {
g_string_append_c(path, sep);
}
g_string_append_printf(path, "%s", base);
}
result = path->str;
g_string_free(path, false);
return result;
}
/* Return pointer to beginning of text without metadata */
char * source_without_metadata(char * source, unsigned long extensions ) {
char *result;
if (has_metadata(source, extensions)) {
/* If we have metadata, then return just the body */
result = strstr(source, "\n\n");
if (result != NULL)
return result+2;
}
/* No metadata, so return original pointer */
return source;
}
/* Given a GString containing MMD source, and optional base directory,
substitute transclusion references in the source
Pass the path to the current folder if available -- should be a full path.
Keep track of what we're parsing to prevent recursion using stack. */
void transclude_source(GString *source, char *basedir, char *stack, int output_format) {
char *base = strdup(basedir);
char *path = strdup(base);
char *start;
char *stop;
char *temp;
int curchar;
size_t pos;
char real[1000];
FILE *input;
GString *folder = NULL;
GString *filename = NULL;
GString *filebuffer = NULL;
GString *stackstring = NULL;
/* Change to file directory */
if (base == NULL) {
base = strdup("");
path = strdup(base);
}
/* Look for override folder inside document */
if (has_metadata(source->str, 0x000000)) {
char *meta = extract_metadata_value(source->str, 0x000000, "transcludebase");
if (meta != NULL)
path = path_from_dir_base(base, meta);
}
if (path == NULL) {
/* We have nowhere to look, so nothing to do */
free(path);
free(base);
return;
}
folder = g_string_new(path);
/* Ensure that folder ends in "/" */
/* TODO: adjust for windows */
if (!(folder->str[strlen(folder->str)-1] == '/') ) {
g_string_append_c(folder, '/');
}
//fprintf(stderr, "Transclude using '%s'\n", folder->str);
/* Iterate through {{foo.txt}} and substitute contents of file without metadata */
start = strstr(source->str,"{{");
while (start != NULL) {
stop = strstr(start,"}}");
if (stop == NULL)
break;
// TODO: Need to check that we found something reasonable
strncpy(real,start+2,stop-start-2);
real[stop-start-2] = '\0';
filename = g_string_new(folder->str);
g_string_append_printf(filename, "%s",real);
/* Adjust for wildcard extensions */
if (strncmp(&filename->str[strlen(filename->str) - 2],".*",2) == 0) {
g_string_erase(filename, strlen(filename->str) - 2, 2);
if (output_format == TEXT_FORMAT) {
g_string_append(filename,".txt");
} else if (output_format == HTML_FORMAT) {
g_string_append(filename,".html");
} else if (output_format == LATEX_FORMAT) {
g_string_append(filename,".tex");
} else if (output_format == BEAMER_FORMAT) {
g_string_append(filename,".tex");
} else if (output_format == MEMOIR_FORMAT) {
g_string_append(filename,".tex");
} else if (output_format == ODF_FORMAT) {
g_string_append(filename,".fodt");
} else if (output_format == OPML_FORMAT) {
g_string_append(filename,".opml");
} else if (output_format == LYX_FORMAT) {
g_string_append(filename,".lyx");
} else if (output_format == RTF_FORMAT) {
g_string_append(filename,".rtf");
} else {
/* default extension -- in this case we only have 1 */
g_string_append(filename,".txt");
}
}
pos = stop - source->str;
/* Don't reparse ourselves */
if (stack != NULL) {
temp = strstr(stack,filename->str);
if ((temp != NULL) && (temp[strlen(filename->str)] == '\n')){
start = strstr(source->str + pos,"{{");
g_string_free(filename, true);
continue;
}
}
/* Read file */
if ((input = fopen(filename->str, "r")) != NULL ) {
filebuffer = g_string_new("");
while ((curchar = fgetc(input)) != EOF)
g_string_append_c(filebuffer, curchar);
fclose(input);
pos = start - source->str;
g_string_erase(source, pos, 2 + stop - start);
/* Update stack list */
stackstring = g_string_new(stack);
g_string_append_printf(stackstring,"%s\n",filename->str);
/* Recursively transclude files */
transclude_source(filebuffer, folder->str, stackstring->str, output_format);
temp = source_without_metadata(filebuffer->str, 0x000000);
g_string_insert(source, pos, temp);
pos += strlen(temp);
g_string_free(filebuffer, true);
g_string_free(stackstring, true);
}
start = strstr(source->str + pos,"{{");
g_string_free(filename, true);
}
g_string_free(folder, true);
free(path);
free(base);
}