ragadjust 0.0.2

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: 8b9adc04576f171cf9c4c6083dd77cb3a4c6405c
4
+ data.tar.gz: ccb09b86ef96d2fedcd1c7ddc67e13e08ecdc6ae
5
+ SHA512:
6
+ metadata.gz: da9ca50b1b6138c211ec192d52177b79a9b8cd894d6d2f6294e4dc0dc25b70a819fff2e06b16a0c215a353817771afbe85c8b800c48d6459afe44168575bc6a1
7
+ data.tar.gz: ea343d4ce197007782587202f8e80860df02769f9cfb1e4a7303d478d92b77f43c3114cdb4681532e062e30e85252feac5cfae998fdb63a2da690a1755b5450e
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ragadjust.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Adam Perfect
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,41 @@
1
+ # Ragadjust
2
+
3
+ Gem to rag-adjust text content based on Mark Boulton's 24ways article: http://24ways.org/2013/run-ragged/ and Nathan Ford's JS implementation: https://github.com/nathanford/ragadjust
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ragadjust', github: 'aperfect/ragadjust'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ragadjust
18
+
19
+ ## Usage
20
+
21
+ Ragadjust::Adjust.ragadjust_content(text_to_adjust, selector, method = 'all')
22
+
23
+ `selector` takes a CSS selector and defaults to:
24
+
25
+ 'p, li, dd, figcaption'
26
+
27
+ `method` options:
28
+
29
+ 'all' - default: apply adjustment to all the below
30
+ 'prepositions' - only rag-adjust prepositions
31
+ 'small-words' - only adjust 1-/2-letter words
32
+ 'dashes' - only adjust space after dashes
33
+ 'emphasis' - only adjust within emphasis tags (em, strong, i, b)
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it ( https://github.com/aperfect/ragadjust/fork )
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,73 @@
1
+ require 'nokogiri'
2
+
3
+ module Ragadjust
4
+
5
+ class Adjust
6
+
7
+ # Rag-adjust the content. Based on Nathan Ford's ragadjust JS
8
+ # https://github.com/nathanford/ragadjust
9
+ def self.ragadjust_content(text_to_adjust, selector = 'p, li, dd, figcaption', method = 'all')
10
+ html = Nokogiri::HTML(text_to_adjust) # Not using .fragment() as .search() doesn't seem to work?
11
+
12
+ preps = /(?<=\s|^|;)((aboard|about|above|across|after|against|along|amid|among|anti|around|before|behind|below|beneath|beside|besides|between|beyond|concerning|considering|despite|down|during|except|excepting|excluding|following|from|inside|into|like|minus|near|onto|opposite|outside|over|past|plus|regarding|round|save|since|than|that|this|through|toward|towards|under|underneath|unlike|until|upon|versus|with|within|without)\s)+/i
13
+
14
+ smallwords = /(?<=\s|^|;)\b(([[:word:]]{1,2})\b\s)/i
15
+
16
+ dashes = /([-–—])\s/i
17
+
18
+
19
+ elements = html.css(selector)
20
+
21
+ elements.search('//text()').each do |node|
22
+
23
+ if node.text?
24
+ content = node.content
25
+
26
+ # prepositions
27
+ if %w{prepositions all}.include? method
28
+ content = content.gsub preps, '\2'+nbsp
29
+ end
30
+
31
+ # small words
32
+ if %w{small-words all}.include? method
33
+ content = content.gsub smallwords, '\2'+nbsp
34
+ end
35
+
36
+ # dashes
37
+ if %w{dashes all}.include? method
38
+ content = content.gsub dashes, '\1'+nbsp
39
+ end
40
+
41
+ node.content = content
42
+ end
43
+
44
+ end
45
+
46
+ # emphasized text
47
+ if %w{emphasis all}.include? method
48
+ elements.css('strong,em,b,i').each do |el|
49
+ # Only emphasized phrases of 2/3 words
50
+ s = el.content.split.size
51
+ if s > 1 && s < 4
52
+ # Get text nodes
53
+ el.search('//text()').each do |n|
54
+ if n.text?
55
+ Ragadjust::Adjust.text_node_replace n, /\s+/, nbsp
56
+ end
57
+ end
58
+ end
59
+ end # elements.css().each
60
+ end
61
+
62
+ html.css('body').inner_html
63
+
64
+ end # def self.ragadjust_content
65
+
66
+ def self.nbsp
67
+ ' '
68
+ end
69
+
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,3 @@
1
+ module Ragadjust
2
+ VERSION = "0.0.2"
3
+ end
data/lib/ragadjust.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "ragadjust/version"
2
+ require "ragadjust/adjust"
3
+
4
+ module Ragadjust
5
+ # Your code goes here...
6
+ end
data/ragadjust.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ragadjust/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ragadjust"
8
+ spec.version = Ragadjust::VERSION
9
+ spec.authors = ["Adam Perfect"]
10
+ spec.email = ["gems@adamperfect.com"]
11
+ spec.summary = "Apply rag-adjustment to HTML content"
12
+ spec.description = "Gem to rag-adjust text content based on Mark Boulton's 24ways article: http://24ways.org/2013/run-ragged/ and Nathan Ford's JS implementation: https://github.com/nathanford/ragadjust"
13
+ spec.homepage = "https://github.com/aperfect/ragadjust"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_runtime_dependency "nokogiri"
25
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ragadjust
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Adam Perfect
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-02 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'Gem to rag-adjust text content based on Mark Boulton''s 24ways article:
56
+ http://24ways.org/2013/run-ragged/ and Nathan Ford''s JS implementation: https://github.com/nathanford/ragadjust'
57
+ email:
58
+ - gems@adamperfect.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/ragadjust.rb
69
+ - lib/ragadjust/adjust.rb
70
+ - lib/ragadjust/version.rb
71
+ - ragadjust.gemspec
72
+ homepage: https://github.com/aperfect/ragadjust
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Apply rag-adjustment to HTML content
96
+ test_files: []