structured_text_renderer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +27 -0
  5. data/.rubocop_todo.yml +35 -0
  6. data/.travis.yml +13 -0
  7. data/.yardopts +4 -0
  8. data/CHANGELOG.md +5 -0
  9. data/Gemfile +3 -0
  10. data/Gemfile.lock +110 -0
  11. data/Guardfile +24 -0
  12. data/LICENSE.txt +21 -0
  13. data/README.md +87 -0
  14. data/Rakefile +33 -0
  15. data/coverage/.last_run.json +5 -0
  16. data/coverage/.resultset.json +1366 -0
  17. data/coverage/.resultset.json.lock +0 -0
  18. data/coverage/assets/0.10.2/application.css +799 -0
  19. data/coverage/assets/0.10.2/application.js +1707 -0
  20. data/coverage/assets/0.10.2/colorbox/border.png +0 -0
  21. data/coverage/assets/0.10.2/colorbox/controls.png +0 -0
  22. data/coverage/assets/0.10.2/colorbox/loading.gif +0 -0
  23. data/coverage/assets/0.10.2/colorbox/loading_background.png +0 -0
  24. data/coverage/assets/0.10.2/favicon_green.png +0 -0
  25. data/coverage/assets/0.10.2/favicon_red.png +0 -0
  26. data/coverage/assets/0.10.2/favicon_yellow.png +0 -0
  27. data/coverage/assets/0.10.2/loading.gif +0 -0
  28. data/coverage/assets/0.10.2/magnify.png +0 -0
  29. data/coverage/assets/0.10.2/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  30. data/coverage/assets/0.10.2/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  31. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  32. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  33. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  34. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  35. data/coverage/assets/0.10.2/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  36. data/coverage/assets/0.10.2/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  37. data/coverage/assets/0.10.2/smoothness/images/ui-icons_222222_256x240.png +0 -0
  38. data/coverage/assets/0.10.2/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  39. data/coverage/assets/0.10.2/smoothness/images/ui-icons_454545_256x240.png +0 -0
  40. data/coverage/assets/0.10.2/smoothness/images/ui-icons_888888_256x240.png +0 -0
  41. data/coverage/assets/0.10.2/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  42. data/coverage/index.html +9110 -0
  43. data/doc/StructuredTextRenderer/BaseBlockRenderer.html +248 -0
  44. data/doc/StructuredTextRenderer/BaseInlineRenderer.html +242 -0
  45. data/doc/StructuredTextRenderer/BaseNodeRenderer.html +300 -0
  46. data/doc/StructuredTextRenderer/BoldRenderer.html +169 -0
  47. data/doc/StructuredTextRenderer/CodeRenderer.html +169 -0
  48. data/doc/StructuredTextRenderer/DocumentRenderer.html +242 -0
  49. data/doc/StructuredTextRenderer/EntryBlockRenderer.html +239 -0
  50. data/doc/StructuredTextRenderer/HeadingFiveRenderer.html +169 -0
  51. data/doc/StructuredTextRenderer/HeadingFourRenderer.html +169 -0
  52. data/doc/StructuredTextRenderer/HeadingOneRenderer.html +169 -0
  53. data/doc/StructuredTextRenderer/HeadingSixRenderer.html +169 -0
  54. data/doc/StructuredTextRenderer/HeadingThreeRenderer.html +169 -0
  55. data/doc/StructuredTextRenderer/HeadingTwoRenderer.html +169 -0
  56. data/doc/StructuredTextRenderer/HrRenderer.html +238 -0
  57. data/doc/StructuredTextRenderer/HyperlinkRenderer.html +254 -0
  58. data/doc/StructuredTextRenderer/ItalicRenderer.html +169 -0
  59. data/doc/StructuredTextRenderer/ListItemRenderer.html +169 -0
  60. data/doc/StructuredTextRenderer/NullRenderer.html +238 -0
  61. data/doc/StructuredTextRenderer/OrderedListRenderer.html +169 -0
  62. data/doc/StructuredTextRenderer/ParagraphRenderer.html +169 -0
  63. data/doc/StructuredTextRenderer/QuoteRenderer.html +169 -0
  64. data/doc/StructuredTextRenderer/Renderer.html +349 -0
  65. data/doc/StructuredTextRenderer/TextRenderer.html +254 -0
  66. data/doc/StructuredTextRenderer/UnderlineRenderer.html +169 -0
  67. data/doc/StructuredTextRenderer/UnorderedListRenderer.html +169 -0
  68. data/doc/StructuredTextRenderer.html +155 -0
  69. data/doc/_index.html +403 -0
  70. data/doc/class_list.html +51 -0
  71. data/doc/css/common.css +1 -0
  72. data/doc/css/full_list.css +58 -0
  73. data/doc/css/style.css +496 -0
  74. data/doc/file.CHANGELOG.html +77 -0
  75. data/doc/file.LICENSE.html +70 -0
  76. data/doc/file.README.html +171 -0
  77. data/doc/file_list.html +66 -0
  78. data/doc/frames.html +17 -0
  79. data/doc/index.html +171 -0
  80. data/doc/js/app.js +292 -0
  81. data/doc/js/full_list.js +216 -0
  82. data/doc/js/jquery.js +4 -0
  83. data/doc/method_list.html +147 -0
  84. data/doc/top-level-namespace.html +110 -0
  85. data/lib/structured_text_renderer/base_node_renderer.rb +18 -0
  86. data/lib/structured_text_renderer/block_renderers/base_block_renderer.rb +20 -0
  87. data/lib/structured_text_renderer/block_renderers/entry_block_renderer.rb +13 -0
  88. data/lib/structured_text_renderer/block_renderers/heading_five_renderer.rb +12 -0
  89. data/lib/structured_text_renderer/block_renderers/heading_four_renderer.rb +12 -0
  90. data/lib/structured_text_renderer/block_renderers/heading_one_renderer.rb +12 -0
  91. data/lib/structured_text_renderer/block_renderers/heading_six_renderer.rb +12 -0
  92. data/lib/structured_text_renderer/block_renderers/heading_three_renderer.rb +12 -0
  93. data/lib/structured_text_renderer/block_renderers/heading_two_renderer.rb +12 -0
  94. data/lib/structured_text_renderer/block_renderers/hr_renderer.rb +11 -0
  95. data/lib/structured_text_renderer/block_renderers/hyperlink_renderer.rb +15 -0
  96. data/lib/structured_text_renderer/block_renderers/list_item_renderer.rb +12 -0
  97. data/lib/structured_text_renderer/block_renderers/ordered_list_renderer.rb +12 -0
  98. data/lib/structured_text_renderer/block_renderers/paragraph_renderer.rb +12 -0
  99. data/lib/structured_text_renderer/block_renderers/quote_renderer.rb +12 -0
  100. data/lib/structured_text_renderer/block_renderers/unordered_list_renderer.rb +12 -0
  101. data/lib/structured_text_renderer/block_renderers.rb +18 -0
  102. data/lib/structured_text_renderer/document_renderers/document_renderer.rb +13 -0
  103. data/lib/structured_text_renderer/document_renderers.rb +1 -0
  104. data/lib/structured_text_renderer/null_renderer.rb +19 -0
  105. data/lib/structured_text_renderer/renderer.rb +46 -0
  106. data/lib/structured_text_renderer/text_renderers/base_inline_renderer.rb +17 -0
  107. data/lib/structured_text_renderer/text_renderers/bold_renderer.rb +12 -0
  108. data/lib/structured_text_renderer/text_renderers/code_renderer.rb +12 -0
  109. data/lib/structured_text_renderer/text_renderers/italic_renderer.rb +12 -0
  110. data/lib/structured_text_renderer/text_renderers/text_renderer.rb +19 -0
  111. data/lib/structured_text_renderer/text_renderers/underline_renderer.rb +12 -0
  112. data/lib/structured_text_renderer/text_renderers.rb +5 -0
  113. data/lib/structured_text_renderer/version.rb +4 -0
  114. data/lib/structured_text_renderer.rb +2 -0
  115. data/spec/lib/structured_text_renderer/block_renderers/base_block_renderer_spec.rb +26 -0
  116. data/spec/lib/structured_text_renderer/block_renderers/entry_block_renderer_spec.rb +15 -0
  117. data/spec/lib/structured_text_renderer/block_renderers/heading_five_renderer_spec.rb +24 -0
  118. data/spec/lib/structured_text_renderer/block_renderers/heading_four_renderer_spec.rb +24 -0
  119. data/spec/lib/structured_text_renderer/block_renderers/heading_one_renderer_spec.rb +24 -0
  120. data/spec/lib/structured_text_renderer/block_renderers/heading_six_renderer_spec.rb +24 -0
  121. data/spec/lib/structured_text_renderer/block_renderers/heading_three_renderer_spec.rb +24 -0
  122. data/spec/lib/structured_text_renderer/block_renderers/heading_two_renderer_spec.rb +24 -0
  123. data/spec/lib/structured_text_renderer/block_renderers/hr_renderer_spec.rb +13 -0
  124. data/spec/lib/structured_text_renderer/block_renderers/hyperlink_renderer_spec.rb +24 -0
  125. data/spec/lib/structured_text_renderer/block_renderers/list_item_renderer_spec.rb +24 -0
  126. data/spec/lib/structured_text_renderer/block_renderers/ordered_list_renderer_spec.rb +56 -0
  127. data/spec/lib/structured_text_renderer/block_renderers/paragraph_renderer_spec.rb +24 -0
  128. data/spec/lib/structured_text_renderer/block_renderers/quote_renderer_spec.rb +25 -0
  129. data/spec/lib/structured_text_renderer/block_renderers/unordered_list_renderer_spec.rb +56 -0
  130. data/spec/lib/structured_text_renderer/document_renderers/document_renderer_spec.rb +42 -0
  131. data/spec/lib/structured_text_renderer/null_renderer_spec.rb +19 -0
  132. data/spec/lib/structured_text_renderer/renderer_spec.rb +308 -0
  133. data/spec/lib/structured_text_renderer/text_renderers/base_inline_renderer_spec.rb +11 -0
  134. data/spec/lib/structured_text_renderer/text_renderers/bold_renderer_spec.rb +11 -0
  135. data/spec/lib/structured_text_renderer/text_renderers/code_renderer_spec.rb +11 -0
  136. data/spec/lib/structured_text_renderer/text_renderers/italic_renderer_spec.rb +11 -0
  137. data/spec/lib/structured_text_renderer/text_renderers/text_renderer_spec.rb +53 -0
  138. data/spec/lib/structured_text_renderer/text_renderers/underline_renderer_spec.rb +11 -0
  139. data/spec/spec_helper.rb +82 -0
  140. data/structured_text_renderer.gemspec +30 -0
  141. 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,11 @@
