Class: Code::Component

Inherits:
Lookbook::BaseComponent
  • Object
show all
Includes:
Lookbook::OutputHelper
Defined in:
app/components/lookbook/code/component.rb

Instance Method Summary collapse

Methods included from Lookbook::OutputHelper

#beautify, #highlight, #markdown, #pretty_json

Constructor Details

#initialize(source: nil, language: :html, line_numbers: false, highlight_lines: [], start_line: 1, wrap: false, theme: nil, dark: false, full_height: false, **html_attrs) ⇒ Component

Returns a new instance of Component.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/components/lookbook/code/component.rb', line 5

def initialize(
  source: nil,
  language: :html,
  line_numbers: false,
  highlight_lines: [],
  start_line: 1,
  wrap: false,
  theme: nil,
  dark: false,
  full_height: false,
  **html_attrs
)
  @source_code = source
  @highlight_opts = {
    language: language,
    line_numbers: line_numbers,
    highlight_lines: highlight_lines,
    start_line: start_line
  }
  @highlight_lines = highlight_lines
  @wrap = wrap
  @theme = theme
  @dark = dark
  @full_height = full_height
  super(**html_attrs)
end

Instance Method Details

#before_renderObject



56
57
58
59
# File 'app/components/lookbook/code/component.rb', line 56

def before_render
  @theme ||= Lookbook.config.highlighter_options.fetch(:theme, :github).to_sym
  @dark ||= !!Lookbook.config.highlighter_options.fetch(:dark, false)
end

#focussed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'app/components/lookbook/code/component.rb', line 44

def focussed?
  @highlight_opts[:highlight_lines].any?
end

#full_height?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/components/lookbook/code/component.rb', line 48

def full_height?
  @full_height
end

#is_dark?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/components/lookbook/code/component.rb', line 52

def is_dark?
  @dark
end

#numbered?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/components/lookbook/code/component.rb', line 40

def numbered?
  @highlight_opts[:line_numbers] == true
end

#sourceObject



36
37
38
# File 'app/components/lookbook/code/component.rb', line 36

def source
  (@source_code || content).strip_heredoc.strip
end

#theme_classnameObject



32
33
34
# File 'app/components/lookbook/code/component.rb', line 32

def theme_classname
  "theme-#{@theme.to_s.tr("_", "-")}"
end