liquid-component 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c85ed0049b8cbfd0481b1d36c1ab305ec215ab8e114a968bd734497d86a832b9
4
+ data.tar.gz: a2a208d862e263960319f0115a15b556e2c122d3d6b077cb9f43374eef3a97a6
5
+ SHA512:
6
+ metadata.gz: 80edd204b2a60ff23829ac5be0e34518c3c20568fd1307f04dcc5e003615175335c746221eaa9b6ffc3b40fa34ac4711fbe5a3042c7801732b50944acba6fcc5
7
+ data.tar.gz: 8757d309373405dfba9bda254a234b65169d64dd54b0f1ae2af0b6d340344fb8d05e39096fd36b3932911cd80f6167251ae705ee89c3d37b67b56e7ed74e56c5
@@ -0,0 +1,77 @@
1
+ # 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, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and 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
59
+ [jared@whitefusion.io](mailto:jared@whitefusion.io). All complaints will
60
+ be reviewed and investigated and will result in a response that is deemed
61
+ necessary and appropriate to the circumstances. The project team is obligated
62
+ to maintain confidentiality with regard to the reporter of an incident.
63
+ Further details of specific enforcement policies may be posted separately.
64
+
65
+ Project maintainers who do not follow or enforce the Code of Conduct in good
66
+ faith may face temporary or permanent repercussions as determined by other
67
+ members of the project's leadership.
68
+
69
+ ## Attribution
70
+
71
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
72
+ available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct.html](https://www.contributor-covenant.org/version/1/4/code-of-conduct.html)
73
+
74
+ [homepage]: https://www.contributor-covenant.org
75
+
76
+ For answers to common questions about this code of conduct, see
77
+ [https://www.contributor-covenant.org/faq](https://www.contributor-covenant.org/faq)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in liquid-component.gemspec
6
+ gemspec
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ liquid-component (0.1.0)
5
+ activesupport (>= 5.0)
6
+ safe_yaml (~> 1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (6.0.3.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ zeitwerk (~> 2.2, >= 2.2.2)
17
+ ansi (1.5.0)
18
+ builder (3.2.4)
19
+ concurrent-ruby (1.1.6)
20
+ i18n (1.8.2)
21
+ concurrent-ruby (~> 1.0)
22
+ minitest (5.14.1)
23
+ minitest-profile (0.0.2)
24
+ minitest-reporters (1.4.2)
25
+ ansi
26
+ builder
27
+ minitest (>= 5.0)
28
+ ruby-progressbar
29
+ rake (12.3.3)
30
+ ruby-progressbar (1.10.1)
31
+ safe_yaml (1.0.5)
32
+ thread_safe (0.3.6)
33
+ tzinfo (1.2.7)
34
+ thread_safe (~> 0.1)
35
+ zeitwerk (2.3.0)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler
42
+ liquid-component!
43
+ minitest (~> 5.0)
44
+ minitest-profile (>= 0.0.2)
45
+ minitest-reporters (~> 1.0)
46
+ rake (~> 12.0)
47
+
48
+ BUNDLED WITH
49
+ 1.17.3
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Jared White
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,128 @@
1
+ # Liquid Component
2
+
3
+ Smart components for the Liquid template language. Use YAML front matter to specify component metadata (name, description) and variable types. Use
4
+ component metadata for validation and reflection (perhaps to build visual preview stories).
5
+
6
+ _Currently under active development._
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'liquid-component'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```sh
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```sh
25
+ $ gem install liquid-component
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ Given a Liquid template string, all you need to do is call `LiquidComponent.parse`:
31
+
32
+ ```ruby
33
+ component = LiquidComponent.parse(template)
34
+ ```
35
+
36
+ The return value will be a `LiquidComponent::Component` object which provides two data structures:
37
+
38
+ * `metadata`: the parsed front matter as a `LiquidComponent::Metadata` object.
39
+ * `content`: the body of the Liquid template.
40
+
41
+ Even if there's no front matter, a component will successfully instantiate, but all the metadata values will be nil. The component object also provides convenience methods to get to the metadata values, so `component.name` is the same as `component.metadata.name`.
42
+
43
+ Here are the possible `LiquidComponent::Metadata` values, all optional:
44
+
45
+ * `name`: the human-readable name of the component.
46
+ * `description`: a description of what the component is for.
47
+ * `variables`: an array of `LiquidComponent::Variable` variable definitions.
48
+ * `additional`: any additional custom YAML front matter, if any.
49
+
50
+ Here are the possible `LiquidComponent::Variable` values:
51
+
52
+ * `name`: the variable identifier.
53
+ * `type`: a symbol indicating a data type…string, boolean, array, etc.
54
+ * `required`: `true` if the variable is required, `false` if it is optional.
55
+ * `description`: an optional description of what the variable is for.
56
+
57
+ ## Spec in progress
58
+
59
+ This is a quick attempt to document a possible spec for Liquid Components. It builds upon the brand new `render` tag functionality but adds block mode, Markdown formatting, and validation.
60
+
61
+ ### Example
62
+
63
+ ```liquid
64
+ {% rendercontent "cards/widget", title: "I'm a Card", show_footer: false, theme: page.theme %}
65
+ I'm the body of the card. I support **Markdown** _formatting!_
66
+ {% endrendercontent %}
67
+ ```
68
+
69
+ ### Defining a Component
70
+
71
+ ```html
72
+ ---
73
+ name: Widget Card
74
+ description: Displays a card about a widget that you can open.
75
+ variables:
76
+ title:
77
+ - string
78
+ - The title of the card displayed in a header along the top.
79
+ show_footer: [boolean, Display bottom footer.]
80
+ theme?: object # optional variable
81
+ content: markdown
82
+ ---
83
+
84
+ <div class="widget card {{ theme | default: "default" }}">
85
+ <div class="card-title">{{ title }}</div>
86
+ <div class="card-body">{{ content }}</div>
87
+ {% if show_footer %}
88
+ <div class="card-footer"><button>Open the Widget</button></div>
89
+ {% endif %}
90
+ </div>
91
+ ```
92
+
93
+ ### Variable Warnings (TODO)
94
+
95
+ If a variable is supplied to a component in the wrong format, or if it's missing when it's required, then the component will emit a Liquid warning. By default it won't interrupt the overall rendering of Liquid templates (but that is configurable). The goal is simply to catch errors at development time, not frustrate the process of generating a working site.
96
+
97
+ ## TODO
98
+
99
+ * Finish fleshing out variable types
100
+ * Variable validation
101
+ * Describe content processors (HTML, Markdown, JSON, YAML, etc.)
102
+ * Investigate multiple "content areas" ([see github/view_component for inspiration](https://github.com/github/view_component/blob/master/README.md#content-areas))
103
+ * How to handle sidecar assets for components (related JS and CSS files)
104
+ * _Interesting thought:_ what if each component rendered out as a Web Component (aka `<liquid-cards-widget>…</liquid-cards-widget>`) and then that could easily be hooked into for implementing CSS/JS frontend functionality? Maybe make that an optional `web_component: true` setting in the front matter.
105
+ * Testing strategy
106
+ * Component previews
107
+
108
+ ----
109
+
110
+ ## Testing
111
+
112
+ Run `bundle exec rake` to run the test suite.
113
+
114
+ ## Development
115
+
116
+ 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).
117
+
118
+ ## Contributing
119
+
120
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bridgetownrb/liquid-component.
121
+
122
+ ## License
123
+
124
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
125
+
126
+ ## Code of Conduct
127
+
128
+ Everyone interacting in the Phaedra project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bridgetownrb/liquid-component/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :spec => :test
4
+ task default: :test
5
+
6
+ require "rake/testtask"
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << "lib" << "test"
9
+ test.pattern = "test/**/test_*.rb"
10
+ test.verbose = true
11
+ end
@@ -0,0 +1,37 @@
1
+ require "liquid-component/version"
2
+
3
+ require "safe_yaml/load"
4
+ SafeYAML::OPTIONS[:suppress_warnings] = true
5
+
6
+ require "liquid-component/component"
7
+ require "liquid-component/metadata"
8
+ require "liquid-component/variable"
9
+
10
+ module LiquidComponent
11
+ FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
12
+
13
+ def self.parse(template)
14
+ template = template.to_s
15
+
16
+ yaml_data = {}
17
+ file_content = nil
18
+
19
+ if has_yaml_header?(template)
20
+ if template = template.match(FRONT_MATTER_REGEXP)
21
+ file_content = template.post_match
22
+ yaml_data = SafeYAML.load(template.captures[0])
23
+ end
24
+ else
25
+ file_content = template
26
+ end
27
+
28
+ Component.new(
29
+ metadata: Metadata.new(yaml_data),
30
+ content: file_content
31
+ )
32
+ end
33
+
34
+ def self.has_yaml_header?(template)
35
+ template.lines.first&.match? %r!\A---\s*\r?\n!
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ module LiquidComponent
2
+ class Component
3
+ attr_accessor :metadata, :content
4
+
5
+ def initialize(metadata:, content:)
6
+ self.metadata = metadata
7
+ self.content = content
8
+ end
9
+
10
+ def name
11
+ metadata.name
12
+ end
13
+
14
+ def description
15
+ metadata.description
16
+ end
17
+
18
+ def variables
19
+ metadata.variables
20
+ end
21
+
22
+ def additional_metadata
23
+ metadata.additional
24
+ end
25
+
26
+ def to_h
27
+ {
28
+ metadata: metadata.to_h,
29
+ content: content
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ require "active_support/core_ext/hash/indifferent_access"
2
+
3
+ module LiquidComponent
4
+ class Metadata
5
+ attr_accessor :name, :description, :variables, :additional
6
+
7
+ def initialize(metadata)
8
+ metadata = metadata.with_indifferent_access
9
+
10
+ self.name = metadata[:name]
11
+ self.description = metadata[:description]
12
+ self.variables = Variable.parse(metadata[:variables])
13
+
14
+ metadata.delete(:name)
15
+ metadata.delete(:description)
16
+ metadata.delete(:variables)
17
+
18
+ self.additional = metadata
19
+ end
20
+
21
+ def to_h
22
+ h = {}
23
+ h[:name] = name if name
24
+ h[:description] = description if description
25
+ h[:variables] = variables.map(&:to_h)
26
+ h[:additional] = additional
27
+
28
+ h
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ module LiquidComponent
2
+ class Variable
3
+ attr_accessor :name, :type, :required, :description
4
+
5
+ def self.parse(variables)
6
+ return [] if variables.nil?
7
+
8
+ variables.map do |variable_name, variable_metadata|
9
+ new(variable_name, variable_metadata)
10
+ end
11
+ end
12
+
13
+ def initialize(variable_name, variable_metadata)
14
+ self.required = true
15
+
16
+ self.name = if variable_name.end_with? "?"
17
+ self.required = false
18
+ variable_name.sub(/\?$/, "").to_sym
19
+ else
20
+ variable_name.to_sym
21
+ end
22
+
23
+ if variable_metadata.is_a?(Array)
24
+ self.type = variable_metadata[0].to_sym
25
+ self.description = variable_metadata[1]
26
+ else
27
+ self.type = variable_metadata.to_sym
28
+ end
29
+ end
30
+
31
+ def to_h
32
+ h = {
33
+ name: name,
34
+ type: type,
35
+ required: required
36
+ }
37
+ h[:description] = description if description
38
+
39
+ h
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module LiquidComponent
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "lib/liquid-component/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "liquid-component"
5
+ spec.version = LiquidComponent::VERSION
6
+ spec.authors = ["Jared White"]
7
+ spec.email = ["jared@whitefusion.io"]
8
+
9
+ spec.summary = %q{Load and parse Liquid Component templates with YAML front matter}
10
+ spec.description = spec.summary
11
+ spec.homepage = "https://github.com/bridgetownrb/liquid-component"
12
+ spec.license = "MIT"
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(script|test|spec|features)/}) }
18
+ end
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "activesupport", ">= 5.0"
22
+ spec.add_runtime_dependency "safe_yaml", "~> 1.0"
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "rake", "~> 12.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ spec.add_development_dependency "minitest-profile", ">= 0.0.2"
28
+ spec.add_development_dependency "minitest-reporters", "~> 1.0"
29
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liquid-component
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jared White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: safe_yaml
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.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: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-profile
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.0.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.0.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
111
+ description: Load and parse Liquid Component templates with YAML front matter
112
+ email:
113
+ - jared@whitefusion.io
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - CODE_OF_CONDUCT.md
119
+ - Gemfile
120
+ - Gemfile.lock
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - lib/liquid-component.rb
125
+ - lib/liquid-component/component.rb
126
+ - lib/liquid-component/metadata.rb
127
+ - lib/liquid-component/variable.rb
128
+ - lib/liquid-component/version.rb
129
+ - liquid-component.gemspec
130
+ homepage: https://github.com/bridgetownrb/liquid-component
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubygems_version: 3.0.8
150
+ signing_key:
151
+ specification_version: 4
152
+ summary: Load and parse Liquid Component templates with YAML front matter
153
+ test_files: []