brb-templates 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: 95c2115da0e8d5c63faf5b616b15ffc0bfb69ace0fc3acb03ad79da917f6b28d
4
+ data.tar.gz: 239cd354e9a666dd3d8a883d8ac44f8fa091bb909245785083cc320ee5d8729f
5
+ SHA512:
6
+ metadata.gz: d5d04521fa9d22453e408d140dd2a0702345081c5e2cace1649f0b5d9e7d3dab7fcc9225d5a3190ce357e12c99d8589c377d7772a81a4d34a2c44df6fd383c40
7
+ data.tar.gz: b04b179f4989a1c2f6377bd432e7b8a6c5d4a8529c8525651158dfafe70030c10a1745e392c50afe313ae2e0ba8fad18ca8346bbffd5bdd5737a58db7347d642
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-10-04
4
+
5
+ - 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 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.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
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.
data/README.md ADDED
@@ -0,0 +1,116 @@
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
+ <h1><%= post.title %></h1>
15
+ <% end %>
16
+ ```
17
+
18
+ Can be this in BRB:
19
+
20
+ ```ruby
21
+ \# Some comment
22
+ \posts.each do |post|
23
+ <h1>\= post.title</h1>
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
+ <h1>\= @name</h1>
71
+ ```
72
+
73
+ I'm exploring a backmatter version where you can write pure Ruby below the template code:
74
+
75
+ ```ruby
76
+ <h1>\= name</h1>
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 UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
95
+
96
+ If bundler is not being used to manage dependencies, install the gem by executing:
97
+
98
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
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).
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
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BRB
4
+ VERSION = "0.1.0"
5
+ end
data/lib/brb.rb ADDED
@@ -0,0 +1,81 @@
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::Erubi
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
+ if (key == "t" && scanner.scan(/(\.[\.\w]+)/)) || scanner.scan(/\((.*?)\)/)
25
+ template.sub ":value", scanner[1]
26
+ else
27
+ template
28
+ end
29
+ end
30
+ end
31
+
32
+ def self.register(key, replacer)
33
+ @values[key.to_s] = replacer
34
+ end
35
+
36
+ register :p, '<%= :value %>'
37
+ register :t, '<%= t ":value" %>'
38
+ register :id, 'id="<%= dom_id(:value) %>"'
39
+ register :class, 'class="<%= class_names(:value) %>"'
40
+ register :attributes, '<%= tag.attributes(:value) %>'
41
+ register :aria, '<%= tag.aria(:value) %>'
42
+ register :data, '<%= tag.data(:value) %>'
43
+ register :lorem, <<~end_of_lorem
44
+ 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.
45
+ end_of_lorem
46
+ end
47
+
48
+ class Erubi < ::ActionView::Template::Handlers::ERB::Erubi
49
+ def initialize(input, ...)
50
+ frontmatter = $1 if input.sub! /\A(.*?)~~~\n/m, ""
51
+ backmatter = $1 if input.sub! /~~~\n(.*?)\z/m, ""
52
+
53
+ @scanner = StringScanner.new(input)
54
+ input = +""
55
+ @mode = :start
56
+
57
+ if @mode == :start
58
+ if scan = @scanner.scan_until(/(?=\\)/)
59
+ input << scan
60
+ @scanner.skip(/\\/)
61
+ @mode = :open
62
+ else
63
+ input << @scanner.rest
64
+ @scanner.terminate
65
+ end
66
+ else
67
+ case token = @scanner.scan(/#|=|#{Sigil.names.join("\\b|")}\b/)
68
+ when "#" then @scanner.scan_until(/\n/)
69
+ when *Sigil.names then input << Sigil.replace(@scanner, token)
70
+ when "=" then input << "<%=" << @scanner.scan_until(/(?=<\/|\r?\n)/) << " %>"
71
+ else
72
+ @scanner.scan_until(/(?=\r?\n)/)&.then { input << "<% " << _1 << " %>" }
73
+ end
74
+
75
+ @mode = :start
76
+ end until @scanner.eos?
77
+
78
+ super
79
+ end
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brb-templates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Timm Hansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionview
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6.1'
41
+ description:
42
+ email:
43
+ - hey@kaspth.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - CODE_OF_CONDUCT.md
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/brb.rb
54
+ - lib/brb/version.rb
55
+ homepage: https://github.com/kaspth/brb
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ allowed_push_host: https://rubygems.org
60
+ homepage_uri: https://github.com/kaspth/brb
61
+ source_code_uri: https://github.com/kaspth/brb
62
+ changelog_uri: https://github.com/kaspth/brb/blob/main/CHANGELOG.md
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 3.0.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.5.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: BRB is backslashed Ruby, a template system that lets you be-right-back to
82
+ ERB.
83
+ test_files: []