add support for scaling recipes to web-interface
This commit is contained in:
parent
92c70970f5
commit
cb2225715f
38
parse.scm
38
parse.scm
|
@ -36,17 +36,17 @@
|
|||
;; Predicates
|
||||
|
||||
(define contains-ingredient?
|
||||
(lambda (recipe search-ingredient)
|
||||
(contains-ingredients? recipe (cons search-ingredient '()))))
|
||||
(lambda (search-ingredient recipe)
|
||||
(contains-ingredients? (list search-ingredient) recipe)))
|
||||
|
||||
(define contains-ingredients?
|
||||
(lambda (recipe search-ingredients)
|
||||
(lambda (search-ingredients recipe)
|
||||
(let ((ingredients (recipe-ingredients recipe)))
|
||||
(cond [(null? search-ingredients) #t]
|
||||
[(ingredient-by-name ingredients
|
||||
(car search-ingredients))
|
||||
(contains-ingredients? recipe
|
||||
(cdr search-ingredients))]
|
||||
[(ingredient-by-name (car search-ingredients)
|
||||
ingredients)
|
||||
(contains-ingredients? (cdr search-ingredients)
|
||||
recipe)]
|
||||
[else #f])
|
||||
)))
|
||||
|
||||
|
@ -63,18 +63,18 @@
|
|||
(ingredient-ref (cdr ingredients) (- n 1)))))
|
||||
|
||||
(define ingredient-by-name
|
||||
(lambda (ingredients search-name)
|
||||
(let ((ingredient (car ingredients)))
|
||||
(lambda (search-name ingredient-list)
|
||||
(let ((ingredient (car ingredient-list)))
|
||||
(cond [(equal? (ingredient-name ingredient)
|
||||
search-name)
|
||||
ingredient]
|
||||
[(null? (cdr ingredients)) #f]
|
||||
[else (ingredient-by-name (cdr ingredients)
|
||||
search-name)]))))
|
||||
[(null? (cdr ingredient-list)) #f]
|
||||
[else (ingredient-by-name search-name
|
||||
(cdr ingredient-list))]))))
|
||||
|
||||
(define recipes-by-ingredients
|
||||
(lambda (recipes search-ingredients)
|
||||
(filter (lambda (x) (contains-ingredients? x search-ingredients))
|
||||
(lambda (search-ingredients recipes)
|
||||
(filter (lambda (x) (contains-ingredients? search-ingredients x))
|
||||
recipes)))
|
||||
|
||||
(define recipe-by-name
|
||||
|
@ -89,15 +89,15 @@
|
|||
|
||||
;; Manipulation
|
||||
|
||||
(define scale-recipe
|
||||
(lambda (recipe wanted-servings)
|
||||
(define scale-recipe-by-servings
|
||||
(lambda (wanted-servings recipe)
|
||||
(let ([name (recipe-name recipe)]
|
||||
[ingredients (recipe-ingredients recipe)]
|
||||
[steps (recipe-steps recipe)]
|
||||
[servings (recipe-servings recipe)])
|
||||
(let ([new-name name]
|
||||
[new-ingredients (map (lambda (ingr)
|
||||
(list (* (/ (car ingr)
|
||||
(cons (* (/ (car ingr)
|
||||
servings)
|
||||
wanted-servings)
|
||||
(cdr ingr)))
|
||||
|
@ -108,9 +108,9 @@
|
|||
))))
|
||||
|
||||
(define scale-recipe-by-factor
|
||||
(lambda (recipe scale-factor)
|
||||
(lambda (scale-factor recipe)
|
||||
(let ([new-servings (* (recipe-servings recipe) scale-factor)])
|
||||
(scale-recipe recipe new-servings))))
|
||||
(scale-recipe-by-servings new-servings recipe))))
|
||||
|
||||
;; Constructors
|
||||
|
||||
|
|
|
@ -3,15 +3,26 @@ require 'sinatra'
|
|||
require 'htmlentities'
|
||||
|
||||
get '/' do
|
||||
servings = params['servings']
|
||||
scaling_factor = params['scale']
|
||||
`cd ..; echo "(display (html-wrap (cookbook-as-html cookbook)))" | petite -q "batch.scm"`
|
||||
end
|
||||
|
||||
get '/ingredient/:ingredient' do
|
||||
ingredient = HTMLEntities.new.decode params['ingredient']
|
||||
`cd ..; echo "(display (html-wrap (cookbook-as-html (recipes-by-ingredients cookbook '(\\"#{ingredient}\\")))))" | petite -q "batch.scm"`
|
||||
`cd ..; echo "(display (html-wrap (cookbook-as-html (recipes-by-ingredients '(\\"#{ingredient}\\" cookbook)))))" | petite -q "batch.scm"`
|
||||
end
|
||||
|
||||
get '/recipe/:title' do
|
||||
title = HTMLEntities.new.decode params['title']
|
||||
`cd ..; echo "(display (html-wrap (recipe-as-html (recipe-by-name cookbook \\"#{title}\\"))))" | petite -q "batch.scm"`
|
||||
servings = params['servings']
|
||||
scaling_factor = params['scale']
|
||||
if servings
|
||||
puts "servings"
|
||||
`cd ..; echo "(display (html-wrap (recipe-as-html (scale-recipe-by-servings #{servings} (recipe-by-name \\"#{title}\\" cookbook)))))" | petite -q "batch.scm"`
|
||||
elsif scaling_factor
|
||||
`cd ..; echo "(display (html-wrap (recipe-as-html (scale-recipe-by-factor #{scaling_factor} (recipe-by-name \\"#{title}\\" cookbook)))))" | petite -q "batch.scm"`
|
||||
else
|
||||
`cd ..; echo "(display (html-wrap (recipe-as-html (recipe-by-name \\"#{title}\\" cookbook))))" | petite -q "batch.scm"`
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user