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.
29 # Inject _ gettext into plain text and tag plain text calls
34 tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
35 nuke_inner_whitespace, action, value, last_line = super(line)
36 value = _(value) unless action && action != '!' || action == '!' && value[0..0] == '=' || value.empty?
37 # translate inline ruby code too
38 value.gsub!(/_\('([^']+)'\)/) {|m| '\''+_($1)+'\''} unless action != '=' || value.empty?
39 attributes_hashes.each{|h| h.each{|v| v.gsub!(/_\('([^']+)'\)/){|m| '\''+_($1)+'\''} if v.is_a? String} unless h.nil? || h.empty?} unless attributes_hashes.nil? || attributes_hashes.empty?
40 [tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
41 nuke_inner_whitespace, action, value, last_line]
43 def push_flat_markdown(line)
45 text = "" unless text.gsub!(/^#{@flat_spaces}/, '')
46 text = _(text) if text != ''
47 @filter_buffer << "#{text}\n"
49 def push_flat_javascript(line)
51 text.gsub!(/_\('(([^']|\\')+)'\)/) {|m| _($1).to_json }
52 @filter_buffer << "#{text}\n"
55 return super(line) if @gettext_filters.nil? || !@gettext_filters.last
56 return send("push_flat_#{@gettext_filters.last}".to_sym, line)
58 def start_filtered(name)
59 @gettext_filters ||= []
60 @gettext_filters.push(name) if ['markdown', 'javascript'].include? name
63 def close_filtered(filter)