-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from publify/move-string-extensions
Move String monkey-patches into a module under PublifyCore
- Loading branch information
Showing
6 changed files
with
55 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
# FIXME: Replace with helpers and/or methods provided by Rails | ||
module PublifyCore | ||
module StringExt | ||
ACCENTS = { %w(á à â ä ã Ã Ä Â À) => "a", | ||
%w(é è ê ë Ë É È Ê) => "e", | ||
%w(í ì î ï I Î Ì) => "i", | ||
%w(ó ò ô ö õ Õ Ö Ô Ò) => "o", | ||
["œ"] => "oe", | ||
["ß"] => "ss", | ||
%w(ú ù û ü U Û Ù) => "u", | ||
%w(ç Ç) => "c" }.freeze | ||
|
||
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 | ||
|
||
# Returns a-string-with-dashes when passed 'a string with dashes'. | ||
# All special chars are stripped in the process | ||
def to_url | ||
return if nil? | ||
|
||
s = downcase.tr("\"'", "") | ||
s = s.gsub(/\P{Word}/, " ") | ||
s.strip.tr_s(" ", "-").tr(" ", "-").sub(/^$/, "-") | ||
end | ||
|
||
def to_title(item, settings, params) | ||
TitleBuilder.new(self).build(item, settings, params) | ||
end | ||
|
||
# Strips any html markup from a string | ||
TYPO_TAG_KEY = 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 | ||
def strip_html | ||
gsub(TAG, "").gsub(/\s+/, " ").strip | ||
end | ||
end | ||
end | ||
|
||
String.include PublifyCore::StringExt |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters