hamli 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6a825c4ad1a1e140b7c8020a0137a278c2844356c0f5ab166fcca716c0f21a88
4
+ data.tar.gz: e603ee2dd8d1698447bf4eb20729b0087b174f679171d2a4d94efd4f2117e580
5
+ SHA512:
6
+ metadata.gz: 759e52536c4111473a6a46d22a44a027db0e96590d2367e9bbd64395bf91cd72c5984cc4341e457f493c99c27f878d5f2142a42f4a26c77175a245c1e242b269
7
+ data.tar.gz: 914d1aa219cba92b697455223ba65bd140336bb7e20fdc5378eaadf6825630d9d392f56e797b84b160a0d96ad84296042e74468b2a6c1b093c4898f05165ce7e
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+ SuggestExtensions: false
7
+ TargetRubyVersion: 2.6
8
+
9
+ Layout/LineLength:
10
+ Enabled: false
11
+
12
+ Lint/InterpolationCheck:
13
+ Enabled: false
14
+
15
+ Metrics:
16
+ Enabled: false
17
+
18
+ RSpec/AnyInstance:
19
+ Enabled: false
20
+
21
+ RSpec/ImplicitSubject:
22
+ Enabled: false
23
+
24
+ RSpec/MultipleExpectations:
25
+ Enabled: false
26
+
27
+ RSpec/MultipleMemoizedHelpers:
28
+ Enabled: false
29
+
30
+ RSpec/NamedSubject:
31
+ Enabled: false
32
+
33
+ Style/CaseLikeIf:
34
+ Enabled: false
35
+
36
+ Style/Documentation:
37
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ ## 0.1.0 - 2022-01-25
6
+
7
+ ### Added
8
+
9
+ - Initial release.
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at r7kamura@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in hamli.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'rubocop', '~> 1.21'
11
+ gem 'rubocop-rspec'
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hamli (0.1.0)
5
+ temple
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.5.0)
12
+ parallel (1.21.0)
13
+ parser (3.1.0.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.2.0)
18
+ rexml (3.2.5)
19
+ rspec (3.10.0)
20
+ rspec-core (~> 3.10.0)
21
+ rspec-expectations (~> 3.10.0)
22
+ rspec-mocks (~> 3.10.0)
23
+ rspec-core (3.10.1)
24
+ rspec-support (~> 3.10.0)
25
+ rspec-expectations (3.10.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.10.0)
28
+ rspec-mocks (3.10.2)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-support (3.10.3)
32
+ rubocop (1.25.0)
33
+ parallel (~> 1.10)
34
+ parser (>= 3.1.0.0)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.8, < 3.0)
37
+ rexml
38
+ rubocop-ast (>= 1.15.1, < 2.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 3.0)
41
+ rubocop-ast (1.15.1)
42
+ parser (>= 3.0.1.1)
43
+ rubocop-rspec (2.7.0)
44
+ rubocop (~> 1.19)
45
+ ruby-progressbar (1.11.0)
46
+ temple (0.8.2)
47
+ unicode-display_width (2.1.0)
48
+
49
+ PLATFORMS
50
+ x86_64-linux
51
+
52
+ DEPENDENCIES
53
+ hamli!
54
+ rake (~> 13.0)
55
+ rspec (~> 3.0)
56
+ rubocop (~> 1.21)
57
+ rubocop-rspec
58
+
59
+ BUNDLED WITH
60
+ 2.3.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ryo Nakamura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # Hamli
2
+
3
+ [![test](https://github.com/r7kamura/hamli/actions/workflows/test.yml/badge.svg)](https://github.com/r7kamura/hamli/actions/workflows/test.yml)
4
+
5
+ Yet another implementation for [Haml](https://github.com/haml/haml) template language.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hamli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ bundle install
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ gem install hamli
25
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/hamli.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/hamli/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'hamli'
7
+ spec.version = Hamli::VERSION
8
+ spec.authors = ['Ryo Nakamura']
9
+ spec.email = ['r7kamura@gmail.com']
10
+
11
+ spec.summary = 'Yet another implementation for Haml template language.'
12
+ spec.homepage = 'https://github.com/r7kamura/hamli'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.6.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+ spec.metadata['rubygems_mfa_required'] = 'true'
31
+
32
+ spec.add_dependency 'temple'
33
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hamli
4
+ module Errors
5
+ class BaseError < StandardError
6
+ end
7
+
8
+ class HamlSyntaxError < BaseError
9
+ # @param [Integer] column
10
+ # @param [String] file_path
11
+ # @param [String] line
12
+ # @param [Integer] line_number
13
+ def initialize(column:, file_path:, line:, line_number:)
14
+ super()
15
+ @column = column
16
+ @file_path = file_path
17
+ @line = line
18
+ @line_number = line_number
19
+ end
20
+
21
+ # @note Override.
22
+ # @return [String]
23
+ def to_s
24
+ <<~TEXT
25
+ #{error_type} at #{@file_path}:#{@line_number}:#{@column}
26
+ #{@line.rstrip}
27
+ #{' ' * (@column - 1)}^
28
+ TEXT
29
+ end
30
+
31
+ private
32
+
33
+ # @return [String]
34
+ def error_type
35
+ self.class.to_s.split('::').last
36
+ end
37
+ end
38
+
39
+ class MalformedIndentationError < HamlSyntaxError
40
+ end
41
+
42
+ class UnexpectedEosError < HamlSyntaxError
43
+ end
44
+
45
+ class UnexpectedIndentationError < HamlSyntaxError
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,368 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'temple'
4
+
5
+ module Hamli
6
+ class Parser < ::Temple::Parser
7
+ # @param [String] source Haml template source.
8
+ # @return [Array]
9
+ def call(source)
10
+ @stacks = [%i[multi]]
11
+ @indents = []
12
+ @scanner = ::StringScanner.new(source)
13
+ parse_block until @scanner.eos?
14
+ @stacks[0]
15
+ end
16
+
17
+ private
18
+
19
+ def parse_block
20
+ return if parse_blank_line
21
+
22
+ parse_indent
23
+
24
+ if @scanner.match?(/%/)
25
+ parse_tag_line
26
+ elsif @scanner.match?(/[#.]/)
27
+ parse_div_line
28
+ else
29
+ parse_text_line
30
+ end
31
+ end
32
+
33
+ # @return [Boolean]
34
+ def parse_blank_line
35
+ if @scanner.scan(/[ \t]*$/)
36
+ parse_line_ending
37
+ true
38
+ else
39
+ false
40
+ end
41
+ end
42
+
43
+ # @return [Boolean]
44
+ def parse_line_ending
45
+ if @scanner.scan(/\r?\n/)
46
+ @stacks.last << [:newline]
47
+ true
48
+ else
49
+ false
50
+ end
51
+ end
52
+
53
+ # Parse indent part.
54
+ # e.g. %div
55
+ # ^^^^
56
+ def parse_indent
57
+ @scanner.scan(/[ \t]*/)
58
+ indent = indent_from_last_match
59
+ @indents << indent if @indents.empty?
60
+
61
+ if indent > @indents.last
62
+ syntax_error!(Errors::UnexpectedIndentationError) unless expecting_indentation?
63
+
64
+ @indents << indent
65
+ else
66
+ @stacks.pop if expecting_indentation?
67
+
68
+ while indent < @indents.last && @indents.length > 1
69
+ @indents.pop
70
+ @stacks.pop
71
+ end
72
+
73
+ syntax_error!(Errors::MalformedIndentationError) if indent != @indents.last
74
+ end
75
+ end
76
+
77
+ # Parse text line part.
78
+ # e.g. abc
79
+ # ^^^
80
+ def parse_text_line
81
+ @stacks.last << [:hamli, :text, parse_text_block]
82
+ parse_line_ending
83
+ end
84
+
85
+ # Parse tag line part.
86
+ # e.g. %div{:a => "b"} c
87
+ # ^^^^^^^^^^^^^^^^^
88
+ def parse_tag_line
89
+ @scanner.pos += 1
90
+ parse_tag_line_body(tag_name: parse_tag_name)
91
+ end
92
+
93
+ # Parse tag line part where %div is omitted.
94
+ # e.g. #a b
95
+ # ^^^^
96
+ def parse_div_line
97
+ parse_tag_line_body(tag_name: 'div')
98
+ end
99
+
100
+ # Parse tag line body part.
101
+ # e.g. %div{:a => "b"} c
102
+ # ^^^^^^^^^^^^^
103
+ # @param [String] tag_name
104
+ def parse_tag_line_body(tag_name:)
105
+ attributes = parse_attributes
106
+ tag = [:html, :tag, tag_name, attributes]
107
+ @stacks.last << tag
108
+
109
+ if @scanner.scan(/[ \t]*$/)
110
+ content = [:multi]
111
+ tag << content
112
+ @stacks << content
113
+ elsif @scanner.scan(/[ \t]*=([<>])*/)
114
+ # TODO
115
+ elsif @scanner.scan(%r{[ \t]*/[ \t]*})
116
+ # TODO
117
+ else
118
+ @scanner.scan(/[ \t]+/)
119
+ tag << [:hamli, :text, parse_text_block]
120
+ end
121
+ parse_line_ending
122
+ end
123
+
124
+ # Parse tag name part.
125
+ # e.g. %div{:a => "b"}
126
+ # ^^^
127
+ # @return [String, nil]
128
+ def parse_tag_name
129
+ @scanner.scan(/([-:\w]+)/)
130
+ end
131
+
132
+ # Parse attribute shortcuts part.
133
+ # e.g. #a
134
+ # ^^
135
+ # e.g. %div#a
136
+ # ^^
137
+ # e.g. %div.a
138
+ # ^^
139
+ # @return [Array<Array>]
140
+ def parse_attribute_shortcuts
141
+ result = []
142
+ while @scanner.scan(/([#.])([-:\w]+)/)
143
+ marker = @scanner[1]
144
+ value = @scanner[2]
145
+ name = marker == '#' ? 'id' : 'class'
146
+ result << [:html, :attr, name, [:static, value]]
147
+ end
148
+ result
149
+ end
150
+
151
+ # Parse attributes part.
152
+ # e.g. #a
153
+ # ^^
154
+ # e.g. %div{:b => "b"}
155
+ # ^^^^^^^^^^^
156
+ def parse_attributes
157
+ attributes = %i[html attrs]
158
+ attributes += parse_attribute_shortcuts
159
+ attributes += parse_attributes_groups
160
+ attributes
161
+ end
162
+
163
+ # Parse attribute braces part.
164
+ # e.g. %div{:a => "b"}
165
+ # ^^^^^^^^^^^
166
+ # e.g. %div(a=b)
167
+ # ^^^^^
168
+ # e.g. %div[a, :b]
169
+ # ^^^^^^^
170
+ # @return [Array]
171
+ def parse_attributes_groups
172
+ result = []
173
+ loop do
174
+ if @scanner.match?(/\{/)
175
+ result += [parse_ruby_attributes]
176
+ elsif @scanner.match?(/\(/)
177
+ result += parse_html_style_attributes
178
+ elsif @scanner.match?(/\[/)
179
+ result += [parse_object_reference]
180
+ else
181
+ break
182
+ end
183
+ end
184
+ result
185
+ end
186
+
187
+ # Parse HTML-style attributes part.
188
+ # e.g. %div(a=b)
189
+ # ^^^^^
190
+ # @return [Array]
191
+ def parse_html_style_attributes
192
+ @scanner.pos += 1
193
+ result = []
194
+ until @scanner.scan(/\)/)
195
+ syntax_error!(Errors::UnexpectedEosError) if @scanner.eos?
196
+
197
+ @scanner.scan(/[ \t\r\n]+/)
198
+
199
+ if (attribute = parse_html_style_attribute)
200
+ result << attribute
201
+ end
202
+ end
203
+ result
204
+ end
205
+
206
+ # Parse HTML-style attribute part.
207
+ # e.g. %div(key1=value1 key2="value2" key3)
208
+ # ^^^^^^^^^^^
209
+ # @return [Array, nil]
210
+ def parse_html_style_attribute
211
+ return unless @scanner.scan(/[-:\w]+/)
212
+
213
+ name = @scanner[1]
214
+
215
+ @scanner.scan(/[ \t]*/)
216
+ return [:html, :attr, name, [:static, true]] unless @scanner.scan(/=/)
217
+
218
+ @scanner.scan(/[ \t]*/)
219
+ unless (quote = @scanner.scan(/["']/))
220
+ return unless (variable = @scanner.scan(/(@@?|\$)?\w+/))
221
+
222
+ return [:html, :attr, name, [:dynamic, variable]]
223
+ end
224
+
225
+ [:html, :attr, name, parse_quoted_attribute_value(quote)]
226
+ end
227
+
228
+ # Parse quoted attribute value part.
229
+ # e.g. %input(type="text")
230
+ # ^^^^^^
231
+ # @note Skip closing quote in {}.
232
+ # @param [String] quote ' or ".
233
+ # @return [Array]
234
+ def parse_quoted_attribute_value(quote)
235
+ begin_ = @scanner.charpos
236
+ end_ = nil
237
+ value = +''
238
+ count = 0
239
+ loop do
240
+ if @scanner.match?(/#{quote}/)
241
+ if count.zero?
242
+ end_ = @scanner.charpos
243
+ @scanner.pos += @scanner.matched_size
244
+ break
245
+ else
246
+ @scanner.pos += @scanner.matched_size
247
+ value << @scanner.matched
248
+ end
249
+ elsif @scanner.skip(/\{/)
250
+ count += 1
251
+ value << @scanner.matched
252
+ elsif @scanner.skip(/\}/)
253
+ count -= 1
254
+ value << @scanner.matched
255
+ else
256
+ value << @scanner.scan(/[^{}#{quote}]*/)
257
+ end
258
+ end
259
+ [:hamli, :interpolate, begin_, end_, value]
260
+ end
261
+
262
+ # Parse object reference attributes part.
263
+ # e.g. %div[a, :b]
264
+ # ^^^^^^^
265
+ # @return [Array]
266
+ def parse_object_reference
267
+ begin_ = @scanner.charpos
268
+ value = @scanner.scan(
269
+ /
270
+ (?<brackets>
271
+ \[
272
+ (?:[^\[\]] | \g<brackets>)*
273
+ \]
274
+ )
275
+ /x
276
+ )
277
+ [:hamli, :object_reference, begin_, @scanner.charpos, value]
278
+ end
279
+
280
+ # Parse ruby attributes part.
281
+ # e.g. %div{:a => "b"}
282
+ # ^^^^^^^^^^^
283
+ # @return [Array]
284
+ def parse_ruby_attributes
285
+ begin_ = @scanner.charpos
286
+ value = @scanner.scan(
287
+ /
288
+ (?<braces>
289
+ \{
290
+ (?:[^{}] | \g<braces>)*
291
+ \}
292
+ )
293
+ /x
294
+ )
295
+ [:hamli, :ruby_attributes, begin_, @scanner.charpos, value]
296
+ end
297
+
298
+ # Parse text block part.
299
+ # e.g. %div abc
300
+ # ^^^^
301
+ # @return [Array]
302
+ def parse_text_block
303
+ result = [:multi]
304
+
305
+ interpolate = parse_interpolate_line
306
+ result << interpolate if interpolate
307
+
308
+ until @scanner.eos?
309
+ if @scanner.scan(/\r?\n[ \t]*(?=\r?\n)/)
310
+ result << [:newline]
311
+ next
312
+ end
313
+
314
+ @scanner.match?(/\r?\n[ \t]*/)
315
+ indent = indent_from_last_match
316
+ break if indent <= @indents.last
317
+
318
+ @scanner.pos += @scanner.matched_size
319
+ result << [:newline]
320
+ result << parse_interpolate_line
321
+ end
322
+
323
+ result
324
+ end
325
+
326
+ # @return [Array, nil]
327
+ def parse_interpolate_line
328
+ return unless @scanner.match?(/[^\r\n]/)
329
+
330
+ begin_ = @scanner.charpos
331
+ value = @scanner.matched
332
+ @scanner.pos += @scanner.matched_size
333
+ end_ = @scanner.charpos
334
+ [:hamli, :interpolate, begin_, end_, value]
335
+ end
336
+
337
+ # @param [Class] syntax_error_class A child class of Hamli::Errors::HamlSyntaxError.
338
+ # @raise [Hamli::Errors::HamlSyntaxError]
339
+ def syntax_error!(syntax_error_class)
340
+ range = Range.new(index: @scanner.charpos, source: @scanner.string)
341
+ raise syntax_error_class.new(
342
+ column: range.column,
343
+ file_path: @file_path,
344
+ line: range.line,
345
+ line_number: range.line_number
346
+ )
347
+ end
348
+
349
+ # @return [Integer]
350
+ def indent_from_last_match
351
+ @scanner.matched.chars.map do |char|
352
+ case char
353
+ when "\t"
354
+ 4
355
+ when ' '
356
+ 1
357
+ else
358
+ 0
359
+ end
360
+ end.sum(0)
361
+ end
362
+
363
+ # @return [Boolean]
364
+ def expecting_indentation?
365
+ @stacks.length > @indents.length
366
+ end
367
+ end
368
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hamli
4
+ # Get line-based information from source code and its index.
5
+ class Range
6
+ # @param [Integer] index 0-indexed per-character index.
7
+ # @param [String] source
8
+ def initialize(index:, source:)
9
+ @index = index
10
+ @source = source
11
+ end
12
+
13
+ # @return [Integer] 1-indexed column index.
14
+ def column
15
+ (@index - line_beginning_index) + 1
16
+ end
17
+
18
+ def line
19
+ @source[line_beginning_index...line_ending_index]
20
+ end
21
+
22
+ # @return [Integer] 1-indexed line index.
23
+ def line_number
24
+ @source[0..@index].scan(/^/).length
25
+ end
26
+
27
+ private
28
+
29
+ # @return [Integer]
30
+ def line_beginning_index
31
+ @source.rindex(/^/, @index) || 0
32
+ end
33
+
34
+ # @return [Integer]
35
+ def line_ending_index
36
+ @source.index(/$/, @index)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hamli
4
+ VERSION = '0.1.0'
5
+ end
data/lib/hamli.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'hamli/version'
4
+
5
+ module Hamli
6
+ autoload :Errors, 'hamli/errors'
7
+ autoload :Parser, 'hamli/parser'
8
+ autoload :Range, 'hamli/range'
9
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hamli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: temple
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - r7kamura@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - hamli.gemspec
44
+ - lib/hamli.rb
45
+ - lib/hamli/errors.rb
46
+ - lib/hamli/parser.rb
47
+ - lib/hamli/range.rb
48
+ - lib/hamli/version.rb
49
+ homepage: https://github.com/r7kamura/hamli
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/r7kamura/hamli
54
+ source_code_uri: https://github.com/r7kamura/hamli
55
+ changelog_uri: https://github.com/r7kamura/hamli/blob/main/CHANGELOG.md
56
+ rubygems_mfa_required: 'true'
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.6.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.1.4
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Yet another implementation for Haml template language.
76
+ test_files: []