1 # Adapted from sinitra-hat: http://github.com/nanoant/sinatra-hat/
3 # Copyright (c) 2009 Adam Strzelecki
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 require 'gettext/tools/parser/ruby'
28 def escape_single_quotes
29 self.gsub(/'/, "\\\\'")
34 # Overriden function that parses Haml tags
35 # Injects gettext call for plain text action.
37 tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
38 nuke_inner_whitespace, action, value, last_line = super(line)
39 @precompiled << "_('#{value.escape_single_quotes}')\n" unless action && action != '!' || action == '!' && value[0..0] == '=' || value.empty?
40 [tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
41 nuke_inner_whitespace, action, value, last_line]
43 # Overriden function that producted Haml plain text
44 # Injects gettext call for plain text action.
46 @precompiled << "_('#{text.escape_single_quotes}')\n"
48 def push_flat_markdown(line)
49 text = line.unstripped
51 @precompiled << "_('#{text.escape_single_quotes}')\n"
53 def push_flat_javascript(line)
54 text = line.unstripped
56 text.gsub(/_\('(([^']|\\')+)'\)/) do |m|
57 @precompiled << "_('#{$1}')"
61 return super(line) if @gettext_filters.nil? || !@gettext_filters.last
62 return send("push_flat_#{@gettext_filters.last}".to_sym, line)
64 def start_filtered(name)
65 @gettext_filters ||= []
66 @gettext_filters.push(name) if ['markdown', 'javascript'].include? name
69 def close_filtered(filter)
80 File.extname(file) == ".haml"
83 def parse(file, ary = [])
84 haml = Haml::Engine.new(IO.readlines(file).join)
85 code = haml.precompiled.split(/$/)
86 GetText::RubyParser.parse_lines(file, code, ary)
90 GetText::RGetText.add_parser(HamlParser)