forked from fletcher/MultiMarkdown-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transclude.c
333 lines (267 loc) · 9.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
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"
#if defined(__WIN32)
#include <windows.h>
#endif
/* Windows can use either `\` or `/` as a separator -- thanks to t-beckmann on github
for suggesting a fix for this. */
bool is_separator(char c) {
#if defined(__WIN32)
return c == '\\' || c == '/';
#else
return c == '/';
#endif
}
/* 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) && (is_separator(base[0]))) {
path = g_string_new(base);
} else {
path = g_string_new(dir);
/* Ensure that folder ends in "/" */
if (!is_separator(path->str[strlen(path->str)-1]) ) {
g_string_append_c(path, sep);
}
g_string_append_printf(path, "%s", base);
}
result = path->str;
g_string_free(path, false);
return result;
}
/* Separate filename and directory from a full path */
/* See http://stackoverflow.com/questions/1575278/function-to-split-a-filepath-into-path-and-file */
void split_path_file(char** dir, char** file, char *path) {
char *slash = path, *next;
#if defined(__WIN32)
const char sep[] = "\\/"; // Windows allows either variant
#else
const char sep[] = "/";
#endif
while ((next = strpbrk(slash + 1, sep))) slash = next;
if (path != slash) slash++;
*dir = my_strndup(path, slash - path);
*file = strdup(slash);
}
/* Return pointer to beginning of text without metadata */
/* NOTE: This is not a new string, and does not need to be freed separately */
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 */
/* TODO: This could miss YAML Metadata that does not contain
blank line afterwards */
result = strstr(source, "\n\n");
if (result != NULL)
return result + 2;
}
/* No metadata, so return original pointer */
/* But check for UTF-8 BOM and skip if present */
if (strncmp(source, "\xef\xbb\xbf",3) == 0)
return source + 3;
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, GString *manifest) {
char *base = NULL;
char *path = NULL;
char *start;
char *stop;
char *temp;
int curchar;
size_t pos;
char real[1000];
FILE *input;
long offset;
if (basedir == NULL) {
base = strdup("");
} else {
base = strdup(basedir);
}
GString *folder = NULL;
GString *filename = NULL;
GString *filebuffer = NULL;
GString *stackstring = NULL;
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;
/* Check that we found something reasonable -- we cap at 1000 characters */
if (stop - start < 1000) {
strncpy(real,start+2,stop-start-2);
real[stop-start-2] = '\0';
if (is_separator(real[0])) {
filename = g_string_new(real);
} else {
filename = g_string_new(folder->str);
g_string_append_printf(filename, "%s",real);
}
if (strcmp(filename->str,"./TOC") == 0) {
pos = stop - source->str;
start = strstr(source->str + pos,"{{");
g_string_free(filename, true);
continue;
}
/* Adjust for wildcard extensions */
/* But not if output_format == 0 */
if (output_format && 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;
/* Add to the manifest (if not already included) */
if (manifest != NULL) {
temp = strstr(manifest->str,filename->str);
offset = temp - manifest->str;
if ((temp != NULL) &&
((temp == manifest->str) || ((manifest->str)[offset - 1] == '\n')) &&
(temp[strlen(filename->str)] == '\n') ){
/* Already on manifest, so don't add again */
} else {
g_string_append_printf(manifest,"%s\n",filename->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 defined(__WIN32)
int wchars_num = MultiByteToWideChar(CP_UTF8, 0, filename->str, -1, NULL, 0);
wchar_t wstr[wchars_num];
MultiByteToWideChar(CP_UTF8, 0, filename->str, -1, wstr, wchars_num);
if ((input = _wfopen(wstr, L"r")) != NULL ) {
#else
if ((input = fopen(filename->str, "r")) != NULL ) {
#endif
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 */
/* We want to reset the base directory if we enter a subdirectory */
char * new_dir;
char * file_only;
split_path_file(&new_dir, &file_only, filename->str);
/* transclude_source(filebuffer, folder->str, stackstring->str, output_format, manifest); */
transclude_source(filebuffer, new_dir, stackstring->str, output_format, manifest);
free(new_dir);
free(file_only);
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);
} else {
/* fprintf(stderr, "error opening file: %s\n", filename->str); */
}
g_string_free(filename, true);
} else {
/* Our "match" was > 1000 characters long */
pos = stop - source->str;
}
start = strstr(source->str + pos,"{{");
}
g_string_free(folder, true);
free(path);
free(base);
}
/* Allow for a footer to specify files to be appended to the end of the text, and then transcluded.
Useful for appending a list of footnotes, citations, abbreviations, etc. to each separate file,
but not including multiple copies when processing the master file. */
void append_mmd_footer(GString *source) {
/* Look for mmd_footer metadata */
if (has_metadata(source->str, 0x000000)) {
char *meta = extract_metadata_value(source->str, 0x000000, "mmdfooter");
if (meta != NULL)
g_string_append_printf(source, "\n\n{{%s}}\n", meta);
}
}
void prepend_mmd_header(GString *source) {
/* Same thing, but to be inserted after metadata and before content */
if (has_metadata(source->str, 0x000000)) {
char *meta = extract_metadata_value(source->str, 0x000000, "mmdheader");
if (meta != NULL) {
char *content = strstr(source->str, "\n\n");
if (content != NULL) {
size_t pos = content - source->str;
g_string_insert_printf(source, pos, "\n\n{{%s}}", meta);
} else {
g_string_append_printf(source, "\n\n{{%s}}\n", meta);
}
}
}
}