structured_text_renderer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +1 -0
- data/.rubocop.yml +27 -0
- data/.rubocop_todo.yml +35 -0
- data/.travis.yml +13 -0
- data/.yardopts +4 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +110 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/Rakefile +33 -0
- data/coverage/.last_run.json +5 -0
- data/coverage/.resultset.json +1366 -0
- data/coverage/.resultset.json.lock +0 -0
- data/coverage/assets/0.10.2/application.css +799 -0
- data/coverage/assets/0.10.2/application.js +1707 -0
- data/coverage/assets/0.10.2/colorbox/border.png +0 -0
- data/coverage/assets/0.10.2/colorbox/controls.png +0 -0
- data/coverage/assets/0.10.2/colorbox/loading.gif +0 -0
- data/coverage/assets/0.10.2/colorbox/loading_background.png +0 -0
- data/coverage/assets/0.10.2/favicon_green.png +0 -0
- data/coverage/assets/0.10.2/favicon_red.png +0 -0
- data/coverage/assets/0.10.2/favicon_yellow.png +0 -0
- data/coverage/assets/0.10.2/loading.gif +0 -0
- data/coverage/assets/0.10.2/magnify.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.10.2/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/index.html +9110 -0
- data/doc/StructuredTextRenderer/BaseBlockRenderer.html +248 -0
- data/doc/StructuredTextRenderer/BaseInlineRenderer.html +242 -0
- data/doc/StructuredTextRenderer/BaseNodeRenderer.html +300 -0
- data/doc/StructuredTextRenderer/BoldRenderer.html +169 -0
- data/doc/StructuredTextRenderer/CodeRenderer.html +169 -0
- data/doc/StructuredTextRenderer/DocumentRenderer.html +242 -0
- data/doc/StructuredTextRenderer/EntryBlockRenderer.html +239 -0
- data/doc/StructuredTextRenderer/HeadingFiveRenderer.html +169 -0
- data/doc/StructuredTextRenderer/HeadingFourRenderer.html +169 -0
- data/doc/StructuredTextRenderer/HeadingOneRenderer.html +169 -0
- data/doc/StructuredTextRenderer/HeadingSixRenderer.html +169 -0
- data/doc/StructuredTextRenderer/HeadingThreeRenderer.html +169 -0
- data/doc/StructuredTextRenderer/HeadingTwoRenderer.html +169 -0
- data/doc/StructuredTextRenderer/HrRenderer.html +238 -0
- data/doc/StructuredTextRenderer/HyperlinkRenderer.html +254 -0
- data/doc/StructuredTextRenderer/ItalicRenderer.html +169 -0
- data/doc/StructuredTextRenderer/ListItemRenderer.html +169 -0
- data/doc/StructuredTextRenderer/NullRenderer.html +238 -0
- data/doc/StructuredTextRenderer/OrderedListRenderer.html +169 -0
- data/doc/StructuredTextRenderer/ParagraphRenderer.html +169 -0
- data/doc/StructuredTextRenderer/QuoteRenderer.html +169 -0
- data/doc/StructuredTextRenderer/Renderer.html +349 -0
- data/doc/StructuredTextRenderer/TextRenderer.html +254 -0
- data/doc/StructuredTextRenderer/UnderlineRenderer.html +169 -0
- data/doc/StructuredTextRenderer/UnorderedListRenderer.html +169 -0
- data/doc/StructuredTextRenderer.html +155 -0
- data/doc/_index.html +403 -0
- data/doc/class_list.html +51 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +58 -0
- data/doc/css/style.css +496 -0
- data/doc/file.CHANGELOG.html +77 -0
- data/doc/file.LICENSE.html +70 -0
- data/doc/file.README.html +171 -0
- data/doc/file_list.html +66 -0
- data/doc/frames.html +17 -0
- data/doc/index.html +171 -0
- data/doc/js/app.js +292 -0
- data/doc/js/full_list.js +216 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +147 -0
- data/doc/top-level-namespace.html +110 -0
- data/lib/structured_text_renderer/base_node_renderer.rb +18 -0
- data/lib/structured_text_renderer/block_renderers/base_block_renderer.rb +20 -0
- data/lib/structured_text_renderer/block_renderers/entry_block_renderer.rb +13 -0
- data/lib/structured_text_renderer/block_renderers/heading_five_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/heading_four_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/heading_one_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/heading_six_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/heading_three_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/heading_two_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/hr_renderer.rb +11 -0
- data/lib/structured_text_renderer/block_renderers/hyperlink_renderer.rb +15 -0
- data/lib/structured_text_renderer/block_renderers/list_item_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/ordered_list_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/paragraph_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/quote_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers/unordered_list_renderer.rb +12 -0
- data/lib/structured_text_renderer/block_renderers.rb +18 -0
- data/lib/structured_text_renderer/document_renderers/document_renderer.rb +13 -0
- data/lib/structured_text_renderer/document_renderers.rb +1 -0
- data/lib/structured_text_renderer/null_renderer.rb +19 -0
- data/lib/structured_text_renderer/renderer.rb +46 -0
- data/lib/structured_text_renderer/text_renderers/base_inline_renderer.rb +17 -0
- data/lib/structured_text_renderer/text_renderers/bold_renderer.rb +12 -0
- data/lib/structured_text_renderer/text_renderers/code_renderer.rb +12 -0
- data/lib/structured_text_renderer/text_renderers/italic_renderer.rb +12 -0
- data/lib/structured_text_renderer/text_renderers/text_renderer.rb +19 -0
- data/lib/structured_text_renderer/text_renderers/underline_renderer.rb +12 -0
- data/lib/structured_text_renderer/text_renderers.rb +5 -0
- data/lib/structured_text_renderer/version.rb +4 -0
- data/lib/structured_text_renderer.rb +2 -0
- data/spec/lib/structured_text_renderer/block_renderers/base_block_renderer_spec.rb +26 -0
- data/spec/lib/structured_text_renderer/block_renderers/entry_block_renderer_spec.rb +15 -0
- data/spec/lib/structured_text_renderer/block_renderers/heading_five_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/heading_four_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/heading_one_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/heading_six_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/heading_three_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/heading_two_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/hr_renderer_spec.rb +13 -0
- data/spec/lib/structured_text_renderer/block_renderers/hyperlink_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/list_item_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/ordered_list_renderer_spec.rb +56 -0
- data/spec/lib/structured_text_renderer/block_renderers/paragraph_renderer_spec.rb +24 -0
- data/spec/lib/structured_text_renderer/block_renderers/quote_renderer_spec.rb +25 -0
- data/spec/lib/structured_text_renderer/block_renderers/unordered_list_renderer_spec.rb +56 -0
- data/spec/lib/structured_text_renderer/document_renderers/document_renderer_spec.rb +42 -0
- data/spec/lib/structured_text_renderer/null_renderer_spec.rb +19 -0
- data/spec/lib/structured_text_renderer/renderer_spec.rb +308 -0
- data/spec/lib/structured_text_renderer/text_renderers/base_inline_renderer_spec.rb +11 -0
- data/spec/lib/structured_text_renderer/text_renderers/bold_renderer_spec.rb +11 -0
- data/spec/lib/structured_text_renderer/text_renderers/code_renderer_spec.rb +11 -0
- data/spec/lib/structured_text_renderer/text_renderers/italic_renderer_spec.rb +11 -0
- data/spec/lib/structured_text_renderer/text_renderers/text_renderer_spec.rb +53 -0
- data/spec/lib/structured_text_renderer/text_renderers/underline_renderer_spec.rb +11 -0
- data/spec/spec_helper.rb +82 -0
- data/structured_text_renderer.gemspec +30 -0
- metadata +361 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
mock_node = {"content" => [{"value" => "foo", "nodeType" => "text"}]}
|
4
|
+
|
5
|
+
mock_node_with_marks = {"content" => [{"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "bold"}]}]}
|
6
|
+
|
7
|
+
describe StructuredTextRenderer::ParagraphRenderer do
|
8
|
+
subject do
|
9
|
+
described_class.new(
|
10
|
+
'text' => StructuredTextRenderer::TextRenderer,
|
11
|
+
'bold' => StructuredTextRenderer::BoldRenderer
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#render' do
|
16
|
+
it 'renders a p' do
|
17
|
+
expect(subject.render(mock_node)).to eq "<p>foo</p>"
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'will propagate marks to text renderers' do
|
21
|
+
expect(subject.render(mock_node_with_marks)).to eq "<p><b>foo</b></p>"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
mock_node = {"content" => [{"nodeType" => "paragraph", "content" => [{"value" => "foo", "nodeType" => "text"}]}]}
|
4
|
+
|
5
|
+
mock_node_with_marks = {"content" => [{"nodeType" => "paragraph", "content" => [{"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "bold"}]}]}]}
|
6
|
+
|
7
|
+
describe StructuredTextRenderer::QuoteRenderer do
|
8
|
+
subject do
|
9
|
+
described_class.new(
|
10
|
+
'paragraph' => StructuredTextRenderer::ParagraphRenderer,
|
11
|
+
'text' => StructuredTextRenderer::TextRenderer,
|
12
|
+
'bold' => StructuredTextRenderer::BoldRenderer
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#render' do
|
17
|
+
it 'renders a blockqoute' do
|
18
|
+
expect(subject.render(mock_node)).to eq "<blockqoute><p>foo</p></blockqoute>"
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'will propagate marks to text renderers' do
|
22
|
+
expect(subject.render(mock_node_with_marks)).to eq "<blockqoute><p><b>foo</b></p></blockqoute>"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
mock_node = {"content" => [
|
4
|
+
{
|
5
|
+
"content" => [{
|
6
|
+
"content" => [{
|
7
|
+
"value" => "foo",
|
8
|
+
"nodeType" => "text",
|
9
|
+
"nodeClass" => "text"
|
10
|
+
}],
|
11
|
+
"nodeType" => "paragraph",
|
12
|
+
"nodeClass" => "block"
|
13
|
+
}],
|
14
|
+
"nodeType" => "list-item",
|
15
|
+
"nodeClass" => "block"
|
16
|
+
}]
|
17
|
+
}
|
18
|
+
|
19
|
+
mock_node_with_marks = {"content" => [
|
20
|
+
{
|
21
|
+
"content" => [{
|
22
|
+
"content" => [{
|
23
|
+
"marks" => [{"type" => "bold"}],
|
24
|
+
"value" => "foo",
|
25
|
+
"nodeType" => "text",
|
26
|
+
"nodeClass" => "text"
|
27
|
+
}],
|
28
|
+
"nodeType" => "paragraph",
|
29
|
+
"nodeClass" => "block"
|
30
|
+
}],
|
31
|
+
"nodeType" => "list-item",
|
32
|
+
"nodeClass" => "block"
|
33
|
+
}]
|
34
|
+
}
|
35
|
+
|
36
|
+
|
37
|
+
describe StructuredTextRenderer::UnorderedListRenderer do
|
38
|
+
subject do
|
39
|
+
described_class.new(
|
40
|
+
'paragraph' => StructuredTextRenderer::ParagraphRenderer,
|
41
|
+
'text' => StructuredTextRenderer::TextRenderer,
|
42
|
+
'bold' => StructuredTextRenderer::BoldRenderer,
|
43
|
+
'list-item' => StructuredTextRenderer::ListItemRenderer
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#render' do
|
48
|
+
it 'renders a ul' do
|
49
|
+
expect(subject.render(mock_node)).to eq "<ul><li><p>foo</p></li></ul>"
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'will propagate marks to text renderers' do
|
53
|
+
expect(subject.render(mock_node_with_marks)).to eq "<ul><li><p><b>foo</b></p></li></ul>"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
mock_document = {
|
4
|
+
"nodeType" => "document",
|
5
|
+
"content" => [
|
6
|
+
{
|
7
|
+
"content" => [{"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "bold"}]}],
|
8
|
+
"nodeType" => "heading-1",
|
9
|
+
}
|
10
|
+
],
|
11
|
+
}
|
12
|
+
|
13
|
+
mock_document_with_unknown_nodetype = {
|
14
|
+
"nodeType" => "document",
|
15
|
+
"content" => [
|
16
|
+
{
|
17
|
+
"content" => [{"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "bold"}]}],
|
18
|
+
"nodeType" => "unknown",
|
19
|
+
}
|
20
|
+
],
|
21
|
+
}
|
22
|
+
|
23
|
+
describe StructuredTextRenderer::DocumentRenderer do
|
24
|
+
subject do
|
25
|
+
described_class.new(
|
26
|
+
'heading-1' => StructuredTextRenderer::HeadingOneRenderer,
|
27
|
+
'text' => StructuredTextRenderer::TextRenderer,
|
28
|
+
'bold' => StructuredTextRenderer::BoldRenderer,
|
29
|
+
nil => StructuredTextRenderer::NullRenderer
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#render' do
|
34
|
+
it 'propagates rendering to renderers' do
|
35
|
+
expect(subject.render(mock_document)).to eq "<h1><b>foo</b></h1>"
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'will raise an error unknown node types' do
|
39
|
+
expect { subject.render(mock_document_with_unknown_nodetype) }.to raise_error "No renderer defined for 'unknown' nodes"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StructuredTextRenderer::NullRenderer do
|
4
|
+
subject { described_class.new }
|
5
|
+
|
6
|
+
describe 'null renderer will raise errors for unknown nodes' do
|
7
|
+
it 'will look for nodeType' do
|
8
|
+
expect { subject.render({'nodeType' => 'foo'}) }.to raise_error "No renderer defined for 'foo' nodes"
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'will look for type' do
|
12
|
+
expect { subject.render({'type' => 'foo'}) }.to raise_error "No renderer defined for 'foo' nodes"
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'if none of the above found, will dump the node' do
|
16
|
+
expect { subject.render({'foo' => 'bar'}) }.to raise_error "No renderer defined for '{\"foo\"=>\"bar\"}' nodes"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,308 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
full_document = {
|
4
|
+
"content" => [
|
5
|
+
{
|
6
|
+
"data" => {},
|
7
|
+
"content" => [
|
8
|
+
{
|
9
|
+
"marks" => [],
|
10
|
+
"value" => "Some heading",
|
11
|
+
"nodeType" => "text",
|
12
|
+
"nodeClass" => "text",
|
13
|
+
}
|
14
|
+
],
|
15
|
+
"nodeType" => "heading-1",
|
16
|
+
"nodeClass" => "block",
|
17
|
+
},
|
18
|
+
{
|
19
|
+
"data" => {},
|
20
|
+
"content" => [
|
21
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
22
|
+
],
|
23
|
+
"nodeType" => "paragraph",
|
24
|
+
"nodeClass" => "block",
|
25
|
+
},
|
26
|
+
{
|
27
|
+
"data" => {
|
28
|
+
"target" => {
|
29
|
+
"sys" => {
|
30
|
+
"id" => "49rofLvvxCOiIMIi6mk8ai",
|
31
|
+
"type" => "Link",
|
32
|
+
"linkType" => "Entry",
|
33
|
+
}
|
34
|
+
}
|
35
|
+
},
|
36
|
+
"content" => [
|
37
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
38
|
+
],
|
39
|
+
"nodeType" => "embedded-entry-block",
|
40
|
+
"nodeClass" => "block",
|
41
|
+
},
|
42
|
+
{
|
43
|
+
"data" => {},
|
44
|
+
"content" => [
|
45
|
+
{
|
46
|
+
"marks" => [],
|
47
|
+
"value" => "Some subheading",
|
48
|
+
"nodeType" => "text",
|
49
|
+
"nodeClass" => "text",
|
50
|
+
}
|
51
|
+
],
|
52
|
+
"nodeType" => "heading-2",
|
53
|
+
"nodeClass" => "block",
|
54
|
+
},
|
55
|
+
{
|
56
|
+
"data" => {},
|
57
|
+
"content" => [
|
58
|
+
{
|
59
|
+
"marks" => [{"data" => {}, "type" => "bold", "object" => "mark"}],
|
60
|
+
"value" => "Some bold",
|
61
|
+
"nodeType" => "text",
|
62
|
+
"nodeClass" => "text",
|
63
|
+
}
|
64
|
+
],
|
65
|
+
"nodeType" => "paragraph",
|
66
|
+
"nodeClass" => "block",
|
67
|
+
},
|
68
|
+
{
|
69
|
+
"data" => {},
|
70
|
+
"content" => [
|
71
|
+
{
|
72
|
+
"marks" => [{"data" => {}, "type" => "italic", "object" => "mark"}],
|
73
|
+
"value" => "Some italics",
|
74
|
+
"nodeType" => "text",
|
75
|
+
"nodeClass" => "text",
|
76
|
+
}
|
77
|
+
],
|
78
|
+
"nodeType" => "paragraph",
|
79
|
+
"nodeClass" => "block",
|
80
|
+
},
|
81
|
+
{
|
82
|
+
"data" => {},
|
83
|
+
"content" => [
|
84
|
+
{
|
85
|
+
"marks" => [{"data" => {}, "type" => "underline", "object" => "mark"}],
|
86
|
+
"value" => "Some underline",
|
87
|
+
"nodeType" => "text",
|
88
|
+
"nodeClass" => "text",
|
89
|
+
}
|
90
|
+
],
|
91
|
+
"nodeType" => "paragraph",
|
92
|
+
"nodeClass" => "block",
|
93
|
+
},
|
94
|
+
{
|
95
|
+
"data" => {},
|
96
|
+
"content" => [
|
97
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
98
|
+
],
|
99
|
+
"nodeType" => "paragraph",
|
100
|
+
"nodeClass" => "block",
|
101
|
+
},
|
102
|
+
{
|
103
|
+
"data" => {},
|
104
|
+
"content" => [
|
105
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
106
|
+
],
|
107
|
+
"nodeType" => "paragraph",
|
108
|
+
"nodeClass" => "block",
|
109
|
+
},
|
110
|
+
{
|
111
|
+
"data" => {
|
112
|
+
"target" => {
|
113
|
+
"sys" => {
|
114
|
+
"id" => "5ZF9Q4K6iWSYIU2OUs0UaQ",
|
115
|
+
"type" => "Link",
|
116
|
+
"linkType" => "Entry",
|
117
|
+
}
|
118
|
+
}
|
119
|
+
},
|
120
|
+
"content" => [
|
121
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
122
|
+
],
|
123
|
+
"nodeType" => "embedded-entry-block",
|
124
|
+
"nodeClass" => "block",
|
125
|
+
},
|
126
|
+
{
|
127
|
+
"data" => {},
|
128
|
+
"content" => [
|
129
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
130
|
+
],
|
131
|
+
"nodeType" => "paragraph",
|
132
|
+
"nodeClass" => "block",
|
133
|
+
},
|
134
|
+
{
|
135
|
+
"data" => {},
|
136
|
+
"content" => [
|
137
|
+
{
|
138
|
+
"marks" => [],
|
139
|
+
"value" => "Some raw content",
|
140
|
+
"nodeType" => "text",
|
141
|
+
"nodeClass" => "text",
|
142
|
+
}
|
143
|
+
],
|
144
|
+
"nodeType" => "paragraph",
|
145
|
+
"nodeClass" => "block",
|
146
|
+
},
|
147
|
+
{
|
148
|
+
"data" => {},
|
149
|
+
"content" => [
|
150
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
151
|
+
],
|
152
|
+
"nodeType" => "paragraph",
|
153
|
+
"nodeClass" => "block",
|
154
|
+
},
|
155
|
+
{
|
156
|
+
"data" => {},
|
157
|
+
"content" => [
|
158
|
+
{
|
159
|
+
"marks" => [],
|
160
|
+
"value" => "An unpublished embed:",
|
161
|
+
"nodeType" => "text",
|
162
|
+
"nodeClass" => "text",
|
163
|
+
}
|
164
|
+
],
|
165
|
+
"nodeType" => "paragraph",
|
166
|
+
"nodeClass" => "block",
|
167
|
+
},
|
168
|
+
{
|
169
|
+
"data" => {},
|
170
|
+
"content" => [
|
171
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
172
|
+
],
|
173
|
+
"nodeType" => "paragraph",
|
174
|
+
"nodeClass" => "block",
|
175
|
+
},
|
176
|
+
{
|
177
|
+
"data" => {
|
178
|
+
"target" => {
|
179
|
+
"sys" => {
|
180
|
+
"id" => "q2hGXkd5tICym64AcgeKK",
|
181
|
+
"type" => "Link",
|
182
|
+
"linkType" => "Entry",
|
183
|
+
}
|
184
|
+
}
|
185
|
+
},
|
186
|
+
"content" => [
|
187
|
+
{"marks" => [], "value" => "", "nodeType" => "text", "nodeClass" => "text"}
|
188
|
+
],
|
189
|
+
"nodeType" => "embedded-entry-block",
|
190
|
+
"nodeClass" => "block",
|
191
|
+
},
|
192
|
+
{
|
193
|
+
"data" => {},
|
194
|
+
"content" => [
|
195
|
+
{
|
196
|
+
"marks" => [],
|
197
|
+
"value" => "Some more content",
|
198
|
+
"nodeType" => "text",
|
199
|
+
"nodeClass" => "text",
|
200
|
+
}
|
201
|
+
],
|
202
|
+
"nodeType" => "paragraph",
|
203
|
+
"nodeClass" => "block",
|
204
|
+
},
|
205
|
+
],
|
206
|
+
"nodeType" => "document",
|
207
|
+
"nodeClass" => "document",
|
208
|
+
}
|
209
|
+
|
210
|
+
describe StructuredTextRenderer::Renderer do
|
211
|
+
subject { described_class.new }
|
212
|
+
|
213
|
+
describe '#render' do
|
214
|
+
it 'by default renders html' do
|
215
|
+
expect(subject.render(full_document)).to eq [
|
216
|
+
"<h1>Some heading</h1>",
|
217
|
+
"<p></p>",
|
218
|
+
'<div>{"target"=>{"sys"=>{"id"=>"49rofLvvxCOiIMIi6mk8ai", "type"=>"Link", "linkType"=>"Entry"}}}</div>',
|
219
|
+
"<h2>Some subheading</h2>",
|
220
|
+
"<p><b>Some bold</b></p>",
|
221
|
+
"<p><i>Some italics</i></p>",
|
222
|
+
"<p><u>Some underline</u></p>",
|
223
|
+
"<p></p>",
|
224
|
+
"<p></p>",
|
225
|
+
'<div>{"target"=>{"sys"=>{"id"=>"5ZF9Q4K6iWSYIU2OUs0UaQ", "type"=>"Link", "linkType"=>"Entry"}}}</div>',
|
226
|
+
"<p></p>",
|
227
|
+
"<p>Some raw content</p>",
|
228
|
+
"<p></p>",
|
229
|
+
"<p>An unpublished embed:</p>",
|
230
|
+
"<p></p>",
|
231
|
+
'<div>{"target"=>{"sys"=>{"id"=>"q2hGXkd5tICym64AcgeKK", "type"=>"Link", "linkType"=>"Entry"}}}</div>',
|
232
|
+
"<p>Some more content</p>",
|
233
|
+
].join("\n")
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
describe 'with all renderers overridden' do
|
238
|
+
subject do
|
239
|
+
described_class.new(
|
240
|
+
'heading-1' => HeadingOneMarkdownRenderer,
|
241
|
+
'heading-2' => HeadingTwoMarkdownRenderer,
|
242
|
+
'paragraph' => ParagraphMarkdownRenderer,
|
243
|
+
'embedded-entry-block' => EntryBlockMarkdownRenderer,
|
244
|
+
'bold' => BoldMarkdownRenderer,
|
245
|
+
'italic' => ItalicMarkdownRenderer,
|
246
|
+
'underline' => UnderlineMarkdownRenderer
|
247
|
+
)
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'renders with the overridden renderers' do
|
251
|
+
expect(subject.render(full_document)).to eq [
|
252
|
+
"# Some heading",
|
253
|
+
"",
|
254
|
+
"",
|
255
|
+
"",
|
256
|
+
"",
|
257
|
+
"```",
|
258
|
+
'{"target"=>{"sys"=>{"id"=>"49rofLvvxCOiIMIi6mk8ai", "type"=>"Link", "linkType"=>"Entry"}}}',
|
259
|
+
"```",
|
260
|
+
"",
|
261
|
+
"## Some subheading",
|
262
|
+
"",
|
263
|
+
"**Some bold**",
|
264
|
+
"",
|
265
|
+
"",
|
266
|
+
"*Some italics*",
|
267
|
+
"",
|
268
|
+
"",
|
269
|
+
"__Some underline__",
|
270
|
+
"",
|
271
|
+
"",
|
272
|
+
"",
|
273
|
+
"",
|
274
|
+
"",
|
275
|
+
"",
|
276
|
+
"",
|
277
|
+
"",
|
278
|
+
"```",
|
279
|
+
'{"target"=>{"sys"=>{"id"=>"5ZF9Q4K6iWSYIU2OUs0UaQ", "type"=>"Link", "linkType"=>"Entry"}}}',
|
280
|
+
"```",
|
281
|
+
"",
|
282
|
+
"",
|
283
|
+
"",
|
284
|
+
"",
|
285
|
+
"",
|
286
|
+
"Some raw content",
|
287
|
+
"",
|
288
|
+
"",
|
289
|
+
"",
|
290
|
+
"",
|
291
|
+
"",
|
292
|
+
"An unpublished embed:",
|
293
|
+
"",
|
294
|
+
"",
|
295
|
+
"",
|
296
|
+
"",
|
297
|
+
"",
|
298
|
+
"```",
|
299
|
+
'{"target"=>{"sys"=>{"id"=>"q2hGXkd5tICym64AcgeKK", "type"=>"Link", "linkType"=>"Entry"}}}',
|
300
|
+
"```",
|
301
|
+
"",
|
302
|
+
"",
|
303
|
+
"Some more content",
|
304
|
+
""
|
305
|
+
].join("\n")
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
mock_node_underline_only = {"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "underline"}]}
|
4
|
+
|
5
|
+
mock_node_italic_only = {"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "italic"}]}
|
6
|
+
|
7
|
+
mock_node_bold_only = {"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "bold"}]}
|
8
|
+
|
9
|
+
mock_node_multiple_marks = {
|
10
|
+
"value" => "foo",
|
11
|
+
"nodeType" => "text",
|
12
|
+
"marks" => [{"type" => "underline"}, {"type" => "italic"}, {"type" => "bold"}],
|
13
|
+
}
|
14
|
+
|
15
|
+
mock_node_unsupported_mark = {"value" => "foo", "nodeType" => "text", "marks" => [{"type" => "foobar"}]}
|
16
|
+
|
17
|
+
describe StructuredTextRenderer::TextRenderer do
|
18
|
+
subject do
|
19
|
+
described_class.new(
|
20
|
+
'text' => StructuredTextRenderer::TextRenderer,
|
21
|
+
'bold' => StructuredTextRenderer::BoldRenderer,
|
22
|
+
'italic' => StructuredTextRenderer::ItalicRenderer,
|
23
|
+
'underline' => StructuredTextRenderer::UnderlineRenderer,
|
24
|
+
nil => StructuredTextRenderer::NullRenderer
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#render' do
|
29
|
+
it 'can render with a single mark' do
|
30
|
+
expect(subject.render(mock_node_bold_only)).to eq "<b>foo</b>"
|
31
|
+
expect(subject.render(mock_node_italic_only)).to eq "<i>foo</i>"
|
32
|
+
expect(subject.render(mock_node_underline_only)).to eq "<u>foo</u>"
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'can render multiple marks' do
|
36
|
+
expect(subject.render(mock_node_multiple_marks)).to eq "<b><i><u>foo</u></i></b>"
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'raises an error for unsupported marks' do
|
40
|
+
expect { subject.render(mock_node_unsupported_mark) }.to raise_error "No renderer defined for 'foobar' nodes"
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'supports custom renderers' do
|
44
|
+
subject = described_class.new(
|
45
|
+
'text' => StructuredTextRenderer::TextRenderer,
|
46
|
+
'bold' => BoldMarkdownRenderer,
|
47
|
+
nil => StructuredTextRenderer::NullRenderer
|
48
|
+
)
|
49
|
+
|
50
|
+
expect(subject.render(mock_node_bold_only)).to eq "**foo**"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start unless RUBY_PLATFORM == 'java'
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'structured_text_renderer'
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.filter_run :focus => true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
end
|
11
|
+
|
12
|
+
class NodeRenderer
|
13
|
+
attr_reader :mappings
|
14
|
+
|
15
|
+
def initialize(mappings = {})
|
16
|
+
@mappings = mappings
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def find_renderer(node)
|
22
|
+
renderer = mappings[node['nodeType']]
|
23
|
+
return if renderer.nil?
|
24
|
+
renderer.new(mappings)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class HeadingOneMarkdownRenderer < NodeRenderer
|
29
|
+
def render(node)
|
30
|
+
node['content'].each_with_object([]) do |c, res|
|
31
|
+
renderer = find_renderer(c)
|
32
|
+
next if renderer.nil?
|
33
|
+
res << "# #{renderer.render(c)}"
|
34
|
+
end.join("\n")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class HeadingTwoMarkdownRenderer < NodeRenderer
|
39
|
+
def render(node)
|
40
|
+
node['content'].each_with_object([]) do |c, res|
|
41
|
+
renderer = find_renderer(c)
|
42
|
+
next if renderer.nil?
|
43
|
+
res << "## #{renderer.render(c)}"
|
44
|
+
end.join("\n")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class ParagraphMarkdownRenderer < NodeRenderer
|
49
|
+
def render(node)
|
50
|
+
paragraphs = node['content'].each_with_object([]) do |c, res|
|
51
|
+
renderer = find_renderer(c)
|
52
|
+
next if renderer.nil?
|
53
|
+
res << "#{renderer.render(c)}"
|
54
|
+
end.join("\n")
|
55
|
+
|
56
|
+
"\n#{paragraphs}\n"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class EntryBlockMarkdownRenderer < NodeRenderer
|
61
|
+
def render(node)
|
62
|
+
"\n```\n#{node['data']}\n```\n"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class BoldMarkdownRenderer < NodeRenderer
|
67
|
+
def render(node)
|
68
|
+
"**#{node['value']}**"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class ItalicMarkdownRenderer < NodeRenderer
|
73
|
+
def render(node)
|
74
|
+
"*#{node['value']}*"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
class UnderlineMarkdownRenderer < NodeRenderer
|
79
|
+
def render(node)
|
80
|
+
"__#{node['value']}__"
|
81
|
+
end
|
82
|
+
end
|