Module: PublifyCore::StringExt
- Defined in:
- lib/publify_core/string_ext.rb
Constant Summary collapse
- ACCENTS =
{ %w(á à â ä ã Ã Ä Â À) => "a", %w(é è ê ë Ë É È Ê) => "e", %w(í ì î ï I Î Ì) => "i", %w(ó ò ô ö õ Õ Ö Ô Ò) => "o", ["œ"] => "oe", ["ß"] => "ss", %w(ú ù û ü U Û Ù) => "u", %w(ç Ç) => "c" }.freeze
- TYPO_TAG_KEY =
Strips any html markup from a string
TYPO_ATTRIBUTE_KEY = /[\w:_-]+/.freeze
- TYPO_ATTRIBUTE_VALUE =
/(?:[A-Za-z0-9]+|(?:'[^']*?'|"[^"]*?"))/.freeze
- TYPO_ATTRIBUTE =
/(?:#{TYPO_ATTRIBUTE_KEY}(?:\s*=\s*#{TYPO_ATTRIBUTE_VALUE})?)/.freeze
- TYPO_ATTRIBUTES =
/(?:#{TYPO_ATTRIBUTE}(?:\s+#{TYPO_ATTRIBUTE})*)/.freeze
- TAG =
%r{<[!/?\[]?(?:#{TYPO_TAG_KEY}|--)(?:\s+#{TYPO_ATTRIBUTES})?\s*(?:[!/?\]]+|--)?>} .freeze
Instance Method Summary collapse
- #strip_html ⇒ Object
- #to_permalink ⇒ Object
- #to_title(item, settings, params) ⇒ Object
-
#to_url ⇒ Object
Returns a-string-with-dashes when passed ‘a string with dashes’.
Instance Method Details
#strip_html ⇒ Object
46 47 48 |
# File 'lib/publify_core/string_ext.rb', line 46 def strip_html gsub(TAG, "").gsub(/\s+/, " ").strip end |
#to_permalink ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/publify_core/string_ext.rb', line 15 def to_permalink string = self ACCENTS.each do |key, value| string = string.tr(key.join, value) end string = string.tr("'", "-") string.gsub(/<[^>]*>/, "").to_url end |
#to_title(item, settings, params) ⇒ Object
34 35 36 |
# File 'lib/publify_core/string_ext.rb', line 34 def to_title(item, settings, params) TitleBuilder.new(self).build(item, settings, params) end |
#to_url ⇒ Object
Returns a-string-with-dashes when passed ‘a string with dashes’. All special chars are stripped in the process
26 27 28 29 30 31 32 |
# File 'lib/publify_core/string_ext.rb', line 26 def to_url return if nil? s = downcase.tr("\"'", "") s = s.gsub(/\P{Word}/, " ") s.strip.tr_s(" ", "-").tr(" ", "-").sub(/^$/, "-") end |