rmultimarkdown 4.5.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/LICENSE +75 -0
  2. data/MultiMarkdown-4/GLibFacade.c +294 -0
  3. data/MultiMarkdown-4/GLibFacade.h +95 -0
  4. data/MultiMarkdown-4/beamer.c +179 -0
  5. data/MultiMarkdown-4/beamer.h +11 -0
  6. data/MultiMarkdown-4/critic.c +111 -0
  7. data/MultiMarkdown-4/critic.h +15 -0
  8. data/MultiMarkdown-4/glib.h +11 -0
  9. data/MultiMarkdown-4/html.c +1062 -0
  10. data/MultiMarkdown-4/html.h +14 -0
  11. data/MultiMarkdown-4/latex.c +1137 -0
  12. data/MultiMarkdown-4/latex.h +16 -0
  13. data/MultiMarkdown-4/libMultiMarkdown.h +157 -0
  14. data/MultiMarkdown-4/lyx.c +2163 -0
  15. data/MultiMarkdown-4/lyx.h +36 -0
  16. data/MultiMarkdown-4/lyxbeamer.c +267 -0
  17. data/MultiMarkdown-4/lyxbeamer.h +11 -0
  18. data/MultiMarkdown-4/memoir.c +79 -0
  19. data/MultiMarkdown-4/memoir.h +10 -0
  20. data/MultiMarkdown-4/multimarkdown.c +469 -0
  21. data/MultiMarkdown-4/odf.c +1202 -0
  22. data/MultiMarkdown-4/odf.h +18 -0
  23. data/MultiMarkdown-4/opml.c +188 -0
  24. data/MultiMarkdown-4/opml.h +15 -0
  25. data/MultiMarkdown-4/parse_utilities.c +752 -0
  26. data/MultiMarkdown-4/parser.c +15679 -0
  27. data/MultiMarkdown-4/parser.h +188 -0
  28. data/MultiMarkdown-4/rng.c +117 -0
  29. data/MultiMarkdown-4/rtf.c +648 -0
  30. data/MultiMarkdown-4/rtf.h +17 -0
  31. data/MultiMarkdown-4/strtok.c +56 -0
  32. data/MultiMarkdown-4/strtok.h +9 -0
  33. data/MultiMarkdown-4/text.c +53 -0
  34. data/MultiMarkdown-4/text.h +11 -0
  35. data/MultiMarkdown-4/transclude.c +218 -0
  36. data/MultiMarkdown-4/transclude.h +26 -0
  37. data/MultiMarkdown-4/writer.c +576 -0
  38. data/MultiMarkdown-4/writer.h +34 -0
  39. data/README.md +75 -0
  40. data/Rakefile +85 -0
  41. data/bin/rmultimarkdown +128 -0
  42. data/ext/extconf.h +3 -0
  43. data/ext/extconf.rb +10 -0
  44. data/ext/multi_markdown.c +100 -0
  45. data/lib/multi_markdown.bundle +0 -0
  46. data/lib/multi_markdown.rb +88 -0
  47. data/lib/multi_markdown/version.rb +6 -0
  48. data/lib/rmultimarkdown.rb +1 -0
  49. data/rmultimarkdown.gemspec +37 -0
  50. data/test/multi_markdown_test.rb +64 -0
  51. metadata +122 -0
@@ -0,0 +1,188 @@
1
+ /* ensure we only load this once */
2
+
3
+ #ifndef PARSER_LIB_H
4
+ #define PARSER_LIB_H
5
+
6
+ #include <stdio.h>
7
+ #include <string.h>
8
+ #include <stdlib.h>
9
+ #include <stdbool.h>
10
+ #include <assert.h>
11
+ #include <time.h>
12
+ #include "glib.h"
13
+ #include "libMultiMarkdown.h"
14
+
15
+ #define TABSTOP 4
16
+
17
+ #define MMD_VERSION "4.5.2"
18
+
19
+ #define MMD_COPYRIGHT \
20
+ "Copyright (c) 2013-2014 Fletcher T. Penney.\n\n" \
21
+ "LyX export code (c) 2013-2014 Charles R. Cowan,\n" \
22
+ "licensed under both GPL and MIT licenses.\n\n" \
23
+ "portions based on peg-markdown - Copyright (c) 2008-2009 John MacFarlane.\n" \
24
+ "peg-markdown is Licensed under either the GPLv2+ or MIT.\n" \
25
+ "portions Copyright (c) 2011 Daniel Jalkut, MIT licensed.\n\n" \
26
+ "This is free software: you are free to change and redistribute it.\n" \
27
+ "There is NO WARRANTY, to the extent permitted by law.\n\n"
28
+
29
+
30
+ #define DEBUG_OFF /* Turn on debugging statements (there's a bunch!)*/
31
+
32
+
33
+ /* This is the type used for the $$ pseudovariable passed to parents */
34
+ #define YYSTYPE node *
35
+
36
+ /* Define a structure to simplify handling of links */
37
+ struct link_data {
38
+ char *label; /* if this is a reference link */
39
+ char *source; /* source URL */
40
+ char *title; /* title string */
41
+ node *attr; /* attribute tree */
42
+ };
43
+
44
+ typedef struct link_data link_data;
45
+
46
+ /* This is the data we store in the parser context */
47
+ typedef struct {
48
+ char *charbuf; /* Input buffer */
49
+ char *original; /* Original input buffer */
50
+ node *result; /* Resulting parse tree */
51
+ unsigned long extensions; /* Extension bitfield */
52
+ node *autolabels; /* Store for later retrieval */
53
+ bool parse_aborted; /* We got bogged down - fail parse */
54
+ clock_t stop_time; /* Note the deadline to complete parsing */
55
+ } parser_data;
56
+
57
+ /* A "scratch pad" for storing data when writing output
58
+ The structure will vary based on what you need */
59
+ typedef struct {
60
+ unsigned long extensions; /* Store copy of extensions for retrieval */
61
+ int padded; /* Track newlines */
62
+ int baseheaderlevel; /* Increase header levels when outputting */
63
+ int language; /* For smart quotes */
64
+ char *table_alignment; /* Hold the alignment string while parsing table */
65
+ int table_column; /* Track the current column number */
66
+ char cell_type; /* What sort of cell type are we in? */
67
+ bool printing_notes; /* Are we printing notes/glossary/etc.? */
68
+ node *notes; /* Store reference notes */
69
+ node *links; /* ... links */
70
+ node *glossary; /* ... glossary */
71
+ node *citations; /* ... citations */
72
+ node *used_notes; /* notes that have been referenced */
73
+ node *result_tree; /* reference to entire result tree */
74
+ int footnote_to_print; /* set while we are printing so we can reverse link */
75
+ int footnote_para_counter; /* so we know which para is last */
76
+ int max_footnote_num; /* so we know if current note is new or repeat */
77
+ bool obfuscate; /* flag that we need to mask email addresses */
78
+ char *latex_footer; /* store for appending at the end */
79
+ bool no_latex_footnote; /* can't use footnotes in some places */
80
+ int odf_para_type; /* what type of paragraph do we need? */
81
+ bool odf_list_needs_end_p; /* is there a <p> that need to be closed */
82
+ int random_seed_base; /* Allow random footnotes */
83
+ int table_row; /* CRC - Track the current row number */
84
+ int lyx_para_type; /* CRC - the type of paragraph being processed */
85
+ int lyx_level; /* CRC - nesting level */
86
+ bool no_lyx_footnote; /* CRC - Can't use footnotes in some places */
87
+ bool lyx_number_headers; /* CRC - Whether to number headers (with or without *) */
88
+ bool lyx_definition_hit; /* CRC - True when a definition has been encountered */
89
+ bool lyx_definition_open; /* CRC - Have not completed a definition list entry */
90
+ bool lyx_in_header; /* CRC - In a table header */
91
+ bool lyx_fragile; /* CRC - in a beamer fragile frame */
92
+ bool lyx_beamerbullet; /* CRC - beamer bullet list (add <+->) */
93
+ int lyx_debug_nest; /* CRC - nesting level for enhanced debugging */
94
+ bool lyx_table_need_line; /* CRC - need a line at the top */
95
+ int lyx_table_total_rows; /* CRC - The total number of rows in the table */
96
+ int lyx_table_total_cols; /* CRC - The total number of columns in the table */
97
+ node *lyx_table_caption; /* CRC - Hold the table caption */
98
+ GString *lyx_debug_pad; /* CRC - padding to indent debugging informaiton */
99
+ } scratch_pad;
100
+
101
+ /* Define smart typography languages -- first in list is default */
102
+ enum language {
103
+ ENGLISH,
104
+ DUTCH,
105
+ FRENCH,
106
+ GERMAN,
107
+ GERMANGUILL,
108
+ SWEDISH,
109
+ };
110
+
111
+ /* Character types for smart typography */
112
+ enum smartelements {
113
+ LSQUOTE,
114
+ RSQUOTE,
115
+ LDQUOTE,
116
+ RDQUOTE,
117
+ NDASH,
118
+ MDASH,
119
+ ELLIP,
120
+ APOS,
121
+ };
122
+
123
+
124
+
125
+ /* parser utilities declarations */
126
+ node * mk_node(int key);
127
+ node * mk_str(char *string);
128
+ node * mk_list(int key, node *list);
129
+ node * mk_link(node *text, char *label, char *source, char *title, node *attr);
130
+ node * mk_pos_node(int key, char *string, unsigned int start, unsigned int stop);
131
+ node * mk_pos_str(char *string, unsigned int start, unsigned int stop);
132
+ node * mk_pos_list(int key, node *list, unsigned int start, unsigned int stop);
133
+
134
+ void free_node(node *n);
135
+ void free_node_tree(node * n);
136
+ void print_node_tree(node * n);
137
+ node * copy_node(node *n);
138
+ node * copy_node_tree(node *n);
139
+
140
+ node * cons(node *new, node *list);
141
+ node * reverse_list(node *list);
142
+ void append_list(node *new, node *list);
143
+
144
+ node * mk_str_from_list(node *list, bool extra_newline);
145
+ GString * concat_string_list(node *list);
146
+
147
+ parser_data * mk_parser_data(char *charbuf, unsigned long extensions);
148
+ void free_parser_data(parser_data *data);
149
+
150
+ char * preformat_text(char *text);
151
+
152
+ scratch_pad * mk_scratch_pad(unsigned long extensions);
153
+ void free_scratch_pad(scratch_pad *scratch);
154
+
155
+ link_data * mk_link_data(char *label, char *source, char *title, node *attr);
156
+ void free_link_data(link_data *l);
157
+ link_data * extract_link_data(char *label, scratch_pad *scratch);
158
+ node * mk_autolink(char *text);
159
+
160
+ void extract_references(node *list, scratch_pad *scratch);
161
+
162
+ bool extension(int ext, unsigned long extensions);
163
+
164
+ /* export utilities */
165
+ void trim_trailing_whitespace(char *str);
166
+ void trim_trailing_newlines(char *str);
167
+
168
+ /* other utilities */
169
+ char * label_from_string(char *str);
170
+ char * clean_string(char *str);
171
+ char * label_from_node_tree(node *n);
172
+ char * label_from_node(node *n);
173
+ void print_raw_node(GString *out, node *n);
174
+ void print_raw_node_tree(GString *out, node*n);
175
+
176
+ char * correct_dimension_units(char *original);
177
+ char * metadata_keys(node *list);
178
+ node * metadata_for_key(char *key, node *list);
179
+ char * metavalue_for_key(char *key, node *list);
180
+
181
+ bool tree_contains_key(node *list, int key);
182
+ int tree_contains_key_count(node *list, int key);
183
+
184
+ bool check_timeout();
185
+
186
+ void debug_node(node *n);
187
+
188
+ #endif
@@ -0,0 +1,117 @@
1
+ /* This program by D E Knuth is in the public domain and freely copyable
2
+ * AS LONG AS YOU MAKE ABSOLUTELY NO CHANGES!
3
+ * It is explained in Seminumerical Algorithms, 3rd edition, Section 3.6
4
+ * (or in the errata to the 2nd edition --- see
5
+ * http://www-cs-faculty.stanford.edu/~knuth/taocp.html
6
+ * in the changes to Volume 2 on pages 171 and following). */
7
+
8
+ /* N.B. The MODIFICATIONS introduced in the 9th printing (2002) are
9
+ included here; there's no backwards compatibility with the original. */
10
+
11
+ /* This version also adopts Brendan McKay's suggestion to
12
+ accommodate naive users who forget to call ran_start(seed). */
13
+
14
+ /* If you find any bugs, please report them immediately to
15
+ * taocp@cs.stanford.edu
16
+ * (and you will be rewarded if the bug is genuine). Thanks! */
17
+
18
+ /************ see the book for explanations and caveats! *******************/
19
+ /************ in particular, you need two's complement arithmetic **********/
20
+
21
+ #define KK 100 /* the long lag */
22
+ #define LL 37 /* the short lag */
23
+ #define MM (1L<<30) /* the modulus */
24
+ #define mod_diff(x,y) (((x)-(y))&(MM-1)) /* subtraction mod MM */
25
+
26
+ long ran_x[KK]; /* the generator state */
27
+
28
+ #ifdef __STDC__
29
+ void ran_array(long aa[],int n)
30
+ #else
31
+ void ran_array(aa,n) /* put n new random numbers in aa */
32
+ long *aa; /* destination */
33
+ int n; /* array length (must be at least KK) */
34
+ #endif
35
+ {
36
+ register int i,j;
37
+ for (j=0;j<KK;j++) aa[j]=ran_x[j];
38
+ for (;j<n;j++) aa[j]=mod_diff(aa[j-KK],aa[j-LL]);
39
+ for (i=0;i<LL;i++,j++) ran_x[i]=mod_diff(aa[j-KK],aa[j-LL]);
40
+ for (;i<KK;i++,j++) ran_x[i]=mod_diff(aa[j-KK],ran_x[i-LL]);
41
+ }
42
+
43
+ /* the following routines are from exercise 3.6--15 */
44
+ /* after calling ran_start, get new randoms by, e.g., "x=ran_arr_next()" */
45
+
46
+ #define QUALITY 1009 /* recommended quality level for high-res use */
47
+ long ran_arr_buf[QUALITY];
48
+ long ran_arr_dummy=-1, ran_arr_started=-1;
49
+ long *ran_arr_ptr=&ran_arr_dummy; /* the next random number, or -1 */
50
+
51
+ #define TT 70 /* guaranteed separation between streams */
52
+ #define is_odd(x) ((x)&1) /* units bit of x */
53
+
54
+ #ifdef __STDC__
55
+ void ran_start(long seed)
56
+ #else
57
+ void ran_start(seed) /* do this before using ran_array */
58
+ long seed; /* selector for different streams */
59
+ #endif
60
+ {
61
+ register int t,j;
62
+ long x[KK+KK-1]; /* the preparation buffer */
63
+ register long ss=(seed+2)&(MM-2);
64
+ for (j=0;j<KK;j++) {
65
+ x[j]=ss; /* bootstrap the buffer */
66
+ ss<<=1; if (ss>=MM) ss-=MM-2; /* cyclic shift 29 bits */
67
+ }
68
+ x[1]++; /* make x[1] (and only x[1]) odd */
69
+ for (ss=seed&(MM-1),t=TT-1; t; ) {
70
+ for (j=KK-1;j>0;j--) x[j+j]=x[j], x[j+j-1]=0; /* "square" */
71
+ for (j=KK+KK-2;j>=KK;j--)
72
+ x[j-(KK-LL)]=mod_diff(x[j-(KK-LL)],x[j]),
73
+ x[j-KK]=mod_diff(x[j-KK],x[j]);
74
+ if (is_odd(ss)) { /* "multiply by z" */
75
+ for (j=KK;j>0;j--) x[j]=x[j-1];
76
+ x[0]=x[KK]; /* shift the buffer cyclically */
77
+ x[LL]=mod_diff(x[LL],x[KK]);
78
+ }
79
+ if (ss) ss>>=1; else t--;
80
+ }
81
+ for (j=0;j<LL;j++) ran_x[j+KK-LL]=x[j];
82
+ for (;j<KK;j++) ran_x[j-LL]=x[j];
83
+ for (j=0;j<10;j++) ran_array(x,KK+KK-1); /* warm things up */
84
+ ran_arr_ptr=&ran_arr_started;
85
+ }
86
+
87
+ #define ran_arr_next() (*ran_arr_ptr>=0? *ran_arr_ptr++: ran_arr_cycle())
88
+ long ran_arr_cycle()
89
+ {
90
+ if (ran_arr_ptr==&ran_arr_dummy)
91
+ ran_start(314159L); /* the user forgot to initialize */
92
+ ran_array(ran_arr_buf,QUALITY);
93
+ ran_arr_buf[KK]=-1;
94
+ ran_arr_ptr=ran_arr_buf+1;
95
+ return ran_arr_buf[0];
96
+ }
97
+
98
+ /* Tweaked to include as a library - Fletcher T. Penney */
99
+ /*#include <stdio.h>
100
+ int main()
101
+ {
102
+ register int m; long a[2009];
103
+ ran_start(310952L);
104
+ for (m=0;m<=2009;m++) ran_array(a,1009);
105
+ printf("%ld\n", a[0]); *//* 995235265 */
106
+ /* ran_start(310952L);
107
+ for (m=0;m<=1009;m++) ran_array(a,2009);
108
+ printf("%ld\n", a[0]); *//* 995235265 */
109
+ /* printf("%ld\n",ran_arr_next());
110
+ return 0;
111
+ } */
112
+
113
+ long ran_num_next()
114
+ {
115
+ return ran_arr_next();
116
+ }
117
+
@@ -0,0 +1,648 @@
1
+ /*
2
+
3
+ rtf.c -- RTF writer
4
+
5
+ (c) 2013 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 "rtf.h"
19
+
20
+
21
+ /* Since RTF sucks, we simplify here */
22
+
23
+ #define kNormalStyle "\\s0 \\qj\\sa180\\f0\\fs24 "
24
+ #define kH1 "\\s1 \\f1\\fs32\\ql\\sb240\\sa180\\b "
25
+ #define kH2 "\\s2 \\f1\\fs28\\ql\\sb240\\sa180\\i\\b "
26
+ #define kH3 "\\s3 \\f1\\fs28\\ql\\sb240\\sa180\\b "
27
+ #define kH4 "\\s4 \\f1\\fs24\\ql\\sb240\\sa180\\i\\b "
28
+ #define kH5 "\\s5 \\f1\\fs24\\ql\\sb240\\sa180\\b "
29
+ #define kH6 "\\s6 \\f1\\fs22\\ql\\sb240\\sa180\\b "
30
+ #define kQuoteStyle "\\s7 \\qj\\sa180\\f0\\fs24\\li720\\ri720 "
31
+ #define kNoteStyle "\\s7 \\qj\\sa180\\f0\\fs24\\li360\\ri360 "
32
+ #define kCodeStyle "\\s7 \\qj\\sa180\\f2\\fs20\\li360\\ri360 "
33
+
34
+ /* print_rtf_node_tree -- convert node tree to RTF */
35
+ void print_rtf_node_tree(GString *out, node *list, scratch_pad *scratch) {
36
+ while (list != NULL) {
37
+ print_rtf_node(out, list, scratch);
38
+ list = list->next;
39
+ }
40
+ }
41
+
42
+ /* print_rtf_node -- convert given node to RTF and append */
43
+ void print_rtf_node(GString *out, node *n, scratch_pad *scratch) {
44
+ int i;
45
+ int lev;
46
+ int old_type;
47
+ char *temp;
48
+ link_data *temp_link_data;
49
+ node *temp_node;
50
+
51
+ switch (n->key) {
52
+ case SPACE:
53
+ case STR:
54
+ /* TODO: Some of the following need improvements */
55
+ case MATHSPAN:
56
+ print_rtf_string(out, n->str, scratch);
57
+ break;
58
+ case METADATA:
59
+ if (scratch->extensions & EXT_SNIPPET)
60
+ break;
61
+ g_string_append_printf(out, "{\\info\n");
62
+ print_rtf_node_tree(out,n->children,scratch);
63
+ g_string_append_printf(out, "}\n");
64
+ scratch->padded = 0;
65
+ break;
66
+ case METAKEY:
67
+ /* Convert key */
68
+ temp = label_from_string(n->str);
69
+ free(n->str);
70
+ n->str = temp;
71
+ if (strcmp(n->str, "baseheaderlevel") == 0) {
72
+ scratch->baseheaderlevel = atoi(n->children->str);
73
+ break;
74
+ } else if (strcmp(n->str, "rtfheaderlevel") == 0) {
75
+ scratch->baseheaderlevel = atoi(n->children->str);
76
+ break;
77
+ } else if (strcmp(n->str, "quoteslanguage") == 0) {
78
+ temp = label_from_node_tree(n->children);
79
+ if ((strcmp(temp, "nl") == 0) || (strcmp(temp, "dutch") == 0)) { scratch->language = DUTCH; } else
80
+ if ((strcmp(temp, "de") == 0) || (strcmp(temp, "german") == 0)) { scratch->language = GERMAN; } else
81
+ if (strcmp(temp, "germanguillemets") == 0) { scratch->language = GERMANGUILL; } else
82
+ if ((strcmp(temp, "fr") == 0) || (strcmp(temp, "french") == 0)) { scratch->language = FRENCH; } else
83
+ if ((strcmp(temp, "sv") == 0) || (strcmp(temp, "swedish") == 0)) { scratch->language = SWEDISH; }
84
+ free(temp);
85
+ break;
86
+ }
87
+
88
+ if (strcmp(n->str, "title") == 0) {
89
+ g_string_append_printf(out, "{\\title ");
90
+ print_rtf_node(out, n->children, scratch);
91
+ g_string_append_printf(out, "}\n");
92
+ } else if (strcmp(n->str, "author") == 0) {
93
+ g_string_append_printf(out, "{\\author ");
94
+ print_rtf_node(out, n->children, scratch);
95
+ g_string_append_printf(out, "}\n");
96
+ } else if (strcmp(n->str, "affiliation") == 0) {
97
+ g_string_append_printf(out, "{\\company ");
98
+ print_rtf_node(out, n->children, scratch);
99
+ g_string_append_printf(out, "}\n");
100
+ } else if (strcmp(n->str, "company") == 0) {
101
+ g_string_append_printf(out, "{\\company ");
102
+ print_rtf_node(out, n->children, scratch);
103
+ g_string_append_printf(out, "}\n");
104
+ } else if (strcmp(n->str, "keywords") == 0) {
105
+ g_string_append_printf(out, "{\\keywords ");
106
+ print_rtf_node(out, n->children, scratch);
107
+ g_string_append_printf(out, "}\n");
108
+ } else if (strcmp(n->str, "copyright") == 0) {
109
+ g_string_append_printf(out, "{\\*\\copyright ");
110
+ print_rtf_node(out, n->children, scratch);
111
+ g_string_append_printf(out, "}\n");
112
+ } else if (strcmp(n->str, "comment") == 0) {
113
+ g_string_append_printf(out, "{\\doccomm ");
114
+ print_rtf_node(out, n->children, scratch);
115
+ g_string_append_printf(out, "}\n");
116
+ } else if (strcmp(n->str, "subject") == 0) {
117
+ g_string_append_printf(out, "{\\subject ");
118
+ print_rtf_node(out, n->children, scratch);
119
+ g_string_append_printf(out, "}\n");
120
+ }
121
+ break;
122
+ case METAVALUE:
123
+ trim_trailing_whitespace(n->str);
124
+ print_rtf_string(out, n->str, scratch);
125
+ break;
126
+ case BLOCKQUOTEMARKER:
127
+ print_rtf_node_tree(out, n->children, scratch);
128
+ break;
129
+ case BLOCKQUOTE:
130
+ old_type = scratch->odf_para_type;
131
+ scratch->odf_para_type = BLOCKQUOTE;
132
+ pad_rtf(out, 2, scratch);
133
+ print_rtf_node_tree(out,n->children,scratch);
134
+ scratch->padded = 1;
135
+ scratch->odf_para_type = old_type;
136
+ break;
137
+ case VERBATIM:
138
+ pad_rtf(out, 2, scratch);
139
+ g_string_append_printf(out, "{\\pard " kCodeStyle);
140
+ print_rtf_code_string(out,n->str,scratch);
141
+ g_string_append_printf(out, "\n\\par}\n");
142
+ scratch->padded = 0;
143
+ break;
144
+ case CODE:
145
+ print_rtf_node_tree(out,n->children,scratch);
146
+ // print_rtf_string(out, n->str, scratch);
147
+ break;
148
+ case PARA:
149
+ pad_rtf(out, 2, scratch);
150
+ switch (scratch->odf_para_type) {
151
+ case BLOCKQUOTE:
152
+ g_string_append_printf(out, "{\\pard " kQuoteStyle);
153
+ break;
154
+ case NOTEREFERENCE:
155
+ case CITATION:
156
+ g_string_append_printf(out, "{\\pard " kNoteStyle);
157
+ break;
158
+ case CODE:
159
+ case VERBATIM:
160
+ g_string_append_printf(out, "{\\pard " kCodeStyle);
161
+ break;
162
+ default:
163
+ g_string_append_printf(out, "{\\pard " kNormalStyle);
164
+ break;
165
+ }
166
+ print_rtf_node_tree(out,n->children,scratch);
167
+ g_string_append_printf(out, "\n\\par}\n");
168
+ scratch->padded = 1;
169
+ break;
170
+ case H1: case H2: case H3: case H4: case H5: case H6:
171
+ lev = n->key - H1 + scratch->baseheaderlevel;
172
+ if (lev > 6)
173
+ lev = 6;
174
+ pad_rtf(out, 2, scratch);
175
+ switch (lev) {
176
+ case 1:
177
+ g_string_append_printf(out, "{\\pard " kH1);
178
+ break;
179
+ case 2:
180
+ g_string_append_printf(out, "{\\pard " kH2);
181
+ break;
182
+ case 3:
183
+ g_string_append_printf(out, "{\\pard " kH3);
184
+ break;
185
+ case 4:
186
+ g_string_append_printf(out, "{\\pard " kH4);
187
+ break;
188
+ case 5:
189
+ g_string_append_printf(out, "{\\pard " kH5);
190
+ break;
191
+ case 6:
192
+ g_string_append_printf(out, "{\\pard " kH6);
193
+ break;
194
+ }
195
+ if (n->children->key == AUTOLABEL) {
196
+ temp = label_from_string(n->children->str);
197
+ g_string_append_printf(out, "{\\*\\bkmkstart %s}{\\*\\bkmkend %s}",temp, temp);
198
+ print_rtf_node_tree(out, n->children->next, scratch);
199
+ } else {
200
+ temp = label_from_node_tree(n->children);
201
+ g_string_append_printf(out, "{\\*\\bkmkstart %s}{\\*\\bkmkend %s}",temp,temp);
202
+ print_rtf_node_tree(out, n->children, scratch);
203
+ }
204
+ free(temp);
205
+ g_string_append_printf(out, "\\par}\n");
206
+ scratch->padded = 1;
207
+ break;
208
+ case TABLE:
209
+ if ((n->children != NULL) && (n->children->key == TABLECAPTION)) {
210
+ if (n->children->children->key == TABLELABEL) {
211
+ temp = label_from_string(n->children->children->str);
212
+ } else {
213
+ temp = label_from_node_tree(n->children->children);
214
+ }
215
+ g_string_append_printf(out, "{\\*\\bkmkstart %s}{\\*\\bkmkend %s}",temp,temp);
216
+ free(temp);
217
+ }
218
+ pad_rtf(out, 2, scratch);
219
+ print_rtf_node_tree(out, n->children, scratch);
220
+ if ((n->children != NULL) && (n->children->key == TABLECAPTION)) {
221
+ g_string_append_printf(out, "{\\pard " kNormalStyle "\\qc ");
222
+ print_rtf_node_tree(out, n->children->children, scratch);
223
+ g_string_append_printf(out, "\\par}\n");
224
+ }
225
+ g_string_append_printf(out, "\\pard\\par\n");
226
+ scratch->padded = 1;
227
+ break;
228
+ case TABLELABEL:
229
+ case TABLECAPTION:
230
+ break;
231
+ case TABLESEPARATOR:
232
+ scratch->table_alignment = n->str;
233
+ break;
234
+ case TABLEHEAD:
235
+ scratch->cell_type = 'h';
236
+ print_rtf_node_tree(out, n->children, scratch);
237
+ scratch->cell_type = 'd';
238
+ break;
239
+ case TABLEROW:
240
+ scratch->table_column = 0;
241
+ g_string_append_printf(out, "\\trowd\\trautofit1\n");
242
+ for (i=0; i < strlen(scratch->table_alignment); i++) {
243
+ g_string_append_printf(out, "\\cellx%d\n",i+1);
244
+ }
245
+ print_rtf_node_tree(out, n->children, scratch);
246
+ g_string_append_printf(out, "\\row\n");
247
+ break;
248
+ case TABLECELL:
249
+ temp = scratch->table_alignment;
250
+ if (strncmp(&temp[scratch->table_column],"h",1) == 0) {
251
+ scratch->table_column++;
252
+ }
253
+ lev = scratch->table_column;
254
+
255
+ g_string_append_printf(out, "\\intbl");
256
+
257
+ if (scratch->cell_type == 'h') {
258
+ g_string_append_printf(out, "\\qc{\\b ");
259
+ } else {
260
+ if ( strncmp(&temp[lev],"r",1) == 0) {
261
+ g_string_append_printf(out, "\\qr");
262
+ } else if ( strncmp(&temp[lev],"R",1) == 0) {
263
+ g_string_append_printf(out, "\\qr");
264
+ } else if ( strncmp(&temp[lev],"c",1) == 0) {
265
+ g_string_append_printf(out, "\\qc");
266
+ } else if ( strncmp(&temp[lev],"C",1) == 0) {
267
+ g_string_append_printf(out, "\\qc");
268
+ } else {
269
+ g_string_append_printf(out, "\\ql");
270
+ }
271
+ }
272
+ g_string_append_printf(out, " {");
273
+ print_rtf_node_tree(out, n->children, scratch);
274
+
275
+ if (scratch->cell_type == 'h')
276
+ g_string_append_printf(out, "}");
277
+
278
+ g_string_append_printf(out, "}\\cell\n");
279
+ scratch->table_column++;
280
+ break;
281
+ case STRONG:
282
+ g_string_append_printf(out, "{\\b ");
283
+ print_rtf_node_tree(out,n->children,scratch);
284
+ g_string_append_printf(out, "}");
285
+ break;
286
+ case EMPH:
287
+ g_string_append_printf(out, "{\\i ");
288
+ print_rtf_node_tree(out,n->children,scratch);
289
+ g_string_append_printf(out, "}");
290
+ break;
291
+ case LINEBREAK:
292
+ g_string_append_printf(out, "\\line ");
293
+ break;
294
+ case LINK:
295
+ temp_link_data = load_link_data(n, scratch);
296
+
297
+ if (temp_link_data == NULL) {
298
+ /* replace original text since no definition found */
299
+ g_string_append_printf(out, "[");
300
+ print_rtf_node(out, n->children, scratch);
301
+ g_string_append_printf(out,"]");
302
+ if (n->children->next != NULL) {
303
+ g_string_append_printf(out, "[");
304
+ print_rtf_node_tree(out, n->children->next, scratch);
305
+ g_string_append_printf(out,"]");
306
+ } else if (n->str != NULL) {
307
+ /* no title label, so see if we stashed str*/
308
+ g_string_append_printf(out, "%s", n->str);
309
+ } else {
310
+ g_string_append_printf(out, "[%s]",n->link_data->label);
311
+ }
312
+
313
+ free_link_data(temp_link_data);
314
+ break;
315
+ }
316
+
317
+ /* Insert link */
318
+ g_string_append_printf(out, "{\\field{\\*\\fldinst{HYPERLINK \"");
319
+ print_rtf_string(out, temp_link_data->source, scratch);
320
+ g_string_append_printf(out, "\"}}{\\fldrslt ");
321
+ if (n->children != NULL)
322
+ print_rtf_node_tree(out, n->children, scratch);
323
+ g_string_append_printf(out, "}}");
324
+
325
+ free(temp_link_data);
326
+ break;
327
+ case BULLETLIST:
328
+ pad(out, 2, scratch);
329
+ g_string_append_printf(out, "\\ls1\\ilvl0 ");
330
+ scratch->padded = 0;
331
+ print_rtf_node_tree(out, n->children, scratch);
332
+ break;
333
+ case ORDEREDLIST:
334
+ pad(out, 2, scratch);
335
+ scratch->padded = 0;
336
+ print_rtf_node_tree(out, n->children, scratch);
337
+ break;
338
+ case LISTITEM:
339
+ g_string_append_printf(out, "{\\listtext \\'95 }");
340
+ print_rtf_node_tree(out, n->children, scratch);
341
+ break;
342
+ case NOTEREFERENCE:
343
+ lev = note_number_for_node(n, scratch);
344
+ temp_node = node_for_count(scratch->used_notes, lev);
345
+ scratch->padded = 2;
346
+
347
+ g_string_append_printf(out, "{\\super\\chftn}{\\footnote\\pard\\plain\\chtfn ");
348
+ print_rtf_node_tree(out, temp_node->children, scratch);
349
+ g_string_append_printf(out, "}");
350
+ scratch->padded = 0;
351
+ break;
352
+ case GLOSSARYTERM:
353
+ print_rtf_string(out, n->children->str, scratch);
354
+ g_string_append_printf(out, ": ");
355
+ break;
356
+ case GLOSSARYSORTKEY:
357
+ break;
358
+ case NOCITATION:
359
+ case CITATION:
360
+ if ((n->link_data != NULL) && (strncmp(n->link_data->label,"[#",2) == 0)) {
361
+ /* external citation */
362
+ g_string_append_printf(out, "%s", n->link_data->label);
363
+ } else {
364
+ /* MMD citation, so output as endnote */
365
+ scratch->printing_notes = 1;
366
+ lev = 0;
367
+ if (n->link_data != NULL)
368
+ lev = note_number_for_label(n->link_data->label, scratch);
369
+ if (lev != 0) {
370
+ temp_node = node_for_count(scratch->used_notes, lev);
371
+
372
+ /* flag that this is used as a citation */
373
+ temp_node->key = CITATIONSOURCE;
374
+ if (lev > scratch->max_footnote_num) {
375
+ /* first use of this citation */
376
+ scratch->max_footnote_num = lev;
377
+
378
+ old_type = scratch->odf_para_type;
379
+ scratch->odf_para_type = CITATION;
380
+
381
+ /* change to represent cite count only */
382
+ lev = cite_count_node_from_end(temp_node);
383
+ g_string_append_printf(out, "{\\super\\chftn}{\\footnote\\ftnalt\\pard\\plain\\chtfn ");
384
+ scratch->padded = 2;
385
+ if (temp_node->children != NULL) {
386
+ print_rtf_node(out, temp_node->children, scratch);
387
+ }
388
+ pad(out, 1, scratch);
389
+ g_string_append_printf(out, "}");
390
+ scratch->odf_para_type = old_type;
391
+ } else {
392
+ /* We are reusing a previous citation */
393
+
394
+ /* Change lev to represent cite count only */
395
+ lev = cite_count_node_from_end(temp_node);
396
+
397
+ g_string_append_printf(out, "REUSE CITATION");
398
+ }
399
+ } else {
400
+ /* not located -- this is external cite */
401
+
402
+ if ((n->link_data != NULL) && (n->key == NOCITATION)) {
403
+ g_string_append_printf(out, "%s", n->link_data->label);
404
+ } else if (n->link_data != NULL) {
405
+ g_string_append_printf(out, "[");
406
+ if (n->children != NULL) {
407
+ print_rtf_node(out, n->children, scratch);
408
+ g_string_append_printf(out, "][");
409
+ }
410
+ g_string_append_printf(out, "#%s]",n->link_data->label);
411
+ }
412
+ }
413
+ }
414
+ scratch->printing_notes = 0;
415
+ if ((n->next != NULL) && (n->next->key == CITATION)) {
416
+ g_string_append_printf(out, " ");
417
+ }
418
+ break;
419
+ case APOSTROPHE:
420
+ print_rtf_localized_typography(out, APOS, scratch);
421
+ break;
422
+ case ELLIPSIS:
423
+ print_rtf_localized_typography(out, ELLIP, scratch);
424
+ break;
425
+ case EMDASH:
426
+ print_rtf_localized_typography(out, MDASH, scratch);
427
+ break;
428
+ case ENDASH:
429
+ print_rtf_localized_typography(out, NDASH, scratch);
430
+ break;
431
+ case SINGLEQUOTED:
432
+ print_rtf_localized_typography(out, LSQUOTE, scratch);
433
+ print_rtf_node_tree(out, n->children, scratch);
434
+ print_rtf_localized_typography(out, RSQUOTE, scratch);
435
+ break;
436
+ case DOUBLEQUOTED:
437
+ print_rtf_localized_typography(out, LDQUOTE, scratch);
438
+ print_rtf_node_tree(out, n->children, scratch);
439
+ print_rtf_localized_typography(out, RDQUOTE, scratch);
440
+ break;
441
+ case LIST:
442
+ case HEADINGSECTION:
443
+ print_rtf_node_tree(out,n->children,scratch);
444
+ break;
445
+ /* TODO: Some of the following need improvements */
446
+ case TABLEBODY:
447
+ case PLAIN:
448
+ print_rtf_node_tree(out,n->children,scratch);
449
+ g_string_append_printf(out, "\\\n");
450
+ break;
451
+ case NOTELABEL:
452
+ case FOOTER:
453
+ case LINKREFERENCE:
454
+ break;
455
+ case GLOSSARYSOURCE:
456
+ case CITATIONSOURCE:
457
+ case NOTESOURCE:
458
+ if (scratch->printing_notes)
459
+ print_html_node_tree(out, n->children, scratch);
460
+ break;
461
+ case IMAGEBLOCK:
462
+ case IMAGE:
463
+ g_string_append_printf(out, "IMAGES CANNOT BE INSERTED INTO AN RTF DOCUMENT FROM MULTIMARKDOWN \\\n");
464
+ break;
465
+ case VARIABLE:
466
+ temp = metavalue_for_key(n->str,scratch->result_tree);
467
+ if (temp == NULL) {
468
+ g_string_append_printf(out, "[%%%s]",n->str);
469
+ } else {
470
+ g_string_append_printf(out, temp);
471
+ free(temp);
472
+ }
473
+ break;
474
+ default:
475
+ fprintf(stderr, "print_rtf_node encountered unknown node key = %d\n",n->key);
476
+ g_string_append_printf(out, "%s",n->str);
477
+ /* Will use in place of real output during development */
478
+ /* exit(EXIT_FAILURE); */
479
+ break;
480
+ }
481
+ }
482
+
483
+ void begin_rtf_output(GString *out, node* list, scratch_pad *scratch) {
484
+ g_string_append_printf(out, "{\\rtf1\\ansi\\deff0 {\\fonttbl\n" \
485
+ "{\\f0\\froman Times New Roman;}\n" \
486
+ "{\\f1\\fswiss Arial;}\n" \
487
+ "{\\f2\\fmodern Courier New;}\n" \
488
+ "}\n" \
489
+ "{\\stylesheet\n" \
490
+ "{" kNormalStyle "Normal;}\n" \
491
+ "{" kH1 "Header 1;}\n" \
492
+ "{" kH2 "Header 2;}\n" \
493
+ "{" kH3 "Header 3;}\n" \
494
+ "{" kH4 "Header 4;}\n" \
495
+ "{" kH5 "Header 5;}\n" \
496
+ "{" kH6 "Header 6;}\n" \
497
+ "{" kQuoteStyle "Quotation;}\n" \
498
+ "{" kNoteStyle "Note;}\n" \
499
+ "}\n" \
500
+ "\\margt1150\\margb1150\\margl1150\\margr1150\n");
501
+ }
502
+
503
+ void end_rtf_output(GString *out, node* list, scratch_pad *scratch) {
504
+ g_string_append_printf(out, "}\n");}
505
+
506
+ /* print_rtf_localized_typography -- convert to "smart" typography */
507
+ void print_rtf_localized_typography(GString *out, int character, scratch_pad *scratch) {
508
+ if (!extension(EXT_SMART, scratch->extensions)) {
509
+ g_string_append_c(out, character);
510
+ return;
511
+ }
512
+ switch (character) {
513
+ case LSQUOTE:
514
+ switch (scratch->language) {
515
+ case SWEDISH:
516
+ g_string_append_printf(out, "&#8217;");
517
+ break;
518
+ case FRENCH:
519
+ g_string_append_printf(out,"&#39;");
520
+ break;
521
+ case GERMAN:
522
+ g_string_append_printf(out,"&#8218;");
523
+ break;
524
+ case GERMANGUILL:
525
+ g_string_append_printf(out,"&#8250;");
526
+ break;
527
+ default:
528
+ g_string_append_printf(out,"\\'91");
529
+ }
530
+ break;
531
+ case RSQUOTE:
532
+ switch (scratch->language) {
533
+ case GERMAN:
534
+ g_string_append_printf(out,"&#8216;");
535
+ break;
536
+ case GERMANGUILL:
537
+ g_string_append_printf(out,"&#8249;");
538
+ break;
539
+ default:
540
+ g_string_append_printf(out,"\\'92");
541
+ }
542
+ break;
543
+ case APOS:
544
+ g_string_append_printf(out,"\\'27");
545
+ break;
546
+ case LDQUOTE:
547
+ switch (scratch->language) {
548
+ case DUTCH:
549
+ case GERMAN:
550
+ g_string_append_printf(out,"&#8222;");
551
+ break;
552
+ case GERMANGUILL:
553
+ g_string_append_printf(out,"&#187;");
554
+ break;
555
+ case FRENCH:
556
+ g_string_append_printf(out,"&#171;");
557
+ break;
558
+ case SWEDISH:
559
+ g_string_append_printf(out, "&#8221;");
560
+ break;
561
+ default:
562
+ g_string_append_printf(out,"\\'93");
563
+ }
564
+ break;
565
+ case RDQUOTE:
566
+ switch (scratch->language) {
567
+ case SWEDISH:
568
+ case DUTCH:
569
+ g_string_append_printf(out,"&#8221;");
570
+ break;
571
+ case GERMAN:
572
+ g_string_append_printf(out,"&#8220;");
573
+ break;
574
+ case GERMANGUILL:
575
+ g_string_append_printf(out,"&#171;");
576
+ break;
577
+ case FRENCH:
578
+ g_string_append_printf(out,"&#187;");
579
+ break;
580
+ default:
581
+ g_string_append_printf(out,"\\'94");
582
+ }
583
+ break;
584
+ case NDASH:
585
+ g_string_append_printf(out,"\\'96");
586
+ break;
587
+ case MDASH:
588
+ g_string_append_printf(out,"\\'97");
589
+ break;
590
+ case ELLIP:
591
+ g_string_append_printf(out,"\\'85");
592
+ break;
593
+ default:;
594
+ }
595
+ }
596
+
597
+ void print_rtf_string(GString *out, char *str, scratch_pad *scratch) {
598
+ if (str == NULL)
599
+ return;
600
+ while (*str != '\0') {
601
+ switch (*str) {
602
+ case '\\':
603
+ g_string_append_printf(out, "\\\\");
604
+ break;
605
+ case '{':
606
+ g_string_append_printf(out, "\\{");
607
+ break;
608
+ case '}':
609
+ g_string_append_printf(out, "\\}");
610
+ break;
611
+ case '\n':
612
+ g_string_append_printf(out, " \n");
613
+ default:
614
+ g_string_append_c(out, *str);
615
+ }
616
+ str++;
617
+ }
618
+ }
619
+
620
+ void print_rtf_code_string(GString *out, char *str, scratch_pad *scratch) {
621
+ if (str == NULL)
622
+ return;
623
+ while (*str != '\0') {
624
+ switch (*str) {
625
+ case '\\':
626
+ g_string_append_printf(out, "\\\\");
627
+ break;
628
+ case '{':
629
+ g_string_append_printf(out, "\\{");
630
+ break;
631
+ case '}':
632
+ g_string_append_printf(out, "\\}");
633
+ break;
634
+ case '\n':
635
+ g_string_append_printf(out, "\\\n");
636
+ break;
637
+ default:
638
+ g_string_append_c(out, *str);
639
+ }
640
+ str++;
641
+ }
642
+ }
643
+ void pad_rtf(GString *out, int num, scratch_pad *scratch) {
644
+ while (num-- > scratch->padded)
645
+ g_string_append_printf(out, "\n");
646
+
647
+ scratch->padded = num;
648
+ }