Class: PhlexUI::Codeblock

Inherits:
Base
  • Object
show all
Defined in:
lib/phlex_ui/codeblock.rb

Constant Summary collapse

FORMATTER =
::Rouge::Formatters::HTML.new
ROUGE_CSS =
Rouge::Themes::Github.mode(:dark).render(scope: ".highlight")

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

#before_template

Constructor Details

#initialize(code, syntax:, clipboard: true, clipboard_success: "Copied!", clipboard_error: "Copy failed!", **attrs) ⇒ Codeblock

Returns a new instance of Codeblock.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/phlex_ui/codeblock.rb', line 10

def initialize(code, syntax:, clipboard: true, clipboard_success: "Copied!", clipboard_error: "Copy failed!", **attrs)
  @code = code
  @syntax = syntax.to_sym
  @clipboard = clipboard
  @clipboard_success = clipboard_success
  @clipboard_error = clipboard_error

  if @syntax == :ruby || @syntax == :html
    @code = @code.gsub(/(?:^|\G) {2}/m, "	")
  end

  super(**attrs)
end

Instance Method Details

#view_templateObject



24
25
26
27
28
29
30
31
# File 'lib/phlex_ui/codeblock.rb', line 24

def view_template
  style { ROUGE_CSS.html_safe } # For faster load times, move this to the head of your document. (Also move ROUGE_CSS value to head of document)
  if @clipboard
    with_clipboard
  else
    codeblock
  end
end