├── .github
└── workflows
│ └── main.yml
├── .gitignore
├── CHANGELOG.md
├── CODE_OF_CONDUCT.md
├── Gemfile
├── Gemfile.lock
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
├── console
└── setup
├── brb.gemspec
├── lib
├── brb.rb
└── brb
│ ├── templates.rb
│ └── version.rb
└── test
├── brb_test.rb
├── partials
├── _basic.html.erb
├── _matter.html.erb
└── _sigils.html.erb
└── test_helper.rb
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: Ruby
2 |
3 | on:
4 | push:
5 | branches:
6 | - main
7 |
8 | pull_request:
9 |
10 | jobs:
11 | build:
12 | runs-on: ubuntu-latest
13 | name: Ruby ${{ matrix.ruby }}
14 | strategy:
15 | matrix:
16 | ruby:
17 | - '3.0'
18 | - '3.3'
19 |
20 | steps:
21 | - uses: actions/checkout@v4
22 | - name: Set up Ruby
23 | uses: ruby/setup-ruby@v1
24 | with:
25 | ruby-version: ${{ matrix.ruby }}
26 | bundler-cache: true
27 | - name: Run tests
28 | run: bundle exec rake
29 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.bundle/
2 | /.yardoc
3 | /_yardoc/
4 | /coverage/
5 | /doc/
6 | /pkg/
7 | /spec/reports/
8 | /tmp/
9 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | ## [Unreleased]
2 |
3 | ## [0.1.0] - 2023-10-04
4 |
5 | - Initial release
6 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
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 kaspth@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.
85 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | source "https://rubygems.org"
4 |
5 | # Specify your gem's dependencies in brb.gemspec
6 | gemspec
7 |
8 | gem "rake", "~> 13.0"
9 |
10 | gem "minitest", "~> 5.0"
11 |
12 | gem "activemodel"
13 |
14 | gem "irb"
15 | gem "debug"
16 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: .
3 | specs:
4 | brb-templates (0.1.1)
5 | actionview (>= 6.1)
6 |
7 | GEM
8 | remote: https://rubygems.org/
9 | specs:
10 | actionpack (7.1.2)
11 | actionview (= 7.1.2)
12 | activesupport (= 7.1.2)
13 | nokogiri (>= 1.8.5)
14 | racc
15 | rack (>= 2.2.4)
16 | rack-session (>= 1.0.1)
17 | rack-test (>= 0.6.3)
18 | rails-dom-testing (~> 2.2)
19 | rails-html-sanitizer (~> 1.6)
20 | actionview (7.1.2)
21 | activesupport (= 7.1.2)
22 | builder (~> 3.1)
23 | erubi (~> 1.11)
24 | rails-dom-testing (~> 2.2)
25 | rails-html-sanitizer (~> 1.6)
26 | activemodel (7.1.2)
27 | activesupport (= 7.1.2)
28 | activesupport (7.1.2)
29 | base64
30 | bigdecimal
31 | concurrent-ruby (~> 1.0, >= 1.0.2)
32 | connection_pool (>= 2.2.5)
33 | drb
34 | i18n (>= 1.6, < 2)
35 | minitest (>= 5.1)
36 | mutex_m
37 | tzinfo (~> 2.0)
38 | base64 (0.2.0)
39 | bigdecimal (3.1.5)
40 | builder (3.2.4)
41 | concurrent-ruby (1.2.2)
42 | connection_pool (2.4.1)
43 | crass (1.0.6)
44 | debug (1.9.1)
45 | irb (~> 1.10)
46 | reline (>= 0.3.8)
47 | drb (2.2.0)
48 | ruby2_keywords
49 | erubi (1.12.0)
50 | i18n (1.14.1)
51 | concurrent-ruby (~> 1.0)
52 | io-console (0.7.1)
53 | irb (1.11.1)
54 | rdoc
55 | reline (>= 0.4.2)
56 | loofah (2.22.0)
57 | crass (~> 1.0.2)
58 | nokogiri (>= 1.12.0)
59 | minitest (5.21.1)
60 | mutex_m (0.2.0)
61 | nokogiri (1.16.0-arm64-darwin)
62 | racc (~> 1.4)
63 | nokogiri (1.16.0-x86_64-linux)
64 | racc (~> 1.4)
65 | psych (5.1.2)
66 | stringio
67 | racc (1.7.3)
68 | rack (3.0.8)
69 | rack-session (2.0.0)
70 | rack (>= 3.0.0)
71 | rack-test (2.1.0)
72 | rack (>= 1.3)
73 | rails-dom-testing (2.2.0)
74 | activesupport (>= 5.0.0)
75 | minitest
76 | nokogiri (>= 1.6)
77 | rails-html-sanitizer (1.6.0)
78 | loofah (~> 2.21)
79 | nokogiri (~> 1.14)
80 | rake (13.1.0)
81 | rdoc (6.6.2)
82 | psych (>= 4.0.0)
83 | reline (0.4.2)
84 | io-console (~> 0.5)
85 | ruby2_keywords (0.0.5)
86 | stringio (3.1.0)
87 | tzinfo (2.0.6)
88 | concurrent-ruby (~> 1.0)
89 |
90 | PLATFORMS
91 | arm64-darwin-22
92 | arm64-darwin-23
93 | x86_64-linux
94 |
95 | DEPENDENCIES
96 | actionpack (>= 6.1)
97 | activemodel
98 | brb-templates!
99 | debug
100 | irb
101 | minitest (~> 5.0)
102 | rake (~> 13.0)
103 |
104 | BUNDLED WITH
105 | 2.5.4
106 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2023 Kasper Timm Hansen
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.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # BRB
2 |
3 | BRB is backslashed Ruby, a template system that lets you be-right-back to ERB.
4 |
5 | BRB aims to be a simpler syntax, but still a superset of ERB, that's aware of the context we're in: HTML.
6 |
7 | We're swapping the usual <% %>, <%= %>, and <%# %> for \, \= and \# — these are also self-terminating expressions.
8 |
9 | So this ERB:
10 |
11 | ```erb
12 | <%# Some comment %>
13 | <% posts.each do |post| %>
14 |
<%= post.title %>
15 | <% end %>
16 | ```
17 |
18 | Can be this in BRB:
19 |
20 | ```ruby
21 | \# Some comment
22 | \posts.each do |post|
23 | \= post.title
24 | \end
25 | ```
26 |
27 | Note: you can also do `\ posts.each` and `\ end`, it just feels a little nicer to nestle once you've written a bit.
28 |
29 | We recognize lines starting with \ or \# as pure Ruby ones so we terminate on \n and convert to `<% %>`.
30 | Same goes for \= except we also terminate on ``, and then convert to `<%= %>`.
31 |
32 | Use the sigil `\p(post.title)` for multiple statements on the same line or to otherwise disambiguate statements.
33 |
34 | ### Preprocessing sigils
35 |
36 | BRB also includes preprocessing sigils. Sigils make common HTML output actions easier to write.
37 |
38 | At template compile time the sigils are replaced with the equivalent ERB:
39 |
40 | ```
41 | \p(post.options) -> <%= post.options %>
42 | \id(post) -> id="<%= dom_id(post) %>"
43 | \class(active: post.active?) -> class="<%= class_names(active: post.active?) %>"
44 | \attributes(post.options) -> <%= tag.attributes(post.options) %>
45 | \data(controller: :list) -> <%= tag.data(controller: :list) %>
46 | \aria(describedby: :post_1) -> <%= tag.aria(describedby: :post_1) %>
47 | \lorem -> Lorem ipsum dolor sit amet…
48 | ```
49 |
50 | There's also a `t` sigil, but it's got a little extra too:
51 |
52 | ```
53 | \t.message -> <%= t ".message" %>
54 | \t(fully.qualified.message) -> <%= t "fully.qualified.message" %>
55 | \t(Some bare words) -> <%= t "Some bare words" %> # Assumes we're using a gettext I18n backend, coming later!
56 | ```
57 |
58 | ### Embed ViewComponents with their templates
59 |
60 | I haven't figured this out, but I'm trying to explore a way to embed a component
61 | in its template. Using `~~~` to separate the frontmatter, a pure Ruby chunk where the component code goes, from the rest of the template file like this:
62 |
63 | ```ruby
64 | # app/views/message_component.html.erb
65 | class MessageComponent < ViewComponent::Base
66 | def initialize(name:) = @name = name
67 | end
68 | ~~~
69 |
70 | \= @name
71 | ```
72 |
73 | I'm exploring a backmatter version where you can write pure Ruby below the template code:
74 |
75 | ```ruby
76 | \= name
77 |
78 | ~~~
79 | # Useful with Nice Partials
80 | partial.helpers do
81 | # …
82 | end
83 | ```
84 |
85 | I'm still trying to figure out what Zeitwerk overrides are needed to make these work.
86 |
87 |
88 | ## Installation
89 |
90 | Bundle BRB and call `BRB.enable` during your Rails app boot to use it.
91 |
92 | Install the gem and add to the application's Gemfile by executing:
93 |
94 | $ bundle add brb-templates
95 |
96 | If bundler is not being used to manage dependencies, install the gem by executing:
97 |
98 | $ gem install brb-templates
99 |
100 | ## Development
101 |
102 | 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.
103 |
104 | 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
105 |
106 | ## Contributing
107 |
108 | Bug reports and pull requests are welcome on GitHub at https://github.com/kaspth/brb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/kaspth/brb/blob/main/CODE_OF_CONDUCT.md).
109 |
110 | ## License
111 |
112 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
113 |
114 | ## Code of Conduct
115 |
116 | Everyone interacting in the Brb project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kaspth/brb/blob/main/CODE_OF_CONDUCT.md).
117 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
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
13 |
--------------------------------------------------------------------------------
/bin/console:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # frozen_string_literal: true
3 |
4 | require "bundler/setup"
5 | require "brb"
6 |
7 | # You can add fixtures and/or initialization code here to make experimenting
8 | # with your gem easier. You can also use a different console, if you like.
9 |
10 | require "irb"
11 | IRB.start(__FILE__)
12 |
--------------------------------------------------------------------------------
/bin/setup:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | set -euo pipefail
3 | IFS=$'\n\t'
4 | set -vx
5 |
6 | bundle install
7 |
8 | # Do any other automated setup that you need to do here
9 |
--------------------------------------------------------------------------------
/brb.gemspec:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require_relative "lib/brb/version"
4 |
5 | Gem::Specification.new do |spec|
6 | spec.name = "brb-templates"
7 | spec.version = BRB::VERSION
8 | spec.authors = ["Kasper Timm Hansen"]
9 | spec.email = ["hey@kaspth.com"]
10 |
11 | spec.summary = "BRB is backslashed Ruby, a template system that lets you be-right-back to ERB."
12 | spec.homepage = "https://github.com/kaspth/brb"
13 | spec.license = "MIT"
14 | spec.required_ruby_version = ">= 3.0.0"
15 |
16 | spec.metadata["allowed_push_host"] = "https://rubygems.org"
17 |
18 | spec.metadata["homepage_uri"] = spec.homepage
19 | spec.metadata["source_code_uri"] = spec.homepage
20 | spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
21 |
22 | # Specify which files should be added to the gem when it is released.
23 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24 | spec.files = Dir.chdir(__dir__) do
25 | `git ls-files -z`.split("\x0").reject do |f|
26 | (File.expand_path(f) == __FILE__) ||
27 | f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
28 | end
29 | end
30 | spec.bindir = "exe"
31 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32 | spec.require_paths = ["lib"]
33 |
34 | spec.add_dependency "actionview", ">= 6.1"
35 | spec.add_development_dependency "actionpack", ">= 6.1"
36 |
37 | # For more information and examples about making a new gem, check out our
38 | # guide at: https://bundler.io/guides/creating_gem.html
39 | end
40 |
--------------------------------------------------------------------------------
/lib/brb.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require "action_view"
4 | require_relative "brb/version"
5 |
6 | module BRB
7 | class ::ActionView::Helpers::TagHelper::TagBuilder
8 | def aria(**options) = attributes(aria: options)
9 | def data(**options) = attributes(data: options)
10 | end
11 |
12 | singleton_class.attr_accessor :logger
13 | @logger = Logger.new "/dev/null"
14 | def self.debug = @logger = Logger.new(STDOUT)
15 |
16 | def self.enable = ActionView::Template::Handlers::ERB.erb_implementation = BRB::Parser
17 |
18 | module Sigil
19 | def self.names = @values.keys
20 | @values = {}
21 |
22 | def self.replace(scanner, key)
23 | @values.fetch(key).then do |template|
24 | case
25 | when key == "t" && scanner.scan(/\.[\.\w]+/) then template.sub ":value", scanner.matched
26 | when scanner.check(/\(/) then template.sub ":value", scan_arguments(scanner)
27 | else
28 | template
29 | end
30 | end
31 | end
32 |
33 | def self.scan_arguments(scanner)
34 | arguments, open_parens = +"", 0
35 |
36 | begin
37 | arguments << scanner.scan_until(/\(|\)/)
38 | open_parens += arguments.last == "(" ? 1 : -1
39 | end until open_parens.zero?
40 |
41 | arguments[1..-2]
42 | end
43 |
44 |
45 | def self.register(key, replacer)
46 | @values[key.to_s] = replacer
47 | end
48 |
49 | register :p, '<%= :value %>'
50 | register :t, '<%= t ":value" %>'
51 | register :id, 'id="<%= dom_id(:value) %>"'
52 | register :class, 'class="<%= class_names(:value) %>"'
53 | register :attributes, '<%= tag.attributes(:value) %>'
54 | register :aria, '<%= tag.aria(:value) %>'
55 | register :data, '<%= tag.data(:value) %>'
56 | register :lorem, <<~end_of_lorem
57 | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
58 | end_of_lorem
59 | end
60 |
61 | class Parser < ::ActionView::Template::Handlers::ERB::Erubi
62 | def initialize(input, ...)
63 | frontmatter = $1 if input.sub! /\A(.*?)~~~\n/m, ""
64 | backmatter = $1 if input.sub! /~~~\n(.*?)\z/m, ""
65 |
66 | @scanner = StringScanner.new(input)
67 | input = +""
68 | @mode = :start
69 |
70 | if @mode == :start
71 | if scan = @scanner.scan_until(/(?=\\)/)
72 | input << scan
73 | @scanner.skip(/\\/)
74 | @mode = :open
75 | else
76 | input << @scanner.rest
77 | @scanner.terminate
78 | end
79 | else
80 | case token = @scanner.scan(/#|=|#{Sigil.names.join("\\b|")}\b/)
81 | when "#" then @scanner.scan_until(/\n/)
82 | when *Sigil.names then input << Sigil.replace(@scanner, token)
83 | when "=" then input << "<%=" << @scanner.scan_until(/(?=<\/|\r?\n)/) << " %>"
84 | else
85 | @scanner.scan_until(/(?=\r?\n)/)&.then { input << "<% " << _1 << " %>" }
86 | end
87 |
88 | @mode = :start
89 | end until @scanner.eos?
90 |
91 | super
92 | end
93 | end
94 | end
95 |
--------------------------------------------------------------------------------
/lib/brb/templates.rb:
--------------------------------------------------------------------------------
1 | require_relative "../brb"
2 |
--------------------------------------------------------------------------------
/lib/brb/version.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module BRB
4 | VERSION = "0.1.1"
5 | end
6 |
--------------------------------------------------------------------------------
/test/brb_test.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require "test_helper"
4 |
5 | class BRBTest < ActionView::TestCase
6 | TestController.prepend_view_path "./test/partials"
7 |
8 | test "version number" do
9 | refute_nil ::BRB::VERSION
10 | end
11 |
12 | test "basic render" do
13 | render "basic", titles: [1,2]
14 | assert_equal <<~HTML, rendered
15 | 1
11
16 | 2
22
17 |
18 |
19 | YO
20 | HTML
21 | end
22 |
23 | test "sigils" do
24 | render "sigils", post: Post.new
25 | assert_equal <<~HTML, rendered
26 | Super
27 | id="post_1"
28 | class="active"
29 | aria-describedby="post_1"
30 | data-controller="list" data-action="order"
31 |
32 | Message
33 | Message
34 | Some Bare Words
35 | HTML
36 |
37 | render inline: <<~BRB
38 | \\lorem
39 | BRB
40 | assert_match(/Lorem ipsum/, rendered)
41 | end
42 |
43 | test "embedded parens" do
44 | render inline: "\\p(dom_id(Post.new)) \\p(dom_id(Post.new))"
45 | assert_equal "post_1 post_1", rendered
46 | end
47 |
48 | test "matter" do
49 | render "matter"
50 | assert_equal <<~HTML, rendered
51 |
52 |
53 |
54 | HTML
55 | end
56 | end
57 |
--------------------------------------------------------------------------------
/test/partials/_basic.html.erb:
--------------------------------------------------------------------------------
1 | \# Some comment
2 | \titles.each do |title|
3 | \= title
\= title\= title
4 | \end
5 |
6 |
7 | \concat "yo".upcase
8 |
9 |
--------------------------------------------------------------------------------
/test/partials/_matter.html.erb:
--------------------------------------------------------------------------------
1 | class MessageComponent < ViewComponent::Base
2 | def initialize(name:) = @name = name
3 | end
4 | ~~~
5 |
6 | \= @name
7 |
8 | ~~~
9 | puts "yo"
10 |
--------------------------------------------------------------------------------
/test/partials/_sigils.html.erb:
--------------------------------------------------------------------------------
1 | \p(post.title)
2 | \id(post)
3 | \class(active: true, inactive: false)
4 | \aria(describedby: :post_1)
5 | \data(controller: :list, action: :order)
6 |
7 | \t.message
8 | \t(fully.qualified.message)
9 | \t(Some bare words)
10 |
--------------------------------------------------------------------------------
/test/test_helper.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4 | require "brb"
5 |
6 | require "irb"
7 | require "debug"
8 | require "active_model"
9 | require "minitest/autorun"
10 |
11 | Minitest.backtrace_filter = Class.new do
12 | def filter(backtrace)
13 | backtrace.grep_v /gems\/(minitest|activesupport|actionview)/
14 | end
15 | end.new
16 |
17 | BRB.enable
18 | BRB.debug if ENV["DEBUG"]
19 |
20 | class Post
21 | include ActiveModel::Model
22 |
23 | def id = 1
24 | def title = "Super"
25 | end
26 |
--------------------------------------------------------------------------------