md_emoji 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,26 +1,38 @@
1
1
  module MdEmoji
2
2
  class Render < Redcarpet::Render::HTML
3
- def normal_text(text)
4
- if include_emoji?(text)
5
- replace_emoji(text)
3
+ def preprocess(document)
4
+ if include_emoji?(document)
5
+ replace_emoji(document)
6
6
  else
7
- text
7
+ document
8
8
  end
9
9
  end
10
10
 
11
+ def block_code(text, lang)
12
+ "<pre><code#{%{ class="#{lang}"} if lang}>#{revert_emoji(text)}</code></pre>"
13
+ end
14
+
15
+ def codespan(text)
16
+ "<code>#{revert_emoji(text)}</code>"
17
+ end
18
+
11
19
  # Replaces valid emoji characters, ie :smile:, with img tags
12
20
  #
13
21
  # Valid emoji charaters are listed in +MdEmoji::EMOJI+
14
22
  def replace_emoji(text)
15
23
  text.gsub(/:(\S+):/) do |emoji|
16
- if MdEmoji::EMOJI.include?($1)
17
- file_name = "#{$1.gsub('+', 'plus')}.png"
24
+
25
+ emoji_code = emoji #.gsub("|", "_")
26
+ emoji = emoji_code.gsub(":", "")
27
+
28
+ if MdEmoji::EMOJI.include?(emoji)
29
+ file_name = "#{emoji.gsub('+', 'plus')}.png"
18
30
  default_size = %{height="20" width="20"}
19
31
 
20
32
  %{<img src="/assets/emojis/#{file_name}" class="emoji" } +
21
- %{title="#{emoji}" alt="#{emoji}" #{default_size}>}
33
+ %{title="#{emoji_code}" alt="#{emoji_code}" #{default_size}>}
22
34
  else
23
- emoji
35
+ emoji_code
24
36
  end
25
37
  end
26
38
  end
@@ -31,5 +43,12 @@ module MdEmoji
31
43
  def include_emoji?(text)
32
44
  text && text[/:\S+:/]
33
45
  end
46
+
47
+ # Replaces emoji images with plain text
48
+ def revert_emoji(text)
49
+ text.gsub(/<[^>]+class="emoji"\stitle="(:\S+:)"[^>]+>/) do |emoji|
50
+ ":#{$1}:"
51
+ end
52
+ end
34
53
  end
35
54
  end
@@ -1,3 +1,3 @@
1
1
  module MdEmoji
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -3,7 +3,8 @@ require 'test_helper'
3
3
  class MdEmojiTest < ActiveSupport::TestCase
4
4
 
5
5
  def setup
6
- @markdown = Redcarpet::Markdown.new(MdEmoji::Render)
6
+ @markdown = Redcarpet::Markdown.new(MdEmoji::Render,
7
+ :fenced_code_blocks => true)
7
8
  end
8
9
 
9
10
  test "adds emoji to text when the emoji exists" do
@@ -22,6 +23,14 @@ class MdEmojiTest < ActiveSupport::TestCase
22
23
  assert_emoji 'plus1', parsed_text
23
24
  end
24
25
 
26
+ test "works on emoji with underscores" do
27
+ text = "This is tense :sweat_drops:"
28
+
29
+ parsed_text = @markdown.render(text)
30
+
31
+ assert_emoji 'sweat_drops', parsed_text
32
+ end
33
+
25
34
  test "skips emoji which aren't supported" do
26
35
  text = "Hello :jordan_byron: world!"
27
36
 
@@ -31,8 +40,6 @@ class MdEmojiTest < ActiveSupport::TestCase
31
40
  end
32
41
 
33
42
  test "works with autolink enabled" do
34
- skip "autolink messes everything up :("
35
-
36
43
  @markdown = Redcarpet::Markdown.new(MdEmoji::Render, :autolink => true)
37
44
 
38
45
  text = ":wink2: http://www.jordanbyron.com"
@@ -44,13 +51,16 @@ class MdEmojiTest < ActiveSupport::TestCase
44
51
  end
45
52
 
46
53
  test "does not render emoji in codeblocks" do
47
- text = %{Look at my codez
48
- ```
49
- def hello
50
- :wink:
51
- end
52
- ```
53
- Everything is ok :smile:}
54
+ text = %{```ruby
55
+ def hello
56
+ :wink:
57
+ end
58
+ ```
59
+ Everything is ok :smile: `:wink2`
60
+
61
+ ```
62
+ :fireengine:
63
+ ```}
54
64
 
55
65
  parsed_text = @markdown.render(text)
56
66
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md_emoji
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,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-29 00:00:00.000000000Z
12
+ date: 2012-02-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &2169729360 !ruby/object:Gem::Requirement
16
+ requirement: &2163314640 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,18 +21,18 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2169729360
24
+ version_requirements: *2163314640
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: redcarpet
27
- requirement: &2169728760 !ruby/object:Gem::Requirement
27
+ requirement: &2163313940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 2.0.0b3
32
+ version: '2.1'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2169728760
35
+ version_requirements: *2163313940
36
36
  description: Rails gem which adds emoji parsing to markdown
37
37
  email:
38
38
  - jordan@ducksoupsoftware.com
@@ -587,7 +587,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
587
587
  version: '0'
588
588
  segments:
589
589
  - 0
590
- hash: 1133397691911705144
590
+ hash: 2938624612538269085
591
591
  required_rubygems_version: !ruby/object:Gem::Requirement
592
592
  none: false
593
593
  requirements:
@@ -596,7 +596,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
596
596
  version: '0'
597
597
  segments:
598
598
  - 0
599
- hash: 1133397691911705144
599
+ hash: 2938624612538269085
600
600
  requirements: []
601
601
  rubyforge_project:
602
602
  rubygems_version: 1.8.15