app/helpers/lookbook/application_helper.rb in lookbook-0.4.6 vs app/helpers/lookbook/application_helper.rb in lookbook-0.4.7
- old
+ new
@@ -1,7 +1,8 @@
require "redcarpet"
require "rouge"
+require "htmlbeautifier"
module Lookbook
module ApplicationHelper
def config
Lookbook::Engine.config.lookbook
@@ -14,16 +15,59 @@
disable_indented_code_blocks: true
})
markdown.render(text).html_safe
end
- def highlight(source, language)
- formatter = Rouge::Formatters::HTML.new(css_class: "highlight")
+ def highlight(source, language, opts = {})
+ formatter = Lookbook::CodeFormatter.new(opts)
lexer = Rouge::Lexer.find(language)
formatter.format(lexer.lex(source)).html_safe
end
- def nav_padding_style(depth)
- "padding-left: calc((#{depth - 1} * 12px) + 0.5rem);"
+ def beautify(source, language = "html")
+ source = source.strip
+ result = language.downcase == "html" ? HtmlBeautifier.beautify(source) : source
+ result.strip.html_safe
+ end
+
+ def icon(name = nil, size: 4, **attrs)
+ render "lookbook/components/icon",
+ name: name,
+ size: size,
+ classes: class_names(attrs[:class]),
+ **attrs.except(:class)
+ end
+
+ def component(name, **attrs, &block)
+ render "lookbook/components/#{name}",
+ classes: class_names(attrs[:class]),
+ **attrs.except(:class),
+ &block
+ end
+
+ if Rails.version.to_f < 6.1
+ def class_names(*args)
+ tokens = build_tag_values(*args).flat_map { |value| value.to_s.split(/\s+/) }.uniq
+ safe_join(tokens, " ")
+ end
+ end
+
+ private
+
+ def build_tag_values(*args)
+ tag_values = []
+ args.each do |tag_value|
+ case tag_value
+ when Hash
+ tag_value.each do |key, val|
+ tag_values << key.to_s if val && key.present?
+ end
+ when Array
+ tag_values.concat build_tag_values(*tag_value)
+ else
+ tag_values << tag_value.to_s if tag_value.present?
+ end
+ end
+ tag_values
end
end
end