Class: Stratus::SourcecodeTag

Inherits:
Liquid::Block
  • Object
show all
Includes:
Liquid::StandardFilters
Defined in:
lib/stratus/tags/sourcecode.rb

Constant Summary collapse

Syntax =
/['|"]+(.*)['|"]+/
EXTS =
{
  'rb'   => 'ruby',
  'ruby'   => 'ruby',
  'css'  => 'css',
  'js'   => 'javascript',
  'javascript'   => 'javascript',
  'html' => 'html',
  'xml'  => 'html'
}
LANG_BY_EXT =
Hash.new {|h,k| h[k] = EXTS.fetch(k, k) }

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, lang, tokens) ⇒ SourcecodeTag

Returns a new instance of SourcecodeTag.



19
20
21
22
23
24
25
26
# File 'lib/stratus/tags/sourcecode.rb', line 19

def initialize(tag_name, lang, tokens)
  super(tag_name, lang, tokens)
  if lang =~ Syntax
    @lang = LANG_BY_EXT[$1]
  else
    raise SyntaxError.new("Syntax Error in tag 'sourcecode' - Valid syntax: sourcecode [language]")
  end
end

Instance Method Details

#render(context) ⇒ Object



28
29
30
# File 'lib/stratus/tags/sourcecode.rb', line 28

def render(context)
  %Q|<pre><code class="#{@lang}">#{escape(super.to_s)}</code></pre>|
end