Class: Wikidotrb::Util::StringUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/wikidotrb/util/stringutil.rb

Class Method Summary collapse

Class Method Details

.to_unix(target_str) ⇒ String

Converts a string to Unix-style format

Parameters:

  • target_str (String)

    The string to be converted

Returns:

  • (String)

    The converted string



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wikidotrb/util/stringutil.rb', line 11

def self.to_unix(target_str)
  # NOTE: This implementation matches the legacy wikidot behavior

  # Convert special characters
  special_char_map = Wikidotrb::Table::CharTable::SPECIAL_CHAR_MAP
  target_str = target_str.chars.map { |char| special_char_map[char] || char }.join

  # Convert to lowercase
  target_str = target_str.downcase

  # Remove non-ASCII characters and replace special cases with regex
  target_str = target_str.gsub(/[^a-z0-9\-:_]/, "-")
                         .gsub(/^_/, ":_")
                         .gsub(/(?<!:)_/, "-")
                         .gsub(/^-*/, "")
                         .gsub(/-*$/, "")
                         .gsub(/-{2,}/, "-")
                         .gsub(/:{2,}/, ":")
                         .gsub(":-", ":")
                         .gsub("-:", ":")
                         .gsub("_-", "_")
                         .gsub("-_", "_")

  # Remove ':' from the beginning and end of the string
  target_str.gsub(/^:/, "").gsub(/:$/, "")
end