cooklang 0.1.0 → 1.0.1

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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +35 -0
  3. data/.gitignore +12 -0
  4. data/.qlty/.gitignore +7 -0
  5. data/.qlty/configs/.yamllint.yaml +21 -0
  6. data/.qlty/qlty.toml +101 -0
  7. data/.rspec +3 -0
  8. data/.rubocop.yml +289 -75
  9. data/Gemfile.lock +65 -26
  10. data/{LICENSE → LICENSE.txt} +6 -6
  11. data/README.md +106 -12
  12. data/Rakefile +5 -1
  13. data/cooklang.gemspec +35 -0
  14. data/lib/cooklang/builders/recipe_builder.rb +64 -0
  15. data/lib/cooklang/builders/step_builder.rb +76 -0
  16. data/lib/cooklang/cookware.rb +43 -0
  17. data/lib/cooklang/formatter.rb +61 -0
  18. data/lib/cooklang/formatters/text.rb +18 -0
  19. data/lib/cooklang/ingredient.rb +60 -0
  20. data/lib/cooklang/lexer.rb +282 -0
  21. data/lib/cooklang/metadata.rb +98 -0
  22. data/lib/cooklang/note.rb +27 -0
  23. data/lib/cooklang/parser.rb +41 -0
  24. data/lib/cooklang/parsers/cookware_parser.rb +133 -0
  25. data/lib/cooklang/parsers/ingredient_parser.rb +179 -0
  26. data/lib/cooklang/parsers/timer_parser.rb +135 -0
  27. data/lib/cooklang/processors/element_parser.rb +45 -0
  28. data/lib/cooklang/processors/metadata_processor.rb +129 -0
  29. data/lib/cooklang/processors/step_processor.rb +208 -0
  30. data/lib/cooklang/processors/token_processor.rb +104 -0
  31. data/lib/cooklang/recipe.rb +72 -0
  32. data/lib/cooklang/section.rb +33 -0
  33. data/lib/cooklang/step.rb +99 -0
  34. data/lib/cooklang/timer.rb +65 -0
  35. data/lib/cooklang/token_stream.rb +130 -0
  36. data/lib/cooklang/version.rb +1 -1
  37. data/lib/cooklang.rb +22 -1
  38. data/spec/comprehensive_spec.rb +179 -0
  39. data/spec/cooklang_spec.rb +38 -0
  40. data/spec/fixtures/canonical.yaml +837 -0
  41. data/spec/formatters/text_spec.rb +189 -0
  42. data/spec/integration/canonical_spec.rb +211 -0
  43. data/spec/lexer_spec.rb +357 -0
  44. data/spec/models/cookware_spec.rb +116 -0
  45. data/spec/models/ingredient_spec.rb +192 -0
  46. data/spec/models/metadata_spec.rb +241 -0
  47. data/spec/models/note_spec.rb +65 -0
  48. data/spec/models/recipe_spec.rb +171 -0
  49. data/spec/models/section_spec.rb +65 -0
  50. data/spec/models/step_spec.rb +236 -0
  51. data/spec/models/timer_spec.rb +173 -0
  52. data/spec/parser_spec.rb +398 -0
  53. data/spec/spec_helper.rb +23 -0
  54. data/spec/token_stream_spec.rb +278 -0
  55. metadata +141 -24
  56. data/.ruby-version +0 -1
  57. data/CHANGELOG.md +0 -5
  58. data/bin/console +0 -15
  59. data/bin/setup +0 -8
data/Gemfile.lock CHANGED
@@ -1,45 +1,84 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cooklang (0.1.0)
4
+ cooklang (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.2)
10
- parallel (1.21.0)
11
- parser (3.1.1.0)
9
+ ast (2.4.3)
10
+ diff-lcs (1.6.2)
11
+ docile (1.4.1)
12
+ json (2.13.2)
13
+ language_server-protocol (3.17.0.5)
14
+ lint_roller (1.1.0)
15
+ parallel (1.27.0)
16
+ parser (3.3.9.0)
12
17
  ast (~> 2.4.1)
