redcarpet-socialable 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7722b1682883781e998aa947c926cb54c3c224d6
4
+ data.tar.gz: d1e2cdbde51c51061923323166a860f720b86510
5
+ SHA512:
6
+ metadata.gz: 468aa26ebb86a4b5c95b5782142e337723a62ad15883e3882652c54d0ada73a4980e051ac6edd7e46e77be4a03c81f70cd485fcdf51c910f06005692a384a52e
7
+ data.tar.gz: ed283cc8cc93ed0f53677ca5fc87603fa20d48964971cf829e6180f0f6eace2cb0c1ced6d31071d1bb4c8688f680b521426b8bd6bb2a5bb3db9e335c75976d6e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rspec"
4
+
5
+ # Specify your gem's dependencies in tweetable-redcapret.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jan Bernacki
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @mention and #hashtag for Markdown
2
+
3
+ This gem provides a HTML renderer for [redcarpet](https://github.com/vmg/redcarpet).
4
+
5
+ ## How to use
6
+
7
+ ```ruby
8
+ gem "tweetable_redcarpet"
9
+ ```
10
+
11
+ ```ruby
12
+ # override the following methods for your case
13
+ class MyAwesomeRenderer < Redcarpet::Socialable
14
+ def highlight_tag?(name)
15
+ Tag.where(name: name.downcase).first_or_create!
16
+ end
17
+
18
+ def highlight_username?(name)
19
+ User.where(username: name).exists?
20
+ end
21
+
22
+ def tag_template(name)
23
+ %(<a>##{name}</a>)
24
+ end
25
+
26
+ def mention_template(name)
27
+ %(<a>@#{name}</a>)
28
+ end
29
+ end
30
+
31
+ # use redcarpet's options https://github.com/vmg/redcarpet
32
+ renderer = MyAwesomeRenderer.new
33
+ # space_after_headers must be true for hashtag feature
34
+ markdown = Redcarpet::Markdown.new(renderer, space_after_headers: true)
35
+
36
+ markdown.render "#foo\n@bar" # => <p><a>#foo</a><br><a>@bar</a></p>
37
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,76 @@
1
+ require "redcarpet"
2
+
3
+ module Redcarpet
4
+ class Socialable < Redcarpet::Render::HTML
5
+ BASE_REGEXP = '(^|[\s])+%s(\b|\-|\.|,|:|;|\?|!|\(|\)|$){1}'
6
+
7
+ def paragraph(text)
8
+ "<p>#{safe_replace(text)}</p>"
9
+ end
10
+
11
+ def list_item(text, list_type)
12
+ "<li>#{safe_replace(text)}</li>"
13
+ end
14
+
15
+ def header(text, level)
16
+ "<h#{level}>#{safe_replace(text)}</h#{level}>"
17
+ end
18
+
19
+ private
20
+ def highlight_tag?(name)
21
+ true
22
+ end
23
+
24
+ def highlight_username?(name)
25
+ true
26
+ end
27
+
28
+ def tag_template(name)
29
+ %(<a href="#" title="#{name}">##{name}</a>)
30
+ end
31
+
32
+ def mention_template(name)
33
+ %(<a href="#" title="#{name}">@#{name}</a>)
34
+ end
35
+
36
+ def safe_replace(text)
37
+ replaces = {}
38
+
39
+ # cut all tags
40
+ text.gsub!(%r{>.*?</}m) do |match|
41
+ md5 = Digest::MD5.hexdigest(match)
42
+ replaces[md5] = match
43
+ "{extraction-#{md5}}"
44
+ end
45
+
46
+ # highlight tags
47
+ regexp = Regexp.new(BASE_REGEXP % '#([\w\-]+)')
48
+ text.gsub!(regexp) do |match|
49
+ before, raw, after = $1, $2, $3
50
+ if highlight_tag?(raw)
51
+ %(#{before}#{tag_template(raw)}#{after})
52
+ else
53
+ match
54
+ end
55
+ end
56
+
57
+ # highlight users
58
+ regexp = Regexp.new(BASE_REGEXP % '@([\w-]+)')
59
+ text.gsub!(regexp) do |match|
60
+ before, raw, after = $1, $2, $3
61
+ if highlight_username?(raw)
62
+ %(#{$1}#{mention_template(raw)}#{$3})
63
+ else
64
+ match
65
+ end
66
+ end
67
+
68
+ # paste all tags
69
+ text.gsub!(/\{extraction-([0-9a-f]{32})\}/) do
70
+ replaces[$1]
71
+ end
72
+
73
+ text
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "redcarpet-socialable"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Jan Bernacki"]
9
+ spec.email = ["releu@me.com"]
10
+ spec.description = %q{@mention and #hashtag features for redcarpet}
11
+ spec.summary = %q{@mention and #hashtag features for redcarpet, the Markdown renderer}
12
+ spec.homepage = "https://github.com/releu/redcarpet-socialable"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "redcarpet", "~> 3.0.0"
23
+ end
@@ -0,0 +1,99 @@
1
+ require File.expand_path("../../lib/redcarpet/socialable", __FILE__)
2
+
3
+ class NoTags < Redcarpet::Socialable
4
+ def highlight_tag?(name)
5
+ false
6
+ end
7
+ end
8
+
9
+ class NoMentions < Redcarpet::Socialable
10
+ def highlight_username?(name)
11
+ false
12
+ end
13
+ end
14
+
15
+ class SpanTags < Redcarpet::Socialable
16
+ def tag_template(name)
17
+ "<span>#{name}</span>"
18
+ end
19
+ end
20
+
21
+ class SpanMentions < Redcarpet::Socialable
22
+ def mention_template(name)
23
+ "<span>#{name}</span>"
24
+ end
25
+ end
26
+
27
+ describe Redcarpet::Socialable do
28
+ let(:renderer) { Redcarpet::Socialable.new }
29
+ let(:markdown) do
30
+ Redcarpet::Markdown.new(renderer, space_after_headers: true)
31
+ end
32
+
33
+ it "returns html" do
34
+ sample = "foo\n\nbar"
35
+ expect(markdown.render(sample)).to match(/</)
36
+ end
37
+
38
+ { "@mentions" => "@", "#hashtags" => "#" }.each do |type, char|
39
+ context "highlighting #{type}" do
40
+ subject { markdown.render(sample) }
41
+
42
+ context "in paragraphs" do
43
+ let(:sample) { "#{char}bar" }
44
+
45
+ it { expect(subject).to match(/<a/) }
46
+ end
47
+
48
+ context "in lists" do
49
+ let(:sample) { "* foo\n* #{char}bar\n* buz" }
50
+
51
+ it { expect(subject).to match(/<a/) }
52
+ end
53
+
54
+ context "in blockquote" do
55
+ let(:sample) { "> foo #{char}bar buz" }
56
+
57
+ it { expect(subject).to match(/<a/) }
58
+ end
59
+
60
+ context "in headers" do
61
+ let(:sample) { "# foo #{char}bar buz" }
62
+
63
+ it { expect(subject).to match(/<a/) }
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "override #highlight_tag?" do
69
+ let(:renderer) { NoTags.new }
70
+ let(:sample) { "#foo" }
71
+ subject { markdown.render(sample) }
72
+
73
+ it { expect(subject).not_to match(/<a/) }
74
+ end
75
+
76
+ describe "override #highlight_mention?" do
77
+ let(:renderer) { NoMentions.new }
78
+ let(:sample) { "@foo" }
79
+ subject { markdown.render(sample) }
80
+
81
+ it { expect(subject).not_to match(/<a/) }
82
+ end
83
+
84
+ describe "override #tag_template?" do
85
+ let(:renderer) { SpanTags.new }
86
+ let(:sample) { "#foo" }
87
+ subject { markdown.render(sample) }
88
+
89
+ it { expect(subject).to match(/<span/) }
90
+ end
91
+
92
+ describe "override #mention_template" do
93
+ let(:renderer) { SpanMentions.new }
94
+ let(:sample) { "@foo" }
95
+ subject { markdown.render(sample) }
96
+
97
+ it { expect(subject).to match(/<span/) }
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redcarpet-socialable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jan Bernacki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redcarpet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ description: '@mention and #hashtag features for redcarpet'
56
+ email:
57
+ - releu@me.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/redcarpet/socialable.rb
68
+ - redcarpet-socialable.gemspec
69
+ - spec/socialable_spec.rb
70
+ homepage: https://github.com/releu/redcarpet-socialable
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: '@mention and #hashtag features for redcarpet, the Markdown renderer'
94
+ test_files:
95
+ - spec/socialable_spec.rb