Class: Jekyll::ExampleComponent
- Inherits:
-
ComponentBlock
- Object
- ComponentBlock
- Jekyll::ExampleComponent
- Includes:
- Liquid::StandardFilters
- Defined in:
- lib/jekyll/jelly/components/blocks/example.rb
Constant Summary collapse
- 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
/^([a-zA-Z0-9.+#-]+)((\s+\w+(=((\w|[0-9_-])+|"([0-9]+\s)*[0-9]+"))?)*)$/
Instance Method Summary collapse
- #add_code_tag(code) ⇒ Object
- #example(code, output) ⇒ Object
-
#initialize(tag_name, markup, tokens) ⇒ ExampleComponent
constructor
A new instance of ExampleComponent.
- #render_rouge(code) ⇒ Object
- #template(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ ExampleComponent
Returns a new instance of ExampleComponent.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jekyll/jelly/components/blocks/example.rb', line 15 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 # $2.scan(/(?:\w+(?:=(?:(?:\w|[0-9_-])+|"[^"]*")?)?)/) do |opt| # 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 @options[:linenos] = "inline" if @options.key?(:linenos) and @options[:linenos] == true end end |
Instance Method Details
#add_code_tag(code) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/jekyll/jelly/components/blocks/example.rb', line 55 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 |
#example(code, output) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jekyll/jelly/components/blocks/example.rb', line 36 def example(code, output) language = @lang.to_s if(language == "html" or language == "") output = "<div class=\"c-card hs-code u-mrg-t-4 u-mrg-b-7\" data-js=\""+@lang.to_s+"\"><div class=\"u-pad-5 hs-code__example\" data-example-id=\"#{@options[:id]}\">\n#{code}\n</div>" else output = "<div class=\"c-card hs-code u-mrg-t-4 u-mrg-b-7\" data-js=\""+@lang.to_s+"\">" end output end |
#render_rouge(code) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/jekyll/jelly/components/blocks/example.rb', line 48 def render_rouge(code) formatter = Rouge::Formatters::HTML.new(line_numbers: @options[:linenos], wrap: false) lexer = Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText code = formatter.format(lexer.lex(code)) "<div class=\"c-clipboard-copy-container js-code-snippet t-bdr-top\"><div class=\"u-pad-5 hs-code__highlight highlight\"><pre>#{code}</pre></div></div></div>" end |
#template(context) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jekyll/jelly/components/blocks/example.rb', line 62 def template(context) content = @props["content"] prefix = context["highlighter_prefix"] || "" suffix = context["highlighter_suffix"] || "" code = content.to_s.strip output = case context.registers[:site].highlighter when 'rouge' render_rouge(code) end if (@lang.to_s == "html") rendered_output = example(code, output) + add_code_tag(output) else rendered_output = example(code, output) + '</div>' end prefix + rendered_output + suffix end |