18
+ racc
19
+ prism (1.4.0)
20
+ racc (1.8.1)
13
21
  rainbow (3.1.1)
14
- regexp_parser (2.2.1)
15
- rexml (3.2.5)
16
- rubocop (1.25.1)
22
+ rake (13.3.0)
23
+ regexp_parser (2.11.2)
24
+ rspec (3.13.1)
25
+ rspec-core (~> 3.13.0)
26
+ rspec-expectations (~> 3.13.0)
27
+ rspec-mocks (~> 3.13.0)
28
+ rspec-core (3.13.5)
29
+ rspec-support (~> 3.13.0)
30
+ rspec-expectations (3.13.5)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.13.0)
33
+ rspec-mocks (3.13.5)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.13.0)
36
+ rspec-support (3.13.5)
37
+ rubocop (1.80.0)
38
+ json (~> 2.3)
39
+ language_server-protocol (~> 3.17.0.2)
40
+ lint_roller (~> 1.1.0)
17
41
  parallel (~> 1.10)
18
- parser (>= 3.1.0.0)
42
+ parser (>= 3.3.0.2)
19
43
  rainbow (>= 2.2.2, < 4.0)
20
- regexp_parser (>= 1.8, < 3.0)
21
- rexml
22
- rubocop-ast (>= 1.15.1, < 2.0)
44
+ regexp_parser (>= 2.9.3, < 3.0)
45
+ rubocop-ast (>= 1.46.0, < 2.0)
23
46
  ruby-progressbar (~> 1.7)
24
- unicode-display_width (>= 1.4.0, < 3.0)
25
- rubocop-ast (1.16.0)
26
- parser (>= 3.1.1.0)
27
- rubocop-performance (1.13.3)
28
- rubocop (>= 1.7.0, < 2.0)
29
- rubocop-ast (>= 0.4.0)
30
- rubocop-rspec (2.9.0)
31
- rubocop (~> 1.19)
32
- ruby-progressbar (1.11.0)
33
- unicode-display_width (2.1.0)
47
+ unicode-display_width (>= 2.4.0, < 4.0)
48
+ rubocop-ast (1.46.0)
49
+ parser (>= 3.3.7.2)
50
+ prism (~> 1.4)
51
+ rubocop-performance (1.25.0)
52
+ lint_roller (~> 1.1)
53
+ rubocop (>= 1.75.0, < 2.0)
54
+ rubocop-ast (>= 1.38.0, < 2.0)
55
+ rubocop-rspec (3.6.0)
56
+ lint_roller (~> 1.1)
57
+ rubocop (~> 1.72, >= 1.72.1)
58
+ ruby-progressbar (1.13.0)
59
+ simplecov (0.22.0)
60
+ docile (~> 1.1)
61
+ simplecov-html (~> 0.11)
62
+ simplecov_json_formatter (~> 0.1)
63
+ simplecov-html (0.13.2)
64
+ simplecov_json_formatter (0.1.4)
65
+ unicode-display_width (3.1.5)
66
+ unicode-emoji (~> 4.0, >= 4.0.4)
67
+ unicode-emoji (4.0.4)
34
68
 
35
69
  PLATFORMS
36
- x86_64-darwin-21
70
+ arm64-darwin-24
71
+ ruby
37
72
 
38
73
  DEPENDENCIES
74
+ bundler (~> 2.7)
39
75
  cooklang!
40
- rubocop (~> 1.23)
41
- rubocop-performance (~> 1.12)
42
- rubocop-rspec (~> 2.6)
76
+ rake (~> 13.0)
77
+ rspec (~> 3.13)
78
+ rubocop (~> 1.80)
79
+ rubocop-performance (~> 1.25)
80
+ rubocop-rspec (~> 3.6)
81
+ simplecov (~> 0.22.0)
43
82
 
44
83
  BUNDLED WITH
