Class: FileSource::Component

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path:, source: nil, highlight_lines: [], lines_around_highlight: nil, start_line: 1, end_line: nil, start_line_number: nil, **html_attrs) ⇒ Component

Returns a new instance of Component.



5
6
7
8
9
10
11
12
13
14
15
# File 'app/components/lookbook/file_source/component.rb', line 5

def initialize(file_path:, source: nil, highlight_lines: [], lines_around_highlight: nil,
  start_line: 1, end_line: nil, start_line_number: nil, **html_attrs)
  @file_path = file_path
  @source = source
  @highlight_lines = highlight_lines.map(&:to_i)
  @start_line = start_line
  @end_line = end_line
  @start_line_number = start_line_number
  @lines_around_highlight = lines_around_highlight
  super(**html_attrs)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



3
4
5
# File 'app/components/lookbook/file_source/component.rb', line 3

def file_path
  @file_path
end

#lines_around_highlightObject (readonly)

Returns the value of attribute lines_around_highlight.



3
4
5
# File 'app/components/lookbook/file_source/component.rb', line 3

def lines_around_highlight
  @lines_around_highlight
end

Instance Method Details

#end_lineObject



63
64
65
66
67
68
69
70
71
# File 'app/components/lookbook/file_source/component.rb', line 63

def end_line
  line_count = source_lines.size
  if lines_around_highlight && @highlight_lines.any?
    last = @highlight_lines.last + lines_around_highlight
    (last <= line_count) ? last : line_count
  else
    (@end_line && (@end_line <= line_count)) ? @end_line : line_count
  end
end

#highlight_linesObject



42
43
44
# File 'app/components/lookbook/file_source/component.rb', line 42

def highlight_lines
  @highlight_lines.map { |num| num - start_line + 1 }
end

#render?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/components/lookbook/file_source/component.rb', line 17

def render?
  source.present?
end

#sourceObject



29
30
31
32
33
34
35
# File 'app/components/lookbook/file_source/component.rb', line 29

def source
  return @source if @source.present?

  @_source ||= File.read(file_path)
rescue
  nil
end

#source_langObject



37
38
39
40
# File 'app/components/lookbook/file_source/component.rb', line 37

def source_lang
  lang = Lang.guess(file_path)
  lang.present? ? lang[:name] : "plaintext"
end

#source_linesObject



46
47
48
# File 'app/components/lookbook/file_source/component.rb', line 46

def source_lines
  @_source_lines ||= source&.split("\n")
end

#start_lineObject



50
51
52
53
54
55
56
57
# File 'app/components/lookbook/file_source/component.rb', line 50

def start_line
  if lines_around_highlight && @highlight_lines.any?
    start = @highlight_lines.first - lines_around_highlight
    (start > 0) ? start : 1
  else
    @start_line
  end
end

#start_line_numberObject



59
60
61
# File 'app/components/lookbook/file_source/component.rb', line 59

def start_line_number
  @start_line_number || start_line
end

#trimmed_sourceObject



21
22
23
24
25
26
27
# File 'app/components/lookbook/file_source/component.rb', line 21

def trimmed_source
  return unless source.present?

  from = start_line - 1
  to = end_line - 1
  source_lines[from..to].join("\n")
end