Sha256: 409b660d74fc3de9c49545bf2c4e204352f022801912d6c02c869bbc1580015b
Contents?: true
Size: 907 Bytes
Versions: 26
Compression:
Stored size: 907 Bytes
Contents
# frozen_string_literal: true require 'rouge' module Bs5 class ExampleComponent < ViewComponent::Base include ComponentsHelper attr_reader :snippet def initialize(snippet:) @snippet = snippet @path = snippet.split('/') @basename = @path.pop end def highlight formatter = Rouge::Formatters::HTML.new lexer = Rouge::Lexer.find(language) formatter.format(lexer.lex(file_content)).html_safe # rubocop:disable Rails/OutputSafety end private def file_content @file_content ||= file.read end def file @file ||= examples_path .join(*@path) .children .find { |ch| ch.basename.to_s.start_with?(@basename) } end def examples_path Engine.root.join('app', 'views', 'bs5', 'examples') end def language file.extname.delete('.') end end end
Version data entries
26 entries across 26 versions & 1 rubygems