mmd-ruby 5.2.0.1

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 (59) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +78 -0
  3. data/MultiMarkdown-5/src/GLibFacade.c +310 -0
  4. data/MultiMarkdown-5/src/GLibFacade.h +100 -0
  5. data/MultiMarkdown-5/src/beamer.c +182 -0
  6. data/MultiMarkdown-5/src/beamer.h +11 -0
  7. data/MultiMarkdown-5/src/critic.c +117 -0
  8. data/MultiMarkdown-5/src/critic.h +15 -0
  9. data/MultiMarkdown-5/src/glib.h +11 -0
  10. data/MultiMarkdown-5/src/html.c +1171 -0
  11. data/MultiMarkdown-5/src/html.h +14 -0
  12. data/MultiMarkdown-5/src/latex.c +1234 -0
  13. data/MultiMarkdown-5/src/latex.h +16 -0
  14. data/MultiMarkdown-5/src/libMultiMarkdown.h +257 -0
  15. data/MultiMarkdown-5/src/lyx.c +2269 -0
  16. data/MultiMarkdown-5/src/lyx.h +37 -0
  17. data/MultiMarkdown-5/src/lyxbeamer.c +265 -0
  18. data/MultiMarkdown-5/src/lyxbeamer.h +11 -0
  19. data/MultiMarkdown-5/src/memoir.c +80 -0
  20. data/MultiMarkdown-5/src/memoir.h +10 -0
  21. data/MultiMarkdown-5/src/multimarkdown.c +559 -0
  22. data/MultiMarkdown-5/src/odf.c +1241 -0
  23. data/MultiMarkdown-5/src/odf.h +18 -0
  24. data/MultiMarkdown-5/src/opml.c +189 -0
  25. data/MultiMarkdown-5/src/opml.h +15 -0
  26. data/MultiMarkdown-5/src/parse_utilities.c +912 -0
  27. data/MultiMarkdown-5/src/parser.c +17341 -0
  28. data/MultiMarkdown-5/src/parser.h +190 -0
  29. data/MultiMarkdown-5/src/rng.c +117 -0
  30. data/MultiMarkdown-5/src/rtf.c +665 -0
  31. data/MultiMarkdown-5/src/rtf.h +17 -0
  32. data/MultiMarkdown-5/src/strtok.c +56 -0
  33. data/MultiMarkdown-5/src/strtok.h +9 -0
  34. data/MultiMarkdown-5/src/text.c +56 -0
  35. data/MultiMarkdown-5/src/text.h +11 -0
  36. data/MultiMarkdown-5/src/toc.c +157 -0
  37. data/MultiMarkdown-5/src/toc.h +15 -0
  38. data/MultiMarkdown-5/src/transclude.c +335 -0
  39. data/MultiMarkdown-5/src/transclude.h +28 -0
  40. data/MultiMarkdown-5/src/version.h +59 -0
  41. data/MultiMarkdown-5/src/writer.c +767 -0
  42. data/MultiMarkdown-5/src/writer.h +38 -0
  43. data/README.md +77 -0
  44. data/Rakefile +88 -0
  45. data/bin/mmd-ruby +123 -0
  46. data/ext/extconf.h +3 -0
  47. data/ext/extconf.rb +10 -0
  48. data/ext/multimarkdown.c +133 -0
  49. data/lib/mmd-jekyll.rb +19 -0
  50. data/lib/mmd-ruby.rb +1 -0
  51. data/lib/mmd.rb +1 -0
  52. data/lib/multimarkdown-ruby.rb +1 -0
  53. data/lib/multimarkdown.bundle +0 -0
  54. data/lib/multimarkdown.rb +69 -0
  55. data/lib/multimarkdown/version.rb +6 -0
  56. data/mmd-ruby.gemspec +37 -0
  57. data/test/extensions_test.rb +174 -0
  58. data/test/multimarkdown_test.rb +77 -0
  59. metadata +120 -0
