typefactory 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74e9982c0c479738a2eff67898054cec39372d32
4
+ data.tar.gz: 74fed332d7ea2a61d3cd9cc883ca195c2f840578
5
+ SHA512:
6
+ metadata.gz: 88bfa03086c7e5d967fabd2b5d8b330db826bd5eaa2e9095fc349590184b802bc85f168d2f96584b56e0629aacd9d75425cc0dc15a473e96cd0248d5f910be69
7
+ data.tar.gz: fad05207ff3b16574d110d641309cd6b2096f12210269b7bc262ccc5cb7137c38ecf4741beb2e563012b096e720aab5776c95e6daad7ab79bdae4d126db9c89b
@@ -0,0 +1,23 @@
1
+ .idea
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
@@ -0,0 +1,2 @@
1
+ --private
2
+ --protected
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ Copyright 2014 Evgeny Boyko, Boandmedia
2
+
3
+ MIT License
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.
@@ -0,0 +1,44 @@
1
+ # Typefactory
2
+
3
+ This simple plugin helps you to prepare your texts for publishing on the web. Typefactory was created for Russian grammar and punctuation but can be used in other languages (such as English or German).
4
+
5
+ _This is my first gem and I'll be glad to hear from you any advices about Ruby/Rails as well as about my English :-)_
6
+
7
+ ## Abilities
8
+
9
+ Current beta version has only methods for processing three levels of quote marks. All used glyphs can be customized.
10
+
11
+ ## Install
12
+
13
+ ### Ruby on Rails
14
+
15
+ $ gem "typefactory", "~> 0.0.10"
16
+ $ bundle install
17
+
18
+
19
+ ### Extension for `String`
20
+
21
+ Typefactory adds method `prepare` for standard Ruby class `String`. You can use this feature as well as default way.
22
+
23
+ 'Some text here'.prepare
24
+ <%= @description.prepare %>
25
+
26
+
27
+ ## Customize
28
+
29
+ Need customization? Please use generator:
30
+ $ rails generate typefactory config
31
+ Now you can modify `config/initializers/typefactory.rb`
32
+
33
+ ## Help to improve
34
+
35
+ If you have text where parsing algorithm makes mistakes, send it to me (eboyko@eboyko.ru), please.
36
+
37
+ ## Contribute
38
+
39
+ 1. Fork it https://github.com/eboyko/typefactory/fork
40
+ 2. Create your feature branch `git checkout -b my-new-feature`
41
+ 3. Commit your changes `git commit -am 'Add some feature'`
42
+ 4. Push to the branch `git push origin my-new-feature`
43
+ 5. Create a new pull request
44
+
@@ -0,0 +1,8 @@
1
+ require "rspec/core/rake_task"
2
+ require "bundler/gem_tasks"
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ['--color', '--format', 'documentation']
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ Typefactory.setup do |config|
2
+
3
+ config.quote_marks = [
4
+ { :left => '«', :right => '»' },
5
+ { :left => '„', :right => '“' },
6
+ { :left => '‘', :right => '’' }
7
+ ]
8
+
9
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class TypefactoryGenerator < ActiveRecord::Generators::Base
4
+
5
+ def self.source_root
6
+ @source_root = File.expand_path("../templates", __FILE__)
7
+ end
8
+
9
+ def config
10
+ copy_file "initializer.rb", "config/initializers/typefactory.rb"
11
+ end
12
+
13
+ end
@@ -0,0 +1,46 @@
1
+ require 'typefactory/core_ext'
2
+ require 'active_support/dependencies'
3
+
4
+ module Typefactory
5
+
6
+ autoload :Processor, 'typefactory/processor'
7
+
8
+ @@quote_marks = [
9
+ { :left => '«', :right => '»' },
10
+ { :left => '„', :right => '“' },
11
+ { :left => '‘', :right => '’' }
12
+ ]
13
+ mattr_accessor :quote_marks
14
+
15
+ @@glyphs = {
16
+ nbsp: { mark: ' ', sign: ' ', letter_code: '&nbsp;', digital_code: '&#160;' },
17
+ quot: { mark: '"', sign: '"', letter_code: '&quot;', digital_code: '&#34;' },
18
+ mdash: { mark: '-', sign: '—', letter_code: '&mdash;', digital_code: '&#151;' }
19
+ # :nbsp => ' ',
20
+ # :ndash => '–',
21
+ # :mdash => '&mdash;',
22
+ # :minus => '−',
23
+ # :section => '§',
24
+ # :paragraph => '¶',
25
+ # :copy => '©',
26
+ # :registered => '®',
27
+ # :trademark => '™',
28
+ # :degree => '°',
29
+ # :inch => '″',
30
+ # :multiple => '×',
31
+ # :middot => '·',
32
+ # :bullit => '•',
33
+ # :quot => '"'
34
+ }
35
+ mattr_accessor :glyphs
36
+
37
+ def self.setup
38
+ yield self
39
+ end
40
+
41
+ # @note In next versions here will be `settings`
42
+ def self.prepare(text)
43
+ Processor.new(text).prepare
44
+ end
45
+
46
+ end
@@ -0,0 +1,7 @@
1
+ String.class_eval do
2
+
3
+ def prepare
4
+ Typefactory::prepare(self)
5
+ end
6
+
7
+ end
@@ -0,0 +1,116 @@
1
+ module Typefactory
2
+
3
+ class Processor
4
+
5
+ @buffer = nil
6
+
7
+ # @param original [String] a text for preparing
8
+ def initialize(original)
9
+ @buffer = original
10
+ end
11
+
12
+ # @return [String] web-prepared text
13
+ def prepare
14
+ cleanup
15
+ escape_html
16
+ quotes
17
+ descape_html
18
+ emdashes
19
+ short_words
20
+ @buffer
21
+ end
22
+
23
+ private
24
+
25
+ # Purging text from already replaced glyphs (includes quotation marks of whole levels)
26
+ def cleanup
27
+
28
+ # Replacing all special chars to human-like marks
29
+ Typefactory::glyphs.each do |glyph, settings|
30
+ @buffer.gsub!(/#{settings[:sign]}|#{settings[:letter_code]}|#{settings[:digital_code]}/, settings[:mark])
31
+ end
32
+
33
+ # Replacing quote marks
34
+ expression = String.new
35
+ Typefactory::quote_marks.each do |mark|
36
+ expression += "#{mark[:left]}|#{mark[:right]}|"
37
+ end
38
+ @buffer.gsub!(/#{expression[0..-2]}/, Typefactory::glyphs[:quot][:mark])
39
+
40
+ end
41
+
42
+ def escape_html
43
+ @buffer.gsub!(/<([^\/].*?)>/) { |block| block.gsub(/"/, '\'') }
44
+ end
45
+
46
+ def descape_html
47
+ @buffer.gsub!(/<([^\/].*?)>/) { |block| block.gsub(/'/, '"') }
48
+ end
49
+
50
+ # Quotes
51
+ def quotes
52
+ result = String.new
53
+ level = -1
54
+ @buffer.split('').each_with_index do |character, index|
55
+ if character == '"'
56
+ side = quote_mark_side(index)
57
+ if side == :left
58
+ level += 1
59
+ result += Typefactory::quote_marks[level][side]
60
+ else
61
+ result += Typefactory::quote_marks[level][side]
62
+ level -= 1
63
+ end
64
+ else
65
+ result += character
66
+ end
67
+ end
68
+ @buffer.replace result
69
+ end
70
+
71
+ def quote_mark_side(index)
72
+ before = space_before_position(index)
73
+ after = space_after_position(index)
74
+ if !before.nil? and !after.nil?
75
+ (before < after) ? :left : :right
76
+ elsif !before.nil? and after.nil?
77
+ :left
78
+ elsif before.nil? and !after.nil?
79
+ :right
80
+ else
81
+ :right
82
+ end
83
+ end
84
+
85
+ def space_before_position(index, length = 4)
86
+ to = (index-1 < 0) ? 0 : index-1
87
+ from = (index-length < 0) ? 0 : index-length
88
+ position = @buffer[from..to].rindex(/\s|\-|>/)
89
+ if position.nil?
90
+ index < length ? 0 : nil
91
+ else
92
+ length - position
93
+ end
94
+ end
95
+
96
+ def space_after_position(index, length = 4)
97
+ to = (index+length > @buffer.length) ? @buffer.length : index+length
98
+ from = (index+1 > @buffer.length) ? @buffer.length : index+1
99
+ position = @buffer[from..to].index(/\s|\-|<|,/)
100
+ if position.nil?
101
+ length if index > @buffer.length - length
102
+ else
103
+ position
104
+ end
105
+ end
106
+
107
+ def short_words
108
+ @buffer.gsub!(/\s([a-zA-Z,а-яА-Я]{1,2})\s/) { |word| " #{$1}#{Typefactory::glyphs[:nbsp][:sign]}" }
109
+ end
110
+
111
+ def emdashes
112
+ @buffer.gsub!(/\s\-\s/, "#{Typefactory::glyphs[:nbsp][:sign]}#{Typefactory::glyphs[:mdash][:sign]} ")
113
+ end
114
+
115
+ end
116
+ end
@@ -0,0 +1,3 @@
1
+ module Typefactory
2
+ VERSION = '0.0.10'
3
+ end
@@ -0,0 +1 @@
1
+ require 'typefactory'
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Typefactory' do
4
+
5
+ describe 'Core' do
6
+ it 'Parameters can be updated successfully' do
7
+ Typefactory.setup do |config|
8
+ config.quote_marks = [
9
+ { :left => '«', :right => '»' },
10
+ { :left => '„', :right => '“' },
11
+ { :left => '‘', :right => '’' }
12
+ ]
13
+ expect(config.quote_marks.class).to eq(Array)
14
+ expect(config.quote_marks[2][:right]).to eq('’')
15
+ end
16
+ end
17
+ end
18
+
19
+ describe 'Quotation (basic examples)' do
20
+ it 'Processing first level' do
21
+ example = 'Типовой "текст" в кавычках'
22
+ expect(example.prepare).to eq('Типовой «текст» в кавычках')
23
+ end
24
+
25
+ it 'Processing second level' do
26
+ example = '"Различные "ученые" от пикап-индустрии"'
27
+ expect(example.prepare).to eq('«Различные „ученые“ от пикап-индустрии»')
28
+ end
29
+
30
+ it 'Processing third level' do
31
+ example = '"Быть может, когда-нибудь "все мы будем жить в "лучшем" мире""'
32
+ expect(example.prepare).to eq('«Быть может, когда-нибудь „все мы будем жить в ‘лучшем’ мире“»')
33
+ end
34
+ end
35
+
36
+
37
+ describe 'Quotation (complicated exampled)' do
38
+ it 'processing marks in start/end positions' do
39
+ example = '"""Никакой" другой" типограф"'
40
+ expect(example.prepare).to eq('«„‘Никакой’ другой“ типограф»')
41
+ end
42
+
43
+ it 'Корректно распознает один уровень кавычек в составных словах с дефисом' do
44
+ example = 'Кавычки-"елочки"'
45
+ expect(example.prepare).to eq('Кавычки-«елочки»')
46
+
47
+ example = '"Елочки"-сосны'
48
+ expect(example.prepare).to eq('«Елочки»-сосны')
49
+ end
50
+
51
+ it 'Расставляет неразрывные пробелы' do
52
+ example = 'Едет Санта на оленях'
53
+ expect(example.prepare).to eq('Едет Санта на оленях')
54
+ end
55
+ end
56
+
57
+ describe 'Glyphs' do
58
+ it 'Process long dashes' do
59
+ example = 'Писец - не только ценный мех'
60
+ expect(example.prepare).to eq('Писец — не только ценный мех')
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'typefactory/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'typefactory'
9
+ spec.version = Typefactory::VERSION
10
+ spec.authors = ['Evgeny Boyko', 'Boandmedia']
11
+ spec.email = ['eboyko@eboyko.ru']
12
+ spec.summary = 'Helps you to prepare your texts for publishing on the web'
13
+ spec.description = 'Soon'
14
+ spec.homepage = 'http://boandmedia.com'
15
+ spec.license = 'MIT'
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_runtime_dependency 'rails', '~> 4.1', '>= 4.1.0'
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rake', '~> 10.1', '>= 10.1.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
25
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typefactory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.10
5
+ platform: ruby
6
+ authors:
7
+ - Evgeny Boyko
8
+ - Boandmedia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.1'
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 4.1.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '4.1'
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.1.0
34
+ - !ruby/object:Gem::Dependency
35
+ name: bundler
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.1'
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 10.1.0
58
+ type: :development
59
+ prerelease: false
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '10.1'
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 10.1.0
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 3.0.0
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '3.0'
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 3.0.0
88
+ description: Soon
89
+ email:
90
+ - eboyko@eboyko.ru
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - ".gitignore"
96
+ - ".yardopts"
97
+ - Gemfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - lib/generators/typefactory/templates/initializer.rb
102
+ - lib/generators/typefactory/typefactory_generator.rb
103
+ - lib/typefactory.rb
104
+ - lib/typefactory/core_ext.rb
105
+ - lib/typefactory/processor.rb
106
+ - lib/typefactory/version.rb
107
+ - spec/spec_helper.rb
108
+ - spec/typefactory_spec.rb
109
+ - typefactory.gemspec
110
+ homepage: http://boandmedia.com
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.3.0
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Helps you to prepare your texts for publishing on the web
134
+ test_files:
135
+ - spec/spec_helper.rb
136
+ - spec/typefactory_spec.rb
137
+ has_rdoc: