html-pipeline-abbr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd2f77d1c2671b8049eb2fc22eb37a23ee733c18
4
+ data.tar.gz: a87f98ce3d7f43032eb84d11c753fecdf06196f1
5
+ SHA512:
6
+ metadata.gz: b913c06f4f63af85f8fea0e6a2ef83ec5abfcb1e2e86b312ee6e1abf9864e0963507533019db112877847cf014e0cb26841f043a7a64301bbcec66fcf20242b9
7
+ data.tar.gz: 01232a4db85b04a369fa55c2d80f3a1c15198aa0928207c910c8288a2aae2bfd2ce35819e1a272f7fb45d559cfe98c5d40b3e9c1eecee9d7ee983c109df9b5dd
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/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in html-pipeline-abbr.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Html::Pipeline::Abbr
2
+
3
+ This adds Abbreviation support to your html-pipeline.
4
+
5
+ Some implementations of Markdown support `<abbr>`, but it is not standard yet.
6
+ It does look like [CommonMark] may include it. In the mean time, hope this
7
+ helps.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'html-pipeline-abbr'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install html-pipeline-abbr
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ pipeline = HTML::Pipeline.new [
29
+ HTML::Pipeline::MarkdownFilter,
30
+ HTML::Pipeline::AbbrFilter,
31
+ HTML::Pipeline::SyntaxHighlightFilter,
32
+ ]
33
+ result = pipeline.call <<-CODE
34
+ Lets generate some *great* HTML.
35
+
36
+ *[HTML]: Hypertext
37
+ CODE
38
+ puts result[:output].to_s
39
+ ```
40
+ Prints:
41
+
42
+ ```
43
+ <p>Lets generate some <strong>great</strong> <abbr title="Hypertxt">HTML</abbr>.</p>
44
+ ```
45
+
46
+ On the flip side, there is also the `ShortenFilter` which will find words that can be abbreviated and shorten them.
47
+
48
+ ```ruby
49
+ pipeline = HTML::Pipeline.new [
50
+ HTML::Pipeline::MarkdownFilter,
51
+ HTML::Pipeline::ShortenFilter,
52
+ HTML::Pipeline::SyntaxHighlightFilter,
53
+ ]
54
+ result = pipeline.call <<-CODE
55
+ Lets generate some *great* Hypertext.
56
+
57
+ *[HTML]: Hypertext
58
+ CODE
59
+ puts result[:output].to_s
60
+ ```
61
+
62
+ It produces the same output:
63
+
64
+ ```
65
+ <p>Lets generate some <strong>great</strong> <abbr title="Hypertxt">HTML</abbr>.</p>
66
+ ```
67
+
68
+ ## Development
69
+
70
+ ## Thanks
71
+
72
+ Appreciated the sample tests and code from:
73
+
74
+ - https://github.com/lifted-studios/html-pipeline-cite
75
+ - https://github.com/JuanitoFatas/html-pipeline-rouge_filter
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it ( https://github.com/kbrock/html-pipeline-abbr/fork )
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
82
+ 4. Push to the branch (`git push origin my-new-feature`)
83
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
9
+ task :default => :test
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,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'html/pipeline/abbr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "html-pipeline-abbr"
8
+ spec.version = Html::Pipeline::Abbr::VERSION
9
+ spec.authors = ["Keenan Brock"]
10
+ spec.email = ["keenan@thebrocks.net"]
11
+
12
+ spec.summary = %q{html-pipeline filter to add or replace abbreviations}
13
+ spec.description = %q{abbr tags are part of the html spec. They are currently
14
+ not part of markdown but potentially part of common mark.
15
+ In the mean time this adds abbr tags}
16
+ spec.homepage = "http://github.com/kbrock/html-pipeline-abbr/"
17
+
18
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
19
+ # delete this section to allow pushing this gem to any host.
20
+ if spec.respond_to?(:metadata)
21
+ # spec.metadata['allowed_push_host'] = ""
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "html-pipeline", ">= 1.0"
28
+ spec.add_dependency "nokogiri", [">= 1.4", "<= 1.6.5"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "minitest", "~> 5.3"
33
+ spec.add_development_dependency "yard", "~> 0.8"
34
+ end
@@ -0,0 +1,7 @@
1
+ module Html
2
+ module Pipeline
3
+ module Abbr
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ require "html/pipeline"
2
+ require "html/pipeline/abbr/version"
3
+ require "html/pipeline/abbr_filter.rb"
4
+ require "html/pipeline/shorten_filter.rb"
@@ -0,0 +1,93 @@
1
+ module HTML
2
+ class Pipeline
3
+ # uses defined abbreviations and add abbr tags around them
4
+
5
+ class AbbrFilter < Filter
6
+ DEFAULT_IGNORED_ANCESTOR_TAGS = %w(pre code tt).freeze
7
+ DEFINITION_PATTERN=%r{(?:^|\n)\*\[([^\]]+)\]: *(.+)$}
8
+
9
+ def call
10
+ abbrs = extract_defs
11
+ replace_abbrs(abbrs) unless abbrs.empty?
12
+ doc
13
+ end
14
+
15
+ private
16
+
17
+ def extract_defs
18
+ abbrs ||= {}
19
+ replace_nodes do |content|
20
+ def_filter(content, abbrs) if content.include?("*[")
21
+ end
22
+ abbrs
23
+ end
24
+
25
+ # find abbrs in the code
26
+ def replace_abbrs(abbrs)
27
+ replace_nodes do |content|
28
+ abbrs_filter(content, abbrs)
29
+ end
30
+ end
31
+
32
+ # for reach of the text nodes that we can modify
33
+ def replace_nodes
34
+ doc.xpath(".//text()").each do |node|
35
+ next if has_ancestor?(node, ignored_ancestor_tags)
36
+
37
+ content = node.to_html
38
+ html = yield content
39
+
40
+ next if html == content || html.nil?
41
+ if html.length == 0
42
+ node.parent.remove
43
+ else
44
+ node.replace(html)
45
+ end
46
+ end
47
+ end
48
+
49
+ def def_filter(content, abbrs)
50
+ content.gsub(DEFINITION_PATTERN) do |match|
51
+ abbrs[$1] = $2
52
+ ""
53
+ end
54
+ end
55
+
56
+ # Return html with abbreviations replaced
57
+ #
58
+ # @return [String] html with all abbreviations replaced
59
+ def abbrs_filter(content, abbrs)
60
+ abbrs.each do |abbr, full|
61
+ content = abbr_filter(content, abbr, full) if content.include?(abbr)
62
+ end
63
+ content
64
+ end
65
+
66
+ # Return html with all of an abbreviation replaced
67
+ #
68
+ # @return [String] html with abbreviation tags
69
+ def abbr_filter(content, abbr, full)
70
+ target_html = abbr_tag(abbr, full)
71
+ content.gsub(abbr) { |_| target_html }
72
+ end
73
+
74
+ # Return html string to use as an abbr tag
75
+ #
76
+ # @return [String] abbr html
77
+ def abbr_tag(abbr, full)
78
+ %(<abbr title="#{full}">#{abbr}</abbr>)
79
+ end
80
+
81
+ # Return ancestor tags to stop processing
82
+ #
83
+ # @return [Array<String>] Ancestor tags.
84
+ def ignored_ancestor_tags
85
+ if context[:ignored_ancestor_tags]
86
+ DEFAULT_IGNORED_ANCESTOR_TAGS | context[:ignored_ancestor_tags]
87
+ else
88
+ DEFAULT_IGNORED_ANCESTOR_TAGS
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,23 @@
1
+ module HTML
2
+ class Pipeline
3
+ # Similar to abbreviate
4
+ # but it starts with the long format and shortens it
5
+ class ShortenFilter < AbbrFilter
6
+
7
+ # Return html with abbreviations replaced
8
+ #
9
+ # @return [String] html with all abbreviations replaced
10
+ def abbrs_filter(content, abbrs)
11
+ abbrs.each do |abbr, full|
12
+ content = abbr_filter(content, abbr, full) if content.include?(full)
13
+ end
14
+ content
15
+ end
16
+
17
+ def abbr_filter(content, abbr, full)
18
+ target_html = abbr_tag(abbr, full)
19
+ content.gsub(full) { |_| target_html }
20
+ end
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html-pipeline-abbr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keenan Brock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: html-pipeline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ - - "<="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.6.5
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '1.4'
44
+ - - "<="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.6.5
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.9'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.9'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.3'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.3'
89
+ - !ruby/object:Gem::Dependency
90
+ name: yard
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.8'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.8'
103
+ description: |-
104
+ abbr tags are part of the html spec. They are currently
105
+ not part of markdown but potentially part of common mark.
106
+ In the mean time this adds abbr tags
107
+ email:
108
+ - keenan@thebrocks.net
109
+ executables: []
110
+ extensions: []
111
+ extra_rdoc_files: []
112
+ files:
113
+ - ".gitignore"
114
+ - ".travis.yml"
115
+ - Gemfile
116
+ - README.md
117
+ - Rakefile
118
+ - bin/setup
119
+ - html-pipeline-abbr.gemspec
120
+ - lib/html/pipeline/abbr.rb
121
+ - lib/html/pipeline/abbr/version.rb
122
+ - lib/html/pipeline/abbr_filter.rb
123
+ - lib/html/pipeline/shorten_filter.rb
124
+ homepage: http://github.com/kbrock/html-pipeline-abbr/
125
+ licenses: []
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.2.2
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: html-pipeline filter to add or replace abbreviations
147
+ test_files: []
148
+ has_rdoc: