rmultimarkdown 6.2.2.1 → 6.4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/ext/Makefile +2 -2
  3. data/ext/mmd/aho-corasick.c +12 -8
  4. data/ext/mmd/beamer.c +29 -0
  5. data/ext/mmd/critic_markup.c +100 -4
  6. data/ext/mmd/critic_markup.h +7 -0
  7. data/ext/mmd/d_string.c +502 -119
  8. data/ext/mmd/epub.c +2 -4
  9. data/ext/mmd/file.c +436 -0
  10. data/ext/mmd/file.h +153 -0
  11. data/ext/mmd/html.c +130 -37
  12. data/ext/mmd/include/d_string.h +20 -19
  13. data/ext/mmd/include/libMultiMarkdown.h +42 -27
  14. data/ext/mmd/include/token.h +15 -15
  15. data/ext/mmd/latex.c +107 -30
  16. data/ext/mmd/lexer.c +19 -7
  17. data/ext/mmd/lexer.h +2 -2
  18. data/ext/mmd/memoir.c +29 -0
  19. data/ext/mmd/mmd.c +65 -39
  20. data/ext/mmd/object_pool.h +4 -4
  21. data/ext/mmd/opendocument-content.c +95 -13
  22. data/ext/mmd/opendocument.c +315 -313
  23. data/ext/mmd/opml-lexer.c +2183 -0
  24. data/ext/mmd/opml-lexer.h +157 -0
  25. data/ext/mmd/opml-parser.c +1193 -0
  26. data/ext/mmd/opml-parser.h +15 -0
  27. data/ext/mmd/opml-reader.c +435 -0
  28. data/ext/mmd/opml-reader.h +111 -0
  29. data/ext/mmd/opml.c +511 -0
  30. data/ext/mmd/opml.h +115 -0
  31. data/ext/mmd/parser.c +2 -0
  32. data/ext/mmd/rng.c +1 -1
  33. data/ext/mmd/scanners.c +51663 -24824
  34. data/ext/mmd/stack.c +4 -2
  35. data/ext/mmd/stack.h +8 -8
  36. data/ext/mmd/textbundle.c +2 -4
  37. data/ext/mmd/token.c +24 -12
  38. data/ext/mmd/token_pairs.c +2 -2
  39. data/ext/mmd/token_pairs.h +10 -10
  40. data/ext/mmd/transclude.c +1 -226
  41. data/ext/mmd/transclude.h +0 -8
  42. data/ext/mmd/uuid.c +3 -3
  43. data/ext/mmd/version.h +3 -3
  44. data/ext/mmd/writer.c +99 -30
  45. data/ext/mmd/writer.h +11 -0
  46. data/lib/multi_markdown.bundle +0 -0
  47. data/lib/multi_markdown/version.rb +1 -1
  48. metadata +13 -5
  49. data/ext/mmd/fodt.c +0 -2288
  50. data/ext/mmd/fodt.h +0 -81
@@ -63,10 +63,10 @@
63
63
  #endif
64
64
 
65
65
  #include "epub.h"
66
+ #include "file.h"
66
67
  #include "html.h"
67
68
  #include "i18n.h"
68
69
  #include "miniz.h"
69
- #include "transclude.h"
70
70
  #include "uuid.h"
71
71
  #include "writer.h"
72
72
  #include "zip.h"
@@ -211,7 +211,7 @@ char * epub_package_document(scratch_pad * scratch) {
211
211
  struct tm * today = localtime(&t);
212
212
 
213
213
  d_string_append_printf(out, "<meta property=\"dcterms:modified\">%d-%02d-%02d</meta>\n",
214
- today->tm_year + 1900, today->tm_mon + 1, today->tm_mday);
214
+ today->tm_year + 1900, today->tm_mon + 1, today->tm_mday);
215
215
  }
216
216
 
217
217
  d_string_append(out, "</metadata>\n");
@@ -437,8 +437,6 @@ static void add_assets(mz_zip_archive * pZip, mmd_engine * e, const char * direc
437
437
  char destination[100] = "OEBPS/assets/";
438
438
  destination[49] = '\0';
439
439
 
440
- mz_bool status;
441
-
442
440
  HASH_ITER(hh, e->asset_hash, a, a_tmp) {
443
441
 
444
442
  memcpy(&destination[13], a->asset_path, 36);
@@ -0,0 +1,436 @@
1
+ /**
2
+
3
+ MultiMarkdown -- Lightweight markup processor to produce HTML, LaTeX, and more.
4
+
5
+ @file file.c
6
+
7
+ @brief
8
+
9
+
10
+ @author Fletcher T. Penney
11
+ @bug
12
+
13
+ **/
14
+
15
+ /*
16
+
17
+ Copyright © 2016 - 2017 Fletcher T. Penney.
18
+
19
+
20
+ The `MultiMarkdown 6` project is released under the MIT License..
21
+
22
+ GLibFacade.c and GLibFacade.h are from the MultiMarkdown v4 project:
23
+
24
+ https://github.com/fletcher/MultiMarkdown-4/
25
+
26
+ MMD 4 is released under both the MIT License and GPL.
27
+
28
+
29
+ CuTest is released under the zlib/libpng license. See CuTest.c for the
30
+ text of the license.
31
+
32
+ uthash library:
33
+ Copyright (c) 2005-2016, Troy D. Hanson
34
+
35
+ Licensed under Revised BSD license
36
+
37
+ miniz library:
38
+ Copyright 2013-2014 RAD Game Tools and Valve Software
39
+ Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
40
+
41
+ Licensed under the MIT license
42
+
43
+ argtable3 library:
44
+ Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
45
+ <sheitmann@users.sourceforge.net>
46
+ All rights reserved.
47
+
48
+ Licensed under the Revised BSD License
49
+
50
+
51
+ ## The MIT License ##
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining
54
+ a copy of this software and associated documentation files (the
55
+ "Software"), to deal in the Software without restriction, including
56
+ without limitation the rights to use, copy, modify, merge, publish,
57
+ distribute, sublicense, and/or sell copies of the Software, and to
58
+ permit persons to whom the Software is furnished to do so, subject to
59
+ the following conditions:
60
+
61
+ The above copyright notice and this permission notice shall be
62
+ included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
65
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
66
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
67
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
68
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
69
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
70
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
71
+
72
+
73
+ ## Revised BSD License ##
74
+
75
+ Redistribution and use in source and binary forms, with or without
76
+ modification, are permitted provided that the following conditions are
77
+ met:
78
+ * Redistributions of source code must retain the above copyright
79
+ notice, this list of conditions and the following disclaimer.
80
+ * Redistributions in binary form must reproduce the above
81
+ copyright notice, this list of conditions and the following
82
+ disclaimer in the documentation and/or other materials provided
83
+ with the distribution.
84
+ * Neither the name of the <organization> nor the
85
+ names of its contributors may be used to endorse or promote
86
+ products derived from this software without specific prior
87
+ written permission.
88
+
89
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
90
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
91
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
92
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT
93
+ HOLDER> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
94
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
95
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR
96
+ PROFITS OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
97
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
98
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
99
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100
+
101
+
102
+ */
103
+
104
+
105
+ #include <stdio.h>
106
+ #include <stdlib.h>
107
+ #include <string.h>
108
+
109
+ #include "d_string.h"
110
+ #include "file.h"
111
+
112
+ #if defined(__WIN32)
113
+ #include <windows.h>
114
+ #endif
115
+
116
+ #define kBUFFERSIZE 4096 // How many bytes to read at a time
117
+
118
+
119
+ /// Scan file into a DString
120
+ DString * scan_file(const char * fname) {
121
+ /* Read from stdin and return a DString *
122
+ `buffer` will need to be freed elsewhere */
123
+
124
+ char chunk[kBUFFERSIZE];
125
+ size_t bytes;
126
+
127
+ FILE * file;
128
+
129
+ #if defined(__WIN32)
130
+ int wchars_num = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
131
+ wchar_t wstr[wchars_num];
132
+ MultiByteToWideChar(CP_UTF8, 0, fname, -1, wstr, wchars_num);
133
+
134
+ if ((file = _wfopen(wstr, L"rb")) == NULL) {
135
+ #else
136
+
137
+ if ((file = fopen(fname, "r")) == NULL ) {
138
+ #endif
139
+
140
+ return NULL;
141
+ }
142
+
143
+ DString * buffer = d_string_new("");
144
+
145
+ while ((bytes = fread(chunk, 1, kBUFFERSIZE, file)) > 0) {
146
+ d_string_append_c_array(buffer, chunk, bytes);
147
+
148
+ if (buffer->currentStringLength <= kBUFFERSIZE) {
149
+ // Strip BOM
150
+ if (strncmp(buffer->str, "\xef\xbb\xbf", 3) == 0) {
151
+ d_string_erase(buffer, 0, 3);
152
+ }
153
+ }
154
+ }
155
+
156
+ fclose(file);
157
+
158
+ return buffer;
159
+ }
160
+
161
+
162
+ /// Scan from stdin into a DString
163
+ DString * stdin_buffer() {
164
+ /* Read from stdin and return a GString *
165
+ `buffer` will need to be freed elsewhere */
166
+
167
+ char chunk[kBUFFERSIZE];
168
+ size_t bytes;
169
+
170
+ DString * buffer = d_string_new("");
171
+
172
+ while ((bytes = fread(chunk, 1, kBUFFERSIZE, stdin)) > 0) {
173
+ d_string_append_c_array(buffer, chunk, bytes);
174
+ }
175
+
176
+ fclose(stdin);
177
+
178
+ return buffer;
179
+ }
180
+
181
+
182
+ /// Windows can use either `\` or `/` as a separator -- thanks to t-beckmann on github
183
+ /// for suggesting a fix for this.
184
+ bool is_separator(char c) {
185
+ #if defined(__WIN32)
186
+ return c == '\\' || c == '/';
187
+ #else
188
+ return c == '/';
189
+ #endif
190
+ }
191
+
192
+
193
+ #ifdef TEST
194
+ void Test_is_separator(CuTest* tc) {
195
+ char * test = "a/\\";
196
+
197
+ #if defined(__WIN32)
198
+ CuAssertIntEquals(tc, false, is_separator(test[0]));
199
+ CuAssertIntEquals(tc, true, is_separator(test[1]));
200
+ CuAssertIntEquals(tc, true, is_separator(test[2]));
201
+ #else
202
+ CuAssertIntEquals(tc, false, is_separator(test[0]));
203
+ CuAssertIntEquals(tc, true, is_separator(test[1]));
204
+ CuAssertIntEquals(tc, false, is_separator(test[2]));
205
+ #endif
206
+ }
207
+ #endif
208
+
209
+
210
+ /// Ensure that path ends in separator
211
+ void add_trailing_sep(DString * path) {
212
+ #if defined(__WIN32)
213
+ char sep = '\\';
214
+ #else
215
+ char sep = '/';
216
+ #endif
217
+
218
+ // Ensure that folder ends in separator
219
+ if ((path->currentStringLength == 0) || (!is_separator(path->str[path->currentStringLength - 1]))) {
220
+ d_string_append_c(path, sep);
221
+ }
222
+ }
223
+
224
+
225
+ /// strndup not available on all platforms
226
+ static char * my_strndup(const char * source, size_t n) {
227
+ if (source == NULL) {
228
+ return NULL;
229
+ }
230
+
231
+ size_t len = 0;
232
+ char * result;
233
+ const char * test = source;
234
+
235
+ // strlen is too slow if strlen(source) >> n
236
+ for (len = 0; len < n; ++len) {
237
+ if (test == '\0') {
238
+ break;
239
+ }
240
+
241
+ test++;
242
+ }
243
+
244
+ result = malloc(len + 1);
245
+
246
+ if (result) {
247
+ memcpy(result, source, len);
248
+ result[len] = '\0';
249
+ }
250
+
251
+ return result;
252
+ }
253
+
254
+
255
+ /// strdup() not available on all platforms
256
+ static char * my_strdup(const char * source) {
257
+ if (source == NULL) {
258
+ return NULL;
259
+ }
260
+
261
+ char * result = malloc(strlen(source) + 1);
262
+
263
+ if (result) {
264
+ strcpy(result, source);
265
+ }
266
+
267
+ return result;
268
+ }
269
+
270
+
271
+ /// Combine directory and base filename to create a full path */
272
+ char * path_from_dir_base(const char * dir, const char * base) {
273
+ if (!dir && !base) {
274
+ return NULL;
275
+ }
276
+
277
+ DString * path = NULL;
278
+ char * result = NULL;
279
+
280
+ if ((base != NULL) && (is_separator(base[0]))) {
281
+ // We have an absolute path
282
+ return my_strdup(base);
283
+ }
284
+
285
+ // We have a directory and relative path
286
+ path = d_string_new(dir);
287
+
288
+ // Ensure that folder ends in separator
289
+ add_trailing_sep(path);
290
+
291
+ // Append filename (if present)
292
+ if (base) {
293
+ d_string_append(path, base);
294
+ }
295
+
296
+ result = path->str;
297
+ d_string_free(path, false);
298
+
299
+ return result;
300
+ }
301
+
302
+
303
+ #ifdef TEST
304
+ void Test_path_from_dir_base(CuTest* tc) {
305
+ char dir[10] = "/foo";
306
+ char base[10] = "bar";
307
+
308
+ char * path = path_from_dir_base(dir, base);
309
+
310
+ #if defined(__WIN32)
311
+ CuAssertStrEquals(tc, "/foo\\bar", path);
312
+ #else
313
+ CuAssertStrEquals(tc, "/foo/bar", path);
314
+ #endif
315
+
316
+ free(path);
317
+ strcpy(base, "/bar");
318
+
319
+ path = path_from_dir_base(dir, base);
320
+
321
+ CuAssertStrEquals(tc, "/bar", path);
322
+
323
+ free(path);
324
+
325
+ path = path_from_dir_base(NULL, NULL);
326
+ CuAssertStrEquals(tc, NULL, path);
327
+ }
328
+ #endif
329
+
330
+
331
+ /// Separate filename and directory from a full path
332
+ ///
333
+ /// See http://stackoverflow.com/questions/1575278/function-to-split-a-filepath-into-path-and-file
334
+ void split_path_file(char ** dir, char ** file, const char * path) {
335
+ const char * slash = path, * next;
336
+
337
+ #if defined(__WIN32)
338
+ const char sep[] = "\\/"; // Windows allows either variant
339
+ #else
340
+ const char sep[] = "/";
341
+ #endif
342
+
343
+ while ((next = strpbrk(slash + 1, sep))) {
344
+ slash = next;
345
+ }
346
+
347
+ if (path != slash) {
348
+ slash++;
349
+ }
350
+
351
+ if (dir) {
352
+ *dir = my_strndup(path, slash - path);
353
+ }
354
+
355
+ if (file) {
356
+ *file = my_strdup(slash);
357
+ }
358
+ }
359
+
360
+
361
+ #ifdef TEST
362
+ void Test_split_path_file(CuTest* tc) {
363
+ char * dir, * file;
364
+
365
+ char * path = "/foo/bar.txt";
366
+ split_path_file(&dir, &file, path);
367
+
368
+ CuAssertStrEquals(tc, "/foo/", dir);
369
+ CuAssertStrEquals(tc, "bar.txt", file);
370
+
371
+ path = "\\foo\\bar.txt";
372
+ split_path_file(&dir, &file, path);
373
+
374
+ #if defined(__WIN32)
375
+ CuAssertStrEquals(tc, "\\foo\\", dir);
376
+ CuAssertStrEquals(tc, "bar.txt", file);
377
+ #else
378
+ CuAssertStrEquals(tc, "", dir);
379
+ CuAssertStrEquals(tc, "\\foo\\bar.txt", file);
380
+ #endif
381
+ }
382
+ #endif
383
+
384
+
385
+ // Windows does not know realpath(), so we need a "windows port"
386
+ // Fix by @f8ttyc8t (<https://github.com/f8ttyc8t>)
387
+ #if (defined(_WIN32) || defined(__WIN32__))
388
+ // Let compiler know where to find GetFullPathName()
389
+ #include <windows.h>
390
+
391
+ char *realpath(const char *path, char *resolved_path) {
392
+ DWORD retval = 0;
393
+ DWORD dwBufSize = 0; // Just in case MAX_PATH differs from PATH_MAX
394
+ TCHAR *buffer = NULL;
395
+
396
+ if (resolved_path == NULL) {
397
+ // realpath allocates appropiate bytes if resolved_path is null. This is to mimic realpath behavior
398
+ dwBufSize = PATH_MAX; // Use windows PATH_MAX constant, because we are in Windows context now.
399
+ buffer = (char*)malloc(dwBufSize);
400
+
401
+ if (buffer == NULL) {
402
+ return NULL; // some really weird is going on...
403
+ }
404
+ } else {
405
+ dwBufSize = MAX_PATH; // buffer has been allocated using MAX_PATH earlier
406
+ buffer = resolved_path;
407
+ }
408
+
409
+ retval = GetFullPathName(path, dwBufSize, buffer, NULL);
410
+
411
+ if (retval == 0) {
412
+ return NULL;
413
+ printf("Failed to GetFullPathName()\n");
414
+ }
415
+
416
+ return buffer;
417
+ }
418
+ #endif
419
+
420
+
421
+ // Convert argument to absolute path
422
+ char * absolute_path_for_argument(const char * arg) {
423
+ char * result = NULL;
424
+ #ifdef PATH_MAX
425
+ // If PATH_MAX defined, use it
426
+ char absolute[PATH_MAX + 1];
427
+ realpath(arg, absolute);
428
+ result = my_strdup(absolute);
429
+ #else
430
+ // If undefined, then we *should* be able to use a NULL pointer to allocate
431
+ result = realpath(arg, NULL);
432
+ #endif
433
+
434
+ return result;
435
+ }
436
+