Update helper with kebab-case conversion

This commit is contained in:
Gabriel Arazas 2023-04-07 06:25:41 +08:00
parent ff59ce0fca
commit fbe1feb221
2 changed files with 6 additions and 5 deletions

View File

@ -24,7 +24,7 @@ class ChatBlock < Asciidoctor::Extensions::BlockProcessor
attrs['avatarsdir'] ||= File.expand_path('./avatars', attrs['iconsdir'])
avatar_sticker = "#{attrs['avatar'].to_snake}/#{attrs['state'].to_snake}.#{attrs['avatarstype']}"
avatar_sticker = "#{attrs['avatar'].to_kebab}/#{attrs['state'].to_kebab}.#{attrs['avatarstype']}"
avatar_img_attrs = {
'target' => parent.image_uri(avatar_sticker, 'avatarsdir'),
'alt' => attrs['name']

View File

@ -1,10 +1,11 @@
# frozen_string_literal: true
# Code copied from https://gist.github.com/komasaru/b3f22d5bcb8555deea1707b84d294045.
class String
def to_snake
self.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
def to_kebab
self.gsub(/\s+/, '-') # Replace all spaces with dashes.
.gsub(/[^a-zA-Z0-9-]/, '') # Remove all non-alphanumerical (and dashes) characters.
.gsub(/-+/, '-') # Reduce all dashes into only one.
.gsub(/^-|-+$/, '') # Remove all leading and trailing dashes.
.downcase
end
end