qiita_marker 0.23.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +113 -0
- data/bin/qiita_marker +123 -0
- data/ext/qiita_marker/arena.c +103 -0
- data/ext/qiita_marker/autolink.c +425 -0
- data/ext/qiita_marker/autolink.h +8 -0
- data/ext/qiita_marker/blocks.c +1596 -0
- data/ext/qiita_marker/buffer.c +278 -0
- data/ext/qiita_marker/buffer.h +116 -0
- data/ext/qiita_marker/case_fold_switch.inc +4327 -0
- data/ext/qiita_marker/chunk.h +135 -0
- data/ext/qiita_marker/cmark-gfm-core-extensions.h +54 -0
- data/ext/qiita_marker/cmark-gfm-extension_api.h +736 -0
- data/ext/qiita_marker/cmark-gfm-extensions_export.h +42 -0
- data/ext/qiita_marker/cmark-gfm.h +817 -0
- data/ext/qiita_marker/cmark-gfm_export.h +42 -0
- data/ext/qiita_marker/cmark-gfm_version.h +7 -0
- data/ext/qiita_marker/cmark.c +55 -0
- data/ext/qiita_marker/cmark_ctype.c +44 -0
- data/ext/qiita_marker/cmark_ctype.h +33 -0
- data/ext/qiita_marker/commonmark.c +529 -0
- data/ext/qiita_marker/config.h +76 -0
- data/ext/qiita_marker/core-extensions.c +27 -0
- data/ext/qiita_marker/entities.inc +2138 -0
- data/ext/qiita_marker/ext_scanners.c +879 -0
- data/ext/qiita_marker/ext_scanners.h +24 -0
- data/ext/qiita_marker/extconf.rb +7 -0
- data/ext/qiita_marker/footnotes.c +63 -0
- data/ext/qiita_marker/footnotes.h +27 -0
- data/ext/qiita_marker/houdini.h +57 -0
- data/ext/qiita_marker/houdini_href_e.c +100 -0
- data/ext/qiita_marker/houdini_html_e.c +66 -0
- data/ext/qiita_marker/houdini_html_u.c +149 -0
- data/ext/qiita_marker/html.c +486 -0
- data/ext/qiita_marker/html.h +27 -0
- data/ext/qiita_marker/inlines.c +1691 -0
- data/ext/qiita_marker/inlines.h +29 -0
- data/ext/qiita_marker/iterator.c +159 -0
- data/ext/qiita_marker/iterator.h +26 -0
- data/ext/qiita_marker/latex.c +466 -0
- data/ext/qiita_marker/linked_list.c +37 -0
- data/ext/qiita_marker/man.c +278 -0
- data/ext/qiita_marker/map.c +122 -0
- data/ext/qiita_marker/map.h +41 -0
- data/ext/qiita_marker/node.c +979 -0
- data/ext/qiita_marker/node.h +125 -0
- data/ext/qiita_marker/parser.h +58 -0
- data/ext/qiita_marker/plaintext.c +235 -0
- data/ext/qiita_marker/plugin.c +36 -0
- data/ext/qiita_marker/plugin.h +34 -0
- data/ext/qiita_marker/qiita_marker.c +1321 -0
- data/ext/qiita_marker/qiita_marker.h +16 -0
- data/ext/qiita_marker/references.c +42 -0
- data/ext/qiita_marker/references.h +26 -0
- data/ext/qiita_marker/registry.c +63 -0
- data/ext/qiita_marker/registry.h +24 -0
- data/ext/qiita_marker/render.c +205 -0
- data/ext/qiita_marker/render.h +62 -0
- data/ext/qiita_marker/scanners.c +10520 -0
- data/ext/qiita_marker/scanners.h +62 -0
- data/ext/qiita_marker/scanners.re +341 -0
- data/ext/qiita_marker/strikethrough.c +167 -0
- data/ext/qiita_marker/strikethrough.h +9 -0
- data/ext/qiita_marker/syntax_extension.c +149 -0
- data/ext/qiita_marker/syntax_extension.h +34 -0
- data/ext/qiita_marker/table.c +822 -0
- data/ext/qiita_marker/table.h +12 -0
- data/ext/qiita_marker/tagfilter.c +60 -0
- data/ext/qiita_marker/tagfilter.h +8 -0
- data/ext/qiita_marker/tasklist.c +156 -0
- data/ext/qiita_marker/tasklist.h +8 -0
- data/ext/qiita_marker/utf8.c +317 -0
- data/ext/qiita_marker/utf8.h +35 -0
- data/ext/qiita_marker/xml.c +181 -0
- data/lib/qiita_marker/config.rb +52 -0
- data/lib/qiita_marker/node/inspect.rb +57 -0
- data/lib/qiita_marker/node.rb +83 -0
- data/lib/qiita_marker/renderer/html_renderer.rb +252 -0
- data/lib/qiita_marker/renderer.rb +135 -0
- data/lib/qiita_marker/version.rb +5 -0
- data/lib/qiita_marker.rb +45 -0
- data/qiita_marker.gemspec +40 -0
- data/test/benchmark.rb +32 -0
- data/test/fixtures/curly.md +1 -0
- data/test/fixtures/dingus.md +10 -0
- data/test/fixtures/strong.md +1 -0
- data/test/fixtures/table.md +10 -0
- data/test/test_attributes.rb +24 -0
- data/test/test_basics.rb +35 -0
- data/test/test_commands.rb +72 -0
- data/test/test_commonmark.rb +36 -0
- data/test/test_doc.rb +130 -0
- data/test/test_encoding.rb +23 -0
- data/test/test_extensions.rb +116 -0
- data/test/test_footnotes.rb +60 -0
- data/test/test_gc.rb +47 -0
- data/test/test_helper.rb +71 -0
- data/test/test_linebreaks.rb +15 -0
- data/test/test_maliciousness.rb +262 -0
- data/test/test_node.rb +89 -0
- data/test/test_options.rb +37 -0
- data/test/test_pathological_inputs.rb +94 -0
- data/test/test_plaintext.rb +46 -0
- data/test/test_renderer.rb +47 -0
- data/test/test_smartpunct.rb +27 -0
- data/test/test_spec.rb +30 -0
- data/test/test_tasklists.rb +43 -0
- data/test/test_xml.rb +107 -0
- metadata +313 -0
@@ -0,0 +1,181 @@
|
|
1
|
+
#include <stdlib.h>
|
2
|
+
#include <stdio.h>
|
3
|
+
#include <string.h>
|
4
|
+
#include <assert.h>
|
5
|
+
|
6
|
+
#include "config.h"
|
7
|
+
#include "cmark-gfm.h"
|
8
|
+
#include "node.h"
|
9
|
+
#include "buffer.h"
|
10
|
+
#include "houdini.h"
|
11
|
+
#include "syntax_extension.h"
|
12
|
+
|
13
|
+
#define BUFFER_SIZE 100
|
14
|
+
|
15
|
+
// Functions to convert cmark_nodes to XML strings.
|
16
|
+
|
17
|
+
static void escape_xml(cmark_strbuf *dest, const unsigned char *source,
|
18
|
+
bufsize_t length) {
|
19
|
+
houdini_escape_html0(dest, source, length, 0);
|
20
|
+
}
|
21
|
+
|
22
|
+
struct render_state {
|
23
|
+
cmark_strbuf *xml;
|
24
|
+
int indent;
|
25
|
+
};
|
26
|
+
|
27
|
+
static CMARK_INLINE void indent(struct render_state *state) {
|
28
|
+
int i;
|
29
|
+
for (i = 0; i < state->indent; i++) {
|
30
|
+
cmark_strbuf_putc(state->xml, ' ');
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
static int S_render_node(cmark_node *node, cmark_event_type ev_type,
|
35
|
+
struct render_state *state, int options) {
|
36
|
+
cmark_strbuf *xml = state->xml;
|
37
|
+
bool literal = false;
|
38
|
+
cmark_delim_type delim;
|
39
|
+
bool entering = (ev_type == CMARK_EVENT_ENTER);
|
40
|
+
char buffer[BUFFER_SIZE];
|
41
|
+
|
42
|
+
if (entering) {
|
43
|
+
indent(state);
|
44
|
+
cmark_strbuf_putc(xml, '<');
|
45
|
+
cmark_strbuf_puts(xml, cmark_node_get_type_string(node));
|
46
|
+
|
47
|
+
if (options & CMARK_OPT_SOURCEPOS && node->start_line != 0) {
|
48
|
+
snprintf(buffer, BUFFER_SIZE, " sourcepos=\"%d:%d-%d:%d\"",
|
49
|
+
node->start_line, node->start_column, node->end_line,
|
50
|
+
node->end_column);
|
51
|
+
cmark_strbuf_puts(xml, buffer);
|
52
|
+
}
|
53
|
+
|
54
|
+
if (node->extension && node->extension->xml_attr_func) {
|
55
|
+
const char* r = node->extension->xml_attr_func(node->extension, node);
|
56
|
+
if (r != NULL)
|
57
|
+
cmark_strbuf_puts(xml, r);
|
58
|
+
}
|
59
|
+
|
60
|
+
literal = false;
|
61
|
+
|
62
|
+
switch (node->type) {
|
63
|
+
case CMARK_NODE_DOCUMENT:
|
64
|
+
cmark_strbuf_puts(xml, " xmlns=\"http://commonmark.org/xml/1.0\"");
|
65
|
+
break;
|
66
|
+
case CMARK_NODE_TEXT:
|
67
|
+
case CMARK_NODE_CODE:
|
68
|
+
case CMARK_NODE_HTML_BLOCK:
|
69
|
+
case CMARK_NODE_HTML_INLINE:
|
70
|
+
cmark_strbuf_puts(xml, " xml:space=\"preserve\">");
|
71
|
+
escape_xml(xml, node->as.literal.data, node->as.literal.len);
|
72
|
+
cmark_strbuf_puts(xml, "</");
|
73
|
+
cmark_strbuf_puts(xml, cmark_node_get_type_string(node));
|
74
|
+
literal = true;
|
75
|
+
break;
|
76
|
+
case CMARK_NODE_LIST:
|
77
|
+
switch (cmark_node_get_list_type(node)) {
|
78
|
+
case CMARK_ORDERED_LIST:
|
79
|
+
cmark_strbuf_puts(xml, " type=\"ordered\"");
|
80
|
+
snprintf(buffer, BUFFER_SIZE, " start=\"%d\"",
|
81
|
+
cmark_node_get_list_start(node));
|
82
|
+
cmark_strbuf_puts(xml, buffer);
|
83
|
+
delim = cmark_node_get_list_delim(node);
|
84
|
+
if (delim == CMARK_PAREN_DELIM) {
|
85
|
+
cmark_strbuf_puts(xml, " delim=\"paren\"");
|
86
|
+
} else if (delim == CMARK_PERIOD_DELIM) {
|
87
|
+
cmark_strbuf_puts(xml, " delim=\"period\"");
|
88
|
+
}
|
89
|
+
break;
|
90
|
+
case CMARK_BULLET_LIST:
|
91
|
+
cmark_strbuf_puts(xml, " type=\"bullet\"");
|
92
|
+
break;
|
93
|
+
default:
|
94
|
+
break;
|
95
|
+
}
|
96
|
+
snprintf(buffer, BUFFER_SIZE, " tight=\"%s\"",
|
97
|
+
(cmark_node_get_list_tight(node) ? "true" : "false"));
|
98
|
+
cmark_strbuf_puts(xml, buffer);
|
99
|
+
break;
|
100
|
+
case CMARK_NODE_HEADING:
|
101
|
+
snprintf(buffer, BUFFER_SIZE, " level=\"%d\"", node->as.heading.level);
|
102
|
+
cmark_strbuf_puts(xml, buffer);
|
103
|
+
break;
|
104
|
+
case CMARK_NODE_CODE_BLOCK:
|
105
|
+
if (node->as.code.info.len > 0) {
|
106
|
+
cmark_strbuf_puts(xml, " info=\"");
|
107
|
+
escape_xml(xml, node->as.code.info.data, node->as.code.info.len);
|
108
|
+
cmark_strbuf_putc(xml, '"');
|
109
|
+
}
|
110
|
+
cmark_strbuf_puts(xml, " xml:space=\"preserve\">");
|
111
|
+
escape_xml(xml, node->as.code.literal.data, node->as.code.literal.len);
|
112
|
+
cmark_strbuf_puts(xml, "</");
|
113
|
+
cmark_strbuf_puts(xml, cmark_node_get_type_string(node));
|
114
|
+
literal = true;
|
115
|
+
break;
|
116
|
+
case CMARK_NODE_CUSTOM_BLOCK:
|
117
|
+
case CMARK_NODE_CUSTOM_INLINE:
|
118
|
+
cmark_strbuf_puts(xml, " on_enter=\"");
|
119
|
+
escape_xml(xml, node->as.custom.on_enter.data,
|
120
|
+
node->as.custom.on_enter.len);
|
121
|
+
cmark_strbuf_putc(xml, '"');
|
122
|
+
cmark_strbuf_puts(xml, " on_exit=\"");
|
123
|
+
escape_xml(xml, node->as.custom.on_exit.data,
|
124
|
+
node->as.custom.on_exit.len);
|
125
|
+
cmark_strbuf_putc(xml, '"');
|
126
|
+
break;
|
127
|
+
case CMARK_NODE_LINK:
|
128
|
+
case CMARK_NODE_IMAGE:
|
129
|
+
cmark_strbuf_puts(xml, " destination=\"");
|
130
|
+
escape_xml(xml, node->as.link.url.data, node->as.link.url.len);
|
131
|
+
cmark_strbuf_putc(xml, '"');
|
132
|
+
cmark_strbuf_puts(xml, " title=\"");
|
133
|
+
escape_xml(xml, node->as.link.title.data, node->as.link.title.len);
|
134
|
+
cmark_strbuf_putc(xml, '"');
|
135
|
+
break;
|
136
|
+
default:
|
137
|
+
break;
|
138
|
+
}
|
139
|
+
if (node->first_child) {
|
140
|
+
state->indent += 2;
|
141
|
+
} else if (!literal) {
|
142
|
+
cmark_strbuf_puts(xml, " /");
|
143
|
+
}
|
144
|
+
cmark_strbuf_puts(xml, ">\n");
|
145
|
+
|
146
|
+
} else if (node->first_child) {
|
147
|
+
state->indent -= 2;
|
148
|
+
indent(state);
|
149
|
+
cmark_strbuf_puts(xml, "</");
|
150
|
+
cmark_strbuf_puts(xml, cmark_node_get_type_string(node));
|
151
|
+
cmark_strbuf_puts(xml, ">\n");
|
152
|
+
}
|
153
|
+
|
154
|
+
return 1;
|
155
|
+
}
|
156
|
+
|
157
|
+
char *cmark_render_xml(cmark_node *root, int options) {
|
158
|
+
return cmark_render_xml_with_mem(root, options, cmark_node_mem(root));
|
159
|
+
}
|
160
|
+
|
161
|
+
char *cmark_render_xml_with_mem(cmark_node *root, int options, cmark_mem *mem) {
|
162
|
+
char *result;
|
163
|
+
cmark_strbuf xml = CMARK_BUF_INIT(mem);
|
164
|
+
cmark_event_type ev_type;
|
165
|
+
cmark_node *cur;
|
166
|
+
struct render_state state = {&xml, 0};
|
167
|
+
|
168
|
+
cmark_iter *iter = cmark_iter_new(root);
|
169
|
+
|
170
|
+
cmark_strbuf_puts(state.xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
171
|
+
cmark_strbuf_puts(state.xml,
|
172
|
+
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n");
|
173
|
+
while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
|
174
|
+
cur = cmark_iter_get_node(iter);
|
175
|
+
S_render_node(cur, ev_type, &state, options);
|
176
|
+
}
|
177
|
+
result = (char *)cmark_strbuf_detach(&xml);
|
178
|
+
|
179
|
+
cmark_iter_free(iter);
|
180
|
+
return result;
|
181
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module QiitaMarker
|
4
|
+
# For Ruby::Enum, these must be classes, not modules
|
5
|
+
module Config
|
6
|
+
# See https://github.com/github/cmark-gfm/blob/master/src/cmark-gfm.h#L673
|
7
|
+
OPTS = {
|
8
|
+
parse: {
|
9
|
+
DEFAULT: 0,
|
10
|
+
SOURCEPOS: (1 << 1),
|
11
|
+
UNSAFE: (1 << 17),
|
12
|
+
VALIDATE_UTF8: (1 << 9),
|
13
|
+
SMART: (1 << 10),
|
14
|
+
LIBERAL_HTML_TAG: (1 << 12),
|
15
|
+
FOOTNOTES: (1 << 13),
|
16
|
+
STRIKETHROUGH_DOUBLE_TILDE: (1 << 14)
|
17
|
+
}.freeze,
|
18
|
+
render: {
|
19
|
+
DEFAULT: 0,
|
20
|
+
SOURCEPOS: (1 << 1),
|
21
|
+
HARDBREAKS: (1 << 2),
|
22
|
+
UNSAFE: (1 << 17),
|
23
|
+
NOBREAKS: (1 << 4),
|
24
|
+
VALIDATE_UTF8: (1 << 9),
|
25
|
+
SMART: (1 << 10),
|
26
|
+
GITHUB_PRE_LANG: (1 << 11),
|
27
|
+
LIBERAL_HTML_TAG: (1 << 12),
|
28
|
+
FOOTNOTES: (1 << 13),
|
29
|
+
STRIKETHROUGH_DOUBLE_TILDE: (1 << 14),
|
30
|
+
TABLE_PREFER_STYLE_ATTRIBUTES: (1 << 15),
|
31
|
+
FULL_INFO_STRING: (1 << 16)
|
32
|
+
}.freeze,
|
33
|
+
format: %i[html xml commonmark plaintext].freeze
|
34
|
+
}.freeze
|
35
|
+
|
36
|
+
def self.process_options(option, type)
|
37
|
+
case option
|
38
|
+
when Symbol
|
39
|
+
OPTS.fetch(type).fetch(option)
|
40
|
+
when Array
|
41
|
+
raise TypeError if option.none?
|
42
|
+
|
43
|
+
# neckbearding around. the map will both check the opts and then bitwise-OR it
|
44
|
+
OPTS.fetch(type).fetch_values(*option).inject(0, :|)
|
45
|
+
else
|
46
|
+
raise TypeError, "option type must be a valid symbol or array of symbols within the #{name}::OPTS[:#{type}] context"
|
47
|
+
end
|
48
|
+
rescue KeyError => e
|
49
|
+
raise TypeError, "option ':#{e.key}' does not exist for #{name}::OPTS[:#{type}]"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
module QiitaMarker
|
6
|
+
class Node
|
7
|
+
module Inspect
|
8
|
+
PP_INDENT_SIZE = 2
|
9
|
+
|
10
|
+
def inspect
|
11
|
+
PP.pp(self, +'', Float::INFINITY)
|
12
|
+
end
|
13
|
+
|
14
|
+
# @param printer [PrettyPrint] pp
|
15
|
+
def pretty_print(printer)
|
16
|
+
printer.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}):", '>') do
|
17
|
+
printer.breakable
|
18
|
+
|
19
|
+
attrs = %i[
|
20
|
+
sourcepos
|
21
|
+
string_content
|
22
|
+
url
|
23
|
+
title
|
24
|
+
header_level
|
25
|
+
list_type
|
26
|
+
list_start
|
27
|
+
list_tight
|
28
|
+
fence_info
|
29
|
+
].map do |name|
|
30
|
+
[name, __send__(name)]
|
31
|
+
rescue NodeError
|
32
|
+
nil
|
33
|
+
end.compact
|
34
|
+
|
35
|
+
printer.seplist(attrs) do |name, value|
|
36
|
+
printer.text "#{name}="
|
37
|
+
printer.pp value
|
38
|
+
end
|
39
|
+
|
40
|
+
if first_child
|
41
|
+
printer.breakable
|
42
|
+
printer.group(PP_INDENT_SIZE) do
|
43
|
+
children = []
|
44
|
+
node = first_child
|
45
|
+
while node
|
46
|
+
children << node
|
47
|
+
node = node.next
|
48
|
+
end
|
49
|
+
printer.text 'children='
|
50
|
+
printer.pp children
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'qiita_marker/node/inspect'
|
4
|
+
|
5
|
+
module QiitaMarker
|
6
|
+
class Node
|
7
|
+
include Enumerable
|
8
|
+
include Inspect
|
9
|
+
|
10
|
+
# Public: An iterator that "walks the tree," descending into children recursively.
|
11
|
+
#
|
12
|
+
# blk - A {Proc} representing the action to take for each child
|
13
|
+
def walk(&block)
|
14
|
+
return enum_for(:walk) unless block
|
15
|
+
|
16
|
+
yield self
|
17
|
+
each do |child|
|
18
|
+
child.walk(&block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Public: Convert the node to an HTML string.
|
23
|
+
#
|
24
|
+
# options - A {Symbol} or {Array of Symbol}s indicating the render options
|
25
|
+
# extensions - An {Array of Symbol}s indicating the extensions to use
|
26
|
+
#
|
27
|
+
# Returns a {String}.
|
28
|
+
def to_html(options = :DEFAULT, extensions = [])
|
29
|
+
opts = Config.process_options(options, :render)
|
30
|
+
_render_html(opts, extensions).force_encoding('utf-8')
|
31
|
+
end
|
32
|
+
|
33
|
+
# Public: Convert the node to an XML string.
|
34
|
+
#
|
35
|
+
# options - A {Symbol} or {Array of Symbol}s indicating the render options
|
36
|
+
#
|
37
|
+
# Returns a {String}.
|
38
|
+
def to_xml(options = :DEFAULT)
|
39
|
+
opts = Config.process_options(options, :render)
|
40
|
+
_render_xml(opts).force_encoding('utf-8')
|
41
|
+
end
|
42
|
+
|
43
|
+
# Public: Convert the node to a CommonMark string.
|
44
|
+
#
|
45
|
+
# options - A {Symbol} or {Array of Symbol}s indicating the render options
|
46
|
+
# width - Column to wrap the output at
|
47
|
+
#
|
48
|
+
# Returns a {String}.
|
49
|
+
def to_commonmark(options = :DEFAULT, width = 120)
|
50
|
+
opts = Config.process_options(options, :render)
|
51
|
+
_render_commonmark(opts, width).force_encoding('utf-8')
|
52
|
+
end
|
53
|
+
|
54
|
+
# Public: Convert the node to a plain text string.
|
55
|
+
#
|
56
|
+
# options - A {Symbol} or {Array of Symbol}s indicating the render options
|
57
|
+
# width - Column to wrap the output at
|
58
|
+
#
|
59
|
+
# Returns a {String}.
|
60
|
+
def to_plaintext(options = :DEFAULT, width = 120)
|
61
|
+
opts = Config.process_options(options, :render)
|
62
|
+
_render_plaintext(opts, width).force_encoding('utf-8')
|
63
|
+
end
|
64
|
+
|
65
|
+
# Public: Iterate over the children (if any) of the current pointer.
|
66
|
+
def each
|
67
|
+
return enum_for(:each) unless block_given?
|
68
|
+
|
69
|
+
child = first_child
|
70
|
+
while child
|
71
|
+
nextchild = child.next
|
72
|
+
yield child
|
73
|
+
child = nextchild
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Deprecated: Please use `each` instead
|
78
|
+
def each_child(&block)
|
79
|
+
warn '[DEPRECATION] `each_child` is deprecated. Please use `each` instead.'
|
80
|
+
each(&block)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,252 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module QiitaMarker
|
4
|
+
class HtmlRenderer < Renderer
|
5
|
+
def document(_)
|
6
|
+
super
|
7
|
+
out("</ol>\n</section>\n") if @written_footnote_ix
|
8
|
+
end
|
9
|
+
|
10
|
+
def header(node)
|
11
|
+
block do
|
12
|
+
out('<h', node.header_level, "#{sourcepos(node)}>", :children,
|
13
|
+
'</h', node.header_level, '>')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def paragraph(node)
|
18
|
+
if @in_tight && node.parent.type != :blockquote
|
19
|
+
out(:children)
|
20
|
+
else
|
21
|
+
block do
|
22
|
+
container("<p#{sourcepos(node)}>", '</p>') do
|
23
|
+
out(:children)
|
24
|
+
if node.parent.type == :footnote_definition && node.next.nil?
|
25
|
+
out(' ')
|
26
|
+
out_footnote_backref
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def list(node)
|
34
|
+
old_in_tight = @in_tight
|
35
|
+
@in_tight = node.list_tight
|
36
|
+
|
37
|
+
block do
|
38
|
+
if node.list_type == :bullet_list
|
39
|
+
container("<ul#{sourcepos(node)}>\n", '</ul>') do
|
40
|
+
out(:children)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
start = if node.list_start == 1
|
44
|
+
"<ol#{sourcepos(node)}>\n"
|
45
|
+
else
|
46
|
+
"<ol start=\"#{node.list_start}\"#{sourcepos(node)}>\n"
|
47
|
+
end
|
48
|
+
container(start, '</ol>') do
|
49
|
+
out(:children)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
@in_tight = old_in_tight
|
55
|
+
end
|
56
|
+
|
57
|
+
def list_item(node)
|
58
|
+
block do
|
59
|
+
tasklist_data = tasklist(node)
|
60
|
+
container("<li#{sourcepos(node)}#{tasklist_data}>#{' ' if tasklist?(node)}", '</li>') do
|
61
|
+
out(:children)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def tasklist(node)
|
67
|
+
return '' unless tasklist?(node)
|
68
|
+
|
69
|
+
state = if checked?(node)
|
70
|
+
'checked="" disabled=""'
|
71
|
+
else
|
72
|
+
'disabled=""'
|
73
|
+
end
|
74
|
+
"><input type=\"checkbox\" #{state} /"
|
75
|
+
end
|
76
|
+
|
77
|
+
def blockquote(node)
|
78
|
+
block do
|
79
|
+
container("<blockquote#{sourcepos(node)}>\n", '</blockquote>') do
|
80
|
+
out(:children)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def hrule(node)
|
86
|
+
block do
|
87
|
+
out("<hr#{sourcepos(node)} />")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def code_block(node)
|
92
|
+
block do
|
93
|
+
if option_enabled?(:GITHUB_PRE_LANG)
|
94
|
+
out("<pre#{sourcepos(node)}")
|
95
|
+
out(' lang="', node.fence_info.split(/\s+/)[0], '"') if node.fence_info && !node.fence_info.empty?
|
96
|
+
out('><code>')
|
97
|
+
else
|
98
|
+
out("<pre#{sourcepos(node)}><code")
|
99
|
+
if node.fence_info && !node.fence_info.empty?
|
100
|
+
out(' class="language-', node.fence_info.split(/\s+/)[0], '">')
|
101
|
+
else
|
102
|
+
out('>')
|
103
|
+
end
|
104
|
+
end
|
105
|
+
out(escape_html(node.string_content))
|
106
|
+
out('</code></pre>')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def html(node)
|
111
|
+
block do
|
112
|
+
if option_enabled?(:UNSAFE)
|
113
|
+
out(tagfilter(node.string_content))
|
114
|
+
else
|
115
|
+
out('<!-- raw HTML omitted -->')
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def inline_html(node)
|
121
|
+
if option_enabled?(:UNSAFE)
|
122
|
+
out(tagfilter(node.string_content))
|
123
|
+
else
|
124
|
+
out('<!-- raw HTML omitted -->')
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def emph(_)
|
129
|
+
out('<em>', :children, '</em>')
|
130
|
+
end
|
131
|
+
|
132
|
+
def strong(_)
|
133
|
+
out('<strong>', :children, '</strong>')
|
134
|
+
end
|
135
|
+
|
136
|
+
def link(node)
|
137
|
+
out('<a href="', node.url.nil? ? '' : escape_href(node.url), '"')
|
138
|
+
out(' title="', escape_html(node.title), '"') if node.title && !node.title.empty?
|
139
|
+
out('>', :children, '</a>')
|
140
|
+
end
|
141
|
+
|
142
|
+
def image(node)
|
143
|
+
out('<img src="', escape_href(node.url), '"')
|
144
|
+
plain do
|
145
|
+
out(' alt="', :children, '"')
|
146
|
+
end
|
147
|
+
out(' title="', escape_html(node.title), '"') if node.title && !node.title.empty?
|
148
|
+
out(' />')
|
149
|
+
end
|
150
|
+
|
151
|
+
def text(node)
|
152
|
+
out(escape_html(node.string_content))
|
153
|
+
end
|
154
|
+
|
155
|
+
def code(node)
|
156
|
+
out('<code>')
|
157
|
+
out(escape_html(node.string_content))
|
158
|
+
out('</code>')
|
159
|
+
end
|
160
|
+
|
161
|
+
def linebreak(_node)
|
162
|
+
out("<br />\n")
|
163
|
+
end
|
164
|
+
|
165
|
+
def softbreak(_)
|
166
|
+
if option_enabled?(:HARDBREAKS)
|
167
|
+
out("<br />\n")
|
168
|
+
elsif option_enabled?(:NOBREAKS)
|
169
|
+
out(' ')
|
170
|
+
else
|
171
|
+
out("\n")
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def table(node)
|
176
|
+
@alignments = node.table_alignments
|
177
|
+
@needs_close_tbody = false
|
178
|
+
out("<table#{sourcepos(node)}>\n", :children)
|
179
|
+
out("</tbody>\n") if @needs_close_tbody
|
180
|
+
out("</table>\n")
|
181
|
+
end
|
182
|
+
|
183
|
+
def table_header(node)
|
184
|
+
@column_index = 0
|
185
|
+
|
186
|
+
@in_header = true
|
187
|
+
out("<thead>\n<tr#{sourcepos(node)}>\n", :children, "</tr>\n</thead>\n")
|
188
|
+
@in_header = false
|
189
|
+
end
|
190
|
+
|
191
|
+
def table_row(node)
|
192
|
+
@column_index = 0
|
193
|
+
if !@in_header && !@needs_close_tbody
|
194
|
+
@needs_close_tbody = true
|
195
|
+
out("<tbody>\n")
|
196
|
+
end
|
197
|
+
out("<tr#{sourcepos(node)}>\n", :children, "</tr>\n")
|
198
|
+
end
|
199
|
+
|
200
|
+
def table_cell(node)
|
201
|
+
align = case @alignments[@column_index]
|
202
|
+
when :left then ' align="left"'
|
203
|
+
when :right then ' align="right"'
|
204
|
+
when :center then ' align="center"'
|
205
|
+
else; ''
|
206
|
+
end
|
207
|
+
out(@in_header ? "<th#{align}#{sourcepos(node)}>" : "<td#{align}#{sourcepos(node)}>", :children, @in_header ? "</th>\n" : "</td>\n")
|
208
|
+
@column_index += 1
|
209
|
+
end
|
210
|
+
|
211
|
+
def strikethrough(_)
|
212
|
+
out('<del>', :children, '</del>')
|
213
|
+
end
|
214
|
+
|
215
|
+
def footnote_reference(node)
|
216
|
+
out("<sup class=\"footnote-ref\"><a href=\"#fn#{node.string_content}\" id=\"fnref#{node.string_content}\">#{node.string_content}</a></sup>")
|
217
|
+
end
|
218
|
+
|
219
|
+
def footnote_definition(_)
|
220
|
+
unless @footnote_ix
|
221
|
+
out("<section class=\"footnotes\">\n<ol>\n")
|
222
|
+
@footnote_ix = 0
|
223
|
+
end
|
224
|
+
|
225
|
+
@footnote_ix += 1
|
226
|
+
out("<li id=\"fn#{@footnote_ix}\">\n", :children)
|
227
|
+
out("\n") if out_footnote_backref
|
228
|
+
out("</li>\n")
|
229
|
+
# </ol>
|
230
|
+
# </section>
|
231
|
+
end
|
232
|
+
|
233
|
+
private
|
234
|
+
|
235
|
+
def out_footnote_backref
|
236
|
+
return false if @written_footnote_ix == @footnote_ix
|
237
|
+
|
238
|
+
@written_footnote_ix = @footnote_ix
|
239
|
+
|
240
|
+
out("<a href=\"#fnref#{@footnote_ix}\" class=\"footnote-backref\">↩</a>")
|
241
|
+
true
|
242
|
+
end
|
243
|
+
|
244
|
+
def tasklist?(node)
|
245
|
+
node.type_string == 'tasklist'
|
246
|
+
end
|
247
|
+
|
248
|
+
def checked?(node)
|
249
|
+
node.tasklist_item_checked?
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|