cook

package module
v0.0.0-...-b667a3b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2023 License: MIT Imports: 5 Imported by: 0

README

go-cook    Go Report Card

This project aims to be a reimplementation, and eventually extension, of the original CooklangCLI, written in Go.

The root package provides the parser and the requisite types under the package name cook. Thus, despite the scope of the project, importing the root will provide a lightweight cook parsing library.


Current State

At present, the parser is almost stable. It has all canonical features, however it will need to be extended in the future to include photos and possibly comments. Canonical tests here are generated using internal/cmd/test_gen to pull the latest tests and generate test code.

The CLI provides basic recipe reading as well as access to a webserver and an API server.

(Implemented) commands are as follows

  help        Help about any command
  init        Creates the default config file.
  read        Parses a recipe file and pretty prints it to stdout
  server      Hosts a local webserver to view/manage recipes.

Most focus so far has been on making a usable web interface. While non-final, it does provide a usable and pleasant interface already.

This functionality will be extended to the CLI once necessary components are finished.

Compilation

To use the parsing library, you can simply run go build, or import the package in a module.

To use the command line utility with a server it is required that you build the Svelte webapp prior to compiling the binary. This requires npm is installed.

The easiest way is to simply run make in the project root which will build the webapp and then compile the binary (embedding the webapp into the binary).

Tests can be regenerated/updated using make canonical

If you don't want to use make, or don't have access, you can view the Makefile to examine the build process for various tasks (build, test gen, formatting etc.).

To build the CLI binary without the webapp, simply run go build in the cmd/cook/ directory.


Author

Jayden Dumouchel -- jdumouch@ualberta.ca | rottenfishbone@pm.me

License

This project is licensed under the MIT License, see LICENSE file for details.

Documentation

Index

Examples

Constants

View Source
const NoQty = 0

The value representing an unparsable Qty

e.g. {Qty = "A splash", QtyVal = cook.NoQty}

Variables

This section is empty.

Functions

func TryParseQty

func TryParseQty(qty string) float64

Attempt to parse a quantity fraction string into a float representation. Additionally, parses decimals/whole values. e.g. "1.5", "5" cook.NoQty is returned on failure

Types

type Chunk

type Chunk interface {
	ToString() string
	// contains filtered or unexported methods
}

Chunks are the building blocks of recipe steps. They are a union of Text, Ingredient, Timer and Cookware.

type Component

type Component struct {
	Name   string  `json:"name"`
	Qty    string  `json:"qty"`
	QtyVal float64 `json:"qtyVal"`
	Unit   string  `json:"unit"`
}

Represents a generic `Component`, used in cooklang to define ingredients, cookware and timers.

type Cookware

type Cookware Component

func (Cookware) ToString

func (x Cookware) ToString() string

Converts cookware to a string, with its name followed by qty and units if they exist.

type Ingredient

type Ingredient Component

func (Ingredient) ToString

func (x Ingredient) ToString() string

Converts an ingredient to a string, with its name followed by qty and units if they exist.

type Recipe

type Recipe struct {
	Name        string            `json:"name"`
	Metadata    map[string]string `json:"metadata"`
	Ingredients []Ingredient      `json:"ingredients"`
	Cookware    []Cookware        `json:"cookware"`
	Timers      []Timer           `json:"timers"`
	Steps       []Step            `json:"steps"`
}

Recipes consist primarily of Metadata and Steps. Steps are stored sequentially and offer a continuous construction of the parsed `.cook` file.

Additionally, the Ingredients, Cookware and Timer members provide a manifest for each of the respective item classes.

Recipes can be easily parsed from a string using the function `ParseRecipe`.

func ParseRecipe

func ParseRecipe(name string, data *[]byte) Recipe

Parses a byte array containing a recipe following the cooklang specifications and returns as a `Recipe` struct

Example
recipeText :=
	`Preheat #deep fryer{} to 190°C.
Slice @potatoes{3} into 1/4" strips.
Optionally, blanch in boiling @water{2%cups}.
Drop into deep fryer for ~{7%mins}.
Remove from fryer and sprinkle @pink salt{} 
Enjoy with @ketchup, or mix in @mayonnaise{equal parts} for fancy sauce.`

data := []byte(recipeText)
recipe := ParseRecipe("Fries", &data)
fmt.Println(recipe.Ingredients)
Output:

[{potatoes 3 3 } {water 2 2 cups} {pink salt  0 } {ketchup  0 } {mayonnaise equal parts 0 }]

func ParseRecipeString

func ParseRecipeString(name string, data string) Recipe
Example
recipeText :=
	`Preheat #deep fryer{} to 190°C.
Slice @potatoes{3} into 1/4" strips.
Optionally, blanch in boiling @water{2%cups}.
Drop into deep fryer for ~{7%mins}.
Remove from fryer and sprinkle @pink salt{} 
Enjoy with @ketchup, or mix in @mayonnaise{equal parts} for fancy sauce.`

recipe := ParseRecipeString("Fries", recipeText)
fmt.Println(recipe.Ingredients)
Output:

[{potatoes 3 3 } {water 2 2 cups} {pink salt  0 } {ketchup  0 } {mayonnaise equal parts 0 }]

type Step

type Step []Chunk

A Step is one part of a recipe, consisting of a set of chunks which can be read in order to build a human readable recipe.

Ingredients, Cookware and Timer are kept as structs to allow for post processing (such as text formatting).

func (*Step) MarshalJSON

func (s *Step) MarshalJSON() ([]byte, error)

Custom JSON encoding wraps each of `Step`'s chunk into a struct that stores the type to allow for unambiguous decoding.

e.g. an ingredient chunk will be wrapped as encoded as: `{'tag': 'ingredient', 'data': {...}}`

func (*Step) UnmarshalJSON

func (s *Step) UnmarshalJSON(data []byte) error

Decodes the custom wrapped chunks created by the encoder back into an array of `Chunk`s.

type Text

type Text string

Text is a string wrapper (to allow for safe inclusion to Chunk interface)

func (Text) ToString

func (x Text) ToString() string

Unwraps Text chunk into a string

type Timer

type Timer Component

func (Timer) ToString

func (x Timer) ToString() string

Converts a timer to a string, with its name followed by qty and units if they exist.

Directories

Path Synopsis
cmd
internal
web
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL