haml_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ module HamlParser
2
+ module RubyMultiline
3
+ def self.read(line_parser, current_text)
4
+ buf = []
5
+ while is_ruby_multiline?(current_text)
6
+ current_text = line_parser.next_line
7
+ buf << current_text
8
+ end
9
+ buf
10
+ end
11
+
12
+ # `text' is a Ruby multiline block if it:
13
+ # - ends with a comma
14
+ # - but not "?," which is a character literal
15
+ # (however, "x?," is a method call and not a literal)
16
+ # - and not "?\," which is a character literal
17
+ def self.is_ruby_multiline?(text)
18
+ text && text.length > 1 && text[-1] == ?, &&
19
+ !((text[-3, 2] =~ /\W\?/) || text[-3, 2] == "?\\")
20
+ end
21
+ private_class_method :is_ruby_multiline?
22
+ end
23
+ end
@@ -0,0 +1,107 @@
1
+ require 'haml_parser/ast'
2
+ require 'haml_parser/ruby_multiline'
3
+ require 'haml_parser/error'
4
+
5
+ module HamlParser
6
+ class ScriptParser
7
+ def initialize(line_parser)
8
+ @line_parser = line_parser
9
+ end
10
+
11
+ def parse(text)
12
+ case text[0]
13
+ when '=', '~'
14
+ parse_script(text)
15
+ when '&'
16
+ parse_sanitized(text)
17
+ when '!'
18
+ parse_unescape(text)
19
+ else
20
+ parse_text(text)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def parse_script(text)
27
+ if text[1] == '='
28
+ create_node(Ast::Text) { |t| t.text = text[2 .. -1].strip }
29
+ else
30
+ node = create_node(Ast::Script)
31
+ script = text[1 .. -1].lstrip
32
+ if script.empty?
33
+ syntax_error!('No Ruby code to evaluate')
34
+ end
35
+ node.script = [script, *RubyMultiline.read(@line_parser, script)].join("\n")
36
+ node.preserve = text[0] == '~'
37
+ node
38
+ end
39
+ end
40
+
41
+ def parse_sanitized(text)
42
+ case
43
+ when text.start_with?('&==')
44
+ create_node(Ast::Text) { |t| t.text = text[3 .. -1].lstrip }
45
+ when text[1] == '=' || text[1] == '~'
46
+ node = create_node(Ast::Script)
47
+ script = text[2 .. -1].lstrip
48
+ if script.empty?
49
+ syntax_error!('No Ruby code to evaluate')
50
+ end
51
+ node.script = [script, *RubyMultiline.read(@line_parser, script)].join("\n")
52
+ node.preserve = text[1] == '~'
53
+ node
54
+ else
55
+ create_node(Ast::Text) { |t| t.text = text[1 .. -1].strip }
56
+ end
57
+ end
58
+
59
+ def parse_unescape(text)
60
+ case
61
+ when text.start_with?('!==')
62
+ create_node(Ast::Text) do |t|
63
+ t.text = text[3 .. -1].lstrip
64
+ t.escape_html = false
65
+ end
66
+ when text[1] == '=' || text[1] == '~'
67
+ node = create_node(Ast::Script)
68
+ node.escape_html = false
69
+ script = text[2 .. -1].lstrip
70
+ if script.empty?
71
+ syntax_error!('No Ruby code to evaluate')
72
+ end
73
+ node.script = [script, *RubyMultiline.read(@line_parser, script)].join("\n")
74
+ node.preserve = text[1] == '~'
75
+ node
76
+ else
77
+ create_node(Ast::Text) do |t|
78
+ t.text = text[1 .. -1].lstrip
79
+ t.escape_html = false
80
+ end
81
+ end
82
+ end
83
+
84
+ def parse_text(text)
85
+ text = text.lstrip
86
+ if text.empty?
87
+ nil
88
+ else
89
+ create_node(Ast::Text) { |t| t.text = text }
90
+ end
91
+ end
92
+
93
+ def syntax_error!(message)
94
+ raise Error.new(message, @line_parser.lineno)
95
+ end
96
+
97
+ def create_node(klass, &block)
98
+ klass.new.tap do |node|
99
+ node.filename = @line_parser.filename
100
+ node.lineno = @line_parser.lineno
101
+ if block
102
+ block.call(node)
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,17 @@
1
+ module HamlParser
2
+ module Utils
3
+ module_function
4
+
5
+ def balance(scanner, start, finish, depth = 1)
6
+ re = /(#{Regexp.escape(start)}|#{Regexp.escape(finish)})/
7
+ while depth > 0 && scanner.scan_until(re)
8
+ if scanner.matched == start
9
+ depth += 1
10
+ else
11
+ depth -= 1
12
+ end
13
+ end
14
+ depth
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module HamlParser
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haml_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kohei Suzuki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coveralls
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.3.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.3.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Parser of Haml template language
98
+ email:
99
+ - eagletmt@gmail.com
100
+ executables:
101
+ - haml_parser
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/haml_parser
115
+ - haml_parser.gemspec
116
+ - lib/haml_parser.rb
117
+ - lib/haml_parser/ast.rb
118
+ - lib/haml_parser/cli.rb
119
+ - lib/haml_parser/element_parser.rb
120
+ - lib/haml_parser/error.rb
121
+ - lib/haml_parser/filter_parser.rb
122
+ - lib/haml_parser/indent_tracker.rb
123
+ - lib/haml_parser/line_parser.rb
124
+ - lib/haml_parser/parser.rb
125
+ - lib/haml_parser/ruby_multiline.rb
126
+ - lib/haml_parser/script_parser.rb
127
+ - lib/haml_parser/utils.rb
128
+ - lib/haml_parser/version.rb
129
+ homepage: https://github.com/eagletmt/haml_parser
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: 2.0.0
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubyforge_project:
149
+ rubygems_version: 2.4.5.1
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Parser of Haml template language
153
+ test_files: []