zfben_extend 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,11 +12,16 @@ A tool to extend ruby string.
12
12
 
13
13
  ```ruby
14
14
  ZfbenExtend::String.to_html('text http://link #tag mail@mail.com @user text', tag_url: '/tagurl/', user_url: '/userurl/')
15
-
16
15
  # => 'text <a href="http://link">http://link</a> <a href="/tagurl/tag">#tag</a> <a href="mailto:mail@mail.com">mail@mail.com</a> <a href="/userurl/user">@user</a> text'
17
16
 
18
17
  ZfbenExtend::String.to_cnbr('a.b', chars: /\./, br: '<hr />')
19
18
  # => 'a.<hr />b'
19
+
20
+ ZfbenExtend::String.markdown(' a')
21
+ # => <div class=\"CodeRay\">\n <div class=\"code\"><pre>a\n</pre></div>\n</div>\n
22
+
23
+ ZfbenExtend::String.markdown("```ruby\n a\n```")
24
+ # => <div class=\"CodeRay\">\n <div class=\"code\"><pre> a\n</pre></div>\n</div>\n
20
25
  ```
21
26
 
22
27
  See more at https://github.com/benz303/zfben_extend/blob/master/test/string_test.rb
@@ -1,4 +1,7 @@
1
1
  # encoding: UTF-8
2
+ require 'redcarpet'
3
+ require 'coderay'
4
+
2
5
  class ZfbenExtend::String < String
3
6
  def self.to_html text, options={}
4
7
  options = {
@@ -77,4 +80,28 @@ class ZfbenExtend::String < String
77
80
  def to_cnbr options = {}
78
81
  self.class.to_cnbr self, options
79
82
  end
83
+
84
+ def self.markdown text, options = {}
85
+ options = {
86
+ autolink: true,
87
+ space_after_headers: true,
88
+ fenced_code_blocks: true,
89
+ no_intra_emphasis: true,
90
+ hard_wrap: true,
91
+ strikethrough: true
92
+ }.merge(options)
93
+ markdown = Redcarpet::Markdown.new(ZfbenExtend::HTMLwithCodeRay, options)
94
+ self.new markdown.render(text)
95
+ end
96
+
97
+ def markdown options = {}
98
+ self.class.markdown self, options
99
+ end
100
+ end
101
+
102
+ class ZfbenExtend::HTMLwithCodeRay < Redcarpet::Render::HTML
103
+ def block_code code, lang
104
+ lang = lang.nil? ? :ruby : lang.to_sym
105
+ CodeRay.scan(code, lang).div
106
+ end
80
107
  end
data/test/string_test.rb CHANGED
@@ -82,3 +82,19 @@ class TestToCnBr < Test::Unit::TestCase
82
82
  assert_equal ZfbenExtend::String.new('a.b').to_cnbr(chars: /\./, br: '<hr />'), 'a.<hr />b'
83
83
  end
84
84
  end
85
+
86
+ class TestMarkdown < Test::Unit::TestCase
87
+ def test_base
88
+ assert_equal ZfbenExtend::String.markdown('a'), "<p>a</p>\n"
89
+ assert_equal ZfbenExtend::String.markdown("a\na"), "<p>a\na</p>\n"
90
+ assert_equal ZfbenExtend::String.markdown("a\n\na"), "<p>a</p>\n\n<p>a</p>\n"
91
+ assert_equal ZfbenExtend::String.markdown(' a'), "<div class=\"CodeRay\">\n <div class=\"code\"><pre>a\n</pre></div>\n</div>\n"
92
+ assert_equal ZfbenExtend::String.markdown("```ruby\n a\n```"), "<div class=\"CodeRay\">\n <div class=\"code\"><pre> a\n</pre></div>\n</div>\n"
93
+
94
+ assert_equal ZfbenExtend::String.new('a').markdown, "<p>a</p>\n"
95
+ assert_equal ZfbenExtend::String.new("a\na").markdown, "<p>a\na</p>\n"
96
+ assert_equal ZfbenExtend::String.new("a\n\na").markdown, "<p>a</p>\n\n<p>a</p>\n"
97
+ assert_equal ZfbenExtend::String.new(' a').markdown, "<div class=\"CodeRay\">\n <div class=\"code\"><pre>a\n</pre></div>\n</div>\n"
98
+ assert_equal ZfbenExtend::String.new("```ruby\n a\n```").markdown, "<div class=\"CodeRay\">\n <div class=\"code\"><pre> a\n</pre></div>\n</div>\n"
99
+ end
100
+ end
data/zfben_extend.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'zfben_extend'
6
- s.version = '0.0.3'
6
+ s.version = '0.0.4'
7
7
  s.authors = ['Ben']
8
8
  s.email = ['ben@zfben.com']
9
9
  s.homepage = 'https://github.com/benz303/zfben_extend'
@@ -14,4 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
15
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
16
  s.require_paths = ['lib']
17
+
18
+ s.add_dependency 'redcarpet'
19
+ s.add_dependency 'coderay'
17
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zfben_extend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-01 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redcarpet
16
+ requirement: &22465880 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *22465880
25
+ - !ruby/object:Gem::Dependency
26
+ name: coderay
27
+ requirement: &22465300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *22465300
14
36
  description: ''
15
37
  email:
16
38
  - ben@zfben.com