redcarpet-form-extension 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: e53e6b979993e1025392b3f55e0c17ee6d33d9023812ff8f5ce9e92a6b105d8f
4
+ data.tar.gz: d7326bc8df6b1e0be9f8d77bbdfdc3170a05e7aac3c8161cd1a37fa509634e89
5
+ SHA512:
6
+ metadata.gz: 491b0f555c8de7cfb3d11f9ff36c84eacd2f3b4e1115bf2a61a134256e927c8af1d2ed55b791768a41feccba34514870c9535685ee22aa10c8800daa84f3a8ec
7
+ data.tar.gz: a8f5f8e25af3791b33e7377aa0ed170cae6898850a8992ff3bd66b4f18c114706cb7deefec9d487de9b8754999aac633e3df14d5c9833250d4e92a0a3a095077
@@ -0,0 +1,54 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ "master" ]
6
+ pull_request:
7
+ branches: [ "master" ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Lint + Test
12
+ runs-on: ubuntu-latest
13
+ permissions:
14
+ contents: read
15
+ packages: write
16
+
17
+ steps:
18
+ - uses: actions/checkout@v3
19
+ - name: Set up Ruby
20
+ uses: ruby/setup-ruby@v1
21
+ with:
22
+ # ruby-version: 3.1.x # Not needed with a .ruby-version file
23
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
24
+
25
+ - name: Run lint
26
+ run: bundle exec rubocop
27
+
28
+ - name: Run tests
29
+ run: bundle exec rspec
30
+
31
+ - name: Publish to GPR
32
+ if: false # always skip job
33
+ run: |
34
+ mkdir -p $HOME/.gem
35
+ touch $HOME/.gem/credentials
36
+ chmod 0600 $HOME/.gem/credentials
37
+ printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
38
+ gem build *.gemspec
39
+ gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
40
+ env:
41
+ GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}"
42
+ OWNER: ${{ github.repository_owner }}
43
+
44
+ - name: Publish to RubyGems
45
+ if: false # always skip job
46
+ run: |
47
+ mkdir -p $HOME/.gem
48
+ touch $HOME/.gem/credentials
49
+ chmod 0600 $HOME/.gem/credentials
50
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
51
+ gem build *.gemspec
52
+ gem push *.gem
53
+ env:
54
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.byebug_history
10
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,100 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ DisplayCopNames: true
7
+ TargetRubyVersion: 3.1
8
+ NewCops: disable
9
+ Exclude:
10
+ - 'vendor/**/*'
11
+ - 'spec/fixtures/**/*'
12
+ - 'tmp/**/*'
13
+ - '.git/**/*'
14
+ - 'bin/*'
15
+
16
+ Naming/PredicateName:
17
+ # Method define macros for dynamically generated method.
18
+ MethodDefinitionMacros:
19
+ - define_method
20
+ - define_singleton_method
21
+ - def_node_matcher
22
+ - def_node_search
23
+
24
+ Style/BlockComments:
25
+ Exclude:
26
+ - 'spec/**/*.rb'
27
+
28
+ Style/FormatStringToken:
29
+ # Because we parse a lot of source codes from strings. Percent arrays
30
+ # look like unannotated format string tokens to this cop.
31
+ Exclude:
32
+ - spec/**/*
33
+
34
+ Layout/EndOfLine:
35
+ EnforcedStyle: lf
36
+
37
+ Layout/ClassStructure:
38
+ Enabled: true
39
+
40
+ Layout/RedundantLineBreak:
41
+ Enabled: true
42
+
43
+ Layout/TrailingWhitespace:
44
+ AllowInHeredoc: false
45
+
46
+ Lint/AmbiguousBlockAssociation:
47
+ Exclude:
48
+ - 'spec/**/*.rb'
49
+
50
+ Layout/HashAlignment:
51
+ EnforcedHashRocketStyle:
52
+ - key
53
+ - table
54
+ EnforcedColonStyle:
55
+ - key
56
+ - table
57
+
58
+ Layout/LineLength:
59
+ Max: 100
60
+ Exclude:
61
+ - 'redcarpet-form-extension.gemspec'
62
+ - 'spec/**/*.rb'
63
+
64
+ Lint/InterpolationCheck:
65
+ Exclude:
66
+ - 'spec/**/*.rb'
67
+
68
+ Lint/UselessAccessModifier:
69
+ MethodCreatingMethods:
70
+ - 'def_matcher'
71
+ - 'def_node_matcher'
72
+
73
+ Metrics/BlockLength:
74
+ Exclude:
75
+ - 'Rakefile'
76
+ - '**/*.rake'
77
+ - 'spec/**/*.rb'
78
+ - '**/*.gemspec'
79
+
80
+ Metrics/ModuleLength:
81
+ Exclude:
82
+ - 'spec/**/*.rb'
83
+
84
+ RSpec/PredicateMatcher:
85
+ EnforcedStyle: explicit
86
+
87
+ RSpec/MessageSpies:
88
+ EnforcedStyle: receive
89
+
90
+ RSpec/NestedGroups:
91
+ Max: 7
92
+
93
+ RSpec/MultipleMemoizedHelpers:
94
+ Enabled: false
95
+
96
+ RSpec/StubbedMock:
97
+ Enabled: false
98
+
99
+ Gemspec/DependencyVersion:
100
+ Enabled: true
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
@@ -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 csofiamsousa@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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in redcarpet-form-extension.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 12.0'
9
+
10
+ # Behaviour Driven Development for Ruby
11
+ gem 'rspec'
12
+
13
+ # RuboCop is a Ruby static code analyzer (a.k.a. linter) and code formatter.
14
+ gem 'rubocop'
15
+
16
+ # A RuboCop plugin for Rake.
17
+ gem 'rubocop-rake'
18
+
19
+ # RSpec-specific analysis for your projects, as an extension to RuboCop.
20
+ gem 'rubocop-rspec'
21
+
22
+ # Byebug is a simple to use and feature rich debugger for Ruby.
23
+ gem 'byebug'
24
+
25
+ # Code coverage for Ruby
26
+ gem 'simplecov', require: false, group: :test
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ redcarpet-form-extension (0.1.0)
5
+ redcarpet (~> 3.5.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ byebug (11.1.3)
12
+ diff-lcs (1.5.0)
13
+ docile (1.4.0)
14
+ parallel (1.22.1)
15
+ parser (3.1.2.0)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.1.1)
18
+ rake (12.3.3)
19
+ redcarpet (3.5.1)
20
+ regexp_parser (2.5.0)
21
+ rexml (3.2.5)
22
+ rspec (3.11.0)
23
+ rspec-core (~> 3.11.0)
24
+ rspec-expectations (~> 3.11.0)
25
+ rspec-mocks (~> 3.11.0)
26
+ rspec-core (3.11.0)
27
+ rspec-support (~> 3.11.0)
28
+ rspec-expectations (3.11.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-mocks (3.11.1)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-support (3.11.0)
35
+ rubocop (1.30.1)
36
+ parallel (~> 1.10)
37
+ parser (>= 3.1.0.0)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ regexp_parser (>= 1.8, < 3.0)
40
+ rexml (>= 3.2.5, < 4.0)
41
+ rubocop-ast (>= 1.18.0, < 2.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 1.4.0, < 3.0)
44
+ rubocop-ast (1.18.0)
45
+ parser (>= 3.1.1.0)
46
+ rubocop-rake (0.6.0)
47
+ rubocop (~> 1.0)
48
+ rubocop-rspec (2.11.1)
49
+ rubocop (~> 1.19)
50
+ ruby-progressbar (1.11.0)
51
+ simplecov (0.21.2)
52
+ docile (~> 1.1)
53
+ simplecov-html (~> 0.11)
54
+ simplecov_json_formatter (~> 0.1)
55
+ simplecov-html (0.12.3)
56
+ simplecov_json_formatter (0.1.4)
57
+ unicode-display_width (2.1.0)
58
+
59
+ PLATFORMS
60
+ x86_64-darwin-20
61
+ x86_64-linux
62
+
63
+ DEPENDENCIES
64
+ byebug
65
+ rake (~> 12.0)
66
+ redcarpet-form-extension!
67
+ rspec
68
+ rubocop
69
+ rubocop-rake
70
+ rubocop-rspec
71
+ simplecov
72
+
73
+ BUNDLED WITH
74
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Sofia Sousa
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,119 @@
1
+ # Redcarpet::Form::Extension
2
+
3
+ A [Redcarpet](https://github.com/vmg/redcarpet) extension with a custom HTML render to handle custom Markdown rules for tags like inputs and textareas.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'redcarpet-form-extension'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install redcarpet-form-extension
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # Initializes a Markdown parser with Redcarpet::Form::Extension::Render::HTML
25
+ markdown = Redcarpet::Markdown.new(Redcarpet::Form::Extension::Render::HTML)
26
+ ```
27
+
28
+ ### Input (inline)
29
+
30
+ Use the follwoing snap code to add an input tag in your html. By default, tag attributes are `type="text" name=""` but you can use `{}` to pass the attributes you need:
31
+
32
+ ```txt
33
+ Some text before ____{ type="number" } and after.
34
+ ```
35
+
36
+ Returns:
37
+
38
+ ```html
39
+ <input type="number" name="" />
40
+ ````
41
+
42
+ ### Checkboxes
43
+
44
+ Also, you can create your fieldset of checkboxes:
45
+
46
+ ```text
47
+ []{value="bike"} I have a bike [x]{value="car"} I have a car [x]{value="boat"} I have a boat
48
+ ```
49
+
50
+ This snap returns the following HTML:
51
+
52
+ ```html
53
+ <fieldset>
54
+ <label><input type="checkbox" name="xekiry[]" value="bike" />I have a bike</label>
55
+ <label><input type="checkbox" name="xekiry[]" value="car" checked />I have a car</label>
56
+ <label><input type="checkbox" name="xekiry[]" value="boat" checked />I have a boat</label>
57
+ </fieldset>
58
+ ```
59
+
60
+ **Note:** If `name` attribute is not passed, a temporary and random string will be generated. For empty
61
+ `value`s, a `opt_` string follwed by the element index will be placed.
62
+
63
+ ### Radio Buttons
64
+
65
+ For a radio buttons group, you can type the following:
66
+
67
+ ```txt
68
+ () HTML (x) CSS () JavaScript
69
+ ```
70
+
71
+ which will return:
72
+
73
+ ```html
74
+ <fieldset>
75
+ <label><input type="radio" name="ybqkvo" value="radio_1" />HTML</label>
76
+ <label><input type="radio" name="ybqkvo" value="radio_2" checked />CSS</label>
77
+ <label><input type="radio" name="ybqkvo" value="radio_3" />JavaScript</label>
78
+ </fieldset>
79
+ ```
80
+
81
+ **Note:** If `name` attribute is not passed, a temporary and random string will be generated. For empty
82
+ `value`s, a `radio_` string follwed by the element index will be placed.
83
+
84
+ ### Textarea
85
+
86
+ Finally, a textarea can be generated using the follwing snap:
87
+
88
+ ```txt
89
+ [textarea]{value="Hello world!" rows="4" cols="10"} Text box
90
+ ```
91
+
92
+ The HTML result is:
93
+
94
+ ```html
95
+ <fieldset>
96
+ <label>Text box</label>
97
+ <textarea name="kaatyu" rows="4" cols="10">
98
+ Hello world!
99
+ </textarea>
100
+ </fieldset>
101
+ ````
102
+
103
+ ## Development
104
+
105
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
106
+
107
+ 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).
108
+
109
+ ## Contributing
110
+
111
+ Bug reports and pull requests are welcome on GitHub at https://github.com/SofiaSousa/redcarpet-form-extension. 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/SofiaSousa/redcarpet-form-extension/blob/master/CODE_OF_CONDUCT.md).
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
116
+
117
+ ## Code of Conduct
118
+
119
+ Everyone interacting in the Redcarpet::Form::Extension project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/SofiaSousa/redcarpet-form-extension/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "redcarpet/form/extension"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redcarpet
4
+ module Form
5
+ module Extension
6
+ module Block
7
+ # rubocop:disable Layout/LineLength
8
+ # A fieldset with checkboxes
9
+ #
10
+ # Ex: []{value="bike"} I have a bike [x]{value="car"} I have a car [x]{value="boat"} I have a boat
11
+ # <fieldset>
12
+ # <label><input type="checkbox" name="123456[]" value="bike" />I have a bike</label>
13
+ # <label><input type="checkbox" name="123456[]" value="car" checked />I have a car</label>
14
+ # <label><input type="checkbox" name="123456[]" value="boat" checked />I have a boat</label>
15
+ # </fieldset>
16
+ # rubocop:enable Layout/LineLength
17
+ class Checkboxes
18
+ def self.pattern
19
+ /\[(x|\s)?\]([{][^}]*[}])?([^{}\]\[]*)/
20
+ end
21
+
22
+ def self.default_attributes
23
+ { 'type' => 'checkbox', 'name' => '', 'value' => '' }
24
+ end
25
+
26
+ # rubocop:disable Metrics/MethodLength
27
+ # rubocop:disable Metrics/AbcSize
28
+ def self.html(matches)
29
+ html = '<fieldset>'
30
+ name = "#{Util.random_string}[]"
31
+
32
+ matches.each_with_index do |item, index|
33
+ checked = item[0]
34
+ label = item[2]
35
+
36
+ attrs = default_attributes.merge!(Util.parse_attributes(item[1]))
37
+ attrs['name'] = name if attrs['name'].empty?
38
+ attrs['value'] = "opt_#{index + 1}" if attrs['value'].empty?
39
+
40
+ html += '<label>' unless label.empty?
41
+ html += "<input #{Util.attributes_to_s(attrs)}"
42
+ html += ' checked' if checked == 'x'
43
+ html += ' />'
44
+ html += "#{label}</label>" unless label.empty?
45
+ end
46
+
47
+ html += '</fieldset>'
48
+ end
49
+ # rubocop:enable Metrics/AbcSize
50
+ # rubocop:enable Metrics/MethodLength
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redcarpet
4
+ module Form
5
+ module Extension
6
+ module Block
7
+ # A fieldset with radion buttons
8
+ #
9
+ # Ex: (){value="html"} HTML (x){value="css"} CSS (){value="js"} JavaScript
10
+ # <fieldset>
11
+ # <label><input type="radio" name="123456" value="html" />HTML</label>
12
+ # <label><input type="radio" name="123456" value="css" checked />CSS</label>
13
+ # <label><input type="radio" name="123456" value="js" />JavaScript</label>
14
+ # </fieldset>
15
+ class RadioButtons
16
+ def self.pattern
17
+ /\((x|\s)?\)([{][^}]*[}])?([^{})()]*)/
18
+ end
19
+
20
+ def self.default_attributes
21
+ { 'type' => 'radio', 'name' => '', 'value' => '' }
22
+ end
23
+
24
+ # rubocop:disable Metrics/MethodLength
25
+ # rubocop:disable Metrics/AbcSize
26
+ def self.html(matches)
27
+ html = '<fieldset>'
28
+ name = Util.random_string
29
+
30
+ matches.each_with_index do |item, index|
31
+ checked = item[0]
32
+ label = item[2]
33
+
34
+ attrs = default_attributes.merge!(Util.parse_attributes(item[1]))
35
+ attrs['name'] = name if attrs['name'].empty?
36
+ attrs['value'] = "radio_#{index + 1}" if attrs['value'].empty?
37
+
38
+ html += '<label>' unless label.empty?
39
+ html += "<input #{Util.attributes_to_s(attrs)}"
40
+ html += ' checked' if checked == 'x'
41
+ html += ' />'
42
+ html += "#{label}</label>" unless label.empty?
43
+ end
44
+
45
+ html += '</fieldset>'
46
+ end
47
+ # rubocop:enable Metrics/AbcSize
48
+ # rubocop:enable Metrics/MethodLength
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redcarpet
4
+ module Form
5
+ module Extension
6
+ module Block
7
+ # A textarea block
8
+ #
9
+ # Ex: [textarea]{value="Hello world!" rows="4" cols="10"} Text box
10
+ # <fieldset>
11
+ # <label>Text box</label>
12
+ # <textarea name="123456" rows="4" cols="10">
13
+ # Hello world!
14
+ # </textarea>
15
+ # </fieldset>
16
+ class Textarea
17
+ def self.pattern
18
+ /\[textarea\]([{][^}]*[}])?([^{}\]\[)]*)/
19
+ end
20
+
21
+ def self.default_attributes
22
+ { 'name' => Util.random_string, 'rows' => '3', 'value' => '' }
23
+ end
24
+
25
+ def self.html(matches)
26
+ html = '<fieldset>'
27
+
28
+ matches.each do |item|
29
+ label = item[1]
30
+ attrs = default_attributes.merge!(Util.parse_attributes(item[0]))
31
+
32
+ html += "<label>#{label}</label>" unless label.empty?
33
+ html += "<textarea #{Util.attributes_to_s(attrs.except('value'))}>"
34
+ html += attrs['value'] unless attrs['value'].empty?
35
+ html += '</textarea>'
36
+ end
37
+
38
+ html += '</fieldset>'
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redcarpet
4
+ module Form
5
+ module Extension
6
+ module Inline
7
+ # A single input tag
8
+ #
9
+ # Ex: ____{ type="number" }
10
+ # <input type="number" name="" />
11
+ class Input
12
+ def self.pattern
13
+ /(____)(\{(.+)\})?/
14
+ end
15
+
16
+ def self.default_attributes
17
+ { 'type' => 'text', 'name' => '' }
18
+ end
19
+
20
+ def self.html(text)
21
+ attrs = default_attributes.merge!(Util.parse_attributes(text))
22
+ "<input #{Util.attributes_to_s(attrs)} />"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../block/checkboxes'
4
+ require_relative '../block/radio_buttons'
5
+ require_relative '../block/textarea'
6
+
7
+ require_relative '../inline/input'
8
+
9
+ module Redcarpet
10
+ module Form
11
+ module Extension
12
+ module Render
13
+ # Custom HTML render
14
+ class HTML < Redcarpet::Render::HTML
15
+ def paragraph(text)
16
+ process_custom_tags(text.strip)
17
+ end
18
+
19
+ private
20
+
21
+ # Processes custom tags (inline tags and block tags)
22
+ def process_custom_tags(text)
23
+ text = CGI.unescape_html(text)
24
+
25
+ # Inline tags
26
+ inline_tags.each do |klass|
27
+ text.gsub!(klass.pattern) { |sub| klass.html(sub) }
28
+ end
29
+
30
+ # Block tags (replace entire line)
31
+ block_tags.each do |klass|
32
+ next unless text.match?(klass.pattern)
33
+
34
+ return klass.html(text.scan(klass.pattern))
35
+ end
36
+
37
+ "<p>#{text}</p>\n"
38
+ end
39
+
40
+ # List of inline tags
41
+ def inline_tags
42
+ [
43
+ Redcarpet::Form::Extension::Inline::Input
44
+ ]
45
+ end
46
+
47
+ # List of block tags
48
+ def block_tags
49
+ [
50
+ Redcarpet::Form::Extension::Block::Checkboxes,
51
+ Redcarpet::Form::Extension::Block::RadioButtons,
52
+ Redcarpet::Form::Extension::Block::Textarea
53
+ ]
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redcarpet
4
+ module Form
5
+ module Extension
6
+ # Utils
7
+ module Util
8
+ def self.attributes_pattern
9
+ /(\S+)=["']([^"']*)["']/
10
+ end
11
+
12
+ def self.parse_attributes(text)
13
+ return {} unless text
14
+
15
+ attr_str = text.match(/[{]([^}]*)/)
16
+
17
+ return {} unless attr_str
18
+ return {} unless attr_str[1]
19
+
20
+ attr_str[1].scan(attributes_pattern).to_h
21
+ end
22
+
23
+ def self.attributes_to_s(attributes)
24
+ return '' unless attributes
25
+
26
+ attributes.map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
27
+ end
28
+
29
+ def self.random_string
30
+ (0...6).map { ('a'..'z').to_a[rand(26)] }.join
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redcarpet
4
+ module Form
5
+ module Extension
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'extension/version'
4
+ require_relative 'extension/render/html'
5
+ require_relative 'extension/util'
6
+
7
+ module Redcarpet
8
+ module Form
9
+ module Extension
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/redcarpet/form/extension/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'redcarpet-form-extension'
7
+ spec.version = Redcarpet::Form::Extension::VERSION
8
+ spec.authors = ['Sofia Sousa']
9
+ spec.email = ['csofiamsousa@gmail.com']
10
+
11
+ spec.summary = 'A Redcarpet extension with a custom HTML render to handle custom Markdown rules for tags like inputs and textareas'
12
+ spec.description = 'A Redcarpet extension with a custom HTML render to handle custom Markdown rules for tags like inputs and textareas'
13
+ spec.homepage = 'https://github.com/SofiaSousa/redcarpet-form-extension'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 3.1.0')
16
+
17
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/SofiaSousa/redcarpet-form-extension'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/SofiaSousa/redcarpet-form-extension'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'redcarpet', '~> 3.5.1'
33
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redcarpet-form-extension
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sofia Sousa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redcarpet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.5.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.5.1
27
+ description: A Redcarpet extension with a custom HTML render to handle custom Markdown
28
+ rules for tags like inputs and textareas
29
+ email:
30
+ - csofiamsousa@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".github/workflows/gem-push.yml"
36
+ - ".gitignore"
37
+ - ".rspec"
38
+ - ".rubocop.yml"
39
+ - ".ruby-version"
40
+ - CODE_OF_CONDUCT.md
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE.txt
44
+ - README.md
45
+ - Rakefile
46
+ - bin/console
47
+ - bin/setup
48
+ - lib/redcarpet/form/extension.rb
49
+ - lib/redcarpet/form/extension/block/checkboxes.rb
50
+ - lib/redcarpet/form/extension/block/radio_buttons.rb
51
+ - lib/redcarpet/form/extension/block/textarea.rb
52
+ - lib/redcarpet/form/extension/inline/input.rb
53
+ - lib/redcarpet/form/extension/render/html.rb
54
+ - lib/redcarpet/form/extension/util.rb
55
+ - lib/redcarpet/form/extension/version.rb
56
+ - redcarpet-form-extension.gemspec
57
+ homepage: https://github.com/SofiaSousa/redcarpet-form-extension
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ homepage_uri: https://github.com/SofiaSousa/redcarpet-form-extension
62
+ source_code_uri: https://github.com/SofiaSousa/redcarpet-form-extension
63
+ changelog_uri: https://github.com/SofiaSousa/redcarpet-form-extension
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.1.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.3.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A Redcarpet extension with a custom HTML render to handle custom Markdown
83
+ rules for tags like inputs and textareas
84
+ test_files: []