wikis: normalize Unicode characters in wiki bodies.
* Introduce an abstraction for normalizing attributes. Very loosely modeled after https://github.com/fnando/normalize_attributes. * Normalize wiki bodies to Unicode NFC form. * Normalize Unicode space characters in wiki bodies (strip zero width spaces, normalize line endings to CRLF, normalize Unicode spaces to ASCII spaces). * Trim spaces from the start and end of wiki page bodies. This may cause wiki page diffs to show spaces being removed even when the user didn't explicitly remove the spaces themselves.
This commit is contained in:
@@ -41,6 +41,19 @@ module Danbooru
|
||||
pattern = Regexp.escape(pattern).gsub(/\\\*/, ".*")
|
||||
match?(/\A#{pattern}\z/i)
|
||||
end
|
||||
|
||||
def normalize_whitespace
|
||||
# Normalize various horizontal space characters to ASCII space.
|
||||
text = gsub(/\p{Zs}|\t/, " ")
|
||||
|
||||
# Strip various zero width space characters.
|
||||
text = text.gsub(/[\u180E\u200B\u200C\u200D\u2060\uFEFF]/, "")
|
||||
|
||||
# Normalize various line ending characters to CRLF.
|
||||
text = text.gsub(/\r?\n|\r|\v|\f|\u0085|\u2028|\u2029/, "\r\n")
|
||||
|
||||
text
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user