Module: EZML::Util
Instance Method Summary collapse
- #balance(scanner, start, finish, count = 0) ⇒ Object
- #check_encoding(str) ⇒ Object
- #check_ezml_encoding(str, &block) ⇒ Object
- #contains_interpolation?(str) ⇒ Boolean
- #handle_interpolation(str) ⇒ Object
- #html_safe(text) ⇒ Object
- #human_indentation(indentation) ⇒ Object
- #inspect_obj(obj) ⇒ Object
- #silence_warnings ⇒ Object
- #unescape_interpolation(str, escape_html = nil) ⇒ Object
Instance Method Details
#balance(scanner, start, finish, count = 0) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ezml/util.rb', line 76 def balance(scanner, start, finish, count = 0) str = ''.dup scanner = StringScanner.new(scanner) unless scanner.is_a? StringScanner regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE) while scanner.scan(regexp) str << scanner.matched count += 1 if scanner.matched[-1] == start count -= 1 if scanner.matched[-1] == finish return [str.strip, scanner.rest] if count == 0 end end |
#check_encoding(str) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ezml/util.rb', line 21 def check_encoding(str) if str.valid_encoding? # Get rid of the Unicode BOM if possible # Shortcut for UTF-8 which might be the majority case if str.encoding == Encoding::UTF_8 return str.gsub(/\A\uFEFF/, '') elsif str.encoding.name =~ /^UTF-(16|32)(BE|LE)?$/ return str.gsub(Regexp.new("\\A\uFEFF".encode(str.encoding)), '') else return str end end encoding = str.encoding newlines = Regexp.new("\r\n|\r|\n".encode(encoding).force_encoding(Encoding::ASCII_8BIT)) str.force_encoding(Encoding::ASCII_8BIT).split(newlines).each_with_index do |line, i| begin line.encode(encoding) rescue Encoding::UndefinedConversionError => e yield <<MSG.rstrip, i + 1 Invalid #{encoding.name} character #{e.error_char.dump} MSG end end return str end |
#check_ezml_encoding(str, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ezml/util.rb', line 48 def check_ezml_encoding(str, &block) str = str.dup if str.frozen? bom, encoding = parse_ezml_magic_comment(str) if encoding; str.force_encoding(encoding) elsif bom; str.force_encoding(Encoding::UTF_8) end return check_encoding(str, &block) end |
#contains_interpolation?(str) ⇒ Boolean
101 102 103 |
# File 'lib/ezml/util.rb', line 101 def contains_interpolation?(str) /#[\{$@]/ === str end |
#handle_interpolation(str) ⇒ Object
70 71 72 73 74 |
# File 'lib/ezml/util.rb', line 70 def handle_interpolation(str) scan = StringScanner.new(str) yield scan while scan.scan(/(.*?)(\\*)#([\{@$])/) scan.rest end |
#html_safe(text) ⇒ Object
16 17 18 19 |
# File 'lib/ezml/util.rb', line 16 def html_safe(text) return unless text text.html_safe end |
#human_indentation(indentation) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ezml/util.rb', line 88 def human_indentation(indentation) if !indentation.include?(?\t) noun = 'space' elsif !indentation.include?(?\s) noun = 'tab' else return indentation.inspect end singular = indentation.length == 1 "#{indentation.length} #{noun}#{'s' unless singular}" end |
#inspect_obj(obj) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ezml/util.rb', line 59 def inspect_obj(obj) case obj when String %Q!"#{obj.gsub(/[\x00-\x7F]+/) {|s| s.dump[1...-1]}}"! when Symbol ":#{inspect_obj(obj.to_s)}" else obj.inspect end end |
#silence_warnings ⇒ Object
9 10 11 12 13 14 |
# File 'lib/ezml/util.rb', line 9 def silence_warnings the_real_stderr, $stderr = $stderr, StringIO.new yield ensure $stderr = the_real_stderr end |
#unescape_interpolation(str, escape_html = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ezml/util.rb', line 105 def unescape_interpolation(str, escape_html = nil) res = ''.dup rest = Haml::Util.handle_interpolation str.dump do |scan| escapes = (scan[2].size - 1) / 2 char = scan[3] # '{', '@' or '$' res << scan.matched[0...-3 - escapes] if escapes % 2 == 1 res << "\##{char}" else interpolated = if char == '{' balance(scan, ?{, ?}, 1)[0][0...-1] else scan.scan(/\w+/) end content = eval("\"#{interpolated}\"") content.prepend(char) if char == '@' || char == '$' content = "Haml::Helpers.html_escape((#{content}))" if escape_html res << "\#{#{content}}" end end res + rest end |