Skip to content

Handlebars scanner #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 53 additions & 51 deletions lib/coderay/helpers/file_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who came up with this abbreviation? Is it really used? I mean, we're not on DOS any more…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, seems to be standard: https://github.com/donpark/hbs, https://github.com/leshill/handlebars_assets. Well, back to 1985 then…

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha - I blame lazy typists ;)

'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
Expand Down
74 changes: 74 additions & 0 deletions lib/coderay/scanners/handlebars.rb
Original file line number Diff line number Diff line change
@@ -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