From d4afb91e49ea3dc0951e97fb49a0e63ed726cfea Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Fri, 7 Apr 2023 06:25:41 +0800 Subject: [PATCH] Update helper with kebab-case conversion --- gems/lib/asciidoctor/chat-block-processor/extension.rb | 2 +- gems/lib/asciidoctor/helpers.rb | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/gems/lib/asciidoctor/chat-block-processor/extension.rb b/gems/lib/asciidoctor/chat-block-processor/extension.rb index ea5d930..52ec823 100644 --- a/gems/lib/asciidoctor/chat-block-processor/extension.rb +++ b/gems/lib/asciidoctor/chat-block-processor/extension.rb @@ -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'] diff --git a/gems/lib/asciidoctor/helpers.rb b/gems/lib/asciidoctor/helpers.rb index e2e7351..2158b38 100644 --- a/gems/lib/asciidoctor/helpers.rb +++ b/gems/lib/asciidoctor/helpers.rb @@ -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