syntax_tree-haml 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 711c40f06bdb83c4889511758c3aa91f3aa7899a938ecdd88fd25d3a8ad5e0b1
4
+ data.tar.gz: 95638948d5c9ffde2ed072daa08de0ac144ed6dc9b700d66f823b5013de53dcd
5
+ SHA512:
6
+ metadata.gz: 40dc20bf9f0953b73458aa88aa6a32fd3ff8b032d5c3045c17304e2203bad3c88f9f45b1bec7f58bf8a5e86bf87890832f12ab8ba7724242210016e0f779dee7
7
+ data.tar.gz: db0b0b019ed7ea98d8283be82cb5f84e84cb40fd537ab592cca582cd5ce0b7119009faaec437d858c81143bfa84de34da4f9c2e0a6bc1c372f140059f0c7730a
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
@@ -0,0 +1,32 @@
1
+ name: Main
2
+ on:
3
+ - push
4
+ - pull_request_target
5
+ jobs:
6
+ ci:
7
+ name: CI
8
+ runs-on: ubuntu-latest
9
+ env:
10
+ CI: true
11
+ steps:
12
+ - uses: actions/checkout@master
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ bundler-cache: true
16
+ ruby-version: '3.1'
17
+ - name: Test
18
+ run: bundle exec rake test
19
+ automerge:
20
+ name: AutoMerge
21
+ needs: ci
22
+ runs-on: ubuntu-latest
23
+ if: github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]'
24
+ steps:
25
+ - uses: actions/github-script@v3
26
+ with:
27
+ script: |
28
+ github.pulls.merge({
29
+ owner: context.payload.repository.owner.login,
30
+ repo: context.payload.repository.name,
31
+ pull_number: context.payload.pull_request.number
32
+ })
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ coverage
2
+ test.haml
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kddnewton@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ syntax_tree-haml (0.1.0)
5
+ haml (>= 5.2)
6
+ syntax_tree
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ docile (1.4.0)
12
+ haml (5.2.2)
13
+ temple (>= 0.8.0)
14
+ tilt
15
+ minitest (5.15.0)
16
+ rake (13.0.6)
17
+ simplecov (0.21.2)
18
+ docile (~> 1.1)
19
+ simplecov-html (~> 0.11)
20
+ simplecov_json_formatter (~> 0.1)
21
+ simplecov-html (0.12.3)
22
+ simplecov_json_formatter (0.1.4)
23
+ syntax_tree (1.2.0)
24
+ temple (0.8.2)
25
+ tilt (2.0.10)
26
+
27
+ PLATFORMS
28
+ x86_64-darwin-21
29
+ x86_64-linux
30
+
31
+ DEPENDENCIES
32
+ bundler
33
+ minitest
34
+ rake
35
+ simplecov
36
+ syntax_tree-haml!
37
+
38
+ BUNDLED WITH
39
+ 2.3.6
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # SyntaxTree::Haml
2
+
3
+ [![Build Status](https://github.com/ruby-syntax-tree/syntax_tree-haml/actions/workflows/main.yml/badge.svg)](https://github.com/ruby-syntax-tree/syntax_tree-haml/actions/workflows/main.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/syntax_tree-haml.svg)](https://rubygems.org/gems/syntax_tree-haml)
5
+
6
+ [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree) support for the [Haml template language](https://haml.info/).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "syntax_tree-haml"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install syntax_tree-haml
23
+
24
+ ## Usage
25
+
26
+ From code:
27
+
28
+ ```ruby
29
+ require "syntax_tree/haml"
30
+
31
+ pp SyntaxTree::Haml.parse(source) # print out the AST
32
+ puts SyntaxTree::Haml.format(source) # format the AST
33
+ ```
34
+
35
+ From the CLI:
36
+
37
+ ```sh
38
+ $ stree ast template.haml
39
+ (root children=[(tag name="span" value="Hello, world!")])
40
+ ```
41
+
42
+ or
43
+
44
+ ```sh
45
+ $ stree format template.haml
46
+ %span Hello, world!
47
+ ```
48
+
49
+ or
50
+
51
+ ```sh
52
+ $ stree write template.haml
53
+ template.haml 1ms
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/syntax_tree-haml.
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/format ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+
6
+ $:.unshift(File.expand_path("../lib", __dir__))
7
+ require "syntax_tree/haml"
8
+
9
+ puts SyntaxTree::Haml.format(ARGF.read)
data/bin/parse ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+
6
+ $:.unshift(File.expand_path("../lib", __dir__))
7
+ require "syntax_tree/haml"
8
+
9
+ pp SyntaxTree::Haml.parse(ARGF.read)
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#html-comments-
6
+ class Comment
7
+ attr_reader :node
8
+
9
+ def initialize(node)
10
+ @node = node
11
+ end
12
+
13
+ def format(q)
14
+ Haml.with_children(node, q) do
15
+ q.text("/")
16
+ q.text("!") if node.value[:revealed]
17
+
18
+ if node.value[:conditional]
19
+ q.text(node.value[:conditional])
20
+ elsif node.value[:text]
21
+ q.text(" #{node.value[:text]}")
22
+ end
23
+ end
24
+ end
25
+
26
+ def pretty_print(q)
27
+ q.group(2, "(comment", ")") do
28
+ q.breakable
29
+
30
+ if node.value[:conditional]
31
+ q.text("conditional=")
32
+ q.pp(node.value[:conditional])
33
+ elsif node.value[:text]
34
+ q.text("text=")
35
+ q.pp(node.value[:text])
36
+ end
37
+
38
+ if node.value[:revealed]
39
+ q.breakable
40
+ q.text("revealed")
41
+ end
42
+
43
+ if node.children.any?
44
+ q.breakable
45
+ q.text("children=")
46
+ q.pp(node.children)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#doctype-
6
+ class Doctype
7
+ TYPES = {
8
+ "basic" => "Basic",
9
+ "frameset" => "Frameset",
10
+ "mobile" => "Mobile",
11
+ "rdfa" => "RDFa",
12
+ "strict" => "Strict",
13
+ "xml" => "XML"
14
+ }
15
+
16
+ VERSIONS = ["1.1", "5"]
17
+
18
+ attr_reader :node
19
+
20
+ def initialize(node)
21
+ @node = node
22
+ end
23
+
24
+ def format(q)
25
+ parts = ["!!!"]
26
+
27
+ parts <<
28
+ if TYPES.key?(node.value[:type])
29
+ TYPES[node.value[:type]]
30
+ elsif VERSIONS.include?(node.value[:version])
31
+ node.value[:version]
32
+ else
33
+ node.value[:type]
34
+ end
35
+
36
+ parts << node.value[:encoding] if node.value[:encoding]
37
+ q.text(parts.join(" "))
38
+ end
39
+
40
+ def pretty_print(q)
41
+ q.group(2, "(doctype", ")") do
42
+ q.breakable
43
+
44
+ if TYPES.key?(node.value[:type])
45
+ q.text("type=")
46
+ q.pp(node.value[:type])
47
+ elsif VERSIONS.include?(node.value[:version])
48
+ q.text("version=")
49
+ q.pp(node.value[:version])
50
+ else
51
+ q.text("type=")
52
+ q.pp(node.value[:type])
53
+ end
54
+
55
+ if node.value[:encoding]
56
+ q.breakable
57
+ q.text("encoding=")
58
+ q.pp(node.value[:encoding])
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#filters
6
+ class Filter
7
+ attr_reader :node
8
+
9
+ def initialize(node)
10
+ @node = node
11
+ end
12
+
13
+ def format(q)
14
+ q.group do
15
+ q.text(":")
16
+ q.text(node.value[:name])
17
+
18
+ q.indent do
19
+ q.breakable(force: true)
20
+
21
+ segments = node.value[:text].strip.split("\n")
22
+ q.seplist(segments, -> { q.breakable(force: true) }) do |segment|
23
+ q.text(segment)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ def pretty_print(q)
30
+ q.group(2, "(filter", ")") do
31
+ q.breakable
32
+ q.text("name=")
33
+ q.text(node.value[:name])
34
+
35
+ q.breakable
36
+ q.text("text=")
37
+ q.pp(node.value[:text])
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#haml-comments--
6
+ class HamlComment
7
+ attr_reader :node
8
+
9
+ def initialize(node)
10
+ @node = node
11
+ end
12
+
13
+ def format(q)
14
+ q.text("-#")
15
+ text = node.value[:text].strip
16
+
17
+ if text.include?("\n")
18
+ q.indent do
19
+ q.breakable(force: true)
20
+ q.seplist(text.split("\n"), -> { q.breakable(force: true) }) do |segment|
21
+ q.text(segment)
22
+ end
23
+ end
24
+ else
25
+ q.text(" #{text}")
26
+ end
27
+ end
28
+
29
+ def pretty_print(q)
30
+ q.group(2, "(haml_comment", ")") do
31
+ q.breakable
32
+ q.text("text=")
33
+ q.pp(node.value[:text])
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#plain-text
6
+ class Plain
7
+ attr_reader :node
8
+
9
+ def initialize(node)
10
+ @node = node
11
+ end
12
+
13
+ def format(q)
14
+ text = node.value[:text]
15
+
16
+ q.text("\\") if escaped?(text)
17
+ q.text(text)
18
+ end
19
+
20
+ def pretty_print(q)
21
+ q.group(2, "(plain", ")") do
22
+ q.breakable
23
+ q.text("text=")
24
+ q.pp(node.value[:text])
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ # If a node comes in as the plain type but starts with one of the special
31
+ # characters that haml parses, then we need to escape it with a \ when
32
+ # printing.
33
+ def escaped?(text)
34
+ ::Haml::Parser::SPECIAL_CHARACTERS.any? do |special|
35
+ text.start_with?(special)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ class Root
6
+ attr_reader :node
7
+
8
+ def initialize(node)
9
+ @node = node
10
+ end
11
+
12
+ def format(q)
13
+ node.children.each do |child|
14
+ child.format(q)
15
+ q.breakable(force: true)
16
+ end
17
+ end
18
+
19
+ def pretty_print(q)
20
+ q.group(2, "(root", ")") do
21
+ if node.children.any?
22
+ q.breakable
23
+ q.text("children=")
24
+ q.pp(node.children)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#inserting_ruby
6
+ class Script
7
+ attr_reader :node
8
+
9
+ def initialize(node)
10
+ @node = node
11
+ end
12
+
13
+ def format(q)
14
+ Haml.with_children(node, q) do
15
+ q.text("&") if node.value[:escape_html]
16
+
17
+ if node.value[:preserve]
18
+ q.text("~")
19
+ else
20
+ q.text("=")
21
+ end
22
+
23
+ q.text(" ")
24
+ q.text(node.value[:text].strip)
25
+ end
26
+ end
27
+
28
+ def pretty_print(q)
29
+ q.group(2, "(script", ")") do
30
+ q.breakable
31
+ q.text("text=")
32
+ q.pp(node.value[:text])
33
+
34
+ if node.value[:escape_html]
35
+ q.breakable
36
+ q.text("escape_html")
37
+ end
38
+
39
+ if node.value[:preserve]
40
+ q.breakable
41
+ q.text("preserve")
42
+ end
43
+
44
+ if node.children.any?
45
+ q.breakable
46
+ q.text("children=")
47
+ q.pp(node.children)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#running-ruby--
6
+ class SilentScript
7
+ attr_reader :node
8
+
9
+ def initialize(node)
10
+ @node = node
11
+ end
12
+
13
+ def format(q)
14
+ q.group do
15
+ q.text("- ")
16
+ q.text(node.value[:text].strip)
17
+
18
+ node.children.each do |child|
19
+ if continuation?(child)
20
+ q.breakable(force: true)
21
+ child.format(q)
22
+ else
23
+ q.indent do
24
+ q.breakable(force: true)
25
+ child.format(q)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def pretty_print(q)
33
+ q.group(2, "(silent_script", ")") do
34
+ q.breakable
35
+ q.text("text=")
36
+ q.pp(node.value[:text])
37
+
38
+ if node.children.any?
39
+ q.breakable
40
+ q.text("children=")
41
+ q.pp(node.children)
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def continuation?(child)
49
+ return false if child.type != :silent_script
50
+
51
+ [node.value[:keyword], child.value[:keyword]] in
52
+ ["case", "in" | "when" | "else"] |
53
+ ["if" | "unless", "elsif" | "else"]
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,306 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ class Tag
6
+ LiteralHashValue = Struct.new(:value)
7
+
8
+ def self.hash_key(key)
9
+ key.match?(/^@|[-:]/) ? "\"#{key}\":" : "#{key}:"
10
+ end
11
+
12
+ def self.hash_value(value)
13
+ case value
14
+ when LiteralHashValue
15
+ value.value
16
+ when String
17
+ "\"#{Quotes.normalize(value, "\"")}\""
18
+ else
19
+ value.to_s
20
+ end
21
+ end
22
+
23
+ class PlainPart < Struct.new(:value)
24
+ def format(q, align)
25
+ q.text(value)
26
+ end
27
+
28
+ def length
29
+ value.length
30
+ end
31
+ end
32
+
33
+ class PrefixPart < Struct.new(:prefix, :value)
34
+ def format(q, align)
35
+ q.text("#{prefix}#{value}")
36
+ end
37
+
38
+ def length
39
+ prefix.length + value.length
40
+ end
41
+ end
42
+
43
+ class HTMLAttributesPart
44
+ attr_reader :values
45
+
46
+ def initialize(raw)
47
+ @values =
48
+ raw[1...-1].split(",").to_h do |keypair|
49
+ keypair[1..-1].split("\" => ")
50
+ end
51
+ end
52
+
53
+ def format(q, align)
54
+ q.group do
55
+ q.text("(")
56
+ q.nest(align) do
57
+ q.seplist(values, -> { q.fill_breakable }, :each_pair) do |key, value|
58
+ q.text("#{key}=#{value}")
59
+ end
60
+ end
61
+ q.text(")")
62
+ end
63
+ end
64
+
65
+ def length
66
+ values.sum { |key, value| key.length + value.length + 3 }
67
+ end
68
+ end
69
+
70
+ class HashAttributesPart < Struct.new(:values)
71
+ def format(q, align)
72
+ format_value(q, values)
73
+ end
74
+
75
+ def length
76
+ values.sum do |key, value|
77
+ key.length + (value.is_a?(String) ? value : value.to_s).length + 3
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ def format_value(q, hash, level = 0)
84
+ q.group do
85
+ q.text("{")
86
+ q.indent do
87
+ q.group do
88
+ q.breakable(level == 0 ? "" : " ")
89
+ q.seplist(hash, nil, :each_pair) do |key, value|
90
+ q.text(Tag.hash_key(key))
91
+ q.text(" ")
92
+
93
+ if value.is_a?(Hash)
94
+ format_value(q, value, level + 1)
95
+ else
96
+ q.text(Tag.hash_value(value))
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ q.breakable(level == 0 ? "" : " ")
103
+ q.text("}")
104
+ end
105
+ end
106
+ end
107
+
108
+ attr_reader :node
109
+
110
+ def initialize(node)
111
+ @node = node
112
+ end
113
+
114
+ def format(q)
115
+ parts = []
116
+
117
+ # If we have a tag that isn't a div, then we need to print out that
118
+ # name of that tag first. If it is a div, first we'll check if there
119
+ # are any other things that would force us to print out the div
120
+ # explicitly, and otherwise we'll leave it off.
121
+ if node.value[:name] != "div"
122
+ parts << PrefixPart.new("%", node.value[:name])
123
+ end
124
+
125
+ # If we have a class attribute, then we're going to print that here
126
+ # using the special class syntax.
127
+ if node.value[:attributes].key?("class")
128
+ parts << PrefixPart.new(".", node.value[:attributes]["class"].tr(" ", "."))
129
+ end
130
+
131
+ # If we have an id attribute, then we're going to print that here
132
+ # using the special id syntax.
133
+ if node.value[:attributes].key?("id")
134
+ parts << PrefixPart.new("#", node.value[:attributes]["id"])
135
+ end
136
+
137
+ # If we're using dynamic attributes on this tag, then they come in as
138
+ # a string that looks like the output of Hash#inspect from Ruby. So
139
+ # here we're going to split it all up and print it out nicely.
140
+ if node.value[:dynamic_attributes].new
141
+ parts << HTMLAttributesPart.new(node.value[:dynamic_attributes].new)
142
+ end
143
+
144
+ # If there are any static attributes that are not class or id (because
145
+ # we already took care of those), then we're going to print them out
146
+ # here.
147
+ static = node.value[:attributes].reject { |key, _| key == "class" || key == "id" }
148
+ parts << HashAttributesPart.new(static) if static.any?
149
+
150
+ # If there are dynamic attributes that don't use the newer syntax, then
151
+ # we're going to print them out here.
152
+ if node.value[:dynamic_attributes].old
153
+ parts << PlainPart.new("%div") if parts.empty?
154
+
155
+ if ::Haml::AttributeParser.available?
156
+ dynamic = parse_attributes(node.value[:dynamic_attributes].old)
157
+ parts <<
158
+ if dynamic.is_a?(LiteralHashValue)
159
+ PlainPart.new(dynamic.value)
160
+ else
161
+ HashAttributesPart.new(dynamic)
162
+ end
163
+ else
164
+ parts << PlainPart.new(node.value[:dynamic_attributes].old)
165
+ end
166
+ end
167
+
168
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#object-reference-
169
+ if node.value[:object_ref] != :nil
170
+ parts << PlainPart.new("%div") if parts.empty?
171
+ parts << PlainPart.new(node.value[:object_ref])
172
+ end
173
+
174
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#whitespace-removal--and-
175
+ parts << PlainPart.new(">") if node.value[:nuke_outer_whitespace]
176
+ parts << PlainPart.new("<") if node.value[:nuke_inner_whitespace]
177
+
178
+ # https://haml.info/docs/yardoc/file.REFERENCE.html#empty-void-tags-
179
+ parts << PlainPart.new("/") if node.value[:self_closing]
180
+
181
+ # If there is a value part, then we're going to print slightly
182
+ # differently as the value goes after the tag declaration.
183
+ if node.value[:value]
184
+ return Haml.with_children(node, q) do
185
+ q.group do
186
+ align = 0
187
+
188
+ parts.each do |part|
189
+ part.format(q, align)
190
+ align += part.length
191
+ end
192
+ end
193
+
194
+ q.indent do
195
+ # Split between the declaration of the tag and the contents of the
196
+ # tag.
197
+ q.breakable("")
198
+
199
+ if node.value[:parse] && node.value[:value].match?(/#[{$@]/)
200
+ # There's a weird case here where if the value includes
201
+ # interpolation and it's marked as { parse: true }, then we
202
+ # don't actually want the = prefix, and we want to remove extra
203
+ # escaping.
204
+ q.if_break { q.text("") }.if_flat { q.text(" ") }
205
+ q.text(node.value[:value][1...-1].gsub(/\\"/, "\""))
206
+ elsif node.value[:parse]
207
+ q.text("= ")
208
+ q.text(node.value[:value])
209
+ else
210
+ q.if_break { q.text("") }.if_flat { q.text(" ") }
211
+ q.text(node.value[:value])
212
+ end
213
+ end
214
+ end
215
+ end
216
+
217
+ # In case none of the other if statements have matched and we're
218
+ # printing a div, we need to explicitly add it back into the array.
219
+ if parts.empty? && node.value[:name] == "div"
220
+ parts << PlainPart.new("%div")
221
+ end
222
+
223
+ Haml.with_children(node, q) do
224
+ align = 0
225
+
226
+ parts.each do |part|
227
+ part.format(q, align)
228
+ align += part.length
229
+ end
230
+ end
231
+ end
232
+
233
+ def pretty_print(q)
234
+ q.group(2, "(tag", ")") do
235
+ q.breakable
236
+ q.text("name=")
237
+ q.pp(node.value[:name])
238
+
239
+ if node.value[:attributes].any?
240
+ q.breakable
241
+ q.text("attributes=")
242
+ q.pp(node.value[:attributes])
243
+ end
244
+
245
+ if node.value[:dynamic_attributes].new
246
+ q.breakable
247
+ q.text("dynamic_attributes.new=")
248
+ q.pp(node.value[:dynamic_attributes].new)
249
+ end
250
+
251
+ if node.value[:dynamic_attributes].old
252
+ q.breakable
253
+ q.text("dynamic_attributes.old=")
254
+ q.pp(node.value[:dynamic_attributes].old)
255
+ end
256
+
257
+ if node.value[:object_ref] != :nil
258
+ q.breakable
259
+ q.text("object_ref=")
260
+ q.pp(node.value[:object_ref])
261
+ end
262
+
263
+ if node.value[:nuke_outer_whitespace]
264
+ q.breakable
265
+ q.text("nuke_outer_whitespace")
266
+ end
267
+
268
+ if node.value[:nuke_inner_whitespace]
269
+ q.breakable
270
+ q.text("nuke_inner_whitespace")
271
+ end
272
+
273
+ if node.value[:self_closing]
274
+ q.breakable
275
+ q.text("self_closing")
276
+ end
277
+
278
+ if node.value[:value]
279
+ q.breakable
280
+ q.text("value=")
281
+ q.pp(node.value[:value])
282
+ end
283
+
284
+ if node.children.any?
285
+ q.breakable
286
+ q.text("children=")
287
+ q.pp(node.children)
288
+ end
289
+ end
290
+ end
291
+
292
+ private
293
+
294
+ def parse_attributes(source)
295
+ case Ripper.sexp(source)
296
+ in [:program, [[:hash, *], *]] if parsed = ::Haml::AttributeParser.parse(source)
297
+ parsed.to_h { |key, value| [key, parse_attributes(value)] }
298
+ in [:program, [[:string_literal, *], *]]
299
+ source[1...-1]
300
+ else
301
+ LiteralHashValue.new(source)
302
+ end
303
+ end
304
+ end
305
+ end
306
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SyntaxTree < Ripper
4
+ module Haml
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "haml"
4
+ require "syntax_tree"
5
+
6
+ require "syntax_tree/haml/comment"
7
+ require "syntax_tree/haml/doctype"
8
+ require "syntax_tree/haml/filter"
9
+ require "syntax_tree/haml/haml_comment"
10
+ require "syntax_tree/haml/plain"
11
+ require "syntax_tree/haml/root"
12
+ require "syntax_tree/haml/script"
13
+ require "syntax_tree/haml/silent_script"
14
+ require "syntax_tree/haml/tag"
15
+
16
+ class Haml::Parser::ParseNode
17
+ def format(q)
18
+ syntax_tree.format(q)
19
+ end
20
+
21
+ def pretty_print(q)
22
+ syntax_tree.pretty_print(q)
23
+ end
24
+
25
+ private
26
+
27
+ def syntax_tree
28
+ case type
29
+ when :comment then SyntaxTree::Haml::Comment.new(self)
30
+ when :doctype then SyntaxTree::Haml::Doctype.new(self)
31
+ when :filter then SyntaxTree::Haml::Filter.new(self)
32
+ when :haml_comment then SyntaxTree::Haml::HamlComment.new(self)
33
+ when :plain then SyntaxTree::Haml::Plain.new(self)
34
+ when :root then SyntaxTree::Haml::Root.new(self)
35
+ when :script then SyntaxTree::Haml::Script.new(self)
36
+ when :silent_script then SyntaxTree::Haml::SilentScript.new(self)
37
+ when :tag then SyntaxTree::Haml::Tag.new(self)
38
+ else
39
+ raise ArgumentError, "Unsupported type: #{type}"
40
+ end
41
+ end
42
+ end
43
+
44
+ class SyntaxTree < Ripper
45
+ module Haml
46
+ def self.parse(source)
47
+ ::Haml::Parser.new({}).call(source)
48
+ end
49
+
50
+ def self.format(source)
51
+ formatter = PP.new([])
52
+ parse(source).format(formatter)
53
+
54
+ formatter.flush
55
+ formatter.output.join
56
+ end
57
+
58
+ def self.with_children(node, q)
59
+ if node.children.empty?
60
+ q.group { yield }
61
+ else
62
+ q.group do
63
+ q.group { yield }
64
+ q.indent do
65
+ node.children.each do |child|
66
+ q.breakable(force: true)
67
+ child.format(q)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ripper"
4
+ require_relative "lib/syntax_tree/haml/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "syntax_tree-haml"
8
+ spec.version = SyntaxTree::Haml::VERSION
9
+ spec.authors = ["Kevin Newton"]
10
+ spec.email = ["kddnewton@gmail.com"]
11
+
12
+ spec.summary = "Syntax Tree support for Haml"
13
+ spec.homepage = "https://github.com/ruby-syntax-tree/syntax_tree-haml"
14
+ spec.license = "MIT"
15
+ spec.metadata = { "rubygems_mfa_required" => "true" }
16
+
17
+ spec.files = Dir.chdir(__dir__) do
18
+ `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ end
22
+
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = %w[lib]
26
+
27
+ spec.add_dependency "haml", ">= 5.2"
28
+ spec.add_dependency "syntax_tree"
29
+
30
+ spec.add_development_dependency "bundler"
31
+ spec.add_development_dependency "minitest"
32
+ spec.add_development_dependency "rake"
33
+ spec.add_development_dependency "simplecov"
34
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syntax_tree-haml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Newton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: haml
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: syntax_tree
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
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: minitest
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: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '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:
98
+ email:
99
+ - kddnewton@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/dependabot.yml"
105
+ - ".github/workflows/main.yml"
106
+ - ".gitignore"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - README.md
111
+ - Rakefile
112
+ - bin/format
113
+ - bin/parse
114
+ - lib/syntax_tree/haml.rb
115
+ - lib/syntax_tree/haml/comment.rb
116
+ - lib/syntax_tree/haml/doctype.rb
117
+ - lib/syntax_tree/haml/filter.rb
118
+ - lib/syntax_tree/haml/haml_comment.rb
119
+ - lib/syntax_tree/haml/plain.rb
120
+ - lib/syntax_tree/haml/root.rb
121
+ - lib/syntax_tree/haml/script.rb
122
+ - lib/syntax_tree/haml/silent_script.rb
123
+ - lib/syntax_tree/haml/tag.rb
124
+ - lib/syntax_tree/haml/version.rb
125
+ - syntax_tree-haml.gemspec
126
+ homepage: https://github.com/ruby-syntax-tree/syntax_tree-haml
127
+ licenses:
128
+ - MIT
129
+ metadata:
130
+ rubygems_mfa_required: 'true'
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubygems_version: 3.3.3
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Syntax Tree support for Haml
150
+ test_files: []