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, start_value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pre_tag_block.rb', line 30

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

.positive_int?(x) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
# File 'lib/pre_tag_block.rb', line 21

def self.positive_int?(x)
  value = begin
    Integer(x)
  rescue StandardError
    false
  end
  value.is_a?(Integer) && value.positive?
end

.remove_surrounding(text) ⇒ Object

remove leading blank lines and trailing whitespace



45
46
47
# File 'lib/pre_tag_block.rb', line 45

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

Instance Method Details

#option(name) ⇒ Object



49
50
51
52
53
54
# File 'lib/pre_tag_block.rb', line 49

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

  @pre_config[name] if @pre_config
end

#render_impl(text) ⇒ Object



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
81
82
83
84
85
86
87
88
89
# File 'lib/pre_tag_block.rb', line 56

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