ubbparser 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +8 -8
  2. data/lib/ubbparser.rb +105 -85
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODA4MjgzN2VjNDlhMGE4OWIzOWE0MTJhMGJkNzRkNzgzZmM3OTIwOQ==
4
+ NjIzZDRkMzFhMTE1YzdjN2EzMzhhZjYwMzBkNDQzZWFjNGQ3OGU1Zg==
5
5
  data.tar.gz: !binary |-
6
- NWFmZjJlMjFmMjZkMDA2NTljMGVlYjJmYjZkNGMzMmUyYWNkMWZjMA==
6
+ NmIyNzE0ZTA1NWZiNzliOTE2YWQ1ODQ3Zjk0ODI0OWIzZTRjM2Y2NQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NzI0OTZmMGEyZGIxNmE5Y2Y3ZTk0ZGQ2NjY1YTVhMTlkOGMzODhiYjNkNThj
10
- ZTY0NjZhNWU5YjljNTc1MDhlOWY4Y2M2YmU4NWQ1NmQ2NGU4YjA2YTYxZmVl
11
- YzlkOGI3ZTI3YWEwMzliYzE2OWJjNDg5NzExMDZkYmY1Zjg3OGM=
9
+ MjVjNWZlMDM1YTczMGE5ZWNmMGY2M2RiN2Y4ZTFlZDk0YjU3YTliMzZiNThk
10
+ ZjRhYTdkNzQyNzc2ZTgxNDE3YWYyMjg4M2JlZGQ2MzRkZDMyMmUyNjQyZWJk
11
+ MTZkYzY5NWQ1Y2Y1YzE2YzY2OTAyNDI5YWI1ZjYxNjIyZWIzNzU=
12
12
  data.tar.gz: !binary |-
13
- NzFkZWU4OGI1OWFiZWNjMDZmMGUwODJmOWE2NDY1ODA1OWUxMGFmNDdlMGZm
14
- YjVkMjE5ZGFjYmVlMzZjYjE1MjA0OGQyNzVkZTc2YjQyZDk2ODIxZTI1YWQx
15
- NTdkMDA2Yzk5MmMzYjk1ZWUwYjRmNTlmYjgwYjRiM2U5ZDU2YmM=
13
+ ZjdmOTA2NzA5Y2MwM2MzNzk0MjJmMjI3Y2YzODJkN2I4Yjc3YmI4YTI3NTI3
14
+ YzY2NmNmZTI1MDM5NDRkNzMwZmI1Y2ZkZTk2MzQ2MmQ4ODZlMjUyYzRhOGU0
15
+ ZDhjMDJlYTRlODQ5OWE1MzNmNzE0NzVmZDc3ZGI3MTY4YTFlODM=
data/lib/ubbparser.rb CHANGED
@@ -40,7 +40,7 @@ module UBBParser
40
40
  # Converts a strings containing key-value-list into a hash. This function is mostly used by the parser itself.
41
41
  # Attributes are given to the render methods as a hash.
42
42
  def self.attrib_str_to_hash(attrib_str)
43
- result = {:original_attrib_str => attrib_str.gsub(/^=/, '')}
43
+ result = {:class_attrib_str => attrib_str.gsub(/^=/, '')}
44
44
 
45
45
  attrib_str.insert(0, 'default') if (attrib_str[0] == '=')
