mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Carve out balanceable methods into attribute parser
This commit is contained in:
parent
99f77830c5
commit
7db8fea28b
2 changed files with 9 additions and 24 deletions
|
@ -3,28 +3,6 @@ require 'ripper'
|
|||
module Hamlit
|
||||
module Concerns
|
||||
module Balanceable
|
||||
# Return a string balancing count of braces.
|
||||
# And change the scanner position to the last brace.
|
||||
def read_braces(scanner)
|
||||
return unless scanner.match?(/{/)
|
||||
|
||||
all_tokens = Ripper.lex(scanner.rest)
|
||||
tokens = fetch_balanced_braces(all_tokens)
|
||||
scanner.pos += tokens.last.first.last + 1
|
||||
tokens.map(&:last).join
|
||||
end
|
||||
|
||||
def read_parentheses(scanner)
|
||||
return unless scanner.match?(/\(/)
|
||||
|
||||
all_tokens = Ripper.lex(scanner.rest)
|
||||
tokens = fetch_balanced_parentheses(all_tokens)
|
||||
scanner.pos += tokens.last.first.last + 1
|
||||
tokens.map(&:last).join
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Given Ripper tokens, return first brace-balanced tokens
|
||||
def fetch_balanced_braces(all_tokens)
|
||||
tokens = []
|
||||
|
@ -62,6 +40,8 @@ module Hamlit
|
|||
tokens
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def balanced_parens_exist?(tokens)
|
||||
open_count = 0
|
||||
|
||||
|
|
|
@ -17,7 +17,12 @@ module Hamlit
|
|||
private
|
||||
|
||||
def parse_old_attributes(scanner)
|
||||
[read_braces(scanner)].compact
|
||||
return [] unless scanner.match?(/{/)
|
||||
|
||||
tokens = Ripper.lex(scanner.rest)
|
||||
tokens = fetch_balanced_braces(tokens)
|
||||
scanner.pos += tokens.last.first.last + 1
|
||||
[tokens.map(&:last).join]
|
||||
end
|
||||
|
||||
def parse_new_attributes(scanner)
|
||||
|
@ -30,7 +35,7 @@ module Hamlit
|
|||
tokens = Ripper.lex(scanner.rest)
|
||||
end
|
||||
|
||||
tokens = fetch_balanced_parentheses(tokens)
|
||||
tokens = fetch_balanced_parentheses(tokens)
|
||||
scanner.pos += tokens.last.first.last + 1
|
||||
[tokens.map(&:last).join]
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue