2014-07-09 23:00:22 -04:00
|
|
|
module Jekyll
|
|
|
|
module Tags
|
|
|
|
class ExampleBlock < Liquid::Block
|
|
|
|
include Liquid::StandardFilters
|
|
|
|
|
|
|
|
# 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
|
2014-12-02 21:46:42 -05:00
|
|
|
SYNTAX = /^([a-zA-Z0-9.+#-]+)((\s+\w+(=((\w|[0-9_-])+|"([0-9]+\s)*[0-9]+"))?)*)$/
|
2014-07-09 23:00:22 -04:00
|
|
|
|
|
|
|
def initialize(tag_name, markup, tokens)
|
|
|
|
super
|
|
|
|
if markup.strip =~ SYNTAX
|
|
|
|
@lang = $1.downcase
|
|
|
|
@options = {}
|
|
|
|
if defined?($2) && $2 != ''
|
|
|
|
# Split along 3 possible forms -- key="<quoted list>", key=value, or key
|
2014-12-02 21:46:42 -05:00
|
|
|
$2.scan(/(?:\w+(?:=(?:(?:\w|[0-9_-])+|"[^"]*")?)?)/) do |opt|
|
2014-07-09 23:00:22 -04:00
|
|
|
key, value = opt.split('=')
|
|
|
|
# If a quoted list, convert to array
|
|
|
|
if value && value.include?("\"")
|
|
|
|
value.gsub!(/"/, "")
|
|
|
|
value = value.split
|
|
|
|
end
|
|
|
|
@options[key.to_sym] = value || true
|
|
|
|
end
|
|
|
|
end
|
2016-06-20 02:02:27 -04:00
|
|
|
@options[:linenos] = false
|
2014-07-09 23:00:22 -04:00
|
|
|
else
|
|
|
|
raise SyntaxError.new <<-eos
|
2016-06-20 02:01:10 -04:00
|
|
|
Syntax Error in tag 'example' while parsing the following markup:
|
2014-07-09 23:00:22 -04:00
|
|
|
|
|
|
|
#{markup}
|
|
|
|
|
2017-09-27 06:08:03 -04:00
|
|
|
Valid syntax: example <lang> [id=foo]
|
2014-07-09 23:00:22 -04:00
|
|
|
eos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
|
|
|
prefix = context["highlighter_prefix"] || ""
|
|
|
|
suffix = context["highlighter_suffix"] || ""
|
|
|
|
code = super.to_s.strip
|
|
|
|
|
|
|
|
output = case context.registers[:site].highlighter
|
|
|
|
|
|
|
|
when 'rouge'
|
|
|
|
render_rouge(code)
|
|
|
|
end
|
|
|
|
|
|
|
|
rendered_output = example(code) + add_code_tag(output)
|
|
|
|
prefix + rendered_output + suffix
|
|
|
|
end
|
|
|
|
|
|
|
|
def example(output)
|
2017-09-27 06:08:03 -04:00
|
|
|
"<div class=\"bd-example\"" + (@options[:id] ? " data-example-id=\"#{@options[:id]}\"" : "") + ">\n#{output}\n</div>"
|
2014-07-09 23:00:22 -04:00
|
|
|
end
|
2016-12-27 17:15:22 -05:00
|
|
|
|
2015-09-03 14:54:01 -04:00
|
|
|
def remove_holderjs(code)
|
2015-12-12 19:01:05 -05:00
|
|
|
code = code.gsub(/data-src="holder.js.+?"/, 'src="..."')
|
2015-09-03 14:54:01 -04:00
|
|
|
end
|
2014-07-09 23:00:22 -04:00
|
|
|
|
2016-12-27 17:15:22 -05:00
|
|
|
def remove_example_classes(code)
|
|
|
|
# Find `bd-` classes and remove them from the highlighted code. Because of how this regex works, it will also
|
|
|
|
# remove classes that are after the `bd-` class. While this is a bug, I left it because it can be helpful too.
|
|
|
|
# To fix the bug, replace `(?=")` with `(?=("|\ ))`.
|
|
|
|
code = code.gsub(/(?!class=".)\ *?bd-.+?(?=")/, "")
|
|
|
|
# Find empty class attributes after the previous regex and remove those too.
|
|
|
|
code = code.gsub(/\ class=""/, "")
|
|
|
|
end
|
|
|
|
|
2014-07-09 23:00:22 -04:00
|
|
|
def render_rouge(code)
|
|
|
|
require 'rouge'
|
|
|
|
formatter = Rouge::Formatters::HTML.new(line_numbers: @options[:linenos], wrap: false)
|
|
|
|
lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
|
2015-09-03 14:54:01 -04:00
|
|
|
code = remove_holderjs(code)
|
2016-12-27 17:15:22 -05:00
|
|
|
code = remove_example_classes(code)
|
2014-07-09 23:00:22 -04:00
|
|
|
code = formatter.format(lexer.lex(code))
|
|
|
|
"<div class=\"highlight\"><pre>#{code}</pre></div>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_code_tag(code)
|
|
|
|
# Add nested <code> tags to code blocks
|
|
|
|
code = code.sub(/<pre>\n*/,'<pre><code class="language-' + @lang.to_s.gsub("+", "-") + '" data-lang="' + @lang.to_s + '">')
|
|
|
|
code = code.sub(/\n*<\/pre>/,"</code></pre>")
|
|
|
|
code.strip
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('example', Jekyll::Tags::ExampleBlock)
|