45
- 2.2.33
84
+ 2.7.1
@@ -1,6 +1,6 @@
1
- MIT License
1
+ The MIT License (MIT)
2
2
 
3
- Copyright (c) 2022 James Brooks
3
+ Copyright (c) 2025 James Brooks
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
  copies of the Software, and to permit persons to whom the Software is
10
10
  furnished to do so, subject to the following conditions:
11
11
 
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
14
 
15
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
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.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,35 +1,129 @@
1
1
  # Cooklang
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cooklang`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ ![Gem Version](https://img.shields.io/gem/v/cooklang)
4
+ [![Test](https://github.com/jamesbrooks/cooklang/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/jamesbrooks/cooklang/actions/workflows/test.yml)
5
+ [![Maintainability](https://qlty.sh/gh/jamesbrooks/projects/cooklang/maintainability.svg)](https://qlty.sh/gh/jamesbrooks/projects/cooklang)
6
+ [![Code Coverage](https://qlty.sh/gh/jamesbrooks/projects/cooklang/coverage.svg)](https://qlty.sh/gh/jamesbrooks/projects/cooklang)
4
7
 
5
- TODO: Delete this and the text above, and describe your gem
8
+ A Ruby parser for the [Cooklang](https://cooklang.org) recipe markup language.
6
9
 
7
10
  ## Installation
8
11
 
9
- Add this line to your application's Gemfile:
12
+ Add to your Gemfile:
10
13
 
11
14
  ```ruby
12
15
  gem 'cooklang'
13
16
  ```
14
17
 
15
- And then execute:
18
+ Or install directly:
16
19
 
17
- $ bundle install
20
+ ```bash
21
+ gem install cooklang
22
+ ```
23
+
24
+ ## Usage
18
25
 
19
- Or install it yourself as:
26
+ ```ruby
27
+ require 'cooklang'
28
+
29
+ recipe_text = <<~RECIPE
30
+ >> title: Pancakes
31
+ >> servings: 4
32
+
33
+ Crack @eggs{3} into a bowl, add @flour{125%g} and @milk{250%ml}.
34
+
35
+ Heat #frying pan over medium heat for ~{5%minutes}.
36
+ Pour batter and cook until golden.
37
+ RECIPE
38
+
39
+ recipe = Cooklang.parse(recipe_text)
40
+
41
+ # Access metadata
42
+ recipe.metadata['title'] # => "Pancakes"
43
+ recipe.metadata['servings'] # => 4
44
+
45
+ # Access ingredients
46
+ recipe.ingredients.each do |ingredient|
47
+ puts "#{ingredient.name}: #{ingredient.quantity} #{ingredient.unit}"
48
+ end
49
+ # => eggs: 3
50
+ # => flour: 125 g
51
+ # => milk: 250 ml
52
+
53
+ # Parse from file
54
+ recipe = Cooklang.parse_file('pancakes.cook')
55
+
56
+ # Format as text
57
+ formatter = Cooklang::Formatters::Text.new(recipe)
58
+ puts formatter.to_s
59
+ # Ingredients:
60
+ # eggs 3
61
+ # flour 125 g
62
+ # milk 250 ml
63
+ #
64
+ # Steps:
65
+ # 1. Crack eggs into a bowl, add flour and milk.
66
+ # 2. Heat frying pan over medium heat for 5 minutes.
67
+ # 3. Pour batter and cook until golden.
68
+ ```
20
69
 
21
- $ gem install cooklang
70
+ ## API
22
71
 
23
- ## Usage
72
+ ```ruby
73
+ # Recipe object
74
+ recipe.metadata # Hash of metadata
75
+ recipe.ingredients # Array of Ingredient objects
76
+ recipe.cookware # Array of Cookware objects
77
+ recipe.timers # Array of Timer objects
78
+ recipe.steps # Array of Step objects
79
+ recipe.steps_text # Array of plain text steps
80
+
81
+ # Ingredient
82
+ ingredient.name # "flour"
83
+ ingredient.quantity # 125
84
+ ingredient.unit # "g"
85
+ ingredient.notes # "sifted"
86
+
87
+ # Cookware
88
+ cookware.name # "frying pan"
89
+ cookware.quantity # 1
90
+
91
+ # Timer
92
+ timer.name # "pasta"
93
+ timer.duration # 10
94
+ timer.unit # "minutes"
95
+ ```
24
96
 
25
- TODO: Write usage instructions here
97
+ ## Cooklang Syntax
98
+
99
+ - **Ingredients**: `@salt`, `@flour{125%g}`, `@onion{1}(diced)`
100
+ - **Cookware**: `#pan`, `#mixing bowl{}`
101
+ - **Timers**: `~{5%minutes}`, `~pasta{10%minutes}`
102
+ - **Comments**: `-- line comment`, `[- block comment -]`
103
+ - **Metadata**: `>> key: value` or YAML front matter
26
104
 