46
46
  attrib_str.scan(/((\S*)=("[^"]*"|'[^']*'|\S*))/) { |_, key, val|
@@ -108,6 +108,7 @@ module UBBParser
108
108
  end
109
109
  method_result = self.send(method_name, inner_text, attributes, parse_options).to_s
110
110
  result << method_result
111
+ scnr.skip(/\n?/)
111
112
  end
112
113
  end
113
114
  end
@@ -176,6 +177,13 @@ module UBBParser
176
177
  "<div style='text-align: center'>#{self.parse(inner_text, parse_options)}</div>"
177
178
  end
178
179
 
180
+ # Assures the inner_text is rendered below floating elements.
181
+ # :category: Render methods
182
+ def self.render_class(inner_text, attributes = {}, parse_options = {})
183
+ classes = attributes[:original_attrib_str].gsub(/'/, "\\'")
184
+ "<div class='#{classes}'>#{self.parse(inner_text, parse_options)}</div>"
185
+ end
186
+
179
187
  # Assures the inner_text is rendered below floating elements.
180
188
  # :category: Render methods
181
189
  def self.render_clear(inner_text, attributes = {}, parse_options = {})
@@ -185,7 +193,8 @@ module UBBParser
185
193
  # Changes the font color of the inner_text
186
194
  # :category: Render methods
187
195
  def self.render_color(inner_text, attributes = {}, parse_options = {})
188
- "<div style='color:#{attributes[:default]}'>#{self.parse(inner_text, parse_options)}</div>"
196
+ color = attributes[:default].gsub(/'/, "\\'")
197
+ "<div style='color:#{color}'>#{self.parse(inner_text, parse_options)}</div>"
189
198
  end
190
199
 
191
200
  # Ignores all the inner_text
@@ -240,71 +249,81 @@ module UBBParser
240
249
  # By default the email address is protected against spoofing, using JavaScript. Use the email parse option to define html classes.
241
250
  # :category: Render methods
242
251
  def self.render_email(inner_text, attributes = {}, parse_options = {})
243
- email = (attributes[:default] || inner_text)
244
- css_class = parse_options[:class_email] || 'ubb-email'
245
- if !self.is_email?(email)
246
- result = "<span class='#{css_class} ubbparser-error'>UBB error: invalid email address #{email}</span>"
247
- elsif parse_options.has_key?(:protect_email) && !parse_options[:protect_email]
248
- result = "<a href='mailto:#{email}' class='#{css_class}'>#{inner_text}</a>"
249
- else
250
- username, domain = email.split('@', 2)
251
- id = 'ubb-email-' + SecureRandom.hex(16)
252
-
253
- # Some generic javascript so every browser can parse this (instantly), regardless of used framework
254
- if inner_text == email
255
- title = 'Protected email address'
256
- js_title = 'email'
257
- else
252
+ css_class = (parse_options[:class_email] || 'ubb-email').to_s.strip
253
+ inner_text = self.parse(inner_text, parse_options) if !attributes[:default].nil?
254
+ email = (attributes[:default] || inner_text)
255
+ if (!self.is_email?(email))
256
+ result = "<span class='#{css_class} ubbparser-error'>UBB error: invalid email address #{email}</span>"
257
+ elsif ((parse_options.has_key?(:protect_email) && !parse_options[:protect_email]) || (attributes[:protected] == "false"))
258
+ result = "<a href='mailto:#{email}' class='#{css_class}'>#{inner_text}</a>"
259
+ else
260
+ username, domain = email.split("@", 2)
261
+ id = "ubb-email-" + SecureRandom.hex(16)
262
+
263
+ # Some generic javascript so every browser can parse this (instantly), regardless of used framework
264
+ if (inner_text == email)
265
+ title = "Protected email address"
266
+ js_title = "email"
267
+ else
258
268
  title = inner_text
259
269
  js_title = "\"#{inner_text}\""
260
270
  end
261
271
  script = "<script type='text/javascript'>obj=document.getElementById(\"#{id}\");email=obj.getAttribute(\"data-username\")+\"@\"+obj.getAttribute(\"data-domain\");obj.href=\"mailto:\"+email;obj.innerHTML=#{js_title}</script>"
262
272
  result = "<a id='#{id}' class='#{css_class}' href='#' data-username='#{username}' data-domain='#{domain}'>#{title}</a>#{script}"
263
273
  end
264
-
265
274
  return result
266
275
  end
267
276
 
268
- # Renders the inner_text in the specified list. The list should contain CSS style font-families, i.e.:
269
- # [font=Arial, Helvetica, Sans]...[/font]
270
- # :category: Render methods
271
- def self.render_font(inner_text, attributes = {}, parse_options = {})
272
- font = attributes[:original_attrib_str].gsub!(/\\|'/) { |c| "\\#{c}" }
273
- "<span style='font-family: #{font}'>#{self.parse(inner_text, parse_options)}</span>"
274
- end
275
-
276
- # Renders the inner_text in a H1 heading.
277
- # :category: Render methods
277
+ # Renders the inner_text in a H1 heading.
278
+ # :category: Render methods
278
279
  def self.render_h1(inner_text, attributes = {}, parse_options = {})
279
280
  "<h1>#{self.parse(inner_text, parse_options)}</h1>"
280
281
  end
281
282
 
282
- # Renders the inner_text in a H2 heading.
283
- # :category: Render methods
283
+ # Renders the inner_text in a H2 heading.
284
+ # :category: Render methods
284
285
  def self.render_h2(inner_text, attributes = {}, parse_options = {})
285
286
  "<h2>#{self.parse(inner_text, parse_options)}</h2>"
286
287
  end
287
288
 
288
- # Renders the inner_text in a H3 heading.
289
- # :category: Render methods
289
+ # Renders the inner_text in a H3 heading.
290
+ # :category: Render methods
290
291
  def self.render_h3(inner_text, attributes = {}, parse_options = {})
291
292
  "<h3>#{self.parse(inner_text, parse_options)}</h3>"
292
293
  end
293
294
 
294
- # Renders a horizontal ruler.
295
- # :category: Render methods
295
+ # Renders the inner_text in a H4 heading.
296
+ # :category: Render methods
297
+ def self.render_h3(inner_text, attributes = {}, parse_options = {})
298
+ "<h4>#{self.parse(inner_text, parse_options)}</h4>"
299
+ end
300
+
301
+ # Renders the inner_text in a H5 heading.
302
+ # :category: Render methods
303
+ def self.render_h3(inner_text, attributes = {}, parse_options = {})
304
+ "<h3>#{self.parse(inner_text, parse_options)}</h5>"
305
+ end
306
+
307
+ # Renders the inner_text in a H6 heading.
308
+ # :category: Render methods
309
+ def self.render_h3(inner_text, attributes = {}, parse_options = {})
310
+ "<h6>#{self.parse(inner_text, parse_options)}</h6>"
311
+ end
312
+
313
+ # Renders a horizontal ruler.
314
+ # :category: Render methods
296
315
  def self.render_hr(inner_text, attributes = {}, parse_options = {})
297
316
  "<hr />#{self.parse(inner_text, parse_options)}"
298
317
  end
299
318
 
300
- # Renders the inner_text in italic.
301
- # :category: Render methods
319
+ # Renders the inner_text in italic.
320
+ # :category: Render methods
302
321
  def self.render_i(inner_text, attributes = {}, parse_options = {})
303
322
  "<em>#{self.parse(inner_text, parse_options)}</em>"
304
323
  end
305
324
 
306
- # Renders an iframe. Use the inner_text as source. Use the :class_iframe parse option to define html classes.
307
- # :category: Render methods
325
+ # Renders an iframe. Use the inner_text as source. Use the :class_iframe parse option to define html classes.
326
+ # :category: Render methods
308
327
  def self.render_iframe(inner_text, attributes = {}, parse_options = {})
309
328
  src = inner_text
310
329
  src = 'http://' + src if (src.match(/^www\./))
@@ -315,15 +334,15 @@ module UBBParser
315
334
  return "<iframe #{attrib_str}></iframe>"
316
335
  end
317
336
 
318
- # Doesn't render the ubb code in the inner_text. It does strip all html-tags from the inner_text
319
- # :category: Render methods
337
+ # Doesn't render the ubb code in the inner_text. It does strip all html-tags from the inner_text
338
+ # :category: Render methods
320
339
  def self.render_ignore(inner_text, attributes = {}, parse_options = {})
321
340
  inner_text
322
341
  end
323
342
 
324
- # Renders an image. Use the :class_img parse option to define html classes.
325
- # :category: Render methods
326
- #noinspection RubyClassVariableUsageInspection
343
+ # Renders an image. Use the :class_img parse option to define html classes.
344
+ # :category: Render methods
345
+ #noinspection RubyClassVariableUsageInspection
327
346
  def self.render_img(inner_text, attributes = {}, parse_options = {})
328
347
  url = inner_text
329
348
  url = @@file_url_convert_method.call(url) unless @@file_url_convert_method.nil?
@@ -335,36 +354,36 @@ module UBBParser
335
354
  return "<img #{attrib_str} />"
336
355
  end
337
356
 
338
- # Renders an image, floated on the left side of the text frame. Use the :class_img_left parse option to define html classes.
339
- # :category: Render methods
357
+ # Renders an image, floated on the left side of the text frame. Use the :class_img_left parse option to define html classes.
358
+ # :category: Render methods
340
359
  def self.render_img_left(inner_text, attributes = {}, parse_options = {})
341
360
  attributes[:styles] = 'float: left; margin: 0px 10px 10px 0px'
342
361
  attributes[:class] = parse_options[:class_img_left] || 'ubb-img-left'
343
362
  render_img(inner_text, attributes, parse_options)
344
363
  end
345
364
 
346
- # Renders an image, floated on the right side of the text frame. Use the :class_img_right parse option to define html classes.
347
- # :category: Render methods
365
+ # Renders an image, floated on the right side of the text frame. Use the :class_img_right parse option to define html classes.
366
+ # :category: Render methods
348
367
  def self.render_img_right(inner_text, attributes = {}, parse_options = {})
349
368
  attributes[:styles] = 'float: left; margin: 0px 0px 10px 10px'
350
369
  attributes[:class] = parse_options[:class_img_right] || 'ubb-img-right'
351
370
  render_img(inner_text, attributes, parse_options)
352
371
  end
353
372
 
354
- # Renders the inner_text with a justified text alignment.
355
- # :category: Render methods
373
+ # Renders the inner_text with a justified text alignment.
374
+ # :category: Render methods
356
375
  def self.render_justify(inner_text, attributes = {}, parse_options = {})
357
376
  "<div style='text-align: justify'>#{self.parse(inner_text, parse_options)}</div>"
358
377
  end
359
378
 
360
- # Renders the inner_text with a left text alignment.
361
- # :category: Render methods
379
+ # Renders the inner_text with a left text alignment.
380
+ # :category: Render methods
362
381
  def self.render_left(inner_text, attributes = {}, parse_options = {})
363
382
  "<div style='text-align: left'>#{self.parse(inner_text, parse_options)}</div>"
364
383
  end
365
384
 
366
- # Renders the inner_text as an ordered list. Each line represents a list item. Use the :class_list parse option to define html classes.
367
- # :category: Render methods
385
+ # Renders the inner_text as an ordered list. Each line represents a list item. Use the :class_list parse option to define html classes.
386
+ # :category: Render methods
368
387
  def self.render_list(inner_text, attributes = {}, parse_options = {})
369
388
  items = inner_text.split(/\n/)
370
389
  items.delete_if { |item| item.strip == '' }
@@ -372,65 +391,66 @@ module UBBParser
372
391
  return (items.empty?) ? '' : "<ol class='#{parse_options[:class_list].to_s.strip}'>" + items.join('') + '</ol>'
373
392
  end
374
393
 
375
- # Renders the inner_text as a paragraph. Use the :class_p parse option to define html classes.
376
- # :category: Render methods
394
+ # Renders the inner_text as a paragraph. Use the :class_p parse option to define html classes.
395
+ # :category: Render methods
377
396
  def self.render_p(inner_text, attributes = {}, parse_options = {})
378
397
  css_class = parse_options[:class_p] || 'ubb-p'
379
398
  "<p class='#{css_class}'>#{self.parse(inner_text, parse_options)}</p>"
380
399
  end
381
400
 
382
- # Renders the inner_text with a right text alignment.
383
- # :category: Render methods
401
+ # Renders the inner_text with a right text alignment.
402
+ # :category: Render methods
384
403
  def self.render_right(inner_text, attributes = {}, parse_options = {})
385
404
  "<div style='text-align: right'>#{self.parse(inner_text, parse_options)}</div>"
386
405
  end
387
406
 
388
- # Renders the inner_text in a <div> block with inline CSS styles, i.e.:
389
- # [style color: red; border: 1px solid green]...[/style]
390
- # :category: Render methods
407
+ # Renders the inner_text in a <div> block with inline CSS styles, i.e.:
408
+ # [style color: red; border: 1px solid green]...[/style]
409
+ # :category: Render methods
391
410
  def self.render_style(inner_text, attributes = {}, parse_options = {})
392
411
  styles = attributes[:original_attrib_str].gsub(/'/, "\\'")
393
412
  "<div style='#{styles}'>#{self.parse(inner_text, parse_options)}</div>"
394
413
  end
395
414
 
396
- # Converts the [table] to a <table>. Always use this in combination with [tr] and [td] or [th]. Use the :class_table parse option to define html classes.
397
- # :category: Render methods
415
+ # Converts the [table] to a <table>. Always use this in combination with [tr] and [td] or [th]. Use the :class_table parse option to define html classes.
416
+ # :category: Render methods
398
417
  def self.render_table(inner_text, attributes = {}, parse_options = {})
399
418
  css_class = parse_options[:class_table] || 'ubb-table'
400
419
  "<table class='#{css_class}'>#{self.parse(inner_text.gsub(/(^\n+)|(\n+$)/, ''), parse_options)}</table>"
401
420
  end
402
421
 
403
- # Converts the [td] to a <td>. Always use this in combination with [table] and [tr].
404
- # :category: Render methods
422
+ # Converts the [td] to a <td>. Always use this in combination with [table] and [tr].
423
+ # :category: Render methods
405
424
  def self.render_td(inner_text, attributes = {}, parse_options = {})
406
425
  "<td>#{self.parse(inner_text, parse_options)}</td>"
407
426
  end
408
427
 
409
- # Converts the [th] to a <th>. Always use this in combination with [table] and [tr].
410
- # :category: Render methods
428
+ # Converts the [th] to a <th>. Always use this in combination with [table] and [tr].
429
+ # :category: Render methods
411
430
  def self.render_th(inner_text, attributes = {}, parse_options = {})
412
431
  "<th>#{self.parse(inner_text, parse_options)}</th>"
413
432
  end
414
433
 
415
- # Converts the [tr] to a <tr>. Always use this in combination with [table] and [td] or [th].
416
- # :category: Render methods
434
+ # Converts the [tr] to a <tr>. Always use this in combination with [table] and [td] or [th].
435
+ # :category: Render methods
417
436
  def self.render_tr(inner_text, attributes = {}, parse_options = {})
418
437
  "<tr>#{self.parse(inner_text.gsub(/(^\n+)|(\n+$)/, ''), parse_options)}</tr>"
419
438
  end
420
439
 
421
- # Renders the inner_text underline. Use this with caution, since underline text is associated with hyperlinks.
422
- # :category: Render methods
440
+ # Renders the inner_text underline. Use this with caution, since underline text is associated with hyperlinks.
441
+ # :category: Render methods
423
442
  def self.render_u(inner_text, attributes = {}, parse_options = {})
424
443
  "<u>#{self.parse(inner_text, parse_options)}</u>"
425
444
  end
426
445
 
427
- # Renders a web addres. There are two options to define:
428
- # [url]www.osingasoftware.nl[/ur]
429
- # [url=www.osingasoftware.nl]Osinga Software[/url]
430
- # Use the :class_url parse option to define html classes.
431
- # :category: Render methods
432
- #noinspection RubyClassVariableUsageInspection
446
+ # Renders a web addres. There are two options to define:
447
+ # [url]www.osingasoftware.nl[/ur]
448
+ # [url=www.osingasoftware.nl]Osinga Software[/url]
449
+ # Use the :class_url parse option to define html classes.
450
+ # :category: Render methods
451
+ #noinspection RubyClassVariableUsageInspection
433
452
  def self.render_url(inner_text, attributes = {}, parse_options = {})
453
+ inner_text = self.parse(inner_text, parse_options) if !attributes[:default].nil?
434
454
  url = (attributes[:default] || inner_text)
435
455
  url = 'http://' + url if (url.match(/^www\./))
436
456
  url = @@file_url_convert_method.call(url) unless @@file_url_convert_method.nil?
@@ -439,8 +459,8 @@ module UBBParser
439
459
  return "<a href='#{url}' class='#{css_class}'>#{inner_text}</a>"
440
460
  end
441
461
 
442
- # Renders a YouTube video using the video id or url in the inner_text.
443
- # :category: Render methods
462
+ # Renders a YouTube video using the video id or url in the inner_text.
463
+ # :category: Render methods
444
464
  def self.render_vimeo(inner_text, attributes = {}, parse_options = {})
445
465
  attributes[:width] ||= 500
446
466
  attributes[:height] ||= 281
@@ -450,8 +470,8 @@ module UBBParser
450
470
  return render_iframe(src, attributes, parse_options)
451
471
  end
452
472
 
453
- # Renders a YouTube video using the video id or url in the inner_text.
454
- # :category: Render methods
473
+ # Renders a YouTube video using the video id or url in the inner_text.
474
+ # :category: Render methods
455
475
  def self.render_youtube(inner_text, attributes = {}, parse_options = {})
456
476
  attributes[:width] ||= 560
457
477
  attributes[:height] ||= 315
@@ -461,9 +481,9 @@ module UBBParser
461
481
  return render_iframe(src, attributes, parse_options)
462
482
  end
463
483
 
464
- # Renders a Youtube, Vimeo or Zideo video using the video id or url in the inner_text.
465
- # It automatically determines which video renderer should be used based on the given url.
466
- # :category: Render methods
484
+ # Renders a Youtube, Vimeo or Zideo video using the video id or url in the inner_text.
485
+ # It automatically determines which video renderer should be used based on the given url.
486
+ # :category: Render methods
467
487
  def self.render_video(inner_text, attributes = {}, parse_options = {})
468
488
  attributes[:class] = "#{attributes[:class]} #{parse_options[:class_zideo]}"
469
489
  url = inner_text
@@ -478,8 +498,8 @@ module UBBParser
478
498
  end
479
499
  end
480
500
 
481
- # Renders a zideo.nl video using the video id or url in the inner_text.
482
- # :category: Render methods
501
+ # Renders a zideo.nl video using the video id or url in the inner_text.
502
+ # :category: Render methods
483
503
  def self.render_zideo(inner_text, attributes = {}, parse_options = {})
484
504
  attributes[:width] ||= 480
485
505
  attributes[:height] ||= :auto
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ubbparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taco Jan Osinga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-22 00:00:00.000000000 Z
11
+ date: 2014-02-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple and flexibel ubb parser.
14
14
  email: info@osingasoftware.nl