Class: Jekyll::Tags::HighlightBlockParam
- Inherits:
-
Liquid::Block
- Object
- Liquid::Block
- Jekyll::Tags::HighlightBlockParam
- 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
-
#initialize(tag_name, markup, tokens) ⇒ HighlightBlockParam
constructor
A new instance of HighlightBlockParam.
- #isNilOrEmpty(var) ⇒ Object
- #render(context) ⇒ Object
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, " Syntax Error in tag '\#{tag_name}' while parsing the following markup:\n\n \#{markup}\n\n Valid syntax: \#{tag_name} <lang> [linenos]\n \\tOR: \#{tag_name} {{ lang_variable }} [linenos]\n \\tOR: \#{tag_name} <lang> {{ [linenos_variable(s)] }}\n \\tOR: \#{tag_name} {{ lang_variable }} {{ [linenos_variable(s)] }}\n MSG\n end\nend\n" |
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, " Language characters can only include Alphanumeric and the following characters, without spaces: . + # _ -\n Your passed language variable: \#{@lang}\n MSG\n end\n elsif @matched[\"lang\"]\n @lang = @matched[\"lang\"].downcase\n else\n raise SyntaxError, <<~MSG\n Unknown Syntax Error in tag 'highlight_param'.\n Please review tag documentation.\n MSG\n end\n\n # puts @lang\n\n if @matched[\"params_var\"]\n @highlight_options = parse_options(@matched[\"params_var\"])\n elsif @matched[\"params\"]\n @highlight_options = parse_options(@matched[\"params\"])\n else\n @highlight_options = parse_options(\"\")\n end\n\n # puts @highlight_options\n\n output =\n case context.registers[:site].highlighter\n when \"rouge\"\n render_rouge(code)\n when \"pygments\"\n render_pygments(code, context)\n else\n render_codehighlighter(code)\n end\n\n rendered_output = add_code_tag(output)\n prefix + rendered_output + suffix\nend\n" |