diff --git a/lib/coderay/helpers/file_type.rb b/lib/coderay/helpers/file_type.rb index a0f9fd61..e6e8aece 100644 --- a/lib/coderay/helpers/file_type.rb +++ b/lib/coderay/helpers/file_type.rb @@ -77,57 +77,59 @@ def shebang filename end TypeFromExt = { - 'c' => :c, - 'cfc' => :xml, - 'cfm' => :xml, - 'clj' => :clojure, - 'css' => :css, - 'diff' => :diff, - 'dpr' => :delphi, - 'erb' => :erb, - 'gemspec' => :ruby, - 'groovy' => :groovy, - 'gvy' => :groovy, - 'h' => :c, - 'haml' => :haml, - 'htm' => :html, - 'html' => :html, - 'html.erb' => :erb, - 'java' => :java, - 'js' => :java_script, - 'json' => :json, - 'lua' => :lua, - 'mab' => :ruby, - 'pas' => :delphi, - 'patch' => :diff, - 'phtml' => :php, - 'php' => :php, - 'php3' => :php, - 'php4' => :php, - 'php5' => :php, - 'prawn' => :ruby, - 'py' => :python, - 'py3' => :python, - 'pyw' => :python, - 'rake' => :ruby, - 'raydebug' => :raydebug, - 'rb' => :ruby, - 'rbw' => :ruby, - 'rhtml' => :erb, - 'rjs' => :ruby, - 'rpdf' => :ruby, - 'ru' => :ruby, # config.ru - 'rxml' => :ruby, - 'sass' => :sass, - 'sql' => :sql, - 'taskpaper' => :taskpaper, - 'template' => :json, # AWS CloudFormation template - 'tmproj' => :xml, - 'xaml' => :xml, - 'xhtml' => :html, - 'xml' => :xml, - 'yaml' => :yaml, - 'yml' => :yaml, + 'c' => :c, + 'cfc' => :xml, + 'cfm' => :xml, + 'clj' => :clojure, + 'css' => :css, + 'diff' => :diff, + 'dpr' => :delphi, + 'erb' => :erb, + 'gemspec' => :ruby, + 'groovy' => :groovy, + 'gvy' => :groovy, + 'h' => :c, + 'haml' => :haml, + 'handlebars' => :handlebars, + 'hbs' => :handlebars, + 'htm' => :html, + 'html' => :html, + 'html.erb' => :erb, + 'java' => :java, + 'js' => :java_script, + 'json' => :json, + 'lua' => :lua, + 'mab' => :ruby, + 'pas' => :delphi, + 'patch' => :diff, + 'phtml' => :php, + 'php' => :php, + 'php3' => :php, + 'php4' => :php, + 'php5' => :php, + 'prawn' => :ruby, + 'py' => :python, + 'py3' => :python, + 'pyw' => :python, + 'rake' => :ruby, + 'raydebug' => :raydebug, + 'rb' => :ruby, + 'rbw' => :ruby, + 'rhtml' => :erb, + 'rjs' => :ruby, + 'rpdf' => :ruby, + 'ru' => :ruby, # config.ru + 'rxml' => :ruby, + 'sass' => :sass, + 'sql' => :sql, + 'taskpaper' => :taskpaper, + 'template' => :json, # AWS CloudFormation template + 'tmproj' => :xml, + 'xaml' => :xml, + 'xhtml' => :html, + 'xml' => :xml, + 'yaml' => :yaml, + 'yml' => :yaml } for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu] TypeFromExt[cpp_alias] = :cpp diff --git a/lib/coderay/scanners/handlebars.rb b/lib/coderay/scanners/handlebars.rb new file mode 100644 index 00000000..08f0bfd6 --- /dev/null +++ b/lib/coderay/scanners/handlebars.rb @@ -0,0 +1,74 @@ +module CodeRay +module Scanners + + load :html + + # Scanner for Handlebars templates. + class Handlebars < Scanner + + register_for :handlebars + title 'Handlebars Template' + + KINDS_NOT_LOC = HTML::KINDS_NOT_LOC + + HANDLEBARS_BLOCK = / + ({{[\{\#\/]?) + (.*?) + ([\}]}}?) + /xm # :nodoc: + + HANDLEBARS_COMMENT_BLOCK = / + {{! + (.*?) + }} + /xm # :nodoc: + + START_OF_HANDLEBARS = /{{/ # :nodoc: + + protected + + def setup + @html_scanner = CodeRay.scanner :html, :tokens => @tokens, :keep_tokens => true, :keep_state => true + @handlebars_attribute_scanner = CodeRay.scanner :html, :tokens => @tokens, :keep_tokens => true, :keep_state => false + end + + def reset_instance + super + @html_scanner.reset + @handlebars_attribute_scanner.reset + end + + def scan_tokens encoder, options + until eos? + if (match = scan_until(/(?=#{START_OF_HANDLEBARS})/o) || scan_rest) and not match.empty? + @html_scanner.tokenize match, :tokens => encoder + + elsif match = scan(/#{HANDLEBARS_COMMENT_BLOCK}/o) + encoder.text_token match, :comment + + elsif match = scan(/#{HANDLEBARS_BLOCK}/o) + start_tag = self[1] + code = self[2] + end_tag = self[3] + + encoder.begin_group :inline + encoder.text_token start_tag, :inline_delimiter + + unless code.empty? + @handlebars_attribute_scanner.tokenize code, :tokens => encoder, :state => :attribute + end + + encoder.text_token end_tag, :inline_delimiter unless end_tag.empty? + encoder.end_group :inline + + else + raise_inspect 'else-case reached!', encoder + end + end + encoder + end + + end + +end +end