27
105
  ## Development
28
106
 
29
- 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.
107
+ ```bash
108
+ # Install dependencies
109
+ bundle install
110
+
111
+ # Run tests
112
+ bundle exec rspec
113
+
114
+ # Run linter
115
+ bundle exec rubocop
116
+ ```
30
117
 
31
- 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).
118
+ ## Resources
119
+
120
+ - [Cooklang Website](https://cooklang.org)
121
+ - [Language Specification](https://cooklang.org/docs/spec/)
32
122
 
33
123
  ## Contributing
34
124
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cooklang.
125
+ Bug reports and pull requests welcome on GitHub.
126
+
127
+ ## License
128
+
129
+ MIT License
data/Rakefile CHANGED
@@ -1,8 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
4
8
  require "rubocop/rake_task"
5
9
 
6
10
  RuboCop::RakeTask.new
7
11
 
8
- task default: :rubocop
12
+ task default: %i[spec rubocop]
data/cooklang.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "cooklang/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "cooklang"
9
+ spec.version = Cooklang::VERSION
10
+ spec.authors = ["James Brooks"]
11
+ spec.email = ["james@jamesbrooks.net"]
12
+
13
+ spec.summary = "A Ruby parser for the Cooklang recipe markup language."
14
+ spec.description = "Cooklang is a markup language for recipes that allows you to define ingredients, cookware, timers, and metadata in a structured way. This gem provides a Ruby parser for Cooklang files."
15
+ spec.homepage = "https://github.com/jamesbrooks/cooklang"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 3.2.0"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/jamesbrooks/cooklang"
21
+ spec.metadata["rubygems_mfa_required"] = "true"
22
+
23
+ spec.files = `git ls-files`.split($/)
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.7"
29
+ spec.add_development_dependency "rake", "~> 13.0"
30
+ spec.add_development_dependency "rspec", "~> 3.13"
31
+ spec.add_development_dependency "rubocop", "~> 1.80"
32
+ spec.add_development_dependency "rubocop-performance", "~> 1.25"
33
+ spec.add_development_dependency "rubocop-rspec", "~> 3.6"
34
+ spec.add_development_dependency "simplecov", "~> 0.22.0"
35
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../recipe"
4
+ require_relative "../step"
5
+ require_relative "../section"
6
+
7
+ module Cooklang
8
+ module Builders
9
+ class RecipeBuilder
10
+ class << self
11
+ def build_recipe(parsed_steps, metadata)
12
+ # Aggregate all elements from steps
13
+ all_ingredients = []
14
+ all_cookware = []
15
+ all_timers = []
16
+ steps = []
17
+ sections = []
18
+
19
+ parsed_steps.each do |step_data|
20
+ all_ingredients.concat(step_data[:ingredients])
21
+ all_cookware.concat(step_data[:cookware])
22
+ all_timers.concat(step_data[:timers])
23
+
24
+ step = Step.new(segments: step_data[:segments])
25
+ steps << step
26
+
27
+ # Create section if this step has a section name
28
+ if step_data[:section_name]
29
+ sections << Section.new(name: step_data[:section_name], steps: [step])
30
+ end
31
+ end
32
+
33
+ # Deduplicate ingredients and cookware
34
+ unique_ingredients = deduplicate_ingredients(all_ingredients)
35
+ unique_cookware = deduplicate_cookware(all_cookware)
36
+
37
+ Recipe.new(
38
+ ingredients: unique_ingredients,
39
+ cookware: unique_cookware,
40
+ timers: all_timers,
41
+ steps: steps,
42
+ metadata: metadata,
43
+ sections: sections
44
+ )
45
+ end
46
+
47
+ private
48
+ def deduplicate_ingredients(ingredients)
49
+ # Group by name AND quantity to preserve different quantities of same ingredient
50
+ grouped = ingredients.group_by { |i| [i.name, i.quantity, i.unit] }
51
+ grouped.values.map(&:first)
52
+ end
53
+
54
+ def deduplicate_cookware(cookware_items)
55
+ # Group by name and prefer items with quantity over those without
56
+ cookware_items.group_by(&:name).map do |_name, items|
57
+ # Prefer items with quantity, then take the first one
58
+ items.find(&:quantity) || items.first
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../step"
4
+
5
+ module Cooklang
6
+ module Builders
7
+ class StepBuilder
8
+ def initialize
9
+ @segments = []
10
+ @ingredients = []
11
+ @cookware = []
12
+ @timers = []
13
+ @section_name = nil
14
+ end
15
+
16
+ def add_text(text)
17
+ @segments << text
18
+ self
19
+ end
20
+
21
+ def add_ingredient(ingredient)
22
+ @ingredients << ingredient
23
+ @segments << ingredient
24
+ self
25
+ end
26
+
27
+ def add_cookware(cookware)
28
+ @cookware << cookware
29
+ @segments << cookware
30
+ self
31
+ end
32
+
33
+ def add_timer(timer)
34
+ @timers << timer
35
+ @segments << timer
36
+ self
37
+ end
38
+
39
+ def add_remaining_text(text)
40
+ if text && !text.empty?
41
+ @segments << text
42
+ end
43
+ self
44
+ end
45
+
46
+ def set_section_name(name)
47
+ @section_name = name unless name&.empty?
48
+ self
49
+ end
50
+
51
+ def build
52
+ # Clean up segments - remove trailing newlines
53
+ cleaned_segments = remove_trailing_newlines(@segments.dup)
54
+
55
+ Step.new(segments: cleaned_segments)
56
+ end
57
+
58
+ # Check if the step has meaningful content
59
+ def has_content?
60
+ @segments.any? { |segment| segment != "\n" && !(segment.is_a?(String) && segment.strip.empty?) }
61
+ end
62
+
63
+ # Access internal collections for compatibility
64
+ attr_reader :segments, :ingredients, :cookware, :timers, :section_name
65
+
66
+ private
67
+ def remove_trailing_newlines(segments)
68
+ # Remove trailing newlines and whitespace-only text segments
69
+ segments.pop while !segments.empty? &&
70
+ (segments.last == "\n" ||
71
+ (segments.last.is_a?(String) && segments.last.strip.empty?))
72
+ segments
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cooklang
4
+ class Cookware
5
+ attr_reader :name, :quantity
6
+
7
+ def initialize(name:, quantity: nil)
8
+ @name = name.to_s.freeze
9
+ @quantity = quantity
10
+ end
11
+
12
+ def to_s
13
+ result = @name
14
+ result += " (#{@quantity})" if @quantity
15
+ result
16
+ end
17
+
18
+ def to_h
19
+ {
20
+ name: @name,
21
+ quantity: @quantity
22
+ }.compact
23
+ end
24
+
25
+ def ==(other)
26
+ return false unless other.is_a?(Cookware)
27
+
28
+ name == other.name && quantity == other.quantity
29
+ end
30
+
31
+ def eql?(other)
32
+ self == other
33
+ end
34
+
35
+ def hash
36
+ [name, quantity].hash
37
+ end
38
+
39
+ def has_quantity?
40
+ !@quantity.nil?
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cooklang
4
+ class Formatter
5
+ attr_reader :recipe
6
+
7
+ def initialize(recipe)
8
+ @recipe = recipe
9
+ end
10
+
11
+ def to_s
12
+ raise NotImplementedError, "Subclasses must implement #to_s"
13
+ end
14
+
15
+ private
16
+ def ingredients_section
17
+ return "" if recipe.ingredients.empty?
18
+
19
+ ingredient_lines = recipe.ingredients.map do |ingredient|
20
+ format_ingredient(ingredient)
21
+ end
22
+
23
+ "Ingredients:\n#{ingredient_lines.join("\n")}\n"
24
+ end
25
+
26
+ def steps_section
27
+ return "" if recipe.steps.empty?
28
+
29
+ step_lines = recipe.steps.each_with_index.map do |step, index|
30
+ " #{index + 1}. #{step.to_text}"
31
+ end
32
+
33
+ "Steps:\n#{step_lines.join("\n")}\n"
34
+ end
35
+
36
+ def format_ingredient(ingredient)
37
+ name = ingredient.name
38
+ quantity_unit = format_quantity_unit(ingredient)
39
+
40
+ # Add 4 spaces after the longest ingredient name for alignment
41
+ name_width = max_ingredient_name_length + 4
42
+ " #{name.ljust(name_width)}#{quantity_unit}"
43
+ end
44
+
45
+ def format_quantity_unit(ingredient)
46
+ if ingredient.quantity && ingredient.unit
47
+ "#{ingredient.quantity} #{ingredient.unit}"
48
+ elsif ingredient.quantity
49
+ ingredient.quantity.to_s
50
+ elsif ingredient.unit
51
+ ingredient.unit
52
+ else
53
+ "some"
54
+ end
55
+ end
56
+
57
+ def max_ingredient_name_length
58
+ @max_ingredient_name_length ||= recipe.ingredients.map(&:name).map(&:length).max || 0
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../formatter"
4
+
5
+ module Cooklang
6
+ module Formatters
7
+ class Text < Formatter
8
+ def to_s
9
+ sections = []
10
+
11
+ sections << ingredients_section unless recipe.ingredients.empty?
12
+ sections << steps_section unless recipe.steps.empty?
13
+
14
+ sections.join("\n").strip
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cooklang
4
+ class Ingredient
5
+ attr_reader :name, :quantity, :unit, :notes
6
+
7
+ def initialize(name:, quantity: nil, unit: nil, notes: nil)
8
+ @name = name.to_s.freeze
9
+ @quantity = quantity
10
+ @unit = unit&.to_s&.freeze
11
+ @notes = notes&.to_s&.freeze
12
+ end
13
+
14
+ def to_s
15
+ result = @name
16
+ result += " #{@quantity}" if @quantity
17
+ result += " #{@unit}" if @unit
18
+ result += " (#{@notes})" if @notes
19
+ result
20
+ end
21
+
22
+ def to_h
23
+ {
24
+ name: @name,
25
+ quantity: @quantity,
26
+ unit: @unit,
27
+ notes: @notes
28
+ }.compact
29
+ end
30
+
31
+ def ==(other)
32
+ return false unless other.is_a?(Ingredient)
33
+
34
+ name == other.name &&
35
+ quantity == other.quantity &&
36
+ unit == other.unit &&
37
+ notes == other.notes
38
+ end
39
+
40
+ def eql?(other)
41
+ self == other
42
+ end
43
+
44
+ def hash
45
+ [name, quantity, unit, notes].hash
46
+ end
47
+
48
+ def has_quantity?
49
+ !@quantity.nil?
50
+ end
51
+
52
+ def has_unit?
53
+ !@unit.nil?
54
+ end
55
+
56
+ def has_notes?
57
+ !@notes.nil?
58
+ end
59
+ end
60
+ end