1
+ require 'spec_helper'
2
+
3
+ mock_node = {"value" => "foo"}
4
+
5
+ describe StructuredTextRenderer::BaseInlineRenderer do
6
+ subject { described_class.new }
7
+
8
+ it 'renders as a span' do
9
+ expect(subject.render(mock_node)).to eq "<span>foo</span>"
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ mock_node = {"value" => "foo"}
4
+
5
+ describe StructuredTextRenderer::BoldRenderer do
6
+ subject { described_class.new }
7
+
8
+ it 'renders as a b' do
9
+ expect(subject.render(mock_node)).to eq "<b>foo</b>"
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ mock_node = {"value" => "foo"}
4
+
5
+ describe StructuredTextRenderer::CodeRenderer do
6
+ subject { described_class.new }
7
+
8
+ it 'renders as a code' do
9
+ expect(subject.render(mock_node)).to eq "<code>foo</code>"
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ mock_node = {"value" => "foo"}
4
+
5
+ describe StructuredTextRenderer::ItalicRenderer do
6
+ subject { described_class.new }
7
+
8
+ it 'renders as a i' do
9
+ expect(subject.render(mock_node)).to eq "<i>foo</i>"
10
+ end
11
+ 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
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ mock_node = {"value" => "foo"}
4
+
5
+ describe StructuredTextRenderer::UnderlineRenderer do
6
+ subject { described_class.new }
7
+
8
+ it 'renders as a u' do
9
+ expect(subject.render(mock_node)).to eq "<u>foo</u>"
10
+ end
11
+ end
@@ -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