rpeg-multimarkdown 0.1.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/README.markdown +7 -4
  3. data/Rakefile +3 -3
  4. data/bin/rpeg-multimarkdown +7 -7
  5. data/ext/extconf.rb +2 -2
  6. data/ext/markdown.c +14 -0
  7. data/ext/markdown_lib.c +72 -18
  8. data/ext/markdown_lib.h +18 -8
  9. data/ext/markdown_output.c +187 -58
  10. data/ext/markdown_parser.c +8315 -6224
  11. data/ext/markdown_peg.h +5 -4
  12. data/ext/odf.c +7 -1
  13. data/ext/odf.h +8 -0
  14. data/ext/parsing_functions.c +42 -2
  15. data/ext/parsing_functions.h +17 -0
  16. data/ext/utility_functions.c +60 -56
  17. data/ext/utility_functions.h +93 -0
  18. data/lib/peg_multimarkdown.rb +16 -16
  19. data/test/MultiMarkdownTest/BeamerTests/Beamer-Tables.tex +2 -2
  20. data/test/MultiMarkdownTest/CompatibilityTests/Automatic Labels.html +35 -0
  21. data/test/MultiMarkdownTest/CompatibilityTests/Automatic Labels.text +45 -0
  22. data/test/MultiMarkdownTest/CompatibilityTests/Base Header Level.html +14 -0
  23. data/test/MultiMarkdownTest/CompatibilityTests/Base Header Level.text +16 -0
  24. data/test/MultiMarkdownTest/CompatibilityTests/BibTeX.html +9 -0
  25. data/test/MultiMarkdownTest/CompatibilityTests/BibTeX.text +10 -0
  26. data/test/MultiMarkdownTest/CompatibilityTests/Citations.html +40 -0
  27. data/test/MultiMarkdownTest/CompatibilityTests/Citations.text +42 -0
  28. data/test/MultiMarkdownTest/CompatibilityTests/Definition Lists.html +40 -0
  29. data/test/MultiMarkdownTest/CompatibilityTests/Definition Lists.text +39 -0
  30. data/test/MultiMarkdownTest/CompatibilityTests/Dutch.html +17 -0
  31. data/test/MultiMarkdownTest/CompatibilityTests/Dutch.text +17 -0
  32. data/test/MultiMarkdownTest/CompatibilityTests/Email.html +40 -0
  33. data/test/MultiMarkdownTest/CompatibilityTests/Email.text +31 -0
  34. data/test/MultiMarkdownTest/CompatibilityTests/English.html +17 -0
  35. data/test/MultiMarkdownTest/CompatibilityTests/English.text +17 -0
  36. data/test/MultiMarkdownTest/CompatibilityTests/Errors.html +9 -0
  37. data/test/MultiMarkdownTest/CompatibilityTests/Errors.text +11 -0
  38. data/test/MultiMarkdownTest/CompatibilityTests/Footnotes.html +23 -0
  39. data/test/MultiMarkdownTest/CompatibilityTests/Footnotes.text +25 -0
  40. data/test/MultiMarkdownTest/CompatibilityTests/French.html +17 -0
  41. data/test/MultiMarkdownTest/CompatibilityTests/French.text +17 -0
  42. data/test/MultiMarkdownTest/CompatibilityTests/German.html +17 -0
  43. data/test/MultiMarkdownTest/CompatibilityTests/German.text +17 -0
  44. data/test/MultiMarkdownTest/CompatibilityTests/GermanGuillemets.html +17 -0
  45. data/test/MultiMarkdownTest/CompatibilityTests/GermanGuillemets.text +17 -0
  46. data/test/MultiMarkdownTest/CompatibilityTests/Glossary.html +29 -0
  47. data/test/MultiMarkdownTest/CompatibilityTests/Glossary.text +28 -0
  48. data/test/MultiMarkdownTest/CompatibilityTests/Headers.html +42 -0
  49. data/test/MultiMarkdownTest/CompatibilityTests/Headers.text +51 -0
  50. data/test/MultiMarkdownTest/CompatibilityTests/Line Breaks.html +13 -0
  51. data/test/MultiMarkdownTest/CompatibilityTests/Line Breaks.text +15 -0
  52. data/test/MultiMarkdownTest/CompatibilityTests/Link Attributes.html +35 -0
  53. data/test/MultiMarkdownTest/CompatibilityTests/Link Attributes.text +51 -0
  54. data/test/MultiMarkdownTest/CompatibilityTests/List Parsing.html +13 -0
  55. data/test/MultiMarkdownTest/CompatibilityTests/List Parsing.text +11 -0
  56. data/test/MultiMarkdownTest/CompatibilityTests/MarkdownInHTML.html +13 -0
  57. data/test/MultiMarkdownTest/CompatibilityTests/MarkdownInHTML.text +19 -0
  58. data/test/MultiMarkdownTest/CompatibilityTests/Math.html +14 -0
  59. data/test/MultiMarkdownTest/CompatibilityTests/Math.text +15 -0
  60. data/test/MultiMarkdownTest/CompatibilityTests/MetaData.html +14 -0
  61. data/test/MultiMarkdownTest/CompatibilityTests/MetaData.text +14 -0
  62. data/test/MultiMarkdownTest/CompatibilityTests/NotMetaData.html +3 -0
  63. data/test/MultiMarkdownTest/CompatibilityTests/NotMetaData.text +4 -0
  64. data/test/MultiMarkdownTest/CompatibilityTests/Sanity.html +77 -0
  65. data/test/MultiMarkdownTest/CompatibilityTests/Sanity.text +77 -0
  66. data/test/MultiMarkdownTest/CompatibilityTests/SmartQuotes.html +22 -0
  67. data/test/MultiMarkdownTest/CompatibilityTests/SmartQuotes.text +22 -0
  68. data/test/MultiMarkdownTest/CompatibilityTests/Swedish.html +17 -0
  69. data/test/MultiMarkdownTest/CompatibilityTests/Swedish.text +17 -0
  70. data/test/MultiMarkdownTest/CompatibilityTests/Tables.html +65 -0
  71. data/test/MultiMarkdownTest/CompatibilityTests/Tables.text +71 -0
  72. data/test/MultiMarkdownTest/MemoirTests/Definition Lists.tex +5 -10
  73. data/test/MultiMarkdownTest/MemoirTests/Sanity.tex +2 -2
  74. data/test/MultiMarkdownTest/MemoirTests/Tables.tex +10 -10
  75. data/test/MultiMarkdownTest/MultiMarkdownTests/Automatic Labels.html +4 -2
  76. data/test/MultiMarkdownTest/MultiMarkdownTests/Base Header Level.html +4 -2
  77. data/test/MultiMarkdownTest/MultiMarkdownTests/BibTeX.html +4 -2
  78. data/test/MultiMarkdownTest/MultiMarkdownTests/Citations.html +6 -4
  79. data/test/MultiMarkdownTest/MultiMarkdownTests/Definition Lists.html +9 -12
  80. data/test/MultiMarkdownTest/MultiMarkdownTests/Definition Lists.tex +5 -10
  81. data/test/MultiMarkdownTest/MultiMarkdownTests/Dutch.html +4 -2
  82. data/test/MultiMarkdownTest/MultiMarkdownTests/Email.html +4 -2
  83. data/test/MultiMarkdownTest/MultiMarkdownTests/English.html +4 -2
  84. data/test/MultiMarkdownTest/MultiMarkdownTests/Errors.html +4 -2
  85. data/test/MultiMarkdownTest/MultiMarkdownTests/Footnotes.html +7 -5
  86. data/test/MultiMarkdownTest/MultiMarkdownTests/French.html +4 -2
  87. data/test/MultiMarkdownTest/MultiMarkdownTests/German.html +4 -2
  88. data/test/MultiMarkdownTest/MultiMarkdownTests/GermanGuillemets.html +4 -2
  89. data/test/MultiMarkdownTest/MultiMarkdownTests/Glossary.html +6 -4
  90. data/test/MultiMarkdownTest/MultiMarkdownTests/Headers.html +6 -2
  91. data/test/MultiMarkdownTest/MultiMarkdownTests/Headers.tex +2 -0
  92. data/test/MultiMarkdownTest/MultiMarkdownTests/Headers.text +2 -0
  93. data/test/MultiMarkdownTest/MultiMarkdownTests/Line Breaks.html +4 -2
  94. data/test/MultiMarkdownTest/MultiMarkdownTests/Link Attributes.html +4 -2
  95. data/test/MultiMarkdownTest/MultiMarkdownTests/List Parsing.html +4 -2
  96. data/test/MultiMarkdownTest/MultiMarkdownTests/MarkdownInHTML.html +4 -2
  97. data/test/MultiMarkdownTest/MultiMarkdownTests/Math.html +4 -2
  98. data/test/MultiMarkdownTest/MultiMarkdownTests/MetaData.html +4 -2
  99. data/test/MultiMarkdownTest/MultiMarkdownTests/NotMetaData.html +3 -0
  100. data/test/MultiMarkdownTest/MultiMarkdownTests/NotMetaData.tex +3 -0
  101. data/test/MultiMarkdownTest/MultiMarkdownTests/NotMetaData.text +4 -0
  102. data/test/MultiMarkdownTest/MultiMarkdownTests/Sanity.html +4 -2
  103. data/test/MultiMarkdownTest/MultiMarkdownTests/Sanity.tex +2 -2
  104. data/test/MultiMarkdownTest/MultiMarkdownTests/SmartQuotes.html +4 -2
  105. data/test/MultiMarkdownTest/MultiMarkdownTests/Swedish.html +4 -2
  106. data/test/MultiMarkdownTest/MultiMarkdownTests/Tables.html +45 -2
  107. data/test/MultiMarkdownTest/MultiMarkdownTests/Tables.tex +32 -10
  108. data/test/MultiMarkdownTest/MultiMarkdownTests/Tables.text +8 -0
  109. data/test/multimarkdown_test.rb +5 -5
  110. metadata +92 -50
data/ext/markdown_peg.h CHANGED
@@ -1,8 +1,9 @@
1
1
  /* markdown_peg.h */
2
- #include "markdown_lib.h"
3
- #include <glib.h>
2
+ #ifndef MARKDOWN_PEG_H
3
+ #define MARKDOWN_PEG_H
4
4
 
5
- extern char *strdup(const char *string);
5
+ #include "markdown_lib.h"
6
+ #include "glib.h"
6
7
 
7
8
  /* Information (label, URL and title) for a link. */
8
9
  struct Link {
@@ -133,10 +134,10 @@ void free_element_list(element * elt);
133
134
  void free_element(element *elt);
134
135
  void print_element_list(GString *out, element *elt, int format, int exts);
135
136
 
136
-
137
137
  element * parse_metadata_only(char *string, int extensions);
138
138
  char * extract_metadata_value(char *text, int extensions, char *key);
139
139
 
140
140
  char * metavalue_for_key(char *key, element *list);
141
141
 
142
142
  element * parse_markdown_for_opml(char *string, int extensions);
143
+ #endif
data/ext/odf.c CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  ***********************************************************************/
16
16
 
17
+ #include "odf.h"
18
+
17
19
 
18
20
  void print_odf_header(GString *out){
19
21
 
@@ -99,7 +101,7 @@ void print_odf_header(GString *out){
99
101
  " style:class=\"html\">\n" \
100
102
  " <style:paragraph-properties fo:margin-left=\"0.3937in\" fo:margin-right=\"0.3937in\" fo:margin-top=\"0in\"\n" \
101
103
  " fo:margin-bottom=\"0.1965in\"\n" \
102
- " fo:text-align=\"justify\"" \
104
+ " fo:text-align=\"justify\"" \
103
105
  " style:justify-single-word=\"false\"" \
104
106
  " fo:text-indent=\"0in\"\n" \
105
107
  " style:auto-text-indent=\"false\"/>\n" \
@@ -130,6 +132,10 @@ void print_odf_header(GString *out){
130
132
  " style:join-border=\"false\"/>\n" \
131
133
  " <style:text-properties fo:font-size=\"6pt\" style:font-size-asian=\"6pt\" style:font-size-complex=\"6pt\"/>\n" \
132
134
  "</style:style>\n" \
135
+ "<style:style style:name=\"Footnote_20_anchor\" style:display-name=\"Footnote anchor\"" \
136
+ " style:family=\"text\">" \
137
+ " <style:text-properties style:text-position=\"super 58%\"/>" \
138
+ " </style:style>" \
133
139
  "</office:styles>\n");
134
140
 
135
141
  /* Automatic style information */
data/ext/odf.h CHANGED
@@ -1,3 +1,11 @@
1
+ #ifndef ODF_H
2
+ #define ODF_H
3
+
1
4
  #include <stdlib.h>
2
5
  #include <stdio.h>
3
6
  #include <glib.h>
7
+
8
+ void print_odf_header(GString *out);
9
+ void print_odf_footer(GString *out);
10
+ #endif
11
+
@@ -1,7 +1,25 @@
1
1
  /* parsing_functions.c - Functions for parsing markdown and
2
2
  * freeing element lists. */
3
3
 
4
- int yyparse(void);
4
+ /* These yy_* functions come from markdown_parser.c which is
5
+ * generated from markdown_parser.leg
6
+ * */
7
+ typedef int (*yyrule)();
8
+
9
+ extern int yyparse();
10
+ extern int yyparsefrom(yyrule);
11
+ extern int yy_References();
12
+ extern int yy_Notes();
13
+ extern int yy_Doc();
14
+
15
+ extern int yy_AutoLabels();
16
+ extern int yy_DocWithMetaData();
17
+ extern int yy_MetaDataOnly();
18
+ extern int yy_DocForOPML();
19
+
20
+ #include "utility_functions.h"
21
+ #include "parsing_functions.h"
22
+ #include "markdown_peg.h"
5
23
 
6
24
  static void free_element_contents(element elt);
7
25
 
@@ -41,6 +59,11 @@ static void free_element_contents(element elt) {
41
59
  case GLOSSARY:
42
60
  case GLOSSARYTERM:
43
61
  case NOTELABEL:
62
+ case CELLSPAN:
63
+ case EMDASH:
64
+ case ENDASH:
65
+ case GLOSSARYSORTKEY:
66
+ case MATHSPAN:
44
67
  free(elt.contents.str);
45
68
  elt.contents.str = NULL;
46
69
  break;
@@ -129,6 +152,12 @@ element * parse_markdown(char *string, int extensions, element *reference_list,
129
152
  yyparsefrom(yy_Doc);
130
153
 
131
154
  charbuf = oldcharbuf; /* restore charbuf to original value */
155
+
156
+ /* if (parse_aborted) {
157
+ free_element_list(parse_result);
158
+ return NULL;
159
+ }*/
160
+
132
161
  return parse_result;
133
162
 
134
163
  }
@@ -144,9 +173,20 @@ element * parse_markdown_with_metadata(char *string, int extensions, element *re
144
173
  oldcharbuf = charbuf;
145
174
  charbuf = string;
146
175
 
147
- yyparsefrom(yy_DocWithMetaData);
176
+ start_time = clock();
148
177
 
178
+ yyparsefrom(yy_DocWithMetaData);
149
179
  charbuf = oldcharbuf; /* restore charbuf to original value */
180
+
181
+ /* reset start_time for subsequent passes */
182
+ start_time = 0;
183
+
184
+ if (parse_aborted) {
185
+ parse_aborted = 0;
186
+ free_element_list(parse_result);
187
+ return NULL;
188
+ }
189
+
150
190
  return parse_result;
151
191
 
152
192
  }
@@ -0,0 +1,17 @@
1
+ #ifndef PARSING_FUNCTIONS_H
2
+ #define PARSING_FUNCTIONS_H
3
+ /* parsing_functions.c - Functions for parsing markdown and
4
+ * freeing element lists. */
5
+
6
+ #include "markdown_peg.h"
7
+
8
+ /* free_element_list - free list of elements recursively */
9
+ void free_element_list(element * elt);
10
+ /* free_element - free element and contents */
11
+ void free_element(element *elt);
12
+
13
+ element * parse_references(char *string, int extensions);
14
+ element * parse_notes(char *string, int extensions, element *reference_list);
15
+ element * parse_markdown(char *string, int extensions, element *reference_list, element *note_list, element *label_list);
16
+
17
+ #endif
@@ -1,12 +1,12 @@
1
1
  /* utility_functions.c - List manipulation functions, element
2
2
  * constructors, and macro definitions for leg markdown parser. */
3
3
 
4
- extern int strcasecmp(const char *string1, const char *string2);
4
+ #include "utility_functions.h"
5
+ #include "markdown_peg.h"
5
6
 
6
- static char *label_from_string(char *str, bool obfuscate) ;
7
- static void localize_typography(GString *out, int character, int language, int output);
7
+ #include <string.h>
8
+ #include <assert.h>
8
9
 
9
- static void print_raw_element_list(GString *out, element *list);
10
10
 
11
11
  /**********************************************************************
12
12
 
@@ -15,14 +15,14 @@ static void print_raw_element_list(GString *out, element *list);
15
15
  ***********************************************************************/
16
16
 
17
17
  /* cons - cons an element onto a list, returning pointer to new head */
18
- static element * cons(element *new, element *list) {
18
+ element * cons(element *new, element *list) {
19
19
  assert(new != NULL);
20
20
  new->next = list;
21
21
  return new;
22
22
  }
23
23
 
24
24
  /* reverse - reverse a list, returning pointer to new list */
25
- static element *reverse(element *list) {
25
+ element *reverse(element *list) {
26
26
  element *new = NULL;
27
27
  element *next = NULL;
28
28
  while (list != NULL) {
@@ -34,7 +34,7 @@ static element *reverse(element *list) {
34
34
  }
35
35
 
36
36
  /* append_list - add element to end of list */
37
- static void append_list(element *new, element *list) {
37
+ void append_list(element *new, element *list) {
38
38
  assert(new != NULL);
39
39
  element *step = list;
40
40
 
@@ -48,7 +48,7 @@ static void append_list(element *new, element *list) {
48
48
 
49
49
  /* concat_string_list - concatenates string contents of list of STR elements.
50
50
  * Frees STR elements as they are added to the concatenation. */
51
- static GString *concat_string_list(element *list) {
51
+ GString *concat_string_list(element *list) {
52
52
  GString *result;
53
53
  element *next;
54
54
  result = g_string_new("");
@@ -69,13 +69,16 @@ static GString *concat_string_list(element *list) {
69
69
 
70
70
  ***********************************************************************/
71
71
 
72
- static char *charbuf = ""; /* Buffer of characters to be parsed. */
73
- static element *references = NULL; /* List of link references found. */
74
- static element *notes = NULL; /* List of footnotes found. */
75
- static element *parse_result; /* Results of parse. */
72
+
73
+ char *charbuf = ""; /* Buffer of characters to be parsed. */
74
+ element *references = NULL; /* List of link references found. */
75
+ element *notes = NULL; /* List of footnotes found. */
76
+ element *parse_result; /* Results of parse. */
76
77
  int syntax_extensions; /* Syntax extensions selected. */
77
78
 
78
- static element *labels = NULL; /* List of labels found in document. */
79
+ element *labels = NULL; /* List of labels found in document. */
80
+ clock_t start_time = 0; /* Used for ensuring we're not stuck in a loop */
81
+ bool parse_aborted = 0; /* flag indicating we ran out of time */
79
82
 
80
83
  /**********************************************************************
81
84
 
@@ -86,7 +89,7 @@ static element *labels = NULL; /* List of labels found in document. */
86
89
  ***********************************************************************/
87
90
 
88
91
  /* mk_element - generic constructor for element */
89
- static element * mk_element(int key) {
92
+ element * mk_element(int key) {
90
93
  element *result = malloc(sizeof(element));
91
94
  result->key = key;
92
95
  result->children = NULL;
@@ -96,7 +99,7 @@ static element * mk_element(int key) {
96
99
  }
97
100
 
98
101
  /* mk_str - constructor for STR element */
99
- static element * mk_str(char *string) {
102
+ element * mk_str(char *string) {
100
103
  element *result;
101
104
  assert(string != NULL);
102
105
  result = mk_element(STR);
@@ -106,7 +109,7 @@ static element * mk_str(char *string) {
106
109
 
107
110
  /* mk_str_from_list - makes STR element by concatenating a
108
111
  * reversed list of strings, adding optional extra newline */
109
- static element * mk_str_from_list(element *list, bool extra_newline) {
112
+ element * mk_str_from_list(element *list, bool extra_newline) {
110
113
  element *result;
111
114
  GString *c = concat_string_list(reverse(list));
112
115
  if (extra_newline)
@@ -120,7 +123,7 @@ static element * mk_str_from_list(element *list, bool extra_newline) {
120
123
  /* mk_list - makes new list with key 'key' and children the reverse of 'lst'.
121
124
  * This is designed to be used with cons to build lists in a parser action.
122
125
  * The reversing is necessary because cons adds to the head of a list. */
123
- static element * mk_list(int key, element *lst) {
126
+ element * mk_list(int key, element *lst) {
124
127
  element *result;
125
128
  result = mk_element(key);
126
129
  result->children = reverse(lst);
@@ -128,7 +131,7 @@ static element * mk_list(int key, element *lst) {
128
131
  }
129
132
 
130
133
  /* mk_link - constructor for LINK element */
131
- static element * mk_link(element *label, char *url, char *title, element *attr, char *id) {
134
+ element * mk_link(element *label, char *url, char *title, element *attr, char *id) {
132
135
  element *result;
133
136
  result = mk_element(LINK);
134
137
  result->contents.link = malloc(sizeof(link));
@@ -141,12 +144,12 @@ static element * mk_link(element *label, char *url, char *title, element *attr,
141
144
  }
142
145
 
143
146
  /* extension = returns true if extension is selected */
144
- static bool extension(int ext) {
147
+ bool extension(int ext) {
145
148
  return (syntax_extensions & ext);
146
149
  }
147
150
 
148
151
  /* match_inlines - returns true if inline lists match (case-insensitive...) */
149
- static bool match_inlines(element *l1, element *l2) {
152
+ bool match_inlines(element *l1, element *l2) {
150
153
  while (l1 != NULL && l2 != NULL) {
151
154
  if (l1->key != l2->key)
152
155
  return false;
@@ -190,7 +193,7 @@ static bool match_inlines(element *l1, element *l2) {
190
193
 
191
194
  /* find_reference - return true if link found in references matching label.
192
195
  * 'link' is modified with the matching url and title. */
193
- static bool find_reference(link *result, element *label) {
196
+ bool find_reference(link *result, element *label) {
194
197
  element *cur = references; /* pointer to walk up list of references */
195
198
  link *curitem;
196
199
  while (cur != NULL) {
@@ -208,7 +211,7 @@ static bool find_reference(link *result, element *label) {
208
211
  /* find_note - return true if note found in notes matching label.
209
212
  if found, 'result' is set to point to matched note. */
210
213
 
211
- static bool find_note(element **result, char *label) {
214
+ bool find_note(element **result, char *label) {
212
215
  element *cur = notes; /* pointer to walk up list of notes */
213
216
  while (cur != NULL) {
214
217
  if (strcmp(label, cur->contents.str) == 0) {
@@ -222,36 +225,10 @@ static bool find_note(element **result, char *label) {
222
225
  }
223
226
 
224
227
 
225
-
226
- /**********************************************************************
227
-
228
- Definitions for leg parser generator.
229
- YY_INPUT is the function the parser calls to get new input.
230
- We take all new input from (static) charbuf.
231
-
232
- ***********************************************************************/
233
-
234
- # define YYSTYPE element *
235
- #ifdef __DEBUG__
236
- # define YY_DEBUG 1
237
- #endif
238
-
239
- #define YY_INPUT(buf, result, max_size) \
240
- { \
241
- int yyc; \
242
- if (charbuf && *charbuf != '\0') { \
243
- yyc= *charbuf++; \
244
- } else { \
245
- yyc= EOF; \
246
- } \
247
- result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \
248
- }
249
-
250
-
251
228
  /* peg-multimarkdown additions */
252
229
 
253
230
  /* print_raw_element - print an element as original text */
254
- static void print_raw_element(GString *out, element *elt) {
231
+ void print_raw_element(GString *out, element *elt) {
255
232
  if (elt->key == LINK) {
256
233
  print_raw_element_list(out,elt->contents.link->label);
257
234
  } else {
@@ -264,7 +241,7 @@ static void print_raw_element(GString *out, element *elt) {
264
241
  }
265
242
 
266
243
  /* print_raw_element_list - print a list of elements as original text */
267
- static void print_raw_element_list(GString *out, element *list) {
244
+ void print_raw_element_list(GString *out, element *list) {
268
245
  while (list != NULL) {
269
246
  print_raw_element(out, list);
270
247
  list = list->next;
@@ -274,7 +251,7 @@ static void print_raw_element_list(GString *out, element *list) {
274
251
  /* label_from_element_list */
275
252
  /* Returns a null-terminated string, which must be freed after use. */
276
253
 
277
- static char *label_from_element_list(element *list, bool obfuscate) {
254
+ char *label_from_element_list(element *list, bool obfuscate) {
278
255
  char *label;
279
256
  char *label2;
280
257
  GString *raw = g_string_new("");
@@ -290,7 +267,7 @@ static char *label_from_element_list(element *list, bool obfuscate) {
290
267
  HTML id */
291
268
  /* Returns a null-terminated string, which must be freed after use. */
292
269
 
293
- static char *label_from_string(char *str, bool obfuscate) {
270
+ char *label_from_string(char *str, bool obfuscate) {
294
271
  bool valid = FALSE;
295
272
  GString *out = g_string_new("");
296
273
  char *label;
@@ -321,7 +298,7 @@ static char *label_from_string(char *str, bool obfuscate) {
321
298
 
322
299
  /* find_label - return true if header, table, etc is found matching label.
323
300
  * 'link' is modified with the matching url and title. */
324
- static bool find_label(link *result, element *label) {
301
+ bool find_label(link *result, element *label) {
325
302
  char *lab;
326
303
  element *cur = labels; /* pointer to walk up list of references */
327
304
  GString *text = g_string_new("");
@@ -347,7 +324,7 @@ static bool find_label(link *result, element *label) {
347
324
  /* localize_typography - return the proper string, based on language chosen */
348
325
  /* Default action is English */
349
326
 
350
- static void localize_typography(GString *out, int character, int lang, int output) {
327
+ void localize_typography(GString *out, int character, int lang, int output) {
351
328
 
352
329
  switch (output) {
353
330
  case HTMLOUT:
@@ -525,11 +502,38 @@ static void localize_typography(GString *out, int character, int lang, int outpu
525
502
  }
526
503
 
527
504
  /* Trim spaces at end of string */
528
- static void trim_trailing_whitespace(char *str) {
505
+ void trim_trailing_whitespace(char *str) {
529
506
  while ( ( str[strlen(str)-1] == ' ' ) ||
530
507
  ( str[strlen(str)-1] == '\n' ) ||
531
508
  ( str[strlen(str)-1] == '\r' ) ||
532
509
  ( str[strlen(str)-1] == '\t' ) ) {
533
510
  str[strlen(str)-1] = '\0';
534
511
  }
535
- }
512
+ }
513
+
514
+ /* Don't let us get caught in "infinite" loop */
515
+ bool check_timeout() {
516
+ /* Once we abort, keep aborting */
517
+ if (parse_aborted)
518
+ return 0;
519
+
520
+ /* We're not timing this run */
521
+ if (start_time == 0)
522
+ return 1;
523
+
524
+ clock_t end = clock();
525
+ double elapsed = ((double) (end - start_time)) / CLOCKS_PER_SEC;
526
+
527
+ /* fprintf(stderr,"%2.2f elapsed; (%4.2f CLOCKS_PER_SEC)\n",elapsed,CLOCKS_PER_SEC); */
528
+ /* fprintf(stderr,"%2.2f elapsed\n",elapsed); */
529
+
530
+
531
+ /* If > 3 clock seconds, then abort */
532
+ float max = 3;
533
+ if (elapsed > max) {
534
+ parse_aborted = 1;
535
+ return 0;
536
+ }
537
+ return 1;
538
+ }
539
+
@@ -0,0 +1,93 @@
1
+ #ifndef UTILITY_FUNCTIONS_H
2
+ #define UTILITY_FUNCTIONS_H
3
+
4
+ #include <stdbool.h>
5
+ #include <glib.h>
6
+
7
+ #include "markdown_peg.h"
8
+
9
+ #include <time.h>
10
+
11
+ /* utility_functions.h - List manipulation functions, element
12
+ * constructors, and macro definitions for leg markdown parser. */
13
+
14
+
15
+ /* cons - cons an element onto a list, returning pointer to new head */
16
+ element * cons(element *new, element *list);
17
+
18
+ /* reverse - reverse a list, returning pointer to new list */
19
+ element *reverse(element *list);
20
+ /* concat_string_list - concatenates string contents of list of STR elements.
21
+ * Frees STR elements as they are added to the concatenation. */
22
+ GString *concat_string_list(element *list);
23
+ /**********************************************************************
24
+
25
+ Global variables used in parsing
26
+
27
+ ***********************************************************************/
28
+
29
+ extern char *charbuf; /* Buffer of characters to be parsed. */
30
+ extern element *references; /* List of link references found. */
31
+ extern element *notes; /* List of footnotes found. */
32
+ extern element *parse_result; /* Results of parse. */
33
+ extern int syntax_extensions; /* Syntax extensions selected. */
34
+
35
+
36
+ extern element *labels; /* List of labels found in document. */
37
+ extern clock_t start_time; /* Used for ensuring we're not stuck in a loop */
38
+ extern bool parse_aborted; /* flag indicating we ran out of time */
39
+
40
+ /**********************************************************************
41
+
42
+ Auxiliary functions for parsing actions.
43
+ These make it easier to build up data structures (including lists)
44
+ in the parsing actions.
45
+
46
+ ***********************************************************************/
47
+
48
+ /* mk_element - generic constructor for element */
49
+ element * mk_element(int key);
50
+
51
+ /* mk_str - constructor for STR element */
52
+ element * mk_str(char *string);
53
+
54
+ /* mk_str_from_list - makes STR element by concatenating a
55
+ * reversed list of strings, adding optional extra newline */
56
+ element * mk_str_from_list(element *list, bool extra_newline);
57
+
58
+ /* mk_list - makes new list with key 'key' and children the reverse of 'lst'.
59
+ * This is designed to be used with cons to build lists in a parser action.
60
+ * The reversing is necessary because cons adds to the head of a list. */
61
+ element * mk_list(int key, element *lst);
62
+
63
+ /* mk_link - constructor for LINK element */
64
+ element * mk_link(element *label, char *url, char *title, element *attr, char *id);
65
+ /* extension = returns true if extension is selected */
66
+ bool extension(int ext);
67
+
68
+ /* match_inlines - returns true if inline lists match (case-insensitive...) */
69
+ bool match_inlines(element *l1, element *l2);
70
+
71
+ /* find_reference - return true if link found in references matching label.
72
+ * 'link' is modified with the matching url and title. */
73
+ bool find_reference(link *result, element *label);
74
+
75
+ /* find_note - return true if note found in notes matching label.
76
+ if found, 'result' is set to point to matched note. */
77
+
78
+ bool find_note(element **result, char *label);
79
+
80
+
81
+ char *label_from_string(char *str, bool obfuscate);
82
+ void localize_typography(GString *out, int character, int language, int output);
83
+ void print_raw_element_list(GString *out, element *list);
84
+ void append_list(element *new, element *list);
85
+ bool find_label(link *result, element *label);
86
+ bool check_timeout();
87
+ void trim_trailing_whitespace(char *str);
88
+ char *label_from_element_list(element *list, bool obfuscate);
89
+ void print_raw_element_list(GString *out, element *list);
90
+ void print_raw_element(GString *out, element *elt);
91
+
92
+ #endif
93
+