@@ -0,0 +1,182 @@
1
+ /*
2
+
3
+ beamer.c -- Beamer add-on to LaTeX writer
4
+
5
+ (c) 2013-2016 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 "beamer.h"
19
+
20
+ /* print_beamer_node_tree -- convert node tree to LaTeX */
21
+ void print_beamer_node_tree(GString *out, node *list, scratch_pad *scratch) {
22
+ while (list != NULL) {
23
+ print_beamer_node(out, list, scratch);
24
+ list = list->next;
25
+ }
26
+ }
27
+
28
+ /* print_beamer_node -- convert given node to LaTeX and append */
29
+ void print_beamer_node(GString *out, node *n, scratch_pad *scratch) {
30
+ int lev;
31
+ char *temp;
32
+
33
+ /* If we are forcing a complete document, and METADATA isn't the first thing,
34
+ we need to close <head> */
35
+ if ((scratch->extensions & EXT_COMPLETE)
36
+ && !(scratch->extensions & EXT_HEAD_CLOSED) &&
37
+ !((n->key == FOOTER) || (n->key == METADATA))) {
38
+ pad(out, 2, scratch);
39
+ scratch->extensions = scratch->extensions | EXT_HEAD_CLOSED;
40
+ }
41
+ switch (n->key) {
42
+ case FOOTER:
43
+ print_beamer_endnotes(out, scratch);
44
+ g_string_append_printf(out, "\\mode<all>\n");
45
+ if (scratch->latex_footer != NULL) {
46
+ pad(out, 2, scratch);
47
+ g_string_append_printf(out,"\\input{%s}\n", scratch->latex_footer);
48
+ }
49
+ if (scratch->extensions & EXT_COMPLETE) {
50
+ g_string_append_printf(out, "\n\\end{document}");
51
+ }
52
+ g_string_append_printf(out, "\\mode*\n");
53
+ break;
54
+ case LISTITEM:
55
+ pad(out, 1, scratch);
56
+ g_string_append_printf(out, "\\item<+-> ");
57
+ scratch->padded = 2;
58
+ print_latex_node_tree(out, n->children, scratch);
59
+ g_string_append_printf(out, "\n");
60
+ break;
61
+ case HEADINGSECTION:
62
+ if (n->children->key -H1 + scratch->baseheaderlevel == 3) {
63
+ pad(out, 2, scratch);
64
+ g_string_append_printf(out, "\\begin{frame}");
65
+ /* TODO: Fix this */
66
+ if (tree_contains_key(n->children,VERBATIM) ||
67
+ tree_contains_key(n->children,VERBATIMFENCE)) {
68
+ g_string_append_printf(out, "[fragile]");
69
+ }
70
+ scratch->padded = 0;
71
+ print_beamer_node_tree(out, n->children, scratch);
72
+ g_string_append_printf(out, "\n\n\\end{frame}\n\n");
73
+ scratch->padded = 2;
74
+ } else if (n->children->key -H1 + scratch->baseheaderlevel == 4) {
75
+ pad(out, 1, scratch);
76
+ g_string_append_printf(out, "\\mode<article>{\n");
77
+ scratch->padded = 0;
78
+ print_beamer_node_tree(out, n->children->next, scratch);
79
+ g_string_append_printf(out, "\n\n}\n\n");
80
+ scratch->padded = 2;
81
+ } else {
82
+ print_beamer_node_tree(out, n->children, scratch);
83
+ }
84
+ break;
85
+ case H1: case H2: case H3: case H4: case H5: case H6:
86
+ pad(out, 2, scratch);
87
+ lev = n->key - H1 + scratch->baseheaderlevel; /* assumes H1 ... H6 are in order */
88
+ switch (lev) {
89
+ case 1:
90
+ g_string_append_printf(out, "\\part{");
91
+ break;
92
+ case 2:
93
+ g_string_append_printf(out, "\\section{");
94
+ break;
95
+ case 3:
96
+ g_string_append_printf(out, "\\frametitle{");
97
+ break;
98
+ default:
99
+ g_string_append_printf(out, "\\emph{");
100
+ break;
101
+ }
102
+ /* generate a label for each header (MMD);
103
+ don't allow footnotes since invalid here */
104
+ scratch->no_latex_footnote = TRUE;
105
+ if (n->children->key == AUTOLABEL) {
106
+ temp = label_from_string(n->children->str);
107
+ print_latex_node_tree(out, n->children->next, scratch);
108
+ g_string_append_printf(out, "}\n\\label{%s}", temp);
109
+ free(temp);
110
+ } else {
111
+ print_latex_node_tree(out, n->children, scratch);
112
+ temp = label_from_node_tree(n->children);
113
+ g_string_append_printf(out, "}\n\\label{%s}", temp);
114
+ free(temp);
115
+ }
116
+ scratch->no_latex_footnote = FALSE;
117
+ scratch->padded = 0;
118
+ break;
119
+ default:
120
+ /* most things are not changed for memoir output */
121
+ print_latex_node(out, n, scratch);
122
+ }
123
+ }
124
+
125
+ /* print_beamer_endnotes */
126
+ void print_beamer_endnotes(GString *out, scratch_pad *scratch) {
127
+ scratch->used_notes = reverse_list(scratch->used_notes);
128
+ scratch->printing_notes = 1;
129
+
130
+ node *note = scratch->used_notes;
131
+ #ifdef DEBUG_ON
132
+ fprintf(stderr, "start endnotes\n");
133
+ #endif
134
+
135
+ if ((note == NULL) || ((note->key == KEY_COUNTER) && (note->next == NULL)))
136
+ return;
137
+ while (note != NULL) {
138
+ if (note->key == CITATIONSOURCE)
139
+ break;
140
+ note = note->next;
141
+ }
142
+
143
+ if (note == NULL)
144
+ return;
145
+
146
+ note = scratch->used_notes;
147
+
148
+ /* TODO: need CITATIONSOURCE to print bibliography */
149
+ #ifdef DEBUG_ON
150
+ fprintf(stderr, "there are endnotes to print\n");
151
+ #endif
152
+
153
+ pad(out, 2, scratch);
154
+ g_string_append_printf(out, "\\part{Bibliography}\n\\begin{frame}[allowframebreaks]\n\\frametitle{Bibliography}\n\\def\\newblock{}\n\\begin{thebibliography}{0}\n");
155
+ while ( note != NULL) {
156
+ if (note->key == KEY_COUNTER) {
157
+ note = note->next;
158
+ continue;
159
+ }
160
+
161
+ pad(out, 1, scratch);
162
+
163
+ if (note->key == CITATIONSOURCE) {
164
+ g_string_append_printf(out, "\\bibitem{%s}\n", note->str);
165
+ scratch->padded = 2;
166
+
167
+ print_latex_node(out, note, scratch);
168
+ pad(out, 1, scratch);
169
+ scratch->padded = 1;
170
+ } else {
171
+ /* footnotes handled elsewhere */
172
+ }
173
+
174
+ note = note->next;
175
+ }
176
+ pad(out,2, scratch);
177
+ g_string_append_printf(out, "\\end{thebibliography}\n\\end{frame}\n\n");
178
+ scratch->padded = 0;
179
+ #ifdef DEBUG_ON
180
+ fprintf(stderr, "finish endnotes\n");
181
+ #endif
182
+ }
@@ -0,0 +1,11 @@
1
+ #ifndef BEAMER_PARSER_H
2
+ #define BEAMER_PARSER_H
3
+
4
+ #include "parser.h"
5
+ #include "latex.h"
6
+
7
+ void print_beamer_node_tree(GString *out, node *list, scratch_pad *scratch);
8
+ void print_beamer_node(GString *out, node *n, scratch_pad *scratch);
9
+ void print_beamer_endnotes(GString *out, scratch_pad *scratch);
10
+
11
+ #endif
@@ -0,0 +1,117 @@
1
+ /*
2
+
3
+ critic.c -- CriticMarkup preprocessor
4
+
5
+ <http://criticmarkup.com>
6
+
7
+ (c) 2013-2016 Fletcher T. Penney (http://fletcherpenney.net/).
8
+
9
+ This program is free software; you can redistribute it and/or modify
10
+ it under the terms of the GNU General Public License or the MIT
11
+ license. See LICENSE for details.
12
+
13
+ This program is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU General Public License for more details.
17
+
18
+ */
19
+
20
+ #include "critic.h"
21
+
22
+ void print_critic_accept_node_tree(GString *out, node *list, scratch_pad *scratch) {
23
+ while (list != NULL) {
24
+ print_critic_accept_node(out, list, scratch);
25
+ list = list->next;
26
+ }
27
+ }
28
+
29
+ void print_critic_reject_node_tree(GString *out, node *list, scratch_pad *scratch) {
30
+ while (list != NULL) {
31
+ print_critic_reject_node(out, list, scratch);
32
+ list = list->next;
33
+ }
34
+ }
35
+
36
+ void print_critic_html_highlight_node_tree(GString *out, node *list, scratch_pad *scratch) {
37
+ while (list != NULL) {
38
+ print_critic_html_highlight_node(out, list, scratch);
39
+ list = list->next;
40
+ }
41
+ }
42
+
43
+ void print_critic_accept_node(GString *out, node *n, scratch_pad *scratch) {
44
+ if (n == NULL)
45
+ return;
46
+
47
+ switch (n->key) {
48
+ case LIST:
49
+ case CRITICSUBSTITUTION:
50
+ case CRITICADDITION:
51
+ case CRITICHIGHLIGHT:
52
+ print_critic_accept_node_tree(out, n->children, scratch);
53
+ break;
54
+ case CRITICDELETION:
55
+ case CRITICCOMMENT:
56
+ break;
57
+ default:
58
+ g_string_append_printf(out,"%s",n->str);
59
+ break;
60
+ }
61
+ }
62
+
63
+ void print_critic_reject_node(GString *out, node *n, scratch_pad *scratch) {
64
+ if (n == NULL)
65
+ return;
66
+
67
+ switch (n->key) {
68
+ case LIST:
69
+ case CRITICSUBSTITUTION:
70
+ case CRITICDELETION:
71
+ case CRITICHIGHLIGHT:
72
+ print_critic_reject_node_tree(out, n->children, scratch);
73
+ break;
74
+ case CRITICADDITION:
75
+ case CRITICCOMMENT:
76
+ break;
77
+ default:
78
+ g_string_append_printf(out,"%s",n->str);
79
+ break;
80
+ }
81
+ }
82
+
83
+ void print_critic_html_highlight_node(GString *out, node *n, scratch_pad *scratch) {
84
+ if (n == NULL)
85
+ return;
86
+
87
+ switch (n->key) {
88
+ case LIST:
89
+ print_critic_html_highlight_node_tree(out, n->children, scratch);
90
+ break;
91
+ case CRITICSUBSTITUTION:
92
+ print_critic_html_highlight_node_tree(out, n->children, scratch);
93
+ break;
94
+ case CRITICADDITION:
95
+ g_string_append_printf(out, "<ins>");
96
+ print_critic_html_highlight_node_tree(out, n->children, scratch);
97
+ g_string_append_printf(out, "</ins>");
98
+ break;
99
+ case CRITICCOMMENT:
100
+ /* Hide comments for now */
101
+ /* g_string_append_printf(out, "<span class=\"critic comment\">%s</span>", n->str); */
102
+ break;
103
+ case CRITICHIGHLIGHT:
104
+ g_string_append_printf(out, "<mark>");
105
+ print_critic_html_highlight_node_tree(out, n->children, scratch);
106
+ g_string_append_printf(out, "</mark>");
107
+ break;
108
+ case CRITICDELETION:
109
+ g_string_append_printf(out, "<del>");
110
+ print_critic_html_highlight_node_tree(out, n->children, scratch);
111
+ g_string_append_printf(out, "</del>");
112
+ break;
113
+ default:
114
+ g_string_append_printf(out,"%s",n->str);
115
+ break;
116
+ }
117
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef CRITIC_PARSER_H
2
+ #define CRITIC_PARSER_H
3
+
4
+ #include "parser.h"
5
+ #include "writer.h"
6
+
7
+
8
+ void print_critic_accept_node_tree(GString *out, node *list, scratch_pad *scratch);
9
+ void print_critic_reject_node_tree(GString *out, node *list, scratch_pad *scratch);
10
+ void print_critic_html_highlight_node_tree(GString *out, node *list, scratch_pad *scratch);
11
+ void print_critic_accept_node(GString *out, node *list, scratch_pad *scratch);
12
+ void print_critic_reject_node(GString *out, node *list, scratch_pad *scratch);
13
+ void print_critic_html_highlight_node(GString *out, node *list, scratch_pad *scratch);
14
+
15
+ #endif
@@ -0,0 +1,11 @@
1
+ /*
2
+ * glib.h
3
+ * MultiMarkdown
4
+ *
5
+ * Created by Daniel Jalkut on 7/26/11.
6
+ * Copyright 2011 __MyCompanyName__. All rights reserved.
7
+ *
8
+ */
9
+
10
+ /* Just a dummy file to keep the glib-dependent sources compiling as we would hope */
11
+ #include "GLibFacade.h"
@@ -0,0 +1,1171 @@
1
+ /*
2
+
3
+ html.c -- HTML writer
4
+
5
+ (c) 2013-2016 Fletcher T. Penney (http://fletcherpenney.net/).
6
+
7
+ Derived from peg-multimarkdown, which was forked from peg-markdown,
8
+ which is (c) 2008 John MacFarlane (jgm at berkeley dot edu), and
9
+ licensed under GNU GPL or MIT.
10
+
11
+ This program is free software; you can redistribute it and/or modify
12
+ it under the terms of the GNU General Public License or the MIT
13
+ license. See LICENSE for details.
14
+
15
+ This program is distributed in the hope that it will be useful,
16
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ GNU General Public License for more details.
19
+
20
+ */
21
+
22
+ #include "html.h"
23
+
24
+ /* #define DEBUG_ON */
25
+
26
+ bool is_html_complete_doc(node *meta);
27
+ void print_col_group(GString *out,scratch_pad *scratch);
28
+
29
+
30
+ /* print_html_node_tree -- convert node tree to HTML */
31
+ void print_html_node_tree(GString *out, node *list, scratch_pad *scratch) {
32
+ while (list != NULL) {
33
+ print_html_node(out, list, scratch);
34
+ list = list->next;
35
+ }
36
+ }
37
+
38
+ /* print_html_node -- convert given node to HTML and append */
39
+ void print_html_node(GString *out, node *n, scratch_pad *scratch) {
40
+ node *temp_node;
41
+ link_data *temp_link_data = NULL;
42
+ char *temp;
43
+ int lev;
44
+ int random;
45
+ char temp_type;
46
+ char *width = NULL;
47
+ char *height = NULL;
48
+ GString *temp_str;
49
+ size_t temp_size;
50
+
51
+ if (n == NULL)
52
+ return;
53
+
54
+ /* debugging statement */
55
+ #ifdef DEBUG_ON
56
+ fprintf(stderr, "print_html_node: %d\n",n->key);
57
+ #endif
58
+
59
+ /* If we are forcing a complete document, and METADATA isn't the first thing,
60
+ we need to close <head> */
61
+ if ((scratch->extensions & EXT_COMPLETE)
62
+ && !(scratch->extensions & EXT_HEAD_CLOSED) &&
63
+ !((n->key == FOOTER) || (n->key == METADATA))) {
64
+ g_string_append_printf(out, "</head>\n<body>\n");
65
+ scratch->extensions = scratch->extensions | EXT_HEAD_CLOSED;
66
+ }
67
+ switch (n->key) {
68
+ case NO_TYPE:
69
+ break;
70
+ case LIST:
71
+ print_html_node_tree(out,n->children,scratch);
72
+ break;
73
+ case STR:
74
+ /* fprintf(stderr, "str: '%s'\n", n->str); */
75
+ print_html_string(out,n->str, scratch);
76
+ break;
77
+ case ABBR:
78
+ if (strlen(n->children->str) == 0) {
79
+ g_string_append_printf(out, "<abbr>");
80
+ } else {
81
+ g_string_append_printf(out, "<abbr title=\"");
82
+ print_html_string(out, n->children->str, scratch);
83
+ g_string_append_printf(out, "\">");
84
+ }
85
+ print_html_string(out,n->str, scratch);
86
+ g_string_append_printf(out, "</abbr>");
87
+ break;
88
+ case ABBRSTART:
89
+ if (strlen(n->children->str) == 0) {
90
+ g_string_append_printf(out, "<abbr>");
91
+ } else {
92
+ g_string_append_printf(out, "<abbr title=\"");
93
+ print_html_string(out, n->children->str, scratch);
94
+ g_string_append_printf(out, "\">");
95
+ }
96
+ print_html_string(out,n->str, scratch);
97
+ break;
98
+ case ABBRSTOP:
99
+ print_html_string(out,n->str, scratch);
100
+ g_string_append_printf(out, "</abbr>");
101
+ break;
102
+ case SPACE:
103
+ g_string_append_printf(out,"%s",n->str);
104
+ break;
105
+ case PLAIN:
106
+ pad(out,1, scratch);
107
+ print_html_node_tree(out,n->children, scratch);
108
+ scratch->padded = 0;
109
+ break;
110
+ case PARA:
111
+ pad(out, 2, scratch);
112
+ g_string_append_printf(out, "<p>");
113
+ print_html_node_tree(out,n->children,scratch);
114
+ if (scratch->footnote_to_print != 0){
115
+ scratch->footnote_para_counter --;
116
+ if (scratch->footnote_para_counter == 0) {
117
+ if (scratch->extensions & EXT_RANDOM_FOOT) {
118
+ srand(scratch->random_seed_base + scratch->footnote_to_print);
119
+ random = rand() % 99999 + 1;
120
+ } else {
121
+ random = scratch->footnote_to_print;
122
+ }
123
+
124
+ g_string_append_printf(out, " <a href=\"#fnref:%d\" title=\"return to article\" class=\"reversefootnote\">&#160;&#8617;</a>", random);
125
+ scratch->footnote_to_print = 0;
126
+ }
127
+ }
128
+ g_string_append_printf(out, "</p>");
129
+ scratch->padded = 0;
130
+ break;
131
+ case HRULE:
132
+ pad(out, 2, scratch);
133
+ g_string_append_printf(out, "<hr />");
134
+ scratch->padded = 0;
135
+ break;
136
+ case HTMLBLOCK:
137
+ pad(out, 2, scratch);
138
+ g_string_append_printf(out, "%s", n->str);
139
+ scratch->padded = 0;
140
+ break;
141
+ case VERBATIM:
142
+ case VERBATIMFENCE:
143
+ pad(out, 2, scratch);
144
+ if ((n->children != NULL) && (n->children->key == VERBATIMTYPE)) {
145
+ trim_trailing_whitespace(n->children->str);
146
+ if (strlen(n->children->str) > 0)
147
+ g_string_append_printf(out, "<pre><code class=\"%s\">", n->children->str);
148
+ else
149
+ g_string_append_printf(out, "%s", "<pre><code>");
150
+ } else {
151
+ g_string_append_printf(out, "%s", "<pre><code>");
152
+ }
153
+ print_html_string(out, n->str, scratch);
154
+ g_string_append_printf(out, "%s", "</code></pre>");
155
+ scratch->padded = 0;
156
+ break;
157
+ case BULLETLIST:
158
+ pad(out, 2, scratch);
159
+ g_string_append_printf(out, "%s", "<ul>");
160
+ scratch->padded = 0;
161
+ print_html_node_tree(out, n->children, scratch);
162
+ pad(out, 1, scratch);
163
+ g_string_append_printf(out, "%s", "</ul>");
164
+ scratch->padded = 0;
165
+ break;
166
+ case ORDEREDLIST:
167
+ pad(out, 2, scratch);
168
+ g_string_append_printf(out, "%s", "<ol>");
169
+ scratch->padded = 0;
170
+ print_html_node_tree(out, n->children, scratch);
171
+ pad(out, 1, scratch);
172
+ g_string_append_printf(out, "</ol>");
173
+ scratch->padded = 0;
174
+ break;
175
+ case LISTITEM:
176
+ pad(out, 1, scratch);
177
+ g_string_append_printf(out, "<li>");
178
+ scratch->padded = 2;
179
+ print_html_node_tree(out, n->children, scratch);
180
+ g_string_append_printf(out, "</li>");
181
+ scratch->padded = 0;
182
+ break;
183
+ case METADATA:
184
+ /* Not if snippet only */
185
+ if (scratch->extensions & EXT_SNIPPET) {
186
+ is_html_complete_doc(n);
187
+ print_html_node_tree(out,n->children, scratch);
188
+ break;
189
+ }
190
+
191
+ if (!(scratch->extensions & EXT_COMPLETE) && (is_html_complete_doc(n))) {
192
+ /* We have metadata to include, and didn't already force complete */
193
+ temp = metavalue_for_key("lang", scratch->result_tree);
194
+ if (temp != NULL) {
195
+ g_string_append_printf(out,
196
+ "<!DOCTYPE html>\n<html lang=\"%s\">\n<head>\n\t<meta charset=\"utf-8\"/>\n",temp);
197
+ free(temp);
198
+ } else {
199
+ g_string_append_printf(out,
200
+ "<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset=\"utf-8\"/>\n");
201
+ }
202
+ /* either way, now we need to be a complete doc */
203
+ scratch->extensions = scratch->extensions | EXT_COMPLETE;
204
+ } else {
205
+ /* Ensure we lowercase metadata */
206
+ is_html_complete_doc(n);
207
+ }
208
+ /* print the metadata */
209
+ scratch->extensions = scratch->extensions | EXT_HEAD_CLOSED;
210
+ print_html_node_tree(out,n->children, scratch);
211
+ if (scratch->extensions & EXT_COMPLETE) {
212
+ /* need to close head and open body */
213
+ g_string_append_printf(out, "</head>\n<body>\n\n");
214
+ }
215
+ break;
216
+ case METAKEY:
217
+ if (strcmp(n->str, "baseheaderlevel") == 0) {
218
+ scratch->baseheaderlevel = atoi(n->children->str);
219
+ break;
220
+ } else if (strcmp(n->str, "xhtmlheaderlevel") == 0) {
221
+ scratch->baseheaderlevel = atoi(n->children->str);
222
+ break;
223
+ } else if (strcmp(n->str, "htmlheaderlevel") == 0) {
224
+ scratch->baseheaderlevel = atoi(n->children->str);
225
+ break;
226
+ } else if (strcmp(n->str, "htmlfooter") == 0) {
227
+ trim_trailing_whitespace(n->children->str);
228
+ scratch->html_footer = strdup(n->children->str);
229
+ break;
230
+ } else if (strcmp(n->str, "quoteslanguage") == 0) {
231
+ temp = label_from_node_tree(n->children);
232
+ if ((strcmp(temp, "nl") == 0) || (strcmp(temp, "dutch") == 0)) { scratch->language = DUTCH; } else
233
+ if ((strcmp(temp, "de") == 0) || (strcmp(temp, "german") == 0)) { scratch->language = GERMAN; } else
234
+ if (strcmp(temp, "germanguillemets") == 0) { scratch->language = GERMANGUILL; } else
235
+ if ((strcmp(temp, "fr") == 0) || (strcmp(temp, "french") == 0)) { scratch->language = FRENCH; } else
236
+ if ((strcmp(temp, "sv") == 0) || (strcmp(temp, "swedish") == 0)) { scratch->language = SWEDISH; }
237
+ free(temp);
238
+ break;
239
+ }
240
+
241
+ /* Don't handle other metadata if we're snippet only */
242
+ if (scratch->extensions & EXT_SNIPPET)
243
+ break;
244
+
245
+ if (strcmp(n->str, "title") == 0) {
246
+ g_string_append_printf(out, "\t<title>");
247
+ print_html_node(out, n->children, scratch);
248
+ g_string_append_printf(out, "</title>\n");
249
+ } else if (strcmp(n->str, "css") == 0) {
250
+ g_string_append_printf(out, "\t<link type=\"text/css\" rel=\"stylesheet\" href=\"");
251
+ print_html_node(out, n->children, scratch);
252
+ g_string_append_printf(out, "\"/>\n");
253
+ } else if (strcmp(n->str, "xhtmlheader") == 0) {
254
+ trim_trailing_whitespace(n->children->str);
255
+ print_raw_node(out, n->children);
256
+ g_string_append_printf(out, "\n");
257
+ } else if (strcmp(n->str, "htmlheader") == 0) {
258
+ trim_trailing_whitespace(n->children->str);
259
+ print_raw_node(out, n->children);
260
+ g_string_append_printf(out, "\n");
261
+ } else if (strcmp(n->str, "mmdfooter") == 0) {
262
+ } else if (strcmp(n->str, "mmdheader") == 0) {
263
+ } else if (strcmp(n->str, "lang") == 0) {
264
+ } else if (strcmp(n->str, "transcludebase") == 0) {
265
+ } else if (strcmp(n->str, "htmlfooter") == 0) {
266
+ } else if (strcmp(n->str, "latexmode") == 0) {
267
+ } else if (strcmp(n->str, "latexinput") == 0) {
268
+ } else if (strcmp(n->str, "latexfooter") == 0) {
269
+ } else if (strcmp(n->str, "bibtex") == 0) {
270
+ } else {
271
+ g_string_append_printf(out,"\t<meta name=\"%s\" content=\"",n->str);
272
+ print_html_node(out,n->children,scratch);
273
+ g_string_append_printf(out,"\"/>\n");
274
+ }
275
+ break;
276
+ case METAVALUE:
277
+ trim_trailing_whitespace(n->str);
278
+ print_html_string(out,n->str, scratch);
279
+ break;
280
+ case FOOTER:
281
+ break;
282
+ case HEADINGSECTION:
283
+ print_html_node_tree(out,n->children,scratch);
284
+ break;
285
+ case H1: case H2: case H3: case H4: case H5: case H6:
286
+ lev = n->key - H1 + scratch->baseheaderlevel; /* assumes H1 ... H6 are in order */
287
+ if (lev > 6)
288
+ lev = 6; /* Max at level 6 */
289
+ pad(out, 2, scratch);
290
+ if ( scratch->extensions & EXT_COMPATIBILITY ) {
291
+ /* Use regular Markdown header format */
292
+ g_string_append_printf(out, "<h%1d>", lev);
293
+ print_html_node_tree(out, n->children, scratch);
294
+ } else if (n->children->key == AUTOLABEL) {
295
+ temp = label_from_string(n->children->str);
296
+ /* use label for header since one was specified (MMD)*/
297
+ g_string_append_printf(out, "<h%d id=\"%s\">", lev,temp);
298
+ print_html_node_tree(out, n->children->next, scratch);
299
+ free(temp);
300
+ } else if ( scratch->extensions & EXT_NO_LABELS ) {
301
+ /* Don't generate a label */
302
+ g_string_append_printf(out, "<h%1d>", lev);
303
+ print_html_node_tree(out, n->children, scratch);
304
+ } else {
305
+ /* generate a label by default for MMD */
306
+ temp = label_from_node_tree(n->children);
307
+ g_string_append_printf(out, "<h%d id=\"%s\">", lev, temp);
308
+ print_html_node_tree(out, n->children, scratch);
309
+ free(temp);
310
+ }
311
+ g_string_append_printf(out, "</h%1d>", lev);
312
+ scratch->padded = 0;
313
+ break;
314
+ case APOSTROPHE:
315
+ print_html_localized_typography(out, APOS, scratch);
316
+ break;
317
+ case ELLIPSIS:
318
+ print_html_localized_typography(out, ELLIP, scratch);
319
+ break;
320
+ case EMDASH:
321
+ print_html_localized_typography(out, MDASH, scratch);
322
+ break;
323
+ case ENDASH:
324
+ print_html_localized_typography(out, NDASH, scratch);
325
+ break;
326
+ case SINGLEQUOTED:
327
+ print_html_localized_typography(out, LSQUOTE, scratch);
328
+ print_html_node_tree(out, n->children, scratch);
329
+ print_html_localized_typography(out, RSQUOTE, scratch);
330
+ break;
331
+ case DOUBLEQUOTED:
332
+ print_html_localized_typography(out, LDQUOTE, scratch);
333
+ print_html_node_tree(out, n->children, scratch);
334
+ print_html_localized_typography(out, RDQUOTE, scratch);
335
+ break;
336
+ case LINEBREAK:
337
+ g_string_append_printf(out, "<br/>\n");
338
+ break;
339
+ case MATHSPAN:
340
+ temp = strdup(n->str);
341
+ if (temp[0] == '$') {
342
+ temp[strlen(temp)-1] = '\0';
343
+ if (temp[1] == '$') {
344
+ temp[strlen(temp)-1] = '\0';
345
+ g_string_append_printf(out, "<span class=\"math\">\\[%s\\]</span>",&temp[2]);
346
+ } else {
347
+ g_string_append_printf(out, "<span class=\"math\">\\(%s\\)</span>",&temp[1]);
348
+ }
349
+ } else if (temp[strlen(temp) - 1] == ']') {
350
+ temp[strlen(temp) - 3] = '\0';
351
+ g_string_append_printf(out, "<span class=\"math\">%s\\]</span>",temp);
352
+ } else {
353
+ temp[strlen(temp) - 3] = '\0';
354
+ g_string_append_printf(out, "<span class=\"math\">%s\\)</span>",temp);
355
+ }
356
+ free(temp);
357
+ break;
358
+ case STRONG:
359
+ g_string_append_printf(out, "<strong>");
360
+ print_html_node_tree(out,n->children,scratch);
361
+ g_string_append_printf(out, "</strong>");
362
+ break;
363
+ case EMPH:
364
+ g_string_append_printf(out, "<em>");
365
+ print_html_node_tree(out,n->children,scratch);
366
+ g_string_append_printf(out, "</em>");
367
+ break;
368
+ case LINKREFERENCE:
369
+ break;
370
+ case LINK:
371
+ #ifdef DEBUG_ON
372
+ fprintf(stderr, "print html link: '%s'\n",n->str);
373
+ #endif
374
+ /* Stash a copy of the link data */
375
+ if (n->link_data != NULL)
376
+ temp_link_data = mk_link_data(n->link_data->label, n->link_data->source, n->link_data->title, n->link_data->attr);
377
+
378
+ /* Do we have proper info? */
379
+ if ((n->link_data->label == NULL) &&
380
+ (n->link_data->source == NULL)) {
381
+ /* we seem to be a [foo][] style link */
382
+ /* so load a label */
383
+ temp_str = g_string_new("");
384
+ print_raw_node_tree(temp_str, n->children);
385
+ n->link_data->label = temp_str->str;
386
+ g_string_free(temp_str, FALSE);
387
+ }
388
+ /* Load reference data */
389
+ if (n->link_data->label != NULL) {
390
+ temp = strdup(n->link_data->label);
391
+
392
+ #ifdef DEBUG_ON
393
+ fprintf(stderr, "print html link: '%s'\n",n->link_data->title);
394
+ fprintf(stderr, "print html link: '%s'\n",temp);
395
+ #endif
396
+ n->link_data->attr = NULL;
397
+ free_link_data(n->link_data);
398
+
399
+ n->link_data = extract_link_data(temp, scratch);
400
+ if (n->link_data == NULL) {
401
+ /* replace original text since no definition found */
402
+ g_string_append_printf(out, "[");
403
+ print_html_node(out, n->children, scratch);
404
+ g_string_append_printf(out,"]");
405
+ if (n->children->next != NULL) {
406
+ g_string_append_printf(out, "[");
407
+ print_html_node_tree(out, n->children->next, scratch);
408
+ g_string_append_printf(out,"]");
409
+ } else if (n->str != NULL) {
410
+ /* no title label, so see if we stashed str*/
411
+ g_string_append_printf(out, "%s", n->str);
412
+ } else {
413
+ g_string_append_printf(out, "[%s]",temp);
414
+ }
415
+
416
+ free(temp);
417
+
418
+ /* Restore stashed copy */
419
+ n->link_data = temp_link_data;
420
+
421
+ break;
422
+ }
423
+ free(temp);
424
+ }
425
+ g_string_append_printf(out, "<a");
426
+ if (n->link_data->source != NULL) {
427
+ g_string_append_printf(out, " href=\"");
428
+ if (strncmp(n->link_data->source,"mailto:", 6) == 0) {
429
+ scratch->obfuscate = 1; /* flag obfuscated */
430
+ }
431
+ print_html_string(out,n->link_data->source, scratch);
432
+ g_string_append_printf(out, "\"");
433
+ }
434
+ if ((n->link_data->title != NULL) && (strlen(n->link_data->title) > 0)) {
435
+ g_string_append_printf(out, " title=\"");
436
+ print_html_string(out, n->link_data->title, scratch);
437
+ g_string_append_printf(out, "\"");
438
+ }
439
+ print_html_node_tree(out, n->link_data->attr, scratch);
440
+ g_string_append_printf(out, ">");
441
+ if (n->children != NULL)
442
+ print_html_node_tree(out,n->children,scratch);
443
+ g_string_append_printf(out, "</a>");
444
+ scratch->obfuscate = 0;
445
+
446
+ /* Restore stashed copy */
447
+ n->link_data->attr = NULL;
448
+ free_link_data(n->link_data);
449
+ n->link_data = temp_link_data;
450
+
451
+ break;
452
+ case ATTRKEY:
453
+ if ( (strcmp(n->str,"height") == 0) || (strcmp(n->str, "width") == 0)) {
454
+ } else {
455
+ g_string_append_printf(out, " %s=\"%s\"", n->str,
456
+ n->children->str);
457
+ }
458
+ break;
459
+ case REFNAME:
460
+ case SOURCE:
461
+ case TITLE:
462
+ break;
463
+ case IMAGEBLOCK:
464
+ pad(out,2, scratch);
465
+ case IMAGE:
466
+ #ifdef DEBUG_ON
467
+ fprintf(stderr, "print image\n");
468
+ #endif
469
+ /* Verify all is well */
470
+ if (n->link_data == NULL) {
471
+ fprintf(stderr, "Invalid IMAGEBLOCK or IMAGE -- (null) link_data\n");
472
+ exit(EXIT_FAILURE);
473
+ }
474
+
475
+ /* Stash a copy of the link data */
476
+ if (n->link_data != NULL)
477
+ temp_link_data = mk_link_data(n->link_data->label, n->link_data->source, n->link_data->title, n->link_data->attr);
478
+
479
+ /* Do we have proper info? */
480
+ if ((n->link_data->label == NULL) &&
481
+ (n->link_data->source == NULL)) {
482
+ /* we seem to be a [foo][] style link */
483
+ /* so load a label */
484
+ temp_str = g_string_new("");
485
+ print_raw_node_tree(temp_str, n->children);
486
+ n->link_data->label = temp_str->str;
487
+ g_string_free(temp_str, FALSE);
488
+ }
489
+ #ifdef DEBUG_ON
490
+ fprintf(stderr, "load reference data\n");
491
+ #endif
492
+ /* Load reference data */
493
+ if (n->link_data->label != NULL) {
494
+ temp = strdup(n->link_data->label);
495
+
496
+ n->link_data->attr = NULL;
497
+ free_link_data(n->link_data);
498
+
499
+ n->link_data = extract_link_data(temp, scratch);
500
+
501
+ if (n->link_data == NULL) {
502
+ g_string_append_printf(out, "![");
503
+ print_html_node_tree(out, n->children, scratch);
504
+ g_string_append_printf(out,"][%s]",temp);
505
+
506
+ /* Restore stashed copy */
507
+ n->link_data = temp_link_data;
508
+
509
+ free(temp);
510
+
511
+ break;
512
+ } else {
513
+ if (n->key == IMAGEBLOCK)
514
+ g_string_append_printf(out, "<figure>\n");
515
+ }
516
+ free(temp);
517
+ } else {
518
+ if (n->key == IMAGEBLOCK)
519
+ g_string_append_printf(out, "<figure>\n");
520
+ }
521
+ #ifdef DEBUG_ON
522
+ fprintf(stderr, "create img\n");
523
+ #endif
524
+ g_string_append_printf(out, "<img");
525
+ if (n->link_data->source != NULL)
526
+ g_string_append_printf(out, " src=\"%s\"",n->link_data->source);
527
+ if (n->children != NULL) {
528
+ g_string_append_printf(out, " alt=\"");
529
+ temp_str = g_string_new("");
530
+ print_raw_node_tree(temp_str, n->children);
531
+ print_html_string(out, temp_str->str, scratch);
532
+ g_string_free(temp_str, true);
533
+ g_string_append_printf(out, "\"");
534
+ } else {
535
+ g_string_append_printf(out, " alt=\"%s\"",n->link_data->title);
536
+ }
537
+ if (!(scratch->extensions & EXT_COMPATIBILITY)) {
538
+ if (n->link_data->label != NULL) {
539
+ temp = label_from_string(n->link_data->label);
540
+ g_string_append_printf(out, " id=\"%s\"",temp);
541
+ free(temp);
542
+ }
543
+ }
544
+ if ((n->link_data->title != NULL) && (strlen(n->link_data->title) > 0)) {
545
+ g_string_append_printf(out, " title=\"");
546
+ print_html_string(out, n->link_data->title, scratch);
547
+ g_string_append_printf(out, "\"");
548
+ }
549
+ #ifdef DEBUG_ON
550
+ fprintf(stderr, "attributes\n");
551
+ #endif
552
+ if (n->link_data->attr != NULL) {
553
+ temp_node = node_for_attribute("height",n->link_data->attr);
554
+ if (temp_node != NULL)
555
+ height = strdup(temp_node->children->str);
556
+ temp_node = node_for_attribute("width",n->link_data->attr);
557
+ if (temp_node != NULL)
558
+ width = strdup(temp_node->children->str);
559
+ if ((height != NULL) || (width != NULL)) {
560
+ #ifdef DEBUG_ON
561
+ fprintf(stderr, "width/height\n");
562
+ #endif
563
+ g_string_append_printf(out, " style=\"");
564
+ if (height != NULL)
565
+ g_string_append_printf(out, "height:%s;", height);
566
+ if (width != NULL)
567
+ g_string_append_printf(out, "width:%s;", width);
568
+ g_string_append_printf(out, "\"");
569
+ }
570
+ #ifdef DEBUG_ON
571
+ fprintf(stderr, "other attributes\n");
572
+ #endif
573
+ print_html_node_tree(out, n->link_data->attr, scratch);
574
+ free(height);
575
+ free(width);
576
+ }
577
+ g_string_append_printf(out, " />");
578
+ if (n->key == IMAGEBLOCK) {
579
+ if (n->children != NULL) {
580
+ temp_str = g_string_new("");
581
+ print_html_node(temp_str,n->children,scratch);
582
+ if (temp_str->currentStringLength > 0) {
583
+ g_string_append_printf(out, "\n<figcaption>");
584
+ g_string_append(out, temp_str->str);
585
+ g_string_append_printf(out, "</figcaption>");
586
+ }
587
+ g_string_free(temp_str, true);
588
+ }
589
+ g_string_append_printf(out,"\n</figure>");
590
+ scratch->padded = 0;
591
+ }
592
+
593
+ /* Restore stashed copy */
594
+ n->link_data->attr = NULL;
595
+ free_link_data(n->link_data);
596
+ n->link_data = temp_link_data;
597
+
598
+ break;
599
+ #ifdef DEBUG_ON
600
+ fprintf(stderr, "finish image\n");
601
+ #endif
602
+ case NOTEREFERENCE:
603
+ lev = note_number_for_node(n, scratch);
604
+ temp_node = node_for_count(scratch->used_notes, lev);
605
+
606
+ if (scratch->extensions & EXT_RANDOM_FOOT) {
607
+ srand(scratch->random_seed_base + lev);
608
+ random = rand() % 99999 + 1;
609
+ } else {
610
+ random = lev;
611
+ }
612
+
613
+ if (temp_node->key == GLOSSARYSOURCE) {
614
+ if (lev > scratch->max_footnote_num) {
615
+ g_string_append_printf(out, "<a href=\"#fn:%d\" id=\"fnref:%d\" title=\"see footnote\" class=\"footnote glossary\">[%d]</a>",
616
+ random, random, lev);
617
+ scratch->max_footnote_num = lev;
618
+ } else {
619
+ g_string_append_printf(out, "<a href=\"#fn:%d\" title=\"see footnote\" class=\"footnote glossary\">[%d]</a>",
620
+ random, lev);
621
+ }
622
+ } else {
623
+ if (lev > scratch->max_footnote_num) {
624
+ g_string_append_printf(out, "<a href=\"#fn:%d\" id=\"fnref:%d\" title=\"see footnote\" class=\"footnote\">[%d]</a>",
625
+ random, random, lev);
626
+ scratch->max_footnote_num = lev;
627
+ } else {
628
+ g_string_append_printf(out, "<a href=\"#fn:%d\" title=\"see footnote\" class=\"footnote\">[%d]</a>",
629
+ random, lev);
630
+ }
631
+ }
632
+ break;
633
+ case NOCITATION:
634
+ case CITATION:
635
+ #ifdef DEBUG_ON
636
+ fprintf(stderr, "print cite\n");
637
+ #endif
638
+ if ((n->link_data != NULL) && (strncmp(n->link_data->label,"[#",2) == 0)) {
639
+ fprintf(stderr, "external cite\n");
640
+ /* external citation (e.g. BibTeX) */
641
+ if (n->key == NOCITATION) {
642
+ g_string_append_printf(out, "<span class=\"notcited\" id=\"%s\"/>",n->str);
643
+ } else {
644
+ g_string_append_printf(out, "<span class=\"externalcitation\">");
645
+ g_string_append_printf(out, "</span>");
646
+ }
647
+ } else {
648
+ #ifdef DEBUG_ON
649
+ fprintf(stderr, "internal cite\n");
650
+ #endif
651
+ /* MMD citation, so output as footnote */
652
+ /* TODO: create separate stream from footnotes */
653
+ lev = 0;
654
+ if (n->link_data != NULL)
655
+ lev = note_number_for_label(n->link_data->label, scratch);
656
+ if (lev != 0) {
657
+ #ifdef DEBUG_ON
658
+ fprintf(stderr, "matching cite found - %d\n",lev);
659
+ #endif
660
+ if (scratch->extensions & EXT_RANDOM_FOOT) {
661
+ srand(scratch->random_seed_base + lev);
662
+ random = rand() % 99999 + 1;
663
+ } else {
664
+ random = lev;
665
+ }
666
+
667
+ temp_node = node_for_count(scratch->used_notes, lev);
668
+ /* flag that this is used as a citation */
669
+ temp_node->key = CITATIONSOURCE;
670
+ if (lev > scratch->max_footnote_num) {
671
+ scratch->max_footnote_num = lev;
672
+ }
673
+ if (n->key == NOCITATION) {
674
+ g_string_append_printf(out, "<span class=\"notcited\" id=\"%d\">",random);
675
+ } else {
676
+ g_string_append_printf(out, "<a class=\"citation\" href=\"#fn:%d\" title=\"Jump to citation\">[",
677
+ random);
678
+ if (n->children != NULL) {
679
+ g_string_append_printf(out, "<span class=\"locator\">");
680
+ print_html_node(out, n->children, scratch);
681
+ g_string_append_printf(out, "</span>, %d]", lev);
682
+ } else {
683
+ g_string_append_printf(out, "%d]", lev);
684
+ }
685
+ }
686
+ g_string_append_printf(out, "<span class=\"citekey\" style=\"display:none\">%s</span>", n->link_data->label);
687
+ if (n->key == NOCITATION) {
688
+ g_string_append_printf(out, "</span>");
689
+ } else {
690
+ g_string_append_printf(out, "</a>");
691
+ }
692
+ } else {
693
+ /* not located -- this is external cite */
694
+ #ifdef DEBUG_ON
695
+ fprintf(stderr, "no match for cite: '%s'\n",n->link_data->label);
696
+ #endif
697
+ if ((n->link_data != NULL) && (n->key == NOCITATION)) {
698
+ g_string_append_printf(out, "<span class=\"notcited\" id=\"%s\"/>",n->link_data->label);
699
+ } else if (n->link_data != NULL) {
700
+ g_string_append_printf(out, "<span class=\"externalcitation\">[");
701
+ if (n->children != NULL) {
702
+ print_html_node(out, n->children, scratch);
703
+ g_string_append_printf(out, "][");
704
+ }
705
+ g_string_append_printf(out, "#%s]</span>",n->link_data->label);
706
+ }
707
+ }
708
+ }
709
+ #ifdef DEBUG_ON
710
+ fprintf(stderr, "finish cite\n");
711
+ #endif
712
+ break;
713
+ case VARIABLE:
714
+ temp = metavalue_for_key(n->str,scratch->result_tree);
715
+ if (temp == NULL) {
716
+ g_string_append_printf(out, "[%%%s]",n->str);
717
+ } else {
718
+ print_html_string(out, temp, scratch);
719
+ free(temp);
720
+ }
721
+ break;
722
+ case GLOSSARYTERM:
723
+ g_string_append_printf(out,"<span class=\"glossary name\">");
724
+ print_html_string(out, n->children->str, scratch);
725
+ g_string_append_printf(out, "</span>");
726
+ if ((n->next != NULL) && (n->next->key == GLOSSARYSORTKEY) ) {
727
+ g_string_append_printf(out, "<span class=\"glossary sort\" style=\"display:none\">");
728
+ print_html_string(out, n->next->str, scratch);
729
+ g_string_append_printf(out, "</span>");
730
+ }
731
+ g_string_append_printf(out, ": ");
732
+ break;
733
+ case GLOSSARYSORTKEY:
734
+ break;
735
+ case CODE:
736
+ g_string_append_printf(out, "<code>");
737
+ print_html_string(out, n->str, scratch);
738
+ g_string_append_printf(out, "</code>");
739
+ break;
740
+ case BLOCKQUOTEMARKER:
741
+ print_html_node_tree(out, n->children, scratch);
742
+ break;
743
+ case BLOCKQUOTE:
744
+ pad(out,2, scratch);
745
+ g_string_append_printf(out, "<blockquote>\n");
746
+ scratch->padded = 2;
747
+ print_html_node_tree(out, n->children, scratch);
748
+ pad(out,1, scratch);
749
+ g_string_append_printf(out, "</blockquote>");
750
+ scratch->padded = 0;
751
+ break;
752
+ case RAW:
753
+ g_string_append_printf(out, "RAW:");
754
+ g_string_append_printf(out,"%s",n->str);
755
+ break;
756
+ case HTML:
757
+ g_string_append_printf(out, "%s", n->str);
758
+ break;
759
+ case DEFLIST:
760
+ pad(out,2, scratch);
761
+ scratch->padded = 1;
762
+ g_string_append_printf(out, "<dl>\n");
763
+ print_html_node_tree(out, n->children, scratch);
764
+ g_string_append_printf(out, "</dl>");
765
+ scratch->padded = 0;
766
+ break;
767
+ case TERM:
768
+ pad(out,1, scratch);
769
+ g_string_append_printf(out, "<dt>");
770
+ print_html_node_tree(out, n->children, scratch);
771
+ g_string_append_printf(out, "</dt>\n");
772
+ scratch->padded = 1;
773
+ break;
774
+ case DEFINITION:
775
+ pad(out,1, scratch);
776
+ scratch->padded = 1;
777
+ g_string_append_printf(out, "<dd>");
778
+ print_html_node_tree(out, n->children, scratch);
779
+ g_string_append_printf(out, "</dd>\n");
780
+ scratch->padded = 0;
781
+ break;
782
+ case TABLE:
783
+ pad(out,2, scratch);
784
+ g_string_append_printf(out, "<table>\n");
785
+ print_html_node_tree(out, n->children, scratch);
786
+ g_string_append_printf(out, "</table>\n");
787
+ scratch->cell_type = 0;
788
+ scratch->padded = 1;
789
+ scratch->table_alignment = NULL;
790
+ scratch->header_column = 0;
791
+ break;
792
+ case TABLESEPARATOR:
793
+ scratch->table_alignment = n->str;
794
+ if (scratch->cell_type == 0)
795
+ print_col_group(out, scratch);
796
+ scratch->cell_type = 'd';
797
+ break;
798
+ case TABLECAPTION:
799
+ if ((n->children != NULL) && (n->children->key == TABLELABEL)) {
800
+ temp = label_from_string(n->children->str);
801
+ } else {
802
+ temp = label_from_node_tree(n->children);
803
+ }
804
+ g_string_append_printf(out, "<caption id=\"%s\">", temp);
805
+ print_html_node_tree(out, n->children, scratch);
806
+ g_string_append_printf(out, "</caption>\n");
807
+ free(temp);
808
+ break;
809
+ case TABLELABEL:
810
+ break;
811
+ case TABLEHEAD:
812
+ /* print column alignment for XSLT processing if needed */
813
+ if (scratch->cell_type == 0)
814
+ print_col_group(out, scratch);
815
+ scratch->cell_type = 'h';
816
+ g_string_append_printf(out, "\n<thead>\n");
817
+ print_html_node_tree(out, n->children, scratch);
818
+ g_string_append_printf(out, "</thead>\n");
819
+ scratch->cell_type = 'd';
820
+ break;
821
+ case TABLEBODY:
822
+ g_string_append_printf(out, "\n<tbody>\n");
823
+ print_html_node_tree(out, n->children, scratch);
824
+ g_string_append_printf(out, "</tbody>\n");
825
+ break;
826
+ case TABLEROW:
827
+ g_string_append_printf(out, "<tr>\n");
828
+ scratch->table_column = 0;
829
+ print_html_node_tree(out, n->children, scratch);
830
+ g_string_append_printf(out, "</tr>\n");
831
+ break;
832
+ case TABLECELL:
833
+ temp = scratch->table_alignment;
834
+ if (strncmp(&temp[scratch->table_column],"h",1) == 0) {
835
+ temp_type = 'h';
836
+ scratch->table_column++;
837
+ } else {
838
+ temp_type = scratch->cell_type;
839
+ }
840
+
841
+ if (scratch->header_column && (scratch->table_column == 0))
842
+ temp_type = 'h';
843
+
844
+ lev = scratch->table_column;
845
+ if ( strncmp(&temp[lev],"r",1) == 0) {
846
+ g_string_append_printf(out, "\t<t%c style=\"text-align:right;\"", temp_type);
847
+ } else if ( strncmp(&temp[lev],"R",1) == 0) {
848
+ g_string_append_printf(out, "\t<t%c style=\"text-align:right;\"", temp_type);
849
+ } else if ( strncmp(&temp[lev],"c",1) == 0) {
850
+ g_string_append_printf(out, "\t<t%c style=\"text-align:center;\"", temp_type);
851
+ } else if ( strncmp(&temp[lev],"C",1) == 0) {
852
+ g_string_append_printf(out, "\t<t%c style=\"text-align:center;\"", temp_type);
853
+ } else if ( strncmp(&temp[lev],"N",1) == 0) {
854
+ g_string_append_printf(out, "\t<t%c", temp_type);
855
+ } else if ( strncmp(&temp[lev],"n",1) == 0) {
856
+ g_string_append_printf(out, "\t<t%c", temp_type);
857
+ } else {
858
+ g_string_append_printf(out, "\t<t%c style=\"text-align:left;\"", temp_type);
859
+ }
860
+ if ((n->children != NULL) && (n->children->key == CELLSPAN)) {
861
+ g_string_append_printf(out, " colspan=\"%d\"",(int)strlen(n->children->str)+1);
862
+ scratch->table_column += (int)strlen(n->children->str);
863
+ }
864
+ g_string_append_printf(out, ">");
865
+ scratch->padded = 2;
866
+ temp_size = out->currentStringLength;
867
+ print_html_node_tree(out, n->children, scratch);
868
+
869
+ /* We have an empty leading cell, so first column is shown as headers */
870
+ /* Disabled for now while I decide whether this should be reimplemented -- was in v2 */
871
+ /* if (scratch->table_column == 0) {
872
+ if (temp_size == out->currentStringLength) {
873
+ scratch->header_column = 1;
874
+ }
875
+ }
876
+ */
877
+
878
+ g_string_append_printf(out, "</t%c>\n", temp_type);
879
+ scratch->table_column++;
880
+ break;
881
+ case CELLSPAN:
882
+ break;
883
+ case GLOSSARYSOURCE:
884
+ if (scratch->printing_notes)
885
+ print_html_node_tree(out, n->children, scratch);
886
+ break;
887
+ case CITATIONSOURCE:
888
+ case NOTESOURCE:
889
+ if (scratch->printing_notes)
890
+ print_html_node_tree(out, n->children, scratch);
891
+ break;
892
+ case SOURCEBRANCH:
893
+ fprintf(stderr,"SOURCEBRANCH\n");
894
+ break;
895
+ case NOTELABEL:
896
+ case GLOSSARYLABEL:
897
+ break;
898
+ case SUPERSCRIPT:
899
+ g_string_append_printf(out, "<sup>%s</sup>",n->str);
900
+ break;
901
+ case SUBSCRIPT:
902
+ g_string_append_printf(out, "<sub>%s</sub>",n->str);
903
+ break;
904
+ case ABBREVIATION:
905
+ case KEY_COUNTER:
906
+ break;
907
+ case TOC:
908
+ g_string_append_printf(out, "<div class=\"TOC\">\n");
909
+ print_html_node_tree(out,n->children, scratch);
910
+ g_string_append_printf(out, "\n</div>");
911
+ break;
912
+ default:
913
+ fprintf(stderr, "print_html_node encountered unknown node key = %d\n",n->key);
914
+ exit(EXIT_FAILURE);
915
+ }
916
+ }
917
+
918
+ /* print_html_endnotes */
919
+ void print_html_endnotes(GString *out, scratch_pad *scratch) {
920
+ int counter = 0;
921
+ int random;
922
+ node *reversed = copy_node_tree(scratch->used_notes);
923
+
924
+ reversed = reverse_list(reversed);
925
+
926
+ scratch->printing_notes = 1;
927
+
928
+ node *note = reversed;
929
+ #ifdef DEBUG_ON
930
+ fprintf(stderr, "start endnotes\n");
931
+ #endif
932
+
933
+ if ((note == NULL) || ((note->key == KEY_COUNTER) && (note->next == NULL))) {
934
+ free_node_tree(reversed);
935
+ return;
936
+ }
937
+
938
+ #ifdef DEBUG_ON
939
+ fprintf(stderr, "there are endnotes to print\n");
940
+ #endif
941
+
942
+ pad(out,2, scratch);
943
+ g_string_append_printf(out, "<div class=\"footnotes\">\n<hr />\n<ol>");
944
+ while ( note != NULL) {
945
+ if (note->key == KEY_COUNTER) {
946
+ note = note->next;
947
+ continue;
948
+ }
949
+
950
+ counter++;
951
+ pad(out, 1, scratch);
952
+
953
+ if (scratch->extensions & EXT_RANDOM_FOOT) {
954
+ srand(scratch->random_seed_base + counter);
955
+ random = rand() % 99999 + 1;
956
+ } else {
957
+ random = counter;
958
+ }
959
+
960
+ if (note->key == CITATIONSOURCE) {
961
+ g_string_append_printf(out, "<li id=\"fn:%d\" class=\"citation\"><span class=\"citekey\" style=\"display:none\">%s</span>",
962
+ random, note->str);
963
+ } else {
964
+ g_string_append_printf(out, "<li id=\"fn:%d\">\n", random);
965
+ }
966
+
967
+
968
+ scratch->padded = 2;
969
+ if ((note->key == NOTESOURCE) || (note->key == GLOSSARYSOURCE))
970
+ scratch->footnote_to_print = counter;
971
+ scratch->footnote_para_counter = tree_contains_key_count(note->children,PARA);
972
+ print_html_node(out, note, scratch);
973
+ pad(out, 1, scratch);
974
+ g_string_append_printf(out, "</li>");
975
+
976
+ note = note->next;
977
+ }
978
+ pad(out,1, scratch);
979
+ g_string_append_printf(out, "</ol>\n</div>\n");
980
+ scratch->padded = 0;
981
+
982
+ free_node_tree(reversed);
983
+ #ifdef DEBUG_ON
984
+ fprintf(stderr, "finish endnotes\n");
985
+ #endif
986
+ }
987
+
988
+ /* Check metadata keys and determine if we need a complete document */
989
+ /* We also preconvert metadata keys to proper formatting -- lowercase with no spaces */
990
+ bool is_html_complete_doc(node *meta) {
991
+ node *step;
992
+ char *temp;
993
+ step = meta->children;
994
+
995
+ while (step != NULL) {
996
+ /* process key to proper label */
997
+ temp = step->str; /* store pointer to original str */
998
+ step->str = label_from_string(step->str);
999
+ free(temp); /* free original since we don't need it */
1000
+ step = step->next;
1001
+ }
1002
+
1003
+ step = meta->children;
1004
+ while (step != NULL) {
1005
+ /* the following types of metadata do not require a complete document */
1006
+ if ((strcmp(step->str, "baseheaderlevel") != 0) &&
1007
+ (strcmp(step->str, "xhtmlheaderlevel") != 0) &&
1008
+ (strcmp(step->str, "htmlheaderlevel") != 0) &&
1009
+ (strcmp(step->str, "latexheaderlevel") != 0) &&
1010
+ (strcmp(step->str, "odfheaderlevel") != 0) &&
1011
+ (strcmp(step->str, "quoteslanguage") != 0))
1012
+ { return TRUE;}
1013
+ step = step->next;
1014
+ }
1015
+
1016
+ return FALSE;
1017
+ }
1018
+
1019
+ /* print_html_localized_typography -- convert to "smart" typography */
1020
+ void print_html_localized_typography(GString *out, int character, scratch_pad *scratch) {
1021
+ if (!extension(EXT_SMART, scratch->extensions)) {
1022
+ g_string_append_c(out, character);
1023
+ return;
1024
+ }
1025
+ switch (character) {
1026
+ case LSQUOTE:
1027
+ switch (scratch->language) {
1028
+ case SWEDISH:
1029
+ g_string_append_printf(out, "&#8217;");
1030
+ break;
1031
+ case FRENCH:
1032
+ g_string_append_printf(out,"&#39;");
1033
+ break;
1034
+ case GERMAN:
1035
+ g_string_append_printf(out,"&#8218;");
1036
+ break;
1037
+ case GERMANGUILL:
1038
+ g_string_append_printf(out,"&#8250;");
1039
+ break;
1040
+ default:
1041
+ g_string_append_printf(out,"&#8216;");
1042
+ }
1043
+ break;
1044
+ case RSQUOTE:
1045
+ switch (scratch->language) {
1046
+ case GERMAN:
1047
+ g_string_append_printf(out,"&#8216;");
1048
+ break;
1049
+ case GERMANGUILL:
1050
+ g_string_append_printf(out,"&#8249;");
1051
+ break;
1052
+ default:
1053
+ g_string_append_printf(out,"&#8217;");
1054
+ }
1055
+ break;
1056
+ case APOS:
1057
+ g_string_append_printf(out,"&#8217;");
1058
+ break;
1059
+ case LDQUOTE:
1060
+ switch (scratch->language) {
1061
+ case DUTCH:
1062
+ case GERMAN:
1063
+ g_string_append_printf(out,"&#8222;");
1064
+ break;
1065
+ case GERMANGUILL:
1066
+ g_string_append_printf(out,"&#187;");
1067
+ break;
1068
+ case FRENCH:
1069
+ g_string_append_printf(out,"&#171;");
1070
+ break;
1071
+ case SWEDISH:
1072
+ g_string_append_printf(out, "&#8221;");
1073
+ break;
1074
+ default:
1075
+ g_string_append_printf(out,"&#8220;");
1076
+ }
1077
+ break;
1078
+ case RDQUOTE:
1079
+ switch (scratch->language) {
1080
+ case SWEDISH:
1081
+ case DUTCH:
1082
+ g_string_append_printf(out,"&#8221;");
1083
+ break;
1084
+ case GERMAN:
1085
+ g_string_append_printf(out,"&#8220;");
1086
+ break;
1087
+ case GERMANGUILL:
1088
+ g_string_append_printf(out,"&#171;");
1089
+ break;
1090
+ case FRENCH:
1091
+ g_string_append_printf(out,"&#187;");
1092
+ break;
1093
+ default:
1094
+ g_string_append_printf(out,"&#8221;");
1095
+ }
1096
+ break;
1097
+ case NDASH:
1098
+ g_string_append_printf(out,"&#8211;");
1099
+ break;
1100
+ case MDASH:
1101
+ g_string_append_printf(out,"&#8212;");
1102
+ break;
1103
+ case ELLIP:
1104
+ g_string_append_printf(out,"&#8230;");
1105
+ break;
1106
+ default:;
1107
+ }
1108
+ }
1109
+
1110
+ /* print_html_string - print string, escaping for HTML */
1111
+ long ran_num_next(); /* Use Knuth's pseudo random generator */
1112
+
1113
+ void print_html_string(GString *out, char *str, scratch_pad *scratch) {
1114
+ if (str == NULL)
1115
+ return;
1116
+ while (*str != '\0') {
1117
+ switch (*str) {
1118
+ case '&':
1119
+ g_string_append_printf(out, "&amp;");
1120
+ break;
1121
+ case '<':
1122
+ g_string_append_printf(out, "&lt;");
1123
+ break;
1124
+ case '>':
1125
+ g_string_append_printf(out, "&gt;");
1126
+ break;
1127
+ case '"':
1128
+ g_string_append_printf(out, "&quot;");
1129
+ break;
1130
+ default:
1131
+ if ((scratch->obfuscate == true) && (extension(EXT_OBFUSCATE, scratch->extensions))
1132
+ && ((int) *str == (((int) *str) & 127))) {
1133
+ if (ran_num_next() % 2 == 0)
1134
+ g_string_append_printf(out, "&#%d;", (int) *str);
1135
+ else
1136
+ g_string_append_printf(out, "&#x%x;", (unsigned int) *str);
1137
+ } else {
1138
+ g_string_append_c(out, *str);
1139
+ }
1140
+ }
1141
+ str++;
1142
+ }
1143
+ }
1144
+
1145
+ /* print_col_group - print column alignment config (used in XSLT processing) */
1146
+ void print_col_group(GString *out,scratch_pad *scratch) {
1147
+ char *temp;
1148
+ int lev;
1149
+ g_string_append_printf(out, "<colgroup>\n");
1150
+ temp = scratch->table_alignment;
1151
+ for (lev=0;lev<strlen(temp);lev++) {
1152
+ if ( strncmp(&temp[lev],"r",1) == 0) {
1153
+ g_string_append_printf(out, "<col style=\"text-align:right;\"/>\n");
1154
+ } else if ( strncmp(&temp[lev],"R",1) == 0) {
1155
+ g_string_append_printf(out, "<col style=\"text-align:right;\" class=\"extended\"/>\n");
1156
+ } else if ( strncmp(&temp[lev],"c",1) == 0) {
1157
+ g_string_append_printf(out, "<col style=\"text-align:center;\"/>\n");
1158
+ } else if ( strncmp(&temp[lev],"C",1) == 0) {
1159
+ g_string_append_printf(out, "<col style=\"text-align:center;\" class=\"extended\"/>\n");
1160
+ } else if ( strncmp(&temp[lev],"L",1) == 0) {
1161
+ g_string_append_printf(out, "<col style=\"text-align:left;\" class=\"extended\"/>\n");
1162
+ } else if ( strncmp(&temp[lev],"N",1) == 0) {
1163
+ g_string_append_printf(out, "<col class=\"extended\"/>\n");
1164
+ } else if ( strncmp(&temp[lev],"n",1) == 0) {
1165
+ g_string_append_printf(out, "<col/>\n");
1166
+ } else {
1167
+ g_string_append_printf(out, "<col style=\"text-align:left;\"/>\n");
1168
+ }
1169
+ }
1170
+ g_string_append_printf(out, "</colgroup>\n");
1171
+ }