markdown_to_rspec 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: 4b78ea54d37cc1e236a80ac842736639c5b53cec947bdc083a64b6d879077ad4
4
+ data.tar.gz: e440bba54a411c95e09acae86e13ad639c1d36299f7d4b11552acd4f97573ba2
5
+ SHA512:
6
+ metadata.gz: c63230d0a5a9f3eca520c93d82c3ea44a592d0a33c4c70b53d664dae10fabde17e98634b03e745c1807157e95f21a29fdff47f0bf85886d73660ae221282777e
7
+ data.tar.gz: 80a9d513a59e738ca15d3eca8e653ffc63613d969eeb5d967574216abd45863b7c0dd683c688bca6ad4af2e99b8e83fb71380a3689a4d55cbdac74644537235b
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # configured
14
+ vendor
15
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,68 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7.0
3
+ EnabledByDefault: true
4
+ Exclude:
5
+ - 'vendor/**/*'
6
+ - 'markdown_to_rspec.gemspec'
7
+
8
+ Bundler/GemComment:
9
+ Enabled: false
10
+
11
+ Metrics/ClassLength:
12
+ Max: 120
13
+
14
+ Metrics/BlockLength:
15
+ Exclude:
16
+ - 'spec/**/*'
17
+ - 'markdown_to_rspec/rake_tasks.rb'
18
+
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ Style/DocumentationMethod:
23
+ Enabled: false
24
+
25
+ Style/ParallelAssignment:
26
+ Enabled: false
27
+
28
+ Style/MethodCallWithArgsParentheses:
29
+ Enabled: false
30
+
31
+ Style/Copyright:
32
+ Enabled: false
33
+
34
+ Style/HashEachMethods:
35
+ Enabled: true
36
+
37
+ Style/StringHashKeys:
38
+ Enabled: false
39
+
40
+ Style/InlineComment:
41
+ Enabled: false
42
+
43
+ Style/HashTransformKeys:
44
+ Enabled: true
45
+
46
+ Style/HashTransformValues:
47
+ Enabled: true
48
+
49
+ Style/ConstantVisibility:
50
+ Enabled: false
51
+
52
+ Style/GuardClause:
53
+ Enabled: false
54
+
55
+ Layout/LineLength:
56
+ Max: 120
57
+
58
+ Layout/EmptyLineAfterGuardClause:
59
+ Enabled: false
60
+
61
+ Layout/MultilineAssignmentLayout:
62
+ EnforcedStyle: same_line
63
+
64
+ Lint/ConstantResolution:
65
+ Enabled: false
66
+
67
+ Lint/MissingSuper:
68
+ Enabled: false
@@ -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. 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,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in markdown_to_rspec.gemspec
6
+ gemspec
7
+
8
+ gem 'rspec', '~> 3.0'
9
+ gem 'rubocop'
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ markdown_to_rspec (0.1.0)
5
+ rake (~> 13.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.1)
11
+ diff-lcs (1.4.4)
12
+ parallel (1.19.2)
13
+ parser (2.7.1.4)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.0.0)
16
+ rake (13.0.1)
17
+ regexp_parser (1.7.1)
18
+ rexml (3.2.4)
19
+ rspec (3.9.0)
20
+ rspec-core (~> 3.9.0)
21
+ rspec-expectations (~> 3.9.0)
22
+ rspec-mocks (~> 3.9.0)
23
+ rspec-core (3.9.2)
24
+ rspec-support (~> 3.9.3)
25
+ rspec-expectations (3.9.2)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-mocks (3.9.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-support (3.9.3)
32
+ rubocop (0.89.1)
33
+ parallel (~> 1.10)
34
+ parser (>= 2.7.1.1)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.7)
37
+ rexml
38
+ rubocop-ast (>= 0.3.0, < 1.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 2.0)
41
+ rubocop-ast (0.3.0)
42
+ parser (>= 2.7.1.4)
43
+ ruby-progressbar (1.10.1)
44
+ unicode-display_width (1.7.0)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ markdown_to_rspec!
51
+ rspec (~> 3.0)
52
+ rubocop
53
+
54
+ BUNDLED WITH
55
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Madogiwa
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.
@@ -0,0 +1,121 @@
1
+ # MarkdownToRspec
2
+
3
+ ![ci](https://github.com/Madogiwa0124/markdown_to_rspec/workflows/ci/badge.svg)
4
+
5
+ converts text in markdown format into text in RSpec format.
6
+
7
+ This gem uses `RDoc::Markdown` to parse the text in markdown format, generates `RDoc::Markup::Document`, and then generates an text in RSpec format from it.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'markdown_to_rspec'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install markdown_to_rspec
24
+
25
+ ## Usage
26
+
27
+ ``` ruby
28
+ # CLI
29
+ $ markdown_to_rspec -f `MARKDOWN_FILE_PATH`
30
+ #=> return A string in RSpec format
31
+ $ markdown_to_rspec -t `MARKDOWN__TEXT`
32
+ #=> return A string in RSpec format
33
+
34
+ # in ruby code
35
+ require 'rspec_to_markdown'
36
+ markdown = "A string in markdown format"
37
+ MarkdownToRspec.to_rspec(markdown)
38
+ #=> return A string in RSpec format
39
+ ```
40
+
41
+ ### Sample
42
+
43
+ The following text is in markdown format
44
+
45
+ ``` markdown
46
+ # Details.
47
+ A screen to check something
48
+ ref: https://example/com/tickets/1
49
+
50
+ ## Initial Display.
51
+
52
+ ### When a record exists.
53
+ * The title must be displayed.
54
+ * The text must be displayed.
55
+
56
+ ### When the record does not exist.
57
+ * The title should not be displayed.
58
+ * The text should not be displayed.
59
+
60
+ ### Other cases.
61
+ * 500 pages to be displayed.
62
+
63
+ # Index.
64
+ A screen to check something
65
+
66
+ ## Initial Display.
67
+ * The items must be displayed.
68
+ ```
69
+
70
+ You can convert it to an text in RSpec format.
71
+
72
+ ``` ruby
73
+ RSpec.describe 'Details.' do
74
+ # A screen to check something
75
+ # ref: https://example/com/tickets/1
76
+ describe 'Initial Display.' do
77
+ context 'When a record exists.' do
78
+ it 'The title must be displayed.' do
79
+ end
80
+ it 'The text must be displayed.' do
81
+ end
82
+ end
83
+ context 'When the record does not exist.' do
84
+ it 'The title should not be displayed.' do
85
+ end
86
+ it 'The text should not be displayed.' do
87
+ end
88
+ end
89
+ context 'Other cases.' do
90
+ it '500 pages to be displayed.' do
91
+ end
92
+ end
93
+ end
94
+ end
95
+ RSpec.describe 'Index.' do
96
+ # A screen to check something
97
+ describe 'Initial Display.' do
98
+ it 'The items must be displayed.' do
99
+ end
100
+ end
101
+ end
102
+ ```
103
+
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ 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).
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Madogiwa0124/markdown_to_rspec. 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/Madogiwa0124/markdown_to_rspec/blob/master/CODE_OF_CONDUCT.md).
113
+
114
+
115
+ ## License
116
+
117
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
118
+
119
+ ## Code of Conduct
120
+
121
+ Everyone interacting in the MarkdownToRspec project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Madogiwa0124/markdown_to_rspec/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'markdown_to_rspec'
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
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -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,19 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'optparse'
5
+ require 'pathname'
6
+ require 'markdown_to_rspec'
7
+ require 'markdown_to_rspec/rake_tasks'
8
+
9
+ option = OptionParser.new
10
+ option.on('-f', '--file FILE_PATH', 'Set the target Markdown file path.') do |v|
11
+ Rake::Task['markdown_to_rspec:to_rspec_from_file'].execute(file_path: v)
12
+ end
13
+ option.on('-t', '--text TEXT', 'Set the target Markdown text.') do |v|
14
+ Rake::Task['markdown_to_rspec:to_rspec_from_text'].execute(text: v)
15
+ end
16
+ option.on('-v', '--version', 'Show gem version.') do
17
+ Rake::Task['markdown_to_rspec:version'].execute
18
+ end
19
+ option.parse!(ARGV)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'markdown_to_rspec/version'
4
+ require 'markdown_to_rspec/to_rspec/formatter/markdown'
5
+
6
+ module MarkdownToRspec
7
+ class InValidFileFormatError < StandardError; end
8
+
9
+ def self.to_rspec(markdown)
10
+ ToRspec::Formatter::Markdown.new(markdown: markdown).call
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+
5
+ namespace :markdown_to_rspec do
6
+ desc 'converts file in markdown format into executable RSpec text.'
7
+ task :to_rspec_from_file, [:file_path] do |_task, args|
8
+ markdown_file_path = Pathname.new(args[:file_path].to_s)
9
+ if markdown_file_path.extname == '.md'
10
+ markdown_text = File.read(markdown_file_path)
11
+ puts MarkdownToRspec.to_rspec(markdown_text)
12
+ else
13
+ raise(MarkdownToRspec::InValidFileFormatError, 'Specify the file as a `.md` format.')
14
+ end
15
+ end
16
+
17
+ desc 'converts text in markdown format into executable RSpec text.'
18
+ task :to_rspec_from_text, [:text] do |_task, args|
19
+ # unescaped new line string.
20
+ markdown_text = args[:text].to_s.gsub('\\n', "\n")
21
+ puts MarkdownToRspec.to_rspec(markdown_text)
22
+ end
23
+
24
+ desc 'Show gem version.'
25
+ task :version do
26
+ puts "markdown_to_rspec: #{MarkdownToRspec::VERSION}"
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './heading'
4
+ require_relative './list'
5
+ require_relative './list_item'
6
+ require_relative './paragraph'
7
+ require_relative './markdown'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Formatter
6
+ class Heading
7
+ def call(rdoc_heading, parent)
8
+ rspec_heading = build_rspec_rspec_heading(rdoc_heading, parent)
9
+ parent.children << rspec_heading if parent
10
+ rspec_heading
11
+ end
12
+
13
+ private
14
+
15
+ def build_rspec_rspec_heading(rdoc_header, parent)
16
+ ToRspec::Item::Heading.new(
17
+ text: rdoc_header.text,
18
+ level: rdoc_header.level,
19
+ parent: parent
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Formatter
6
+ class List
7
+ def call(rdoc_list, parent)
8
+ list = ToRspec::Item::List.new(parent: parent)
9
+ rdoc_list.items.each { |item| format(item, list) }
10
+ parent.children << list if parent
11
+ list
12
+ end
13
+
14
+ private
15
+
16
+ def format(item, parent)
17
+ {
18
+ 'RDoc::Markup::ListItem' => Formatter::ListItem.new
19
+ }[item.class.name]&.call(item, parent)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Formatter
6
+ class ListItem
7
+ def call(rdoc_list_item, parent)
8
+ @rdoc_list_item = rdoc_list_item
9
+ # We don't allow nesting of it, so we only get the first element.
10
+ paragraph_text = find_paragraph.parts[0]
11
+ list_item = ToRspec::Item::ListItem.new(text: paragraph_text, parent: parent)
12
+ parent.children << list_item if parent
13
+ list_item
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :rdoc_list_item
19
+
20
+ def find_paragraph
21
+ check_paragraph = ->(obj) { obj.class.name == 'RDoc::Markup::Paragraph' }
22
+ rdoc_list_item.parts.find(&check_paragraph)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rdoc'
4
+ require 'rdoc/markdown'
5
+ require 'markdown_to_rspec/to_rspec/item/all'
6
+ require_relative './all'
7
+
8
+ module MarkdownToRspec
9
+ module ToRspec
10
+ module Formatter
11
+ class Markdown
12
+ def initialize(markdown:)
13
+ @markdown = markdown
14
+ @parsed_markdown = RDoc::Markdown.parse(markdown)
15
+ @items = []
16
+ end
17
+
18
+ attr_reader :markdown, :parsed_markdown, :items
19
+
20
+ def call
21
+ parsed_markdown.parts.each do |part|
22
+ item = format(part, find_parent(part))
23
+ @items << item
24
+ end
25
+ # Each item renders a child element, so only top-level items are converted.
26
+ items.select { |item| item.parent.nil? }.map(&:convert).join("\n")
27
+ end
28
+
29
+ private
30
+
31
+ def find_parent(rdoc_item)
32
+ return headings.last unless rdoc_item.respond_to?(:level)
33
+
34
+ # Get a lower level Header than rdoc_item most recent one (lower is higher)
35
+ headings.reverse.find { |heading| heading.level < rdoc_item.level }
36
+ end
37
+
38
+ def headings
39
+ items.select { |item| item.class == Item::Heading }
40
+ end
41
+
42
+ def format(part, parent)
43
+ formatters = {
44
+ 'RDoc::Markup::Heading' => Formatter::Heading.new,
45
+ 'RDoc::Markup::List' => Formatter::List.new,
46
+ 'RDoc::Markup::Paragraph' => Formatter::Paragraph.new
47
+ }
48
+ formatters[part.class.name]&.call(part, parent)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Formatter
6
+ class Paragraph
7
+ def call(rdoc_paragraph, parent)
8
+ rspec_paragraph = build_rspec_rspec_paragraph(rdoc_paragraph, parent)
9
+ parent.children << rspec_paragraph if parent
10
+ rspec_paragraph
11
+ end
12
+
13
+ private
14
+
15
+ def build_rspec_rspec_paragraph(rdoc_paragraph, parent)
16
+ ToRspec::Item::Paragraph.new(
17
+ text: rdoc_paragraph.text,
18
+ parent: parent
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './base'
4
+ require_relative './heading'
5
+ require_relative './list'
6
+ require_relative './list_item'
7
+ require_relative './paragraph'
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Item
6
+ class Base
7
+ def depth
8
+ raise NotImplementedError
9
+ end
10
+
11
+ def children
12
+ raise NotImplementedError
13
+ end
14
+
15
+ def parent
16
+ raise NotImplementedError
17
+ end
18
+
19
+ def convert
20
+ raise NotImplementedError
21
+ end
22
+
23
+ #
24
+ # insert indentation with depth in mind.
25
+ # For example, if depth is 2, indext("foo bar") #=> " foo bar"
26
+ #
27
+ # @param [String] line The string of the line to insert the indent.
28
+ # @param [Intger] indent_length The number of indented characters.
29
+ # @param [String] indent_string Character to be used for indentation.
30
+ #
31
+ # @return [String] Indented string.
32
+ # ex) depth: 2
33
+ #
34
+ def indent(line, indent_length = 2, indent_string = ' ')
35
+ line.dup.prepend(indent_string * indent_length * depth)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Item
6
+ class Heading < Base
7
+ def initialize(level:, text:, parent: nil, children: [])
8
+ @level = level
9
+ @text = text
10
+ @parent = parent
11
+ @children = children
12
+ # If there is a parent, nest it and add it to the parent's child elements.
13
+ @depth = parent ? parent.depth + 1 : 0
14
+ end
15
+
16
+ attr_reader :level, :text, :depth, :parent
17
+ attr_accessor :children
18
+
19
+ def convert
20
+ template(level_to_syntax, text)
21
+ .split("\n")
22
+ .map { |line| indent(line) }
23
+ .insert(1, convert_children) # Insert the result of a child element between do...end.
24
+ .join("\n")
25
+ end
26
+
27
+ private
28
+
29
+ def convert_children
30
+ children.map(&:convert).join("\n")
31
+ end
32
+
33
+ def template(syntax, text)
34
+ <<~TEXT
35
+ #{syntax} '#{text.gsub(/"|'/, '')}' do
36
+ end
37
+ TEXT
38
+ end
39
+
40
+ def level_to_syntax
41
+ case level
42
+ when 1 then 'RSpec.describe'
43
+ when 2 then 'describe'
44
+ else 'context'
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Item
6
+ class List < Base
7
+ def initialize(parent: nil, children: [])
8
+ @parent = parent
9
+ @children = children
10
+ @depth = parent ? parent.depth + 1 : 0
11
+ end
12
+
13
+ attr_reader :depth, :parent
14
+ attr_accessor :children
15
+
16
+ def convert
17
+ children.map(&:convert).join("\n")
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Item
6
+ class ListItem < Base
7
+ def initialize(text:, parent: nil, children: [])
8
+ @text = text
9
+ @parent = parent
10
+ @children = children
11
+ # The parent element(List), does not have a drawing element, so it inherits the parent's depth.
12
+ @depth = parent ? parent.depth : 0
13
+ end
14
+
15
+ attr_reader :text, :depth, :parent, :children
16
+
17
+ def convert
18
+ template(text).split("\n").map { |line| indent(line) }.join("\n")
19
+ end
20
+
21
+ private
22
+
23
+ def template(text)
24
+ <<~TEXT
25
+ it '#{text.gsub("\n", ' ').gsub(/"|'/, '')}' do
26
+ end
27
+ TEXT
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ module ToRspec
5
+ module Item
6
+ class Paragraph < Base
7
+ def initialize(text:, parent: nil, children: [])
8
+ @text = text
9
+ @parent = parent
10
+ @children = children
11
+ @depth = parent ? parent.depth + 1 : 0
12
+ end
13
+
14
+ attr_reader :text, :depth, :parent, :children
15
+
16
+ def convert
17
+ lines = text.split("\n").map { |line| template(line) }
18
+ lines.map { |line| indent(line) }.join("\n")
19
+ end
20
+
21
+ private
22
+
23
+ def template(text)
24
+ "# #{text}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MarkdownToRspec
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,25 @@
1
+ require_relative 'lib/markdown_to_rspec/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "markdown_to_rspec"
5
+ spec.version = MarkdownToRspec::VERSION
6
+ spec.authors = ["Madogiwa"]
7
+ spec.email = ["madogiwa0124@gmail.com"]
8
+
9
+ spec.summary = "This gem converts text in markdown format into text in RSpec format."
10
+ spec.description = "This gem converts text in markdown format into text in RSpec format."
11
+ spec.homepage = "https://github.com/Madogiwa0124/markdown_to_rspec"
12
+ spec.license = "MIT"
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.required_ruby_version = ">= 2.5.0"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|\.github)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+ spec.add_dependency "rake", '~> 13.0'
25
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown_to_rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Madogiwa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ description: This gem converts text in markdown format into text in RSpec format.
28
+ email:
29
+ - madogiwa0124@gmail.com
30
+ executables:
31
+ - markdown_to_rspec
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/console
45
+ - bin/setup
46
+ - exe/markdown_to_rspec
47
+ - lib/markdown_to_rspec.rb
48
+ - lib/markdown_to_rspec/rake_tasks.rb
49
+ - lib/markdown_to_rspec/to_rspec/formatter/all.rb
50
+ - lib/markdown_to_rspec/to_rspec/formatter/heading.rb
51
+ - lib/markdown_to_rspec/to_rspec/formatter/list.rb
52
+ - lib/markdown_to_rspec/to_rspec/formatter/list_item.rb
53
+ - lib/markdown_to_rspec/to_rspec/formatter/markdown.rb
54
+ - lib/markdown_to_rspec/to_rspec/formatter/paragraph.rb
55
+ - lib/markdown_to_rspec/to_rspec/item/all.rb
56
+ - lib/markdown_to_rspec/to_rspec/item/base.rb
57
+ - lib/markdown_to_rspec/to_rspec/item/heading.rb
58
+ - lib/markdown_to_rspec/to_rspec/item/list.rb
59
+ - lib/markdown_to_rspec/to_rspec/item/list_item.rb
60
+ - lib/markdown_to_rspec/to_rspec/item/paragraph.rb
61
+ - lib/markdown_to_rspec/version.rb
62
+ - markdown_to_rspec.gemspec
63
+ homepage: https://github.com/Madogiwa0124/markdown_to_rspec
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/Madogiwa0124/markdown_to_rspec
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.5.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.1.2
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: This gem converts text in markdown format into text in RSpec format.
87
+ test_files: []