tiny-safe-mod 0.0.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 +7 -0
- data/stringex-2.8.6/Gemfile +51 -0
- data/stringex-2.8.6/MIT-LICENSE +20 -0
- data/stringex-2.8.6/README.md +162 -0
- data/stringex-2.8.6/Rakefile +57 -0
- data/stringex-2.8.6/VERSION +1 -0
- data/stringex-2.8.6/init.rb +3 -0
- data/stringex-2.8.6/lib/stringex/acts_as_url/adapter/active_record.rb +23 -0
- data/stringex-2.8.6/lib/stringex/acts_as_url/adapter/base.rb +240 -0
- data/stringex-2.8.6/lib/stringex/acts_as_url/adapter/data_mapper.rb +61 -0
- data/stringex-2.8.6/lib/stringex/acts_as_url/adapter/mongoid.rb +37 -0
- data/stringex-2.8.6/lib/stringex/acts_as_url/adapter.rb +26 -0
- data/stringex-2.8.6/lib/stringex/acts_as_url.rb +107 -0
- data/stringex-2.8.6/lib/stringex/configuration/acts_as_url.rb +50 -0
- data/stringex-2.8.6/lib/stringex/configuration/base.rb +58 -0
- data/stringex-2.8.6/lib/stringex/configuration/configurator.rb +25 -0
- data/stringex-2.8.6/lib/stringex/configuration/string_extensions.rb +20 -0
- data/stringex-2.8.6/lib/stringex/configuration.rb +4 -0
- data/stringex-2.8.6/lib/stringex/configuration_lite.rb +3 -0
- data/stringex-2.8.6/lib/stringex/core_ext.rb +10 -0
- data/stringex-2.8.6/lib/stringex/localization/backend/base.rb +13 -0
- data/stringex-2.8.6/lib/stringex/localization/backend/i18n.rb +86 -0
- data/stringex-2.8.6/lib/stringex/localization/backend/internal.rb +47 -0
- data/stringex-2.8.6/lib/stringex/localization/conversion_expressions.rb +154 -0
- data/stringex-2.8.6/lib/stringex/localization/converter.rb +129 -0
- data/stringex-2.8.6/lib/stringex/localization/default_conversions.rb +89 -0
- data/stringex-2.8.6/lib/stringex/localization.rb +112 -0
- data/stringex-2.8.6/lib/stringex/rails/railtie.rb +10 -0
- data/stringex-2.8.6/lib/stringex/string_extensions.rb +228 -0
- data/stringex-2.8.6/lib/stringex/unidecoder.rb +82 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x00.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x01.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x02.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x03.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x04.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x05.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x06.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x07.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x09.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x0a.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x0b.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x0c.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x0d.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x0e.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x0f.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x10.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x11.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x12.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x13.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x14.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x15.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x16.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x17.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x18.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x1e.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x1f.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x20.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x21.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x22.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x23.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x24.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x25.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x26.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x27.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x28.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x2e.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x2f.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x30.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x31.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x32.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x33.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x4d.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x4e.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x4f.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x50.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x51.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x52.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x53.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x54.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x55.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x56.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x57.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x58.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x59.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x5a.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x5b.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x5c.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x5d.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x5e.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x5f.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x60.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x61.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x62.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x63.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x64.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x65.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x66.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x67.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x68.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x69.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x6a.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x6b.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x6c.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x6d.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x6e.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x6f.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x70.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x71.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x72.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x73.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x74.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x75.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x76.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x77.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x78.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x79.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x7a.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x7b.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x7c.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x7d.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x7e.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x7f.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x80.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x81.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x82.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x83.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x84.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x85.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x86.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x87.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x88.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x89.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x8a.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x8b.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x8c.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x8d.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x8e.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x8f.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x90.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x91.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x92.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x93.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x94.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x95.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x96.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x97.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x98.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x99.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x9a.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x9b.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x9c.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x9d.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x9e.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/x9f.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xa0.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xa1.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xa2.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xa3.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xa4.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xac.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xad.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xae.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xaf.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb0.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb1.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb2.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb3.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb4.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb5.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb6.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb7.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb8.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xb9.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xba.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xbb.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xbc.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xbd.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xbe.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xbf.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc0.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc1.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc2.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc3.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc4.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc5.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc6.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc7.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc8.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xc9.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xca.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xcb.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xcc.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xcd.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xce.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xcf.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd0.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd1.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd2.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd3.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd4.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd5.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd6.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xd7.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xf9.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xfa.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xfb.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xfc.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xfd.yml +256 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xfe.yml +257 -0
- data/stringex-2.8.6/lib/stringex/unidecoder_data/xff.yml +257 -0
- data/stringex-2.8.6/lib/stringex/version.rb +8 -0
- data/stringex-2.8.6/lib/stringex.rb +17 -0
- data/stringex-2.8.6/lib/stringex_lite.rb +15 -0
- data/stringex-2.8.6/locales/da.yml +76 -0
- data/stringex-2.8.6/locales/de.yml +77 -0
- data/stringex-2.8.6/locales/en.yml +69 -0
- data/stringex-2.8.6/locales/fr.yml +72 -0
- data/stringex-2.8.6/locales/nb.yml +76 -0
- data/stringex-2.8.6/locales/nl.yml +69 -0
- data/stringex-2.8.6/locales/no.yml +76 -0
- data/stringex-2.8.6/locales/pl.yml +88 -0
- data/stringex-2.8.6/locales/pt-BR.yml +69 -0
- data/stringex-2.8.6/locales/ru.yml +69 -0
- data/stringex-2.8.6/locales/sv.yml +76 -0
- data/stringex-2.8.6/stringex.gemspec +290 -0
- data/stringex-2.8.6/test/performance/localization_performance_test.rb +24 -0
- data/stringex-2.8.6/test/test_helper.rb +2 -0
- data/stringex-2.8.6/test/unit/acts_as_url/adapter/activerecord.rb +71 -0
- data/stringex-2.8.6/test/unit/acts_as_url/adapter/datamapper.rb +83 -0
- data/stringex-2.8.6/test/unit/acts_as_url/adapter/mongoid.rb +82 -0
- data/stringex-2.8.6/test/unit/acts_as_url_configuration_test.rb +51 -0
- data/stringex-2.8.6/test/unit/acts_as_url_integration_test.rb +425 -0
- data/stringex-2.8.6/test/unit/localization/da_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization/de_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization/default_test.rb +115 -0
- data/stringex-2.8.6/test/unit/localization/en_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization/fr_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization/nl_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization/pl_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization/pt_br_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization/ru_test.rb +130 -0
- data/stringex-2.8.6/test/unit/localization/sv_test.rb +118 -0
- data/stringex-2.8.6/test/unit/localization_18n_failure_test.rb +40 -0
- data/stringex-2.8.6/test/unit/localization_test.rb +162 -0
- data/stringex-2.8.6/test/unit/string_extensions_configuration_test.rb +17 -0
- data/stringex-2.8.6/test/unit/string_extensions_test.rb +321 -0
- data/stringex-2.8.6/test/unit/unicode_point_suite/basic_greek_test.rb +113 -0
- data/stringex-2.8.6/test/unit/unicode_point_suite/basic_latin_test.rb +144 -0
- data/stringex-2.8.6/test/unit/unicode_point_suite/codepoint_test_helper.rb +32 -0
- data/stringex-2.8.6/test/unit/unidecoder/bad_localization.yml +1 -0
- data/stringex-2.8.6/test/unit/unidecoder/localization.yml +4 -0
- data/stringex-2.8.6/test/unit/unidecoder_test.rb +104 -0
- data/stringex-2.8.6/test/unit/version_test.rb +12 -0
- data/tiny-safe-mod.gemspec +11 -0
- metadata +293 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
require 'stringex/localization/conversion_expressions'
|
|
4
|
+
|
|
5
|
+
module Stringex
|
|
6
|
+
module Localization
|
|
7
|
+
class Converter
|
|
8
|
+
include ConversionExpressions
|
|
9
|
+
|
|
10
|
+
attr_reader :ending_whitespace, :options, :starting_whitespace, :string
|
|
11
|
+
|
|
12
|
+
def initialize(string, options = {})
|
|
13
|
+
@string = string.dup
|
|
14
|
+
@options = Stringex::Configuration::StringExtensions.default_settings.merge(options)
|
|
15
|
+
string =~ /^(\s+)/
|
|
16
|
+
@starting_whitespace = $1 unless $1 == ''
|
|
17
|
+
string =~ /(\s+)$/
|
|
18
|
+
@ending_whitespace = $1 unless $1 == ''
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def cleanup_accented_html_entities!
|
|
22
|
+
string.gsub! expressions.accented_html_entity, '\1'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def cleanup_characters!
|
|
26
|
+
string.gsub! expressions.cleanup_characters, ' '
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def cleanup_html_entities!
|
|
30
|
+
string.gsub! expressions.cleanup_html_entities, ''
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def cleanup_smart_punctuation!
|
|
34
|
+
expressions.smart_punctuation.each do |expression, replacement|
|
|
35
|
+
string.gsub! expression, replacement
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def normalize_currency!
|
|
40
|
+
string.gsub!(/(\d+),(\d+)/, '\1\2')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def smart_strip!
|
|
44
|
+
string.strip!
|
|
45
|
+
@string = "#{starting_whitespace}#{string}#{ending_whitespace}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def strip!
|
|
49
|
+
string.strip!
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def strip_html_tags!
|
|
53
|
+
string.gsub! expressions.html_tag, ''
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def translate!(*conversions)
|
|
57
|
+
conversions.each do |conversion|
|
|
58
|
+
send conversion
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
protected
|
|
63
|
+
|
|
64
|
+
def unreadable_control_characters
|
|
65
|
+
string.gsub! expressions.unreadable_control_characters, ''
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def abbreviations
|
|
69
|
+
string.gsub! expressions.abbreviation do |x|
|
|
70
|
+
x.gsub '.', ''
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def apostrophes
|
|
75
|
+
string.gsub! expressions.apostrophe, '\1\2'
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def characters
|
|
79
|
+
expressions.characters.each do |key, expression|
|
|
80
|
+
next if key == :slash && options[:allow_slash]
|
|
81
|
+
replacement = translate(key)
|
|
82
|
+
replacement = " #{replacement} " unless replacement == '' || key == :dot
|
|
83
|
+
string.gsub! expression, replacement
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def currencies
|
|
88
|
+
if has_currencies?
|
|
89
|
+
[:currencies_complex, :currencies_simple].each do |type|
|
|
90
|
+
expressions.send(type).each do |key, expression|
|
|
91
|
+
string.gsub! expression, " #{translate(key, :currencies)} "
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def ellipses
|
|
98
|
+
string.gsub! expressions.characters[:ellipsis], " #{translate(:ellipsis)} "
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def html_entities
|
|
102
|
+
expressions.html_entities.each do |key, expression|
|
|
103
|
+
string.gsub! expression, translate(key, :html_entities)
|
|
104
|
+
end
|
|
105
|
+
string.squeeze! ' '
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def vulgar_fractions
|
|
109
|
+
expressions.vulgar_fractions.each do |key, expression|
|
|
110
|
+
string.gsub! expression, translate(key, :vulgar_fractions)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def expressions
|
|
117
|
+
ConversionExpressions
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def has_currencies?
|
|
121
|
+
string =~ CURRENCIES_SUPPORTED
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def translate(key, scope = :characters)
|
|
125
|
+
Localization.translate scope, key
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
module Stringex
|
|
4
|
+
module Localization
|
|
5
|
+
module DefaultConversions
|
|
6
|
+
CHARACTERS = {
|
|
7
|
+
and: "and",
|
|
8
|
+
at: "at",
|
|
9
|
+
degrees: "degrees",
|
|
10
|
+
divide: "divided by",
|
|
11
|
+
dot: '\1 dot \2',
|
|
12
|
+
ellipsis: "dot dot dot",
|
|
13
|
+
equals: "equals",
|
|
14
|
+
number: "number",
|
|
15
|
+
percent: "percent",
|
|
16
|
+
plus: "plus",
|
|
17
|
+
slash: "slash",
|
|
18
|
+
star: "star",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
CURRENCIES_SIMPLE = {
|
|
22
|
+
generic: '\1 dollars',
|
|
23
|
+
dollars: '\1 dollars',
|
|
24
|
+
euros: '\1 euros',
|
|
25
|
+
pounds: '\1 pounds',
|
|
26
|
+
yen: '\1 yen',
|
|
27
|
+
}
|
|
28
|
+
CURRENCIES_COMPLEX = {
|
|
29
|
+
dollars_cents: '\1 dollars \2 cents',
|
|
30
|
+
euros_cents: '\1 euros \2 cents',
|
|
31
|
+
pounds_pence: '\1 pounds \2 pence',
|
|
32
|
+
}
|
|
33
|
+
CURRENCIES = CURRENCIES_SIMPLE.merge(CURRENCIES_COMPLEX)
|
|
34
|
+
|
|
35
|
+
HTML_ENTITIES = {
|
|
36
|
+
amp: "and",
|
|
37
|
+
cent: " cents",
|
|
38
|
+
copy: "(c)",
|
|
39
|
+
deg: " degrees ",
|
|
40
|
+
divide: " divided by ",
|
|
41
|
+
double_quote: '"',
|
|
42
|
+
ellipsis: "...",
|
|
43
|
+
en_dash: "-",
|
|
44
|
+
em_dash: "--",
|
|
45
|
+
frac14: "one fourth",
|
|
46
|
+
frac12: "half",
|
|
47
|
+
frac34: "three fourths",
|
|
48
|
+
gt: ">",
|
|
49
|
+
lt: "<",
|
|
50
|
+
nbsp: " ",
|
|
51
|
+
pound: " pounds ",
|
|
52
|
+
reg: "(r)",
|
|
53
|
+
single_quote: "'",
|
|
54
|
+
times: "x",
|
|
55
|
+
trade: "(tm)",
|
|
56
|
+
yen: " yen "
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
TRANSLITERATIONS = {}
|
|
60
|
+
|
|
61
|
+
# Ordered by denominator then numerator of the value
|
|
62
|
+
VULGAR_FRACTIONS = {
|
|
63
|
+
half: "half",
|
|
64
|
+
one_third: "one third",
|
|
65
|
+
two_thirds: "two thirds",
|
|
66
|
+
one_fourth: "one fourth",
|
|
67
|
+
three_fourths: "three fourths",
|
|
68
|
+
one_fifth: "one fifth",
|
|
69
|
+
two_fifths: "two fifths",
|
|
70
|
+
three_fifths: "three fifths",
|
|
71
|
+
four_fifths: "four fifths",
|
|
72
|
+
one_sixth: "one sixth",
|
|
73
|
+
five_sixths: "five sixths",
|
|
74
|
+
one_eighth: "one eighth",
|
|
75
|
+
three_eighths: "three eighths",
|
|
76
|
+
five_eighths: "five eighths",
|
|
77
|
+
seven_eighths: "seven eighths",
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class << self
|
|
81
|
+
%w{characters currencies html_entities transliterations vulgar_fractions}.each do |conversion_type|
|
|
82
|
+
define_method conversion_type do
|
|
83
|
+
const_get conversion_type.upcase
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
require 'stringex/localization/converter'
|
|
4
|
+
require 'stringex/localization/default_conversions'
|
|
5
|
+
require 'stringex/localization/backend/base'
|
|
6
|
+
require 'stringex/localization/backend/internal'
|
|
7
|
+
require 'stringex/localization/backend/i18n'
|
|
8
|
+
|
|
9
|
+
module Stringex
|
|
10
|
+
module Localization
|
|
11
|
+
include DefaultConversions
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
def backend
|
|
15
|
+
@backend ||= i18n_present? ? Backend::I18n : Backend::Internal
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def backend=(sym_or_class)
|
|
19
|
+
if sym_or_class.is_a?(Symbol)
|
|
20
|
+
@backend = case sym_or_class
|
|
21
|
+
when :internal
|
|
22
|
+
Backend::Internal
|
|
23
|
+
when :i18n
|
|
24
|
+
ensure_i18n!
|
|
25
|
+
Backend::I18n
|
|
26
|
+
else
|
|
27
|
+
raise "Invalid backend :#{sym_or_class}"
|
|
28
|
+
end
|
|
29
|
+
else
|
|
30
|
+
@backend = sym_or_class
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def store_translations(locale, scope, data)
|
|
35
|
+
backend.store_translations(locale, scope, data)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def translate(scope, key, options = {})
|
|
39
|
+
return if key == "." # I18n doesn't support dots as translation keys so we don't either
|
|
40
|
+
|
|
41
|
+
locale = options[:locale] || self.locale
|
|
42
|
+
|
|
43
|
+
translation = initial_translation(scope, key, locale)
|
|
44
|
+
|
|
45
|
+
return translation unless translation.nil?
|
|
46
|
+
|
|
47
|
+
if locale != default_locale
|
|
48
|
+
translate scope, key, options.merge(locale: default_locale)
|
|
49
|
+
else
|
|
50
|
+
default_conversion(scope, key) || options[:default]
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def locale
|
|
55
|
+
backend.locale
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def locale=(new_locale)
|
|
59
|
+
backend.locale = new_locale
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def default_locale
|
|
63
|
+
backend.default_locale
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def default_locale=(new_locale)
|
|
67
|
+
backend.default_locale = new_locale
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def with_locale(new_locale, &block)
|
|
71
|
+
new_locale = default_locale if new_locale == :default
|
|
72
|
+
backend.with_locale new_locale, &block
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def with_default_locale(&block)
|
|
76
|
+
with_locale default_locale, &block
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def reset!
|
|
80
|
+
backend.reset!
|
|
81
|
+
@backend = nil
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def convert(string, options = {}, &block)
|
|
85
|
+
converter = Converter.new(string, options)
|
|
86
|
+
converter.instance_exec(&block)
|
|
87
|
+
converter.smart_strip!
|
|
88
|
+
converter.string
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
private
|
|
92
|
+
|
|
93
|
+
def initial_translation(scope, key, locale)
|
|
94
|
+
backend.initial_translation(scope, key, locale)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def default_conversion(scope, key)
|
|
98
|
+
return unless DefaultConversions.respond_to?(scope)
|
|
99
|
+
DefaultConversions.send(scope)[key]
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def i18n_present?
|
|
103
|
+
defined?(I18n) && I18n.respond_to?(:translate)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def ensure_i18n!
|
|
107
|
+
raise Backend::I18nNotDefined unless defined?(I18n)
|
|
108
|
+
raise Backend::I18nMissingTranslate unless I18n.respond_to?(:translate)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module Stringex
|
|
2
|
+
class Railtie < ::Rails::Railtie #:nodoc:
|
|
3
|
+
initializer "stringex" do |app|
|
|
4
|
+
locales = app.config.i18n.available_locales
|
|
5
|
+
pattern = locales.blank? ? "*" : "{#{locales.join(',')}}"
|
|
6
|
+
files = Dir[File.join(Stringex::Localization::Backend::I18n::LOAD_PATH_BASE, "#{pattern}.yml")]
|
|
7
|
+
I18n.load_path.concat(files)
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
module Stringex
|
|
4
|
+
module StringExtensions
|
|
5
|
+
def self.configure(&block)
|
|
6
|
+
Stringex::Configuration::StringExtensions.configure(&block)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.unconfigure!
|
|
10
|
+
Stringex::Configuration::StringExtensions.unconfigure!
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# These methods are all included into the String class.
|
|
14
|
+
module PublicInstanceMethods
|
|
15
|
+
# Removes specified character from the beginning and/or end of the string and then performs
|
|
16
|
+
# <tt>String#squeeze(character)</tt>, condensing runs of the character within the string.
|
|
17
|
+
#
|
|
18
|
+
# Note: This method has been superceded by ActiveSupport's squish method.
|
|
19
|
+
def collapse(character = " ")
|
|
20
|
+
sub(/^#{character}*/, "").sub(/#{character}*$/, "").squeeze(character)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Converts HTML entities into the respective non-accented letters. Examples:
|
|
24
|
+
#
|
|
25
|
+
# "á".convert_accented_entities # => "a"
|
|
26
|
+
# "ç".convert_accented_entities # => "c"
|
|
27
|
+
# "è".convert_accented_entities # => "e"
|
|
28
|
+
# "î".convert_accented_entities # => "i"
|
|
29
|
+
# "ø".convert_accented_entities # => "o"
|
|
30
|
+
# "ü".convert_accented_entities # => "u"
|
|
31
|
+
#
|
|
32
|
+
# Note: This does not do any conversion of Unicode/ASCII accented-characters. For that
|
|
33
|
+
# functionality please use <tt>to_ascii</tt>.
|
|
34
|
+
def convert_accented_html_entities
|
|
35
|
+
stringex_convert do
|
|
36
|
+
cleanup_accented_html_entities!
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Converts various common plaintext characters to a more URI-friendly representation.
|
|
41
|
+
# Examples:
|
|
42
|
+
#
|
|
43
|
+
# "foo & bar".convert_misc_characters # => "foo and bar"
|
|
44
|
+
# "Chanel #9".convert_misc_characters # => "Chanel number nine"
|
|
45
|
+
# "user@host".convert_misc_characters # => "user at host"
|
|
46
|
+
# "google.com".convert_misc_characters # => "google dot com"
|
|
47
|
+
# "$10".convert_misc_characters # => "10 dollars"
|
|
48
|
+
# "*69".convert_misc_characters # => "star 69"
|
|
49
|
+
# "100%".convert_misc_characters # => "100 percent"
|
|
50
|
+
# "windows/mac/linux".convert_misc_characters # => "windows slash mac slash linux"
|
|
51
|
+
#
|
|
52
|
+
# It allows localization of conversions so you can use it to convert characters into your own language.
|
|
53
|
+
# Example:
|
|
54
|
+
#
|
|
55
|
+
# I18n.backend.store_translations :de, { stringex: { characters: { and: "und" } } }
|
|
56
|
+
# I18n.locale = :de
|
|
57
|
+
# "ich & dich".convert_misc_characters # => "ich und dich"
|
|
58
|
+
#
|
|
59
|
+
# Note: Because this method will convert any & symbols to the string "and",
|
|
60
|
+
# you should run any methods which convert HTML entities (convert_accented_html_entities and convert_miscellaneous_html_entities)
|
|
61
|
+
# before running this method.
|
|
62
|
+
def convert_miscellaneous_characters(options = {})
|
|
63
|
+
stringex_convert(options) do
|
|
64
|
+
normalize_currency!
|
|
65
|
+
translate! :ellipses, :currencies, :abbreviations, :characters, :apostrophes
|
|
66
|
+
cleanup_characters!
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Converts HTML entities (taken from common Textile/RedCloth formattings) into plain text formats.
|
|
71
|
+
#
|
|
72
|
+
# Note: This isn't an attempt at complete conversion of HTML entities, just those most likely
|
|
73
|
+
# to be generated by Textile.
|
|
74
|
+
def convert_miscellaneous_html_entities
|
|
75
|
+
stringex_convert do
|
|
76
|
+
translate! :html_entities
|
|
77
|
+
cleanup_html_entities!
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Converts MS Word 'smart punctuation' to ASCII
|
|
82
|
+
#
|
|
83
|
+
def convert_smart_punctuation
|
|
84
|
+
stringex_convert do
|
|
85
|
+
cleanup_smart_punctuation!
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Converts vulgar fractions from supported HTML entities and Unicode to plain text formats.
|
|
90
|
+
def convert_vulgar_fractions
|
|
91
|
+
stringex_convert do
|
|
92
|
+
translate! :vulgar_fractions
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def convert_unreadable_control_characters
|
|
97
|
+
stringex_convert do
|
|
98
|
+
translate! :unreadable_control_characters
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Returns the string limited in size to the value of limit.
|
|
103
|
+
def limit(limit = nil, truncate_words = true, whitespace_replacement_token = "-")
|
|
104
|
+
if limit.nil?
|
|
105
|
+
self
|
|
106
|
+
else
|
|
107
|
+
truncate_words == false ? self.whole_word_limit(limit, whitespace_replacement_token) : self[0...limit]
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def whole_word_limit(limit, whitespace_replacement_token = "-")
|
|
112
|
+
whole_words = []
|
|
113
|
+
words = self.split(whitespace_replacement_token)
|
|
114
|
+
|
|
115
|
+
words.each do |word|
|
|
116
|
+
if word.size > limit
|
|
117
|
+
break
|
|
118
|
+
else
|
|
119
|
+
whole_words << word
|
|
120
|
+
limit -= (word.size + 1)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
whole_words.join(whitespace_replacement_token)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# Performs multiple text manipulations. Essentially a shortcut for typing them all. View source
|
|
129
|
+
# below to see which methods are run.
|
|
130
|
+
def remove_formatting(options = {})
|
|
131
|
+
strip_html_tags.
|
|
132
|
+
convert_smart_punctuation.
|
|
133
|
+
convert_accented_html_entities.
|
|
134
|
+
convert_vulgar_fractions.
|
|
135
|
+
convert_unreadable_control_characters.
|
|
136
|
+
convert_miscellaneous_html_entities.
|
|
137
|
+
convert_miscellaneous_characters(options).
|
|
138
|
+
to_ascii.
|
|
139
|
+
# NOTE: String#to_ascii may convert some Unicode characters to ascii we'd already transliterated
|
|
140
|
+
# so we need to do it again just to be safe
|
|
141
|
+
convert_miscellaneous_characters(options).
|
|
142
|
+
collapse
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Replace runs of whitespace in string. Defaults to a single space but any replacement
|
|
146
|
+
# string may be specified as an argument. Examples:
|
|
147
|
+
#
|
|
148
|
+
# "Foo bar".replace_whitespace # => "Foo bar"
|
|
149
|
+
# "Foo bar".replace_whitespace("-") # => "Foo-bar"
|
|
150
|
+
def replace_whitespace(replacement = " ")
|
|
151
|
+
gsub(/\s+/, replacement)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# Removes HTML tags from text.
|
|
155
|
+
# NOTE: This code is simplified from Tobias Luettke's regular expression in Typo[http://typosphere.org].
|
|
156
|
+
def strip_html_tags(leave_whitespace = false)
|
|
157
|
+
string = stringex_convert do
|
|
158
|
+
strip_html_tags!
|
|
159
|
+
end
|
|
160
|
+
leave_whitespace ? string : string.replace_whitespace(' ')
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Returns the string converted (via Textile/RedCloth) to HTML format
|
|
164
|
+
# or self [with a friendly warning] if Redcloth is not available.
|
|
165
|
+
#
|
|
166
|
+
# Using <tt>:lite</tt> argument will cause RedCloth to not wrap the HTML in a container
|
|
167
|
+
# P element, which is useful behavior for generating header element text, etc.
|
|
168
|
+
# This is roughly equivalent to ActionView's <tt>textilize_without_paragraph</tt>
|
|
169
|
+
# except that it makes RedCloth do all the work instead of just gsubbing the return
|
|
170
|
+
# from RedCloth.
|
|
171
|
+
def to_html(lite_mode = false)
|
|
172
|
+
if defined?(RedCloth)
|
|
173
|
+
if lite_mode
|
|
174
|
+
RedCloth.new(self, [:lite_mode]).to_html
|
|
175
|
+
else
|
|
176
|
+
if self =~ /<pre>/
|
|
177
|
+
RedCloth.new(self).to_html.tr("\t", "")
|
|
178
|
+
else
|
|
179
|
+
RedCloth.new(self).to_html.tr("\t", "").gsub(/\n\n/, "")
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
else
|
|
183
|
+
warn "String#to_html was called without RedCloth being successfully required"
|
|
184
|
+
self
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Create a URI-friendly representation of the string. This is used internally by
|
|
189
|
+
# acts_as_url[link:classes/Stringex/ActsAsUrl/ClassMethods.html#M000012]
|
|
190
|
+
# but can be called manually in order to generate an URI-friendly version of any string.
|
|
191
|
+
def to_url(options = {})
|
|
192
|
+
return self if options[:exclude] && options[:exclude].include?(self)
|
|
193
|
+
options = stringex_default_options.merge(options)
|
|
194
|
+
whitespace_replacement_token = options[:replace_whitespace_with]
|
|
195
|
+
dummy = remove_formatting(options).
|
|
196
|
+
replace_whitespace(whitespace_replacement_token).
|
|
197
|
+
collapse(whitespace_replacement_token).
|
|
198
|
+
limit(options[:limit], options[:truncate_words], whitespace_replacement_token)
|
|
199
|
+
dummy.downcase! unless options[:force_downcase] == false
|
|
200
|
+
dummy
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
private
|
|
204
|
+
|
|
205
|
+
def stringex_convert(options = {}, &block)
|
|
206
|
+
Localization.convert self, options, &block
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def stringex_default_options
|
|
210
|
+
Stringex::Configuration::StringExtensions.new.settings.marshal_dump
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# These methods are extended onto the String class itself.
|
|
215
|
+
module PublicClassMethods
|
|
216
|
+
# Returns string of random characters with a length matching the specified limit. Excludes 0
|
|
217
|
+
# to avoid confusion between 0 and O.
|
|
218
|
+
def random(limit)
|
|
219
|
+
strong_alphanumerics = %w{
|
|
220
|
+
a b c d e f g h i j k l m n o p q r s t u v w x y z
|
|
221
|
+
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
|
|
222
|
+
1 2 3 4 5 6 7 8 9
|
|
223
|
+
}
|
|
224
|
+
Array.new(limit, "").collect{strong_alphanumerics[rand(61)]}.join
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
|
|
3
|
+
require 'yaml'
|
|
4
|
+
require 'stringex/localization'
|
|
5
|
+
|
|
6
|
+
module Stringex
|
|
7
|
+
module Unidecoder
|
|
8
|
+
# Contains Unicode codepoints, loading as needed from YAML files
|
|
9
|
+
CODEPOINTS = Hash.new{|h, k|
|
|
10
|
+
h[k] = ::YAML.load_file(File.join(File.expand_path(File.dirname(__FILE__)), "unidecoder_data", "#{k}.yml"))
|
|
11
|
+
} unless defined?(CODEPOINTS)
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# Returns string with its UTF-8 characters transliterated to ASCII ones
|
|
15
|
+
#
|
|
16
|
+
# You're probably better off just using the added String#to_ascii
|
|
17
|
+
def decode(string)
|
|
18
|
+
string.chars.map{|char| decoded(char)}.join
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Returns character for the given Unicode codepoint
|
|
22
|
+
def encode(codepoint)
|
|
23
|
+
["0x#{codepoint}".to_i(16)].pack("U")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Returns Unicode codepoint for the given character
|
|
27
|
+
def get_codepoint(character)
|
|
28
|
+
"%04x" % character.unpack("U")[0]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Returns string indicating which file (and line) contains the
|
|
32
|
+
# transliteration value for the character
|
|
33
|
+
def in_yaml_file(character)
|
|
34
|
+
unpacked = character.unpack("U")[0]
|
|
35
|
+
"#{code_group(unpacked)}.yml (line #{grouped_point(unpacked) + 2})"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def decoded(character)
|
|
41
|
+
localized(character) || from_yaml(character)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def localized(character)
|
|
45
|
+
Localization.translate(:transliterations, character)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def from_yaml(character)
|
|
49
|
+
return character unless character.ord > 128
|
|
50
|
+
unpacked = character.unpack("U")[0]
|
|
51
|
+
CODEPOINTS[code_group(unpacked)][grouped_point(unpacked)]
|
|
52
|
+
rescue
|
|
53
|
+
# Hopefully this won't come up much
|
|
54
|
+
# TODO: Make this note something to the user that is reportable to me perhaps
|
|
55
|
+
"?"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Returns the Unicode codepoint grouping for the given character
|
|
59
|
+
def code_group(unpacked_character)
|
|
60
|
+
"x%02x" % (unpacked_character >> 8)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Returns the index of the given character in the YAML file for its codepoint group
|
|
64
|
+
def grouped_point(unpacked_character)
|
|
65
|
+
unpacked_character & 255
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
module Stringex
|
|
72
|
+
module StringExtensions
|
|
73
|
+
module PublicInstanceMethods
|
|
74
|
+
# Returns string with its UTF-8 characters transliterated to ASCII ones. Example:
|
|
75
|
+
#
|
|
76
|
+
# "⠋⠗⠁⠝⠉⠑".to_ascii #=> "france"
|
|
77
|
+
def to_ascii
|
|
78
|
+
Stringex::Unidecoder.decode(self)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|