Class: JekyllPreModule::PreTagBlock

Inherits:
JekyllSupport::JekyllBlock
  • Object
show all
Includes:
JekyllPreVersion
Defined in:
lib/pre_tag_block.rb

Constant Summary collapse

@@prefix =
"<button class='copyBtn' data-clipboard-target="
@@suffix =
" title='Copy to clipboard'><img src='/assets/images/clippy.svg' " \
"alt='Copy to clipboard' style='width: 13px'></button>"

Constants included from JekyllPreVersion

JekyllPreVersion::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.highlight(content, pattern) ⇒ Object



13
14
15
# File 'lib/pre_tag_block.rb', line 13

def self.highlight(content, pattern)
  content.gsub(Regexp.new(pattern), "<span class='bg_yellow'>\\0</span>")
end

.make_copy_button(pre_id) ⇒ Object



17
18
19
# File 'lib/pre_tag_block.rb', line 17

def self.make_copy_button(pre_id)
  "#{@@prefix}'##{pre_id}'#{@@suffix}"
end

.number_content(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pre_tag_block.rb', line 21

def self.number_content(content)
  lines = content.split("\n")
  digits = lines.length.to_s.length
  i = 0
  numbered_content = lines.map do |line|
    i += 1
    number = i.to_s.rjust(digits, ' ')
    "<span class='unselectable numbered_line'> #{number}: </span>#{line}"
  end
  result = numbered_content.join("\n")
  result += "\n" unless result.end_with?("\n")
  result
end

.remove_surrounding(text) ⇒ Object

remove leading blank lines and trailing whitespace



36
37
38
# File 'lib/pre_tag_block.rb', line 36

def self.remove_surrounding(text)
  text.gsub(/\A(\s?\n)*/, '').rstrip
end

Instance Method Details

#option(name) ⇒ Object



40
41
42
43
44
45
# File 'lib/pre_tag_block.rb', line 40

def option(name)
  value = @helper.parameter_specified? name
  return value unless value.nil?

  @pre_config[name] if @pre_config
end

#render_impl(text) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pre_tag_block.rb', line 47

def render_impl(text)
  text = PreTagBlock.remove_surrounding text

  @helper.gem_file __FILE__ # Enables plugin attribution

  @pre_config = @config['pre']

  @class            = option 'class'
  @clear            = option 'clear'
  @dark             = ' dark' if option 'dark'
  @dedent           = option 'dedent'
  @highlight        = option 'highlight'
  @id               = option 'id'
  @make_copy_button = option 'copyButton'
  @number_lines     = option 'number'
  @style            = option 'style'
  @wrapper_class    = option 'wrapper_class'
  @wrapper_style    = option 'wrapper_style'

  @class = @class ? " #{@class}" : ''
  @style = @style ? " style='#{@style}'" : ''
  @wrapper_class = @wrapper_class ? " #{@wrapper_class}" : ''
  @wrapper_style = @wrapper_style ? " style='#{@wrapper_style}'" : ''

  # If a label was specified, use it, otherwise concatenate any dangling parameters and use that as the label
  label_implicit = @helper.argv.join(' ')
  label_option   = option 'label'
  @label = label_option && label_option.to_s != 'true' ? label_option : label_implicit

  @logger.debug { "@make_copy_button = '#{@make_copy_button}'; @label = '#{@label}'" }

  text = text.dedent if @dedent
  make_pre(text)
end