lightgraf 0.1.1.pre.beta.1 → 0.1.2.pre.beta.1

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
2
  SHA256:
3
- metadata.gz: 887b14c815b3b0891625506fd0c8a24853b93fb713f7446159f177d539ff182d
4
- data.tar.gz: e2212a9792924b7096ede20918a7299c53dd36b405b5403a5e35bfcf00a3dee6
3
+ metadata.gz: f42e55aa76e452a1f8c6161ae839f18903d413f620d4422d716f93007c00b52c
4
+ data.tar.gz: e013d4eadbe41ef6c9ff13b4dfd10b655397c497a147b6af0cc686fe9bd209ba
5
5
  SHA512:
6
- metadata.gz: db9724f20aa5aa288a3dea179066e6f9a31fc84da8af9baa72ebc4bf35609ca8b3f2bd38bd2ae94a85f2eb5cbf8566e6079e8df76e43440eb52c889b88f9f435
7
- data.tar.gz: 5b0a64c0fd10d0a549ab7a5204bf3ac6718bb404ceff95f6918d0c6509676520ed6053a50bc7c1daafa513c7d2ea0d33b5e6f7e2d3dc0cc59bf4ab2963c9148f
6
+ metadata.gz: 27a36ac1441718ba1384347a63bce7f09e68ab25b9831229f08de73918273ecaba9cf20b8ae17a27f57bcde0224241f04a1ca5d1669c0746846402ecdf9aac84
7
+ data.tar.gz: 7cbe398dd363ca780fb92a0acb2132aacbc548b65235f5595e6788c0c600a6e5c3e4d5df84c0c634474a076d73cca5b70f4443286594b5bcb228d261eb65f6bd
@@ -0,0 +1,38 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ "master" ]
13
+ pull_request:
14
+ branches: [ "master" ]
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ test:
21
+
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby-version: ['2.6']
26
+
27
+ steps:
28
+ - uses: actions/checkout@v3
29
+ - name: Set up Ruby
30
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
+ # uses: ruby/setup-ruby@v1
33
+ uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
34
+ with:
35
+ ruby-version: ${{ matrix.ruby-version }}
36
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37
+ - name: Run tests
38
+ run: bundle exec rake
data/CHANGELOG.md CHANGED
@@ -1 +1,3 @@
1
- ## Initial
1
+ ## In this update:
2
+ - Added formatting options
3
+ - Security improvements
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lightgraf (0.1.1.pre.beta.1)
4
+ lightgraf (0.1.2.pre.beta.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -14,7 +14,7 @@ GEM
14
14
  parser (3.2.2.1)
15
15
  ast (~> 2.4.1)
16
16
  rainbow (3.1.1)
17
- rake (10.5.0)
17
+ rake (13.0.6)
18
18
  regexp_parser (2.8.0)
19
19
  rexml (3.2.5)
20
20
  rubocop (1.50.2)
@@ -36,12 +36,12 @@ PLATFORMS
36
36
  ruby
37
37
 
38
38
  DEPENDENCIES
39
- bundler (~> 1.17)
39
+ bundler (>= 2.2.33)
40
40
  irb
41
41
  lightgraf!
42
42
  minitest (~> 5.0)
43
- rake (~> 10.0)
43
+ rake (~> 13.0)
44
44
  rubocop
45
45
 
46
46
  BUNDLED WITH
47
- 1.17.2
47
+ 2.4.13
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Lightgraf
2
+ [![Gem Version](https://badge.fury.io/rb/lightgraf.svg)](https://badge.fury.io/rb/lightgraf)
2
3
 
3
4
  Lightweight typography tool for Rails. It handles for you:
4
5
  - __Quotes__:
@@ -18,6 +19,14 @@ Lightweight typography tool for Rails. It handles for you:
18
19
  А что я? А я ничего\
19
20
  `↓` \
20
21
  А(NBSP)что я?(NBSP)А(NBSP)я(NBSP)ничего
22
+
23
+ - __No-breakables__:
24
+
25
+ Call me: 232-12-34\
26
+ `↓` \
27
+ Call me: \<nobr\>232-12-34\</nobr\>
28
+
29
+ It is fast and efficient, handling complex text of ~5800 characters at 0.06 sec
21
30
 
22
31
  ## Installation
23
32
 
@@ -37,7 +46,10 @@ Or install it yourself as:
37
46
 
38
47
  ## Usage
39
48
 
40
- TODO: Write usage instructions here
49
+ ```ruby
50
+ require 'lightgraf'
51
+ fixed_text = Lightgraf.fix "My text"
52
+ ```
41
53
 
42
54
  ## Development
43
55
 
@@ -6,22 +6,34 @@ module Lightgraf
6
6
  module Fixtures
7
7
  INCORRECT_QUOTES = %w[' " `].freeze
8
8
  HYPHENS = %w[- – —].freeze
9
- SPACES = [' ', ' ', ' ', ' ', ' '].freeze
10
- TAG_L = %(<)
11
- TAG_R = %(>)
12
- QUOT_RU_A_L = %(«)
13
- QUOT_RU_A_R = %(»)
14
- QUOT_RU_B_L = %(„)
15
- QUOT_RU_B_R = %(“)
16
- QUOT_EN_A_L = %(“)
17
- QUOT_EN_A_R = %(”)
18
- QUOT_EN_B_L = %(‘)
19
- QUOT_EN_B_R = %(’)
20
- HTML_NOBR_L = %(<nobr>)
21
- HTML_NOBR_R = %(</nobr>)
22
- HYPHEN = HYPHENS[2]
23
- SPACE = SPACES[0]
24
- NBSP = SPACES[2]
9
+ SPACES = [' ', ' ', ' ', ' ', ' ', '​', '‍', '‌', 'ㅤ', ' ', ' ', ' ', '⠀', '⁣', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '⁠'].freeze
10
+ TAG_L = %(<)
11
+ TAG_R = %(>)
12
+ QUOT_RU_A_L = %(«)
13
+ QUOT_RU_A_R = %(»)
14
+ QUOT_RU_B_L = %(„)
15
+ QUOT_RU_B_R = %(“)
16
+ QUOT_EN_A_L = %(“)
17
+ QUOT_EN_A_R = %(”)
18
+ QUOT_EN_B_L = %(‘)
19
+ QUOT_EN_B_R = %(’)
20
+ AMPERSAND = %(&)
21
+ HYPHEN = HYPHENS[2]
22
+ SPACE = SPACES[0]
23
+ NBSP = SPACES[2]
24
+ HTML_AMPERSAND = %(&amp;)
25
+ HTML_QUOT_RU_A_L = %(&laquo;)
26
+ HTML_QUOT_RU_A_R = %(&raquo;)
27
+ HTML_QUOT_RU_B_L = %(&bdquo;)
28
+ HTML_QUOT_RU_B_R = %(&ldquo;)
29
+ HTML_QUOT_EN_A_L = %(&ldquo;)
30
+ HTML_QUOT_EN_A_R = %(&rdquo;)
31
+ HTML_QUOT_EN_B_L = %(&lsquo;)
32
+ HTML_QUOT_EN_B_R = %(&rsquo;)
33
+ HTML_NOBR_L = %(<nobr>)
34
+ HTML_NOBR_R = %(</nobr>)
35
+ HTML_HYPHEN = %(&mdash;)
36
+ HTML_NBSP = %(&nbsp;)
25
37
  end
26
38
 
27
39
  end
@@ -1,3 +1,9 @@
1
+ # Handcrafted by Aydar N.
2
+ # 2023
3
+ #
4
+ # me@aydar.media
5
+ #
6
+
1
7
  # frozen_string_literal: true
2
8
 
3
9
  require 'lightgraf/fixtures'
@@ -11,11 +17,12 @@ module Lightgraf
11
17
 
12
18
  def self.fix(
13
19
  text,
14
- html_encode: true,
20
+ format: :plaintext,
15
21
  disable_quotes: false,
16
22
  disable_hyphens: false,
17
23
  disable_nbsp: false,
18
24
  disable_nobr: false,
25
+ disable_escape: false,
19
26
  lang_check_max_take: 5,
20
27
  nbsp_max_length: 3
21
28
  )
@@ -29,6 +36,7 @@ module Lightgraf
29
36
  last_space = nil
30
37
  last_hyphen = nil
31
38
  need_nobr_r = false
39
+ offset = 0
32
40
  (0..text.length - 1).each do |i|
33
41
  char = text[i]
34
42
  if char == TAG_L
@@ -42,14 +50,23 @@ module Lightgraf
42
50
  elsif inside.last == :tag
43
51
  fixed += char
44
52
  next
53
+ elsif !disable_escape and format == :html and char == AMPERSAND
54
+ fixed += HTML_AMPERSAND
55
+ next
45
56
  elsif [QUOT_RU_A_L, QUOT_EN_A_L].include?(char)
46
57
  inside << :quote_a
47
58
  elsif [QUOT_RU_A_R, QUOT_EN_A_R].include?(char)
48
59
  inside.pop if inside.last == :quote_a
49
60
  elsif SPACES.include?(char)
61
+ if last_space == i - 1
62
+ last_space = i
63
+ next
64
+ end
50
65
  need_next = false
51
66
  if !disable_nbsp and (i < nbsp_max_length or (!last_space.nil? and last_hyphen != i - 1 and (i - last_space) <= nbsp_max_length) or HYPHENS.include?(text[i + 1]))
52
- fixed += NBSP
67
+ ins = case format; when :html then HTML_NBSP; when :plaintext then NBSP; end
68
+ fixed += ins
69
+ offset += ins.length - 1
53
70
  need_next = true
54
71
  end
55
72
  if need_nobr_r
@@ -58,13 +75,17 @@ module Lightgraf
58
75
  end
59
76
  last_space = i
60
77
  next if need_next
78
+ fixed += SPACE
79
+ next
61
80
  elsif HYPHENS.include?(char)
62
81
  need_next = false
63
82
  if !disable_hyphens and (i.zero? or last_space == i - 1)
64
- fixed += HYPHEN
83
+ ins = case format; when :html then HTML_HYPHEN; when :plaintext then HYPHEN; end
84
+ fixed += ins
85
+ offset += ins.length - 1
65
86
  need_next = true
66
- elsif html_encode and !disable_nobr and !need_nobr_r and last_space != i - 1 and !whitespace?(text[i + 1])
67
- fixed.insert (last_space.nil? ? 0 : last_space + 1), HTML_NOBR_L
87
+ elsif format == :html and !disable_nobr and !need_nobr_r and last_space != i - 1 and !whitespace?(text[i + 1])
88
+ fixed.insert (last_space.nil? ? 0 : last_space + offset + 1), HTML_NOBR_L
68
89
  need_nobr_r = true
69
90
  end
70
91
  last_hyphen = i
@@ -73,37 +94,37 @@ module Lightgraf
73
94
  case inside.last
74
95
  when :quote_a
75
96
  if char == quote_char.last
76
- fixed += quote_lang.last == :ru ? QUOT_RU_A_R : QUOT_EN_A_R
97
+ fixed += case format; when :html then (quote_lang.last == :ru ? HTML_QUOT_RU_A_R : HTML_QUOT_EN_A_R); when :plaintext then (quote_lang.last == :ru ? QUOT_RU_A_R : QUOT_EN_A_R); end
77
98
  inside.pop
78
99
  quote_char.pop
79
100
  quote_lang.pop
80
101
  else
81
102
  quote_lang << (cyrillic?(text[i, lang_check_max_take]) ? :ru : :en)
82
- fixed += quote_lang.last == :ru ? QUOT_RU_B_L : QUOT_EN_B_L
103
+ fixed += case format; when :html then (quote_lang.last == :ru ? HTML_QUOT_RU_B_L : HTML_QUOT_EN_B_L); when :plaintext then (quote_lang.last == :ru ? QUOT_RU_B_L : QUOT_EN_B_L); end
83
104
  inside << :quote_b
84
105
  quote_char << char
85
106
  end
86
107
  when :quote_b
87
108
  if char == quote_char.last
88
- fixed += quote_lang.last == :ru ? QUOT_RU_B_R : QUOT_EN_B_R
109
+ fixed += case format; when :html then (quote_lang.last == :ru ? HTML_QUOT_RU_B_R : HTML_QUOT_EN_B_R); when :plaintext then (quote_lang.last == :ru ? QUOT_RU_B_R : QUOT_EN_B_R); end
89
110
  inside << :quote_a
90
111
  quote_char.pop
91
112
  quote_lang.pop
92
113
  else
93
114
  quote_lang << (cyrillic?(text[i, lang_check_max_take]) ? :ru : :en)
94
- fixed += quote_lang.last == :ru ? QUOT_RU_A_L : QUOT_EN_A_L
115
+ fixed += case format; when :html then (quote_lang.last == :ru ? HTML_QUOT_RU_A_L : HTML_QUOT_EN_A_L); when :plaintext then (quote_lang.last == :ru ? QUOT_RU_A_L : QUOT_EN_A_L); end
95
116
  inside << :quote_a
96
117
  quote_char << char
97
118
  end
98
119
  when nil
99
120
  quote_lang = (cyrillic?(text[i, lang_check_max_take]) ? [:ru] : [:en])
100
- fixed += quote_lang.last == :ru ? QUOT_RU_A_L : QUOT_EN_A_L
121
+ fixed += case format; when :html then (quote_lang.last == :ru ? HTML_QUOT_RU_A_L : HTML_QUOT_EN_A_L); when :plaintext then (quote_lang.last == :ru ? QUOT_RU_A_L : QUOT_EN_A_L); end
101
122
  inside << :quote_a
102
123
  quote_char = [char]
103
124
  end
104
125
  next
105
126
  end
106
- fixed += html_encode ? CGI.escape_html(char) : char
127
+ fixed += char
107
128
  end
108
129
  fixed += HTML_NOBR_R if need_nobr_r
109
130
  fixed
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lightgraf
4
- VERSION = '0.1.1-beta.1'
4
+ VERSION = '0.1.2-beta.1'
5
5
  end
data/lib/lightgraf.rb CHANGED
@@ -15,17 +15,18 @@ module Lightgraf
15
15
 
16
16
  # Fixes formatting of a block of text, returning formatted text
17
17
  # == Parameters:
18
- # +text+:: +String+ of text to format
19
- # +html_encode+:: (Optional) Whether to html-encode output
20
- # +disable_quotes+:: (Optional) Whether to disable fixing quotation
21
- # +disable_hyphens+:: (Optional) Whether to disable fixing hyphens
22
- # +disable_nbsp+:: (Optional) Whether to disable fixing whitespaces
23
- # +disable_nobr+:: (Optional) Whether to disable adding <nobr>
24
- # +lang_check_max_take+:: (Optional) Max number of chars to consider while analyzing quotation language
18
+ # +text+:: +String+: Text to format
19
+ # +format+:: Optional +Sym+: How to format output (+:plaintext+ / +:html+)
20
+ # +disable_quotes+:: Optional +Bool+: Whether to disable fixing quotation
21
+ # +disable_hyphens+:: Optional +Bool+: Whether to disable fixing hyphens
22
+ # +disable_nbsp+:: Optional +Bool+: Whether to disable fixing whitespaces
23
+ # +disable_nobr+:: Optional +Bool+: Whether to disable adding <nobr>
24
+ # +disable_escape+:: Optional +Bool+: Whether to disable escaping html-specific chars like +&+
25
+ # +lang_check_max_take+:: Optional +Int+: Max number of chars to consider while analyzing quotation language
25
26
  # == Returns:
26
27
  # +String+:: Formatted text
27
- def self.fix(text, html_encode: true, disable_quotes: false, disable_hyphens: false, disable_nbsp: false, disable_nobr: false, lang_check_max_take: 5)
28
- Internal.fix text, html_encode: html_encode, disable_quotes: disable_quotes, disable_hyphens: disable_hyphens, disable_nbsp: disable_nbsp, disable_nobr: disable_nobr, lang_check_max_take: lang_check_max_take
28
+ def self.fix(text, format: :plaintext, disable_quotes: false, disable_hyphens: false, disable_nbsp: false, disable_nobr: false, disable_escape: false, lang_check_max_take: 5)
29
+ Internal.fix text, format: format, disable_quotes: disable_quotes, disable_hyphens: disable_hyphens, disable_nbsp: disable_nbsp, disable_nobr: disable_nobr, disable_escape: disable_escape, lang_check_max_take: lang_check_max_take
29
30
  end
30
31
 
31
32
  end
data/lightgraf.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.required_ruby_version = '>= 2.6.3'
14
14
 
15
15
  spec.summary = 'Lightweight typography helper for Russian language'
16
- # spec.description = "TODO: Write a longer description"
16
+ spec.description = 'Lightgraf is a lightweight typography tool intended for use in Rails primarily with Russian. Formats quotes, hyphens and whitespaces.'
17
17
  spec.homepage = 'https://rubygems.org/gems/lightgraf'
18
18
  spec.license = 'MIT'
19
19
 
@@ -26,9 +26,9 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ['lib']
28
28
 
29
- spec.add_development_dependency 'bundler', '~> 1.17'
29
+ spec.add_development_dependency 'bundler', '>= 2.2.33'
30
30
  spec.add_development_dependency 'irb'
31
31
  spec.add_development_dependency 'minitest', '~> 5.0'
32
- spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rake', '~> 13.0'
33
33
  spec.add_development_dependency 'rubocop'
34
34
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightgraf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.pre.beta.1
4
+ version: 0.1.2.pre.beta.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aydar N.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-21 00:00:00.000000000 Z
11
+ date: 2023-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
19
+ version: 2.2.33
20
20
  type: :development
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: '1.17'
26
+ version: 2.2.33
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: irb
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '13.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '13.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,13 +80,15 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description:
83
+ description: Lightgraf is a lightweight typography tool intended for use in Rails
84
+ primarily with Russian. Formats quotes, hyphens and whitespaces.
84
85
  email:
85
86
  - me@aydar.media
86
87
  executables: []
87
88
  extensions: []
88
89
  extra_rdoc_files: []
89
90
  files:
91
+ - ".github/workflows/ruby.yml"
90
92
  - ".gitignore"
91
93
  - ".rubocop.yml"
92
94
  - CHANGELOG.md
@@ -121,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
123
  - !ruby/object:Gem::Version
122
124
  version: 1.3.1
123
125
  requirements: []
124
- rubygems_version: 3.0.3
126
+ rubygems_version: 3.2.3
125
127
  signing_key:
126
128
  specification_version: 4
127
129
  summary: Lightweight typography helper for Russian language