builder_links 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aad7804603d689547d35466bb518b5616883f773
4
+ data.tar.gz: 558b85e8bebd0b58bd84519cb0a2ce9cc275c7cc
5
+ SHA512:
6
+ metadata.gz: 49cf2f7203ce44e892b33724eb74912a3658dcefa8ba4a51dba1a6e7c0b349042c0a48d198cd400e57c753c34f7330ffa4fed16c2d8d85e9adaf5a87cf7f8388
7
+ data.tar.gz: 9992e30d93e246e794ea5de1e205b0ab04e6e83c530412be8ebc053b8506f6ba72915060e64617365bb6ac8e83e1c9fd415b512e75564af387d5f26223a864c1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in builder_links.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # BuilderLinks
2
+
3
+ A ruby gem to generate links automatically based on a text, keywords and urls is given. Useful for example to increase dinamically the internal links in your site and improve SEO metrics.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your Gemfile's application:
9
+
10
+ ```ruby
11
+ gem 'builder_links'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install builder_links
21
+
22
+ ## Configuration
23
+ Use an initializer builder_links.rb
24
+
25
+ ```ruby
26
+ # all parameters based on which allow to find keywords and build the links
27
+ # builder_links will use anchortext to find text and uri to generate the link
28
+ BuilderLinks.setup do |config|
29
+ config.patterns = [
30
+ {anchortext: 'google', uri: 'http://www.google.com'},
31
+ {anchortext: 'builder links', uri: 'https://github.com/jsanroman/builder_links'},
32
+ {anchortext: "I wanted to illuminate the whole earth", uri: 'https://es.wikipedia.org/wiki/Nikola_Tesla'},
33
+ ]
34
+
35
+ # maximum links generated to each call
36
+ config.total_links = 5
37
+ # maximum links generated to each pattern given
38
+ config.links_per_pattern = 1
39
+ end
40
+ ```
41
+
42
+ ## Usage
43
+ Call directly to `BuilderLinks.text("")`
44
+ ```ruby
45
+ BuilderLinks.text("<p>I wanted to illuminate the whole earth. There is enough electricity to become a second sun.</p>")
46
+ ```
47
+ ```ruby
48
+ #<p><a href="https://es.wikipedia.org/wiki/Nikola_Tesla">I wanted to illuminate the whole earth</a>. There is enough electricity to become a second sun.</p>
49
+ ```
50
+
51
+ Or you can add `builder_links` to your activerecord model, that will generate a method `builder_links(:field)`
52
+ ```ruby
53
+ class Card < ActiveRecord::Base
54
+ attr_accessible :text, :user_text
55
+
56
+ builder_links
57
+ end
58
+
59
+ # Then you can use builder_links method
60
+ Card.find(1).builder_links(:text)
61
+ Card.find(1).builder_links(:user_text)
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/jsanroman/builder_links/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "builder_links"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'builder_links/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'builder_links'
8
+ spec.version = BuilderLinks::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.required_ruby_version = '>= 1.9'
11
+
12
+ spec.licenses = ["MIT"]
13
+ spec.authors = ["Javi Sanromán"]
14
+ spec.email = ["javisanroman@gmail.com"]
15
+
16
+ spec.summary = %q{A ruby gem to generate links automatically based on a text, keywords and urls is given}
17
+ spec.description = %q{A ruby gem to generate links automatically based on a text, keywords and urls is given. Useful for example to increase dinamically the internal links in your site and improve SEO metrics.}
18
+ spec.homepage = 'https://github.com/jsanroman/builder_links'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.9'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'activerecord'
28
+ spec.add_development_dependency 'nokogiri'
29
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+
3
+ module BuilderLinks
4
+ module ActiveRecord
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def builder_links
9
+ define_method 'builder_links' do |arg|
10
+ return false if arg.blank?
11
+ return false unless attributes.include?(arg.to_s)
12
+
13
+ analize = BuilderLinks::Analize.new(attributes[arg.to_s])
14
+ analize.run
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveRecord::Base.send(:include, BuilderLinks::ActiveRecord)
@@ -0,0 +1,64 @@
1
+ module BuilderLinks
2
+ class Analize
3
+ def initialize(text)
4
+ @doc = Nokogiri::HTML(text)
5
+ @analized_text = nil
6
+ end
7
+
8
+ def run
9
+ return @analized_text unless @analized_text.blank?
10
+
11
+ total_links = 0
12
+ BuilderLinks.patterns.each do |pattern|
13
+ links_per_pattern = 0
14
+ @doc.search('p').children.each do |child|
15
+ break if max_links_generated?(total_links, links_per_pattern)
16
+
17
+ if analize_node(child, pattern)
18
+ links_per_pattern += 1
19
+ total_links += 1
20
+ end
21
+ end
22
+ end
23
+
24
+ @analized_text = @doc.at('body').nil? ? '' : @doc.at('body').inner_html
25
+ end
26
+
27
+ private
28
+
29
+ def analize_node(node, pattern)
30
+ if %('text', 'strong').include?(node.name) && node.children.count < 2
31
+ replace_text = node.content
32
+
33
+ prefix_suffix = pattern[:anchortext].include?(' ') ? '' : ' '
34
+ result = replace_text.sub!(/(#{prefix_suffix}#{pattern[:anchortext]}#{prefix_suffix})/i,
35
+ '<a href="' + pattern[:uri] + '" title="\1">\1</a>')
36
+
37
+ unless result.nil?
38
+ if node.name == 'text'
39
+ node.replace replace_text
40
+ else
41
+ new_node = @doc.create_element node.name
42
+ new_node.inner_html = replace_text
43
+ node.replace new_node
44
+ end
45
+
46
+ return true
47
+ end
48
+ end
49
+
50
+ false
51
+ end
52
+
53
+ def max_links_generated?(total_links, links_per_pattern)
54
+ if !BuilderLinks.links_per_pattern.nil? && links_per_pattern >= BuilderLinks.links_per_pattern
55
+ return true
56
+ end
57
+ if !BuilderLinks.total_links.nil? && total_links >= BuilderLinks.total_links
58
+ return true
59
+ end
60
+
61
+ false
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,25 @@
1
+ module BuilderLinks::Config
2
+ def config
3
+ yield self
4
+ end
5
+
6
+ def define_setting(name, default = nil)
7
+ class_variable_set("@@#{name}", default)
8
+
9
+ define_class_method "#{name}=" do |value|
10
+ class_variable_set("@@#{name}", value)
11
+ end
12
+
13
+ define_class_method name do
14
+ class_variable_get("@@#{name}")
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def define_class_method(name, &block)
21
+ (class << self; self; end).instance_eval do
22
+ define_method name, &block
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module BuilderLinks
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ require 'builder_links/version'
2
+ require 'builder_links/config'
3
+ require 'builder_links/analize'
4
+ require 'builder_links/active_record'
5
+
6
+ module BuilderLinks
7
+ extend Config
8
+
9
+ define_setting :patterns
10
+ define_setting :links_per_pattern
11
+ define_setting :total_links
12
+ @patterns = []
13
+ @patterns.reverse!
14
+ @links_per_pattern = nil
15
+ @total_links = nil
16
+
17
+ def self.setup(&block)
18
+ yield self
19
+ end
20
+
21
+ def self.text(text)
22
+ analize = BuilderLinks::Analize.new(text)
23
+ analize.run
24
+ end
25
+
26
+ def self.logger
27
+ @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
28
+ end
29
+
30
+ def self.logger=(logger)
31
+ @@logger = logger
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: builder_links
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Javi Sanromán
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-24 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A ruby gem to generate links automatically based on a text, keywords
70
+ and urls is given. Useful for example to increase dinamically the internal links
71
+ in your site and improve SEO metrics.
72
+ email:
73
+ - javisanroman@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - builder_links.gemspec
87
+ - lib/builder_links.rb
88
+ - lib/builder_links/active_record.rb
89
+ - lib/builder_links/analize.rb
90
+ - lib/builder_links/config.rb
91
+ - lib/builder_links/version.rb
92
+ homepage: https://github.com/jsanroman/builder_links
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '1.9'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A ruby gem to generate links automatically based on a text, keywords and
116
+ urls is given
117
+ test_files: []