tag-extractor 0.1.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.
Files changed (2) hide show
  1. data/lib/tag_extractor.rb +64 -0
  2. metadata +46 -0
@@ -0,0 +1,64 @@
1
+ module TagExtractor
2
+ @@separator = nil
3
+
4
+ def self.tag_separator=(s)
5
+ @@separator = s
6
+ end
7
+
8
+ def self.tag_separator
9
+ @@separator || raise(TagSeparatorError)
10
+ end
11
+
12
+ class StringExtractor
13
+ attr_reader :source
14
+
15
+ def initialize(source)
16
+ @source = source
17
+ end
18
+
19
+ def extract_with_separator(separator = nil)
20
+ @source.scan(get_regex(separator))
21
+ end
22
+
23
+ def extract(separator = nil)
24
+ extract_with_separator(separator).collect { |t| t.slice!(0); t }
25
+ end
26
+
27
+ private
28
+ def get_regex(separator = nil)
29
+ tag_separator = separator || TagExtractor::tag_separator
30
+ %r{(?:#{tag_separator})[a-zA-Z](?:\w|-)*}
31
+ end
32
+ end # StringExtractor
33
+
34
+ class HTMLExtractor < StringExtractor
35
+ def convert_tags_to_html_links(separator = nil, options = { class: nil }, &block)
36
+ @source.gsub!(get_regex(separator)) { |name|
37
+ link = block.call(name.slice(1..-1)) || ''
38
+ '<a ' + (options[:class].nil? ? '' : 'class="' + options[:class] + '" ') + 'href="' + link + '">' + name + '</a>'
39
+ }
40
+ end
41
+ alias :linkify_tags :convert_tags_to_html_links
42
+ end
43
+
44
+ class TagSeparatorError < StandardError
45
+ def initialize
46
+ super "Could not find any tag separator"
47
+ end
48
+ end
49
+ end
50
+
51
+ class String
52
+ def extract_tags(separator = nil, with_separator = false)
53
+ if with_separator
54
+ TagExtractor::StringExtractor.new(self).extract_with_separator(separator)
55
+ else
56
+ TagExtractor::StringExtractor.new(self).extract(separator)
57
+ end
58
+ end
59
+
60
+ def convert_tags_to_html_links(separator = nil, &block)
61
+ TagExtractor::HTMLExtractor.new(self).convert_tags_to_html_links(separator, &block)
62
+ end
63
+ alias :linkify_tags :convert_tags_to_html_links
64
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tag-extractor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - GabrielDehan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Allow tag extraction and tag conversion in ruby
15
+ email: dehan.gabriel@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/tag_extractor.rb
21
+ homepage: https://github.com/gabriel-dehan/TagExtractor
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: A minimal ruby library for tag extraction
45
+ test_files: []
46
+ has_rdoc: