Class: Jekyll::Tags::HighlightBlockParam

Inherits:
Liquid::Block
  • Object
show all
Includes:
Liquid::StandardFilters
Defined in:
lib/jekyll-highlight-param.rb

Constant Summary collapse

PARAM_SYNTAX =

The regular expression syntax checker. Start with the language specifier. Follow that by zero or more space separated options that take one of three forms: name, name=value, or name=“<quoted list>”

<quoted list> is a space-separated list of numbers

Both the language specifier and the options can be passed as liquid variables, please consult the documentation at github.com/UriShX/jekyll-highlight-param/blob/master/README.md#usage.

%r!(\w+([.]\w+)*)!x.freeze
LANG_SYNTAX =
%r!([a-zA-Z0-9.+#_-]+)!x.freeze
OPTIONS_SYNTAX =
%r!(\s+\w+(=(\w+|"([0-9]+\s)*[0-9]+")?)*)!.freeze
VARIABLE_SYNTAX =
%r!
  ^(
    \{\{\s*
    (?<lang_var>#{PARAM_SYNTAX})
    \s*\}\}|
    (?<lang>#{LANG_SYNTAX})
  )
  \s*
  ((?<fault1>[}]+\s*|)
  (
    \{\{\s*
    (?<params_var>(#{PARAM_SYNTAX}))
    \s*\}\}|
    (?<params>(#{OPTIONS_SYNTAX}+))
  )
  (?<fault2>.*))?
!mx.freeze
LEADING_OR_TRAILING_LINE_TERMINATORS =
%r!\A(\n|\r)+|(\n|\r)+\z!.freeze

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ HighlightBlockParam

Returns a new instance of HighlightBlockParam.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll-highlight-param.rb', line 47

def initialize(tag_name, markup, tokens)
  super
  markup  = markup.strip
  @matched = markup.match(VARIABLE_SYNTAX)
  # print @matched.captures.to_s + "\n"
  if !@matched or !isNilOrEmpty(@matched["fault1"]) or !isNilOrEmpty(@matched["fault2"])
    raise SyntaxError, <<~MSG
      Syntax Error in tag '#{tag_name}' while parsing the following markup:

      #{markup}

      Valid syntax: #{tag_name} <lang> [linenos]
                \tOR: #{tag_name} {{ lang_variable }} [linenos]
                \tOR: #{tag_name} <lang> {{ [linenos_variable(s)] }}
                \tOR: #{tag_name} {{ lang_variable }} {{ [linenos_variable(s)] }}
    MSG
  end
end

Instance Method Details

#isNilOrEmpty(var) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/jekyll-highlight-param.rb', line 37

def isNilOrEmpty(var)
  if var.nil?
    return true
  elsif var.strip.empty?
    return true
  else
    return false
  end
end

#render(context) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/jekyll-highlight-param.rb', line 68

def render(context)
  prefix = context["highlighter_prefix"] || ""
  suffix = context["highlighter_suffix"] || ""
  code = super.to_s.gsub(LEADING_OR_TRAILING_LINE_TERMINATORS, "")

  if @matched["lang_var"]
    @lang = context[@matched["lang_var"]].downcase
    @lang.match(LANG_SYNTAX)
    unless $& == @lang
      raise ArgumentError, <<~MSG
        Language characters can only include Alphanumeric and the following characters, without spaces: . + # _ -
        Your passed language variable: #{@lang}
        MSG
    end
  elsif @matched["lang"]
    @lang = @matched["lang"].downcase
  else
    raise SyntaxError, <<~MSG
      Unknown Syntax Error in tag 'highlight_param'.
      Please review tag documentation.
      MSG
  end

  # puts @lang

  if @matched["params_var"]
    @highlight_options = parse_options(@matched["params_var"])
  elsif @matched["params"]
    @highlight_options = parse_options(@matched["params"])
  else
    @highlight_options = parse_options("")
  end

  # puts @highlight_options

  output =
    case context.registers[:site].highlighter
    when "rouge"
      render_rouge(code)
    when "pygments"
      render_pygments(code, context)
    else
      render_codehighlighter(code)
    end

  rendered_output = add_code_tag(output)
  prefix + rendered_output + suffix
end