translighterate 0.0.2 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 702abdb37c389df4421a0b0e337ea946dfd838c3
4
- data.tar.gz: a1330ad9a48e6c9c8a7a0f0a8e616acf755beece
2
+ SHA256:
3
+ metadata.gz: 517bd6a4f3df331190cde122301b9240e04a4354b1294b4e5143a48e2c38d5f4
4
+ data.tar.gz: cd79f3e4bc4e5723bc45f5f91f12ed5a61bffc13dec9ad84e4371c66b8215e0f
5
5
  SHA512:
6
- metadata.gz: c08ef0b945c23cf01aff56ea4b55f5d011316ddb0e35bd36d30b69f5b358712f869cb04119b445ef9ea9276165364cb3ff0927d56b13e6c8670efd1882709066
7
- data.tar.gz: 643c4f1bacaac643553767dd2c1022409e721a532a4fa8e47c75d19ab83892e7bbfd73209926230e11951bcf376857fe65f96e61c989b8863ae68827a0702e55
6
+ metadata.gz: b488c79f9a5913d25b1eeaefeb7ec9511e6281a81b7876d70e5f5e3e3a0a1d11bc117cc01b0069d5736e9c514934444a6c7aacc51f7ca7b6ad5ce50ff4358437
7
+ data.tar.gz: aaa55ff30a6c6823105a185ca703a2e7d02c5b8245c47cb24ebff58696a92a67daa5a5c6b6c42ddb1fa2deb3d262315692cd856a37c75b34a836b4df27ccad0b
@@ -0,0 +1,12 @@
1
+ name: Run tests
2
+ on: [push]
3
+ jobs:
4
+ main:
5
+ runs-on: ubuntu-latest
6
+ steps:
7
+ - uses: actions/checkout@v2
8
+ - uses: ruby/setup-ruby@v1
9
+ with:
10
+ ruby-version: 3.0.1
11
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
12
+ - run: bin/rspec
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in translighterate.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jeremy Fleischman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # translighterate = transliterate + highlight [![Build Status](https://github.com/jfly/translighterate/actions/workflows/main.yml/badge.svg)](https://github.com/jfly/translighterate/actions/workflows/main.yml)
2
+
3
+ (Created after getting no responses to this [Stack Overflow question](http://stackoverflow.com/questions/34166987/rails-gem-for-transliterate-highlight).)
4
+
5
+ This is a version of Rails's [highlight](http://apidock.com/rails/ActionView/Helpers/TextHelper/highlight) that is transliteration aware.
6
+
7
+ For example, highlighting 'duss' in 'Düsseldorf' will actually match something:
8
+
9
+ ```ruby
10
+ irb(main):001:0> require 'translighterate'
11
+ => true
12
+ irb(main):002:0> Translighterate.highlight("Düsseldorf", "duss")
13
+ => "<mark>Düss</mark>eldorf"
14
+ ```
15
+
16
+ as opposed to this behavior with Rails's [highlight](http://apidock.com/rails/ActionView/Helpers/TextHelper/highlight):
17
+
18
+ ```ruby
19
+ irb(main):002:0> require 'action_view'
20
+ => true
21
+ irb(main):004:0> include ActionView::Helpers::TextHelper
22
+ => Object
23
+ irb(main):005:0> highlight("Düsseldorf", "duss")
24
+ => "Düsseldorf"
25
+ ```
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'translighterate'
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ $ bundle
38
+
39
+ Or install it yourself as:
40
+
41
+ $ gem install translighterate
42
+
43
+ ## Usage
44
+
45
+ We have one class `Translighterate` with one method `highlight`.
46
+
47
+ ```ruby
48
+ irb(main):001:0> require 'translighterate'
49
+ => true
50
+ irb(main):002:0> Translighterate.highlight("Düsseldorf", "duss")
51
+ => "<mark>Düss</mark>eldorf"
52
+ ```
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ### Testing
61
+
62
+ `bin/rspec` to run the tests.
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jfly/translighterate.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "translighterate"
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(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,7 +1,10 @@
1
+ require "translighterate/version"
1
2
  require 'action_view'
2
3
  include ActionView::Helpers::TextHelper
3
4
 
4
- class Translighterate
5
+ module Translighterate
6
+ # Copied (and modified!) from
7
+ # https://github.com/rails/rails/blob/495ef4124e292dcc7062e3e1f04f623053113101/actionview/lib/action_view/helpers/text_helper.rb#L130
5
8
  def self.highlight(text, phrases, options = {})
6
9
  text = sanitize(text) if options.fetch(:sanitize, true)
7
10
 
@@ -14,16 +17,45 @@ class Translighterate
14
17
 
15
18
  # Transliteration trick from:
16
19
  # https://github.com/cubing/worldcubeassociation.org/issues/238#issuecomment-234702800
17
- transliterated = text.mb_chars.normalize(:kd).gsub(/[\p{Mn}]/,'').to_s
18
- transliterated.gsub(/(#{match})(?![^<]*?>)/i) do |found|
19
- original_text_found = text[Range.new(*$~.offset(0), true)]
20
- if block_given?
21
- yield original_text_found
22
- else
23
- highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
24
- highlighter.gsub(/\\1/, original_text_found)
25
- end
20
+ transliterated = text.chars.map { |ch| transliterate_char(ch) }.join
21
+
22
+ previous_match_end_index = 0
23
+ result = ""
24
+ transliterated.scan(/(#{match})(?![^<]*?>)/i) do
25
+ match_range = $~.offset(0)
26
+ result += text[previous_match_end_index...match_range[0]]
27
+ original_text_found = text[Range.new(*match_range, true)]
28
+ previous_match_end_index = match_range[1]
29
+
30
+ result += if block_given?
31
+ yield original_text_found
32
+ else
33
+ highlighter = options.fetch(:highlighter, '<mark>\1</mark>')
34
+ highlighter.gsub(/\\1/, original_text_found)
35
+ end
26
36
  end
37
+ result += text[previous_match_end_index..-1]
27
38
  end.html_safe
28
39
  end
29
40
  end
41
+
42
+ def transliterate_char(ch)
43
+ raise if ch.length != 1
44
+ original_char = ch
45
+
46
+ mappings = {
47
+ "ł" => "l",
48
+ "Ł" => "L",
49
+ }
50
+
51
+ ch = if mappings.key?(ch)
52
+ mappings[ch]
53
+ else
54
+ ch.mb_chars.unicode_normalize(:nfkd).gsub(/[\p{Mn}]/, '').unicode_normalize(:nfc).to_s
55
+ end
56
+ if ch.length != 1
57
+ original_char
58
+ else
59
+ ch
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Translighterate
2
+ VERSION = "0.3.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "translighterate/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "translighterate"
8
+ spec.version = Translighterate::VERSION
9
+ spec.authors = ["Jeremy Fleischman"]
10
+ spec.email = ["jeremyfleischman@gmail.com"]
11
+
12
+ spec.summary = "A version of Rails's highlight that is transliteration aware."
13
+ spec.description = "For example, highlighting 'duss' in 'Düsseldorf' will actually match something."
14
+ spec.homepage = "https://github.com/jfly/translighterate"
15
+
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
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_dependency "actionview", ">= 6.0"
26
+
27
+ spec.add_development_dependency "bundler", ">= 2.2"
28
+ spec.add_development_dependency "rake", ">= 13.0"
29
+ spec.add_development_dependency "rspec", ">= 3.10"
30
+ spec.add_development_dependency "byebug", ">= 11.1"
31
+ end
metadata CHANGED
@@ -1,84 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translighterate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Fleischman
8
- autorequire:
9
- bindir: bin
8
+ autorequire:
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-04 00:00:00.000000000 Z
11
+ date: 2021-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '6.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
26
+ version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '3.5'
33
+ version: '2.2'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '3.5'
40
+ version: '2.2'
41
41
  - !ruby/object:Gem::Dependency
42
- name: byebug
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '9.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '9.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rake
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - "~>"
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
- version: '11.2'
75
+ version: '11.1'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - "~>"
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: '11.2'
82
+ version: '11.1'
69
83
  description: For example, highlighting 'duss' in 'Düsseldorf' will actually match
70
84
  something.
71
- email: jeremyfleischman@gmail.com
85
+ email:
86
+ - jeremyfleischman@gmail.com
72
87
  executables: []
73
88
  extensions: []
74
89
  extra_rdoc_files: []
75
90
  files:
91
+ - ".github/workflows/main.yml"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/rspec
100
+ - bin/setup
76
101
  - lib/translighterate.rb
102
+ - lib/translighterate/version.rb
103
+ - translighterate.gemspec
77
104
  homepage: https://github.com/jfly/translighterate
78
105
  licenses:
79
106
  - MIT
80
107
  metadata: {}
81
- post_install_message:
108
+ post_install_message:
82
109
  rdoc_options: []
83
110
  require_paths:
84
111
  - lib
@@ -93,9 +120,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
120
  - !ruby/object:Gem::Version
94
121
  version: '0'
95
122
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 2.5.1
98
- signing_key:
123
+ rubygems_version: 3.2.15
124
+ signing_key:
99
125
  specification_version: 4
100
126
  summary: A version of Rails's highlight that is transliteration aware.
101
127
  test_files: []