md_to_bbcode 1.0.0 → 2.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30246a88b518806835d54cc211033ee2204fc1e970c78e446cae7e6b97d3c7c2
4
- data.tar.gz: 7ca2a66301454a044269065b8e49366f533ba9cd4f0600ac8003341e704bcdf8
3
+ metadata.gz: e80378d305456770e761ee303fc155f8a645d4532d70381892d88e05e41d4202
4
+ data.tar.gz: 53c6a99ebd4dedfcb73888c000c8ec78a92eec96f023379b3272af699889bf70
5
5
  SHA512:
6
- metadata.gz: 2c3008f40d593f396206c92a4254aa6fedc2e81fcc23e255d78f470a142c144eac82076ee6847e1b51d1a19c6b3937383deee37fc0084b206d4c6c9ebe085239
7
- data.tar.gz: 99ed4f207dcb62f868e85b2f81758a898b07ff733a19cc9a470c6e22ff087a07dad458a3f5c07059ba9c1d2be2686a9af82fd22dd2742fa19e5a39a01328feeb
6
+ metadata.gz: 348e73481b0f2e4f628c9ac5036748d0cdb0d04ada03749da007bf63dcb1df227516dfab72401a36bea5b0d12d1f58de1269ffe61d6090aabd5b6615f6e58e55
7
+ data.tar.gz: c56f09407124239d60cde3731e90030c3dd98672f0090f1b6dc08b21b2c220551a7501fb2734092bf4f6510949b5110b90f185bc9c0fbd2fcdddd5c7328cd734
data/lib/md_to_bbcode.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'redcarpet'
2
+ require 'md_to_bbcode/bbcode_renderer'
1
3
  require 'md_to_bbcode/core_extensions/string/md_to_bbcode'
2
4
 
3
5
  module MdToBbcode
@@ -9,50 +11,18 @@ module MdToBbcode
9
11
  # Result::
10
12
  # * String: BBCode converted string
11
13
  def self.md_to_bbcode(markdown)
12
- bbcode_lines = []
13
- already_in_list = false
14
- markdown.
15
- # Images (do this gsub before links and any single tags, like bold, headings...)
16
- gsub(/!\[(.*?)\]\((.*?)\)/, '[img]\2[/img]').
17
- # Links
18
- gsub(/\[(.*?)\]\((.*?)\)/, '[url=\2]\1[/url]').
19
- # Bold
20
- gsub(/\*\*(.*?)\*\*/m, '[b]\1[/b]').
21
- # Heading 1
22
- gsub(/^# (.*?)$/, '[size=6][b]\1[/b][/size]').
23
- # Heading 2
24
- gsub(/^## (.*?)$/, '[size=6]\1[/size]').
25
- # Heading 3
26
- gsub(/^### (.*?)$/, '[size=5][b]\1[/b][/size]').
27
- # Heading 4
28
- gsub(/^#### (.*?)$/, '[size=5]\1[/size]').
29
- # Code blocks (do this before in-line code)
30
- gsub(/```(.*?)\n(.*?)```/m, "[code]\\2[/code]").
31
- # In-line code
32
- gsub(/`(.*?)`/, '[b][font=Courier New]\1[/font][/b]').
33
- # Perform lists transformations
34
- split("\n").each do |line|
35
- if line =~ /^\* (.+)$/
36
- # Single bullet line
37
- bbcode_lines << '[list]' unless already_in_list
38
- bbcode_lines << "[*]#{$1}"
39
- already_in_list = true
40
- elsif line =~ /^\d\. (.+)$/
41
- # Single numbered line
42
- bbcode_lines << '[list=1]' unless already_in_list
43
- bbcode_lines << "[*]#{$1}"
44
- already_in_list = true
45
- else
46
- if already_in_list && !(line =~ /^ (.*)$/)
47
- bbcode_lines << '[/list]'
48
- already_in_list = false
49
- end
50
- bbcode_lines << line
51
- end
52
- end
53
- bbcode_lines << '[/list]' if already_in_list
54
- bbcode_lines << '' if markdown.end_with?("\n")
55
- bbcode_lines.join("\n")
14
+ bbcode = Redcarpet::Markdown.new(
15
+ BbcodeRenderer,
16
+ fenced_code_blocks: true,
17
+ lax_spacing: false
18
+ ).render(markdown)
19
+ if markdown.end_with?("\n")
20
+ # Sometimes redcarpet removes new lines (after lists), so add them back if needed
21
+ bbcode.end_with?("\n") ? bbcode : "#{bbcode}\n"
22
+ else
23
+ # Sometimes redcarpet adds new lines (after bold markers), so strip them if needed
24
+ bbcode.end_with?("\n") ? bbcode.strip : bbcode
25
+ end
56
26
  end
57
27
 
58
28
  end
@@ -0,0 +1,127 @@
1
+ require 'redcarpet/render_strip'
2
+
3
+ module MdToBbcode
4
+
5
+ class BbcodeRenderer < Redcarpet::Render::StripDown
6
+
7
+ MARKER_BOLD = '--MdToBbcode--Bold--'
8
+ MARKER_ITALIC = '--MdToBbcode--Italic--'
9
+
10
+ def preprocess(doc)
11
+ # Due to Redcarpet bug https://github.com/vmg/redcarpet/issues/396 we have to handle bold and italic conversions before hand
12
+ # Be careful to mark them uniquely so that we don't convert them if they were in a code block
13
+ # TODO: Remove this when Redcarpet bug will be fixed
14
+ doc.
15
+ gsub('**', MARKER_BOLD).
16
+ gsub(/\*(\S|$)/, "#{MARKER_ITALIC}\\1").
17
+ gsub(/(\S)\*/, "\\1#{MARKER_ITALIC}")
18
+ end
19
+
20
+ def postprocess(doc)
21
+ # Convert bold and italic correctly due to bug https://github.com/vmg/redcarpet/issues/396
22
+ bbcode_lines = []
23
+ in_bold_text = false
24
+ in_italic_text = false
25
+ # Due to Redcarpet bug https://github.com/vmg/redcarpet/issues/600 we have to change some in-line code to blocks
26
+ # TODO: Remove when Redcarpet will be fixed
27
+ doc.split("\n").map do |line|
28
+ case line
29
+ when /^(\s*)\[b\]\[font=Courier New\]([^\[]*)$/
30
+ "#{$1}[code]"
31
+ when /^(\s*)\[\/font\]\[\/b\]$/
32
+ "#{$1}[/code]"
33
+ else
34
+ # Replace the bold markers with [b] or [/b], and
35
+ # make sure we remove occurences of bold text ([b] and [/b] already part of the text) when bold is already applied: this can happen as we use bold text for inline code
36
+ # TODO: Remove this when https://github.com/vmg/redcarpet/issues/396 will be corrected
37
+ fields = line.split(MARKER_BOLD) + (line.end_with?(MARKER_BOLD) ? [''] : [])
38
+ line = fields.map.with_index do |field, idx|
39
+ content = in_bold_text ? field.gsub('[b]', '').gsub('[/b]', '') : field
40
+ if idx == fields.size - 1
41
+ content
42
+ else
43
+ in_bold_text = !in_bold_text
44
+ "#{content}[#{in_bold_text ? '' : '/'}b]"
45
+ end
46
+ end.join
47
+ # Italic
48
+ if in_italic_text
49
+ line.gsub!(/#{Regexp.escape(MARKER_ITALIC)}(\S.*?)#{Regexp.escape(MARKER_ITALIC)}/, '[/i]\1[i]')
50
+ else
51
+ line.gsub!(/#{Regexp.escape(MARKER_ITALIC)}(\S.*?)#{Regexp.escape(MARKER_ITALIC)}/, '[i]\1[/i]')
52
+ end
53
+ if in_italic_text && line =~ /.*\S#{Regexp.escape(MARKER_ITALIC)}.*/
54
+ line.gsub!(/(.*\S)#{Regexp.escape(MARKER_ITALIC)}(.*)/, '\1[/i]\2')
55
+ in_italic_text = false
56
+ elsif !in_italic_text && line =~ /.*#{Regexp.escape(MARKER_ITALIC)}\S.*/
57
+ line.gsub!(/(.*)#{Regexp.escape(MARKER_ITALIC)}(\S.*)/, '\1[i]\2')
58
+ in_italic_text = true
59
+ end
60
+ line
61
+ end
62
+ end.join("\n").gsub('<br>', "\n")
63
+ end
64
+
65
+ def emphasis(text)
66
+ "[i]#{text}[/i]"
67
+ end
68
+
69
+ def double_emphasis(text)
70
+ # In case the text already contains bold tags (which can be the case as codespan uses them), remove them.
71
+ "[b]#{text.gsub('[b]', '').gsub('[/b]', '')}[/b]"
72
+ end
73
+
74
+ def link(link, title, content)
75
+ "[url=#{link}]#{content}[/url]"
76
+ end
77
+
78
+ def header(text, header_level)
79
+ case header_level
80
+ when 1
81
+ "\n[size=6][b]#{text}[/b][/size]\n\n"
82
+ when 2
83
+ "\n[size=6]#{text}[/size]\n\n"
84
+ when 3
85
+ "\n[size=5][b]#{text}[/b][/size]\n\n"
86
+ when 4
87
+ "\n[size=5]#{text}[/size]\n\n"
88
+ else
89
+ "\n[size=4][b]#{text}[/b][/size]\n\n"
90
+ end
91
+ end
92
+
93
+ def codespan(code)
94
+ "[b][font=Courier New]#{code.
95
+ gsub(MARKER_BOLD, '**').
96
+ gsub(MARKER_ITALIC, '*')
97
+ }[/font][/b]"
98
+ end
99
+
100
+ def image(link, title, alt_text)
101
+ "[img]#{link}[/img]"
102
+ end
103
+
104
+ def list(contents, list_type)
105
+ case list_type
106
+ when :ordered
107
+ "[list=1]\n#{contents}[/list]\n"
108
+ else
109
+ "[list]\n#{contents}[/list]\n"
110
+ end
111
+ end
112
+
113
+ def list_item(text, list_type)
114
+ # If the item spans multiple lines, prefix each new line with 2 spaces to keep the list running.
115
+ "[*]#{text.split(/(?<=\n)/).join(" ")}"
116
+ end
117
+
118
+ def block_code(code, language)
119
+ "[code]#{code.
120
+ gsub(MARKER_BOLD, '**').
121
+ gsub(MARKER_ITALIC, '*')
122
+ }[/code]\n"
123
+ end
124
+
125
+ end
126
+
127
+ end
@@ -1,5 +1,5 @@
1
1
  module MdToBbcode
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '2.0.0'
4
4
 
5
5
  end
data/spec/api_spec.rb CHANGED
@@ -1,25 +1,78 @@
1
1
  describe MdToBbcode do
2
2
 
3
+ it 'converts and keeps new lines' do
4
+ md = <<~EOS
5
+ Line 1
6
+
7
+ Line 2<br>
8
+ Line 3<br><br>
9
+ Line 4<br>
10
+
11
+
12
+ Line 5
13
+
14
+
15
+ Line 6
16
+ EOS
17
+ expect(md.md_to_bbcode).to eq(<<~EOS)
18
+ Line 1
19
+ Line 2
20
+
21
+ Line 3
22
+
23
+
24
+ Line 4
25
+
26
+ Line 5
27
+ Line 6
28
+ EOS
29
+ end
30
+
3
31
  it 'converts heading 1' do
4
- expect('# Heading 1'.md_to_bbcode).to eq '[size=6][b]Heading 1[/b][/size]'
32
+ expect('# Heading 1'.md_to_bbcode).to eq "\n[size=6][b]Heading 1[/b][/size]"
5
33
  end
6
34
 
7
35
  it 'converts heading 2' do
8
- expect('## Heading 2'.md_to_bbcode).to eq '[size=6]Heading 2[/size]'
36
+ expect('## Heading 2'.md_to_bbcode).to eq "\n[size=6]Heading 2[/size]"
9
37
  end
10
38
 
11
39
  it 'converts heading 3' do
12
- expect('### Heading 3'.md_to_bbcode).to eq '[size=5][b]Heading 3[/b][/size]'
40
+ expect('### Heading 3'.md_to_bbcode).to eq "\n[size=5][b]Heading 3[/b][/size]"
13
41
  end
14
42
 
15
43
  it 'converts heading 4' do
16
- expect('#### Heading 4'.md_to_bbcode).to eq '[size=5]Heading 4[/size]'
44
+ expect('#### Heading 4'.md_to_bbcode).to eq "\n[size=5]Heading 4[/size]"
17
45
  end
18
46
 
19
47
  it 'converts bold text' do
20
48
  expect('**Bold text**'.md_to_bbcode).to eq '[b]Bold text[/b]'
21
49
  end
22
50
 
51
+ it 'converts italic text' do
52
+ expect('*Italic text*'.md_to_bbcode).to eq '[i]Italic text[/i]'
53
+ end
54
+
55
+ it 'converts italic text with underscore' do
56
+ expect('_Italic text_'.md_to_bbcode).to eq '[i]Italic text[/i]'
57
+ end
58
+
59
+ it 'converts italic text after a list' do
60
+ md = <<~EOS
61
+ * List item
62
+ *Italic text*
63
+ EOS
64
+ expect(md.md_to_bbcode).to eq(<<~EOS)
65
+ [list]
66
+ [*]List item
67
+ [i]Italic text[/i]
68
+ [/list]
69
+ EOS
70
+ end
71
+
72
+ it 'converts italic and bold mixed text' do
73
+ expect('*Italic and **bold** text* and then **bold and *italic* text**'.md_to_bbcode).to eq '[i]Italic and [b]bold[/b] text[/i] and then [b]bold and [i]italic[/i] text[/b]'
74
+ end
75
+
23
76
  it 'converts links' do
24
77
  expect('[Link text](https://my.domain.com/path)'.md_to_bbcode).to eq '[url=https://my.domain.com/path]Link text[/url]'
25
78
  end
@@ -28,22 +81,129 @@ describe MdToBbcode do
28
81
  expect('`in-line code`'.md_to_bbcode).to eq '[b][font=Courier New]in-line code[/font][/b]'
29
82
  end
30
83
 
84
+ it 'converts inline code in bold text' do
85
+ expect('**Bold `in-line code` to display**'.md_to_bbcode).to eq '[b]Bold [font=Courier New]in-line code[/font] to display[/b]'
86
+ end
87
+
88
+ it 'converts inline code in multi-line bold text' do
89
+ md = <<~EOS
90
+ `Before in-line` followed by **bold with `some in-line` and
91
+ containing a multi-line `in-line code`
92
+ to display along with `in-line code` again** and `after as-well`.
93
+ EOS
94
+ expect(md.md_to_bbcode).to eq(<<~EOS)
95
+ [b][font=Courier New]Before in-line[/font][/b] followed by [b]bold with [font=Courier New]some in-line[/font] and
96
+ containing a multi-line [font=Courier New]in-line code[/font]
97
+ to display along with [font=Courier New]in-line code[/font] again[/b] and [b][font=Courier New]after as-well[/font][/b].
98
+ EOS
99
+ end
100
+
101
+ it 'converts inline code in italic text' do
102
+ expect('*Italic `in-line code` to display*'.md_to_bbcode).to eq '[i]Italic [b][font=Courier New]in-line code[/font][/b] to display[/i]'
103
+ end
104
+
105
+ it 'converts inline code in a multi-line bold text' do
106
+ md = <<~EOS
107
+ **Bold
108
+ `in-line code`
109
+ on
110
+ multi-line**
111
+ EOS
112
+ expect(md.md_to_bbcode).to eq(<<~EOS)
113
+ [b]Bold
114
+ [font=Courier New]in-line code[/font]
115
+ on
116
+ multi-line[/b]
117
+ EOS
118
+ end
119
+
120
+ it 'converts inline code in a multi-line italic text' do
121
+ md = <<~EOS
122
+ *Italic
123
+ `in-line code`
124
+ on
125
+ multi-line*
126
+ EOS
127
+ expect(md.md_to_bbcode).to eq(<<~EOS)
128
+ [i]Italic
129
+ [b][font=Courier New]in-line code[/font][/b]
130
+ on
131
+ multi-line[/i]
132
+ EOS
133
+ end
134
+
135
+ it 'converts bold text in links' do
136
+ expect("This is [a link with **bold** text](http://my.domain.com/path)".md_to_bbcode).to eq 'This is [url=http://my.domain.com/path]a link with [b]bold[/b] text[/url]'
137
+ end
138
+
139
+ it 'converts inline code in links' do
140
+ expect('This is [a link with `inline` code](http://my.domain.com/path)'.md_to_bbcode).to eq 'This is [url=http://my.domain.com/path]a link with [b][font=Courier New]inline[/font][/b] code[/url]'
141
+ end
142
+
143
+ it 'does not convert other formatting in inline code' do
144
+ expect('`# in-line *code* that is **not formatted**`'.md_to_bbcode).to eq '[b][font=Courier New]# in-line *code* that is **not formatted**[/font][/b]'
145
+ end
146
+
31
147
  it 'converts images' do
32
148
  expect('![Image text](https://my.domain.com/image.jpg)'.md_to_bbcode).to eq '[img]https://my.domain.com/image.jpg[/img]'
33
149
  end
34
150
 
35
151
  it 'converts bold text on several lines' do
36
152
  md = <<~EOS
37
- **Bold
38
- text
39
- on
40
- multi-line**
153
+ **Bold
154
+ text
155
+ on
156
+ multi-line**
157
+ EOS
158
+ expect(md.md_to_bbcode).to eq(<<~EOS)
159
+ [b]Bold
160
+ text
161
+ on
162
+ multi-line[/b]
163
+ EOS
164
+ end
165
+
166
+ it 'converts bold text on several lines mixed with single lines' do
167
+ md = <<~EOS
168
+ **Bold text** on **single and
169
+ on
170
+ multi-line** but **with** some **new
171
+ text
172
+ on** other **lines**
173
+ all
174
+ **mixed** up
175
+ EOS
176
+ expect(md.md_to_bbcode).to eq(<<~EOS)
177
+ [b]Bold text[/b] on [b]single and
178
+ on
179
+ multi-line[/b] but [b]with[/b] some [b]new
180
+ text
181
+ on[/b] other [b]lines[/b]
182
+ all
183
+ [b]mixed[/b] up
184
+ EOS
185
+ end
186
+
187
+ it 'converts bold and italic texts on several lines mixed with single lines' do
188
+ md = <<~EOS
189
+ **Bold text** on **single and
190
+ *on italic
191
+ words* on
192
+ multi-line** but **with** some **new
193
+ text
194
+ on** other **lines**
195
+ all *italic **and bold**
196
+ **mixed*** up
41
197
  EOS
42
198
  expect(md.md_to_bbcode).to eq(<<~EOS)
43
- [b]Bold
44
- text
45
- on
46
- multi-line[/b]
199
+ [b]Bold text[/b] on [b]single and
200
+ [i]on italic
201
+ words[/i] on
202
+ multi-line[/b] but [b]with[/b] some [b]new
203
+ text
204
+ on[/b] other [b]lines[/b]
205
+ all [i]italic [b]and bold[/b]
206
+ [b]mixed[/b][/i] up
47
207
  EOS
48
208
  end
49
209
 
@@ -62,6 +222,35 @@ describe MdToBbcode do
62
222
  EOS
63
223
  end
64
224
 
225
+ it 'converts different numbered lists separated with a blank line' do
226
+ md = <<~EOS
227
+ 1. List item 11
228
+ 2. List item 12
229
+ 3. List item 13
230
+ In list item
231
+
232
+ Not list
233
+
234
+ 1. List item 21
235
+ 2. List item 22
236
+ 3. List item 23
237
+ EOS
238
+ expect(md.md_to_bbcode).to eq(<<~EOS)
239
+ [list=1]
240
+ [*]List item 11
241
+ [*]List item 12
242
+ [*]List item 13
243
+ In list item
244
+ [/list]
245
+ Not list
246
+ [list=1]
247
+ [*]List item 21
248
+ [*]List item 22
249
+ [*]List item 23
250
+ [/list]
251
+ EOS
252
+ end
253
+
65
254
  it 'converts numbered list' do
66
255
  md = <<~EOS
67
256
  1. List item 1
@@ -77,25 +266,77 @@ describe MdToBbcode do
77
266
  EOS
78
267
  end
79
268
 
80
- it 'converts multi-line numbered list' do
269
+ it 'converts multi-line numbered list without indent' do
81
270
  md = <<~EOS
82
271
  1. List item 1
272
+ And another line 1
273
+ Again 1
274
+ 2. List item 2
275
+ And another line 2
276
+ Again 2
277
+ 3. List item 3
278
+ And another line 3
279
+ Again 3
280
+ EOS
281
+ expect(md.md_to_bbcode).to eq(<<~EOS)
282
+ [list=1]
283
+ [*]List item 1
83
284
  And another line 1
84
-
285
+ Again 1
286
+ [*]List item 2
287
+ And another line 2
288
+ Again 2
289
+ [*]List item 3
290
+ And another line 3
291
+ Again 3
292
+ [/list]
293
+ EOS
294
+ end
295
+
296
+ it 'converts multi-line numbered list with indent' do
297
+ md = <<~EOS
298
+ 1. List item 1
299
+ And another line 1
300
+ Again 1
85
301
  2. List item 2
86
302
  And another line 2
87
-
303
+ Again 2
88
304
  3. List item 3
89
305
  And another line 3
306
+ Again 3
90
307
  EOS
91
308
  expect(md.md_to_bbcode).to eq(<<~EOS)
92
309
  [list=1]
93
310
  [*]List item 1
94
311
  And another line 1
95
-
312
+ Again 1
96
313
  [*]List item 2
97
314
  And another line 2
98
-
315
+ Again 2
316
+ [*]List item 3
317
+ And another line 3
318
+ Again 3
319
+ [/list]
320
+ EOS
321
+ end
322
+
323
+ it 'converts multi-line numbered list with empty lines' do
324
+ md = <<~EOS
325
+ 1. List item 1
326
+ And another line 1<br>
327
+ 2. List item 2
328
+ And another line 2<br>
329
+ 3. List item 3
330
+ And another line 3
331
+ EOS
332
+ expect(md.md_to_bbcode).to eq(<<~EOS)
333
+ [list=1]
334
+ [*]List item 1
335
+ And another line 1
336
+
337
+ [*]List item 2
338
+ And another line 2
339
+
99
340
  [*]List item 3
100
341
  And another line 3
101
342
  [/list]
@@ -122,11 +363,90 @@ describe MdToBbcode do
122
363
  EOS
123
364
  end
124
365
 
366
+ it 'does not convert other items in code blocks' do
367
+ md = <<~EOS
368
+ ```ruby
369
+ # This is not a heading
370
+ def hello
371
+ puts 'Hello **world**'
372
+ puts `hostname`
373
+ end
374
+ # Call function
375
+ hello
376
+ puts '[This is a link](https://google.com)'
377
+ puts <<~EOS2
378
+ * And this
379
+ * is
380
+ * a list!
381
+ EOS2
382
+ $stdin.gets
383
+ ```
384
+ EOS
385
+ expect(md.md_to_bbcode).to eq(<<~EOS)
386
+ [code]# This is not a heading
387
+ def hello
388
+ puts 'Hello **world**'
389
+ puts `hostname`
390
+ end
391
+ # Call function
392
+ hello
393
+ puts '[This is a link](https://google.com)'
394
+ puts <<~EOS2
395
+ * And this
396
+ * is
397
+ * a list!
398
+ EOS2
399
+ $stdin.gets
400
+ [/code]
401
+ EOS
402
+ end
403
+
404
+ it 'converts multi-line list with code blocks' do
405
+ md = <<~EOS
406
+ 1. List item 1
407
+ ```ruby
408
+ puts 'This is code 1'
409
+ $stdin.gets
410
+ ```
411
+ 2. List item 2
412
+ ```ruby
413
+ puts 'This is code 2'
414
+ $stdin.gets
415
+ ```
416
+ 3. List item 3
417
+ ```ruby
418
+ puts 'This is code 3'
419
+ $stdin.gets
420
+ ```
421
+ EOS
422
+ expect(md.md_to_bbcode).to eq(<<~EOS)
423
+ [list=1]
424
+ [*]List item 1
425
+ [code]
426
+ puts 'This is code 1'
427
+ $stdin.gets
428
+ [/code]
429
+ [*]List item 2
430
+ [code]
431
+ puts 'This is code 2'
432
+ $stdin.gets
433
+ [/code]
434
+ [*]List item 3
435
+ [code]
436
+ puts 'This is code 3'
437
+ $stdin.gets
438
+ [/code]
439
+ [/list]
440
+ EOS
441
+ end
442
+
125
443
  it 'converts a whole text mixing markups' do
126
444
  md = <<~EOS
127
445
  # Heading h1
128
446
 
129
447
  Normal text
448
+ And another line<br>
449
+ with a line break
130
450
 
131
451
  **Bold text**
132
452
 
@@ -136,9 +456,10 @@ describe MdToBbcode do
136
456
  followed
137
457
  by** not bold on multi-lines.
138
458
 
139
- **Bold text including a [link to Google](https://www.google.com).**
459
+ **Bold text including an italic *[link to Google](https://www.google.com)*.**
140
460
 
141
461
  This is a bullet list:
462
+
142
463
  * Bullet item 1
143
464
  * Bullet item 2
144
465
  * Bullet item 3
@@ -148,6 +469,7 @@ describe MdToBbcode do
148
469
  Here is a link to [Google](https://www.google.com/) in a middle of a line.
149
470
 
150
471
  An inline code block `this is code` to be inserted.
472
+ And **another one in bold: `this is *code* that **is preformatted**` but written** in bold.
151
473
 
152
474
  ### Heading h3
153
475
 
@@ -165,14 +487,25 @@ describe MdToBbcode do
165
487
  ```
166
488
 
167
489
  This is a numbered list:
490
+
168
491
  1. Numbered item 1
169
492
  2. Numbered item 2
170
493
  3. Numbered item 3
171
494
 
495
+ Here is some Ruby:
496
+
497
+ ```ruby
498
+ puts 'Hello' * 3 * 5
499
+ # World
500
+ puts `echo World`
501
+ ```
502
+
172
503
  This is a numbered list with 1 item:
504
+
173
505
  1. Numbered item 1
174
506
 
175
507
  This is a numbered list multi-line:
508
+
176
509
  1. Numbered item 1
177
510
  Additional content 1
178
511
  2. Numbered item 2
@@ -187,39 +520,40 @@ describe MdToBbcode do
187
520
  And ending text
188
521
  EOS
189
522
  expect(md.md_to_bbcode).to eq(<<~EOS)
523
+
190
524
  [size=6][b]Heading h1[/b][/size]
191
-
525
+
192
526
  Normal text
193
-
527
+ And another line
528
+
529
+ with a line break
194
530
  [b]Bold text[/b]
195
-
196
531
  Not bold followed by [b]bold and followed by[/b] not bold.
197
-
198
532
  Not bold followed by [b]bold and
199
533
  followed
200
534
  by[/b] not bold on multi-lines.
201
-
202
- [b]Bold text including a [url=https://www.google.com]link to Google[/url].[/b]
203
-
535
+ [b]Bold text including an italic [i][url=https://www.google.com]link to Google[/url][/i].[/b]
204
536
  This is a bullet list:
205
537
  [list]
206
538
  [*]Bullet item 1
207
539
  [*]Bullet item 2
208
540
  [*]Bullet item 3
209
541
  [/list]
210
-
542
+
211
543
  [size=6]Heading h2[/size]
212
-
544
+
213
545
  Here is a link to [url=https://www.google.com/]Google[/url] in a middle of a line.
214
-
215
546
  An inline code block [b][font=Courier New]this is code[/font][/b] to be inserted.
216
-
547
+ And [b]another one in bold: [font=Courier New]this is *code* that **is preformatted**[/font] but written[/b] in bold.
548
+
217
549
  [size=5][b]Heading h3[/b][/size]
218
-
550
+
551
+
219
552
  [size=5]Heading h4 with [b][font=Courier New]embedded code[/font][/b][/size]
220
-
553
+
221
554
  Here is a code block in json:
222
- [code]{
555
+ [code]
556
+ {
223
557
  "my_json": {
224
558
  "is": "super",
225
559
  "great": "and",
@@ -227,33 +561,32 @@ describe MdToBbcode do
227
561
  }
228
562
  }
229
563
  [/code]
230
-
231
564
  This is a numbered list:
232
565
  [list=1]
233
566
  [*]Numbered item 1
234
567
  [*]Numbered item 2
235
568
  [*]Numbered item 3
236
569
  [/list]
237
-
570
+ Here is some Ruby:
571
+ [code]puts 'Hello' * 3 * 5
572
+ # World
573
+ puts `echo World`
574
+ [/code]
238
575
  This is a numbered list with 1 item:
239
576
  [list=1]
240
577
  [*]Numbered item 1
241
578
  [/list]
242
-
243
579
  This is a numbered list multi-line:
244
580
  [list=1]
245
581
  [*]Numbered item 1
246
582
  Additional content 1
247
583
  [*]Numbered item 2
248
584
  Additional content 2
249
-
250
585
  [*]Numbered item 3
251
586
  Additional content 3
252
587
  [/list]
253
-
254
588
  Here is an inserted image:
255
589
  [img]https://x-aeon.com/muriel.jpg[/img]
256
-
257
590
  And ending text
258
591
  EOS
259
592
  end
File without changes
data/spec/spec_helper.rb CHANGED
@@ -46,6 +46,10 @@ RSpec.configure do |config|
46
46
  # triggering implicit auto-inclusion in groups with matching metadata.
47
47
  config.shared_context_metadata_behavior = :apply_to_host_groups
48
48
 
49
+ config.expect_with :rspec do |c|
50
+ c.max_formatted_output_length = 10_000
51
+ end
52
+
49
53
  # The settings below are suggested to provide a good initial experience
50
54
  # with RSpec, but feel free to customize to your heart's content.
51
55
  =begin
metadata CHANGED
@@ -1,29 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md_to_bbcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Muriel Salvan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-20 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redcarpet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.5'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - ">="
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sem_ver_components
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
18
46
  - !ruby/object:Gem::Version
19
- version: '0'
47
+ version: '0.0'
20
48
  type: :development
21
49
  prerelease: false
22
50
  version_requirements: !ruby/object:Gem::Requirement
23
51
  requirements:
24
- - - ">="
52
+ - - "~>"
25
53
  - !ruby/object:Gem::Version
26
- version: '0'
54
+ version: '0.0'
27
55
  description: Provides an API and an executable to convert Markdown text to BBCode
28
56
  format
29
57
  email:
@@ -35,6 +63,7 @@ extra_rdoc_files: []
35
63
  files:
36
64
  - bin/md_to_bbcode
37
65
  - lib/md_to_bbcode.rb
66
+ - lib/md_to_bbcode/bbcode_renderer.rb
38
67
  - lib/md_to_bbcode/core_extensions/string/md_to_bbcode.rb
39
68
  - lib/md_to_bbcode/version.rb
40
69
  - spec/api_spec.rb
@@ -59,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
88
  - !ruby/object:Gem::Version
60
89
  version: '0'
61
90
  requirements: []
62
- rubygems_version: 3.1.2
91
+ rubygems_version: 3.2.3
63
92
  signing_key:
64
93
  specification_version: 4
65
94
  summary: Convert Markdown text to Bbcode