cooklang

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2022 License: MIT Imports: 7 Imported by: 4

README

cooklang-go Go Reference

Cooklang parser in Go

Usage

Check example_test.go for sample usage.

Documentation

Overview

Package cooklang provides a parser for .cook defined recipes as defined in https://cooklang.org/docs/spec/

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cookware

type Cookware struct {
	IsNumeric   bool    // true if the amount is numeric
	Name        string  // cookware name
	Quantity    float64 // quantity of the cookware
	QuantityRaw string  // quantity of the cookware as raw text
}

Cookware represents a cookware item

type Ingredient

type Ingredient struct {
	Name   string           // name of the ingredient
	Amount IngredientAmount // optional ingredient amount (default: 1)
}

Ingredient represents a recipe ingredient

type IngredientAmount

type IngredientAmount struct {
	IsNumeric   bool    // true if the amount is numeric
	Quantity    float64 // quantity of the ingredient
	QuantityRaw string  // quantity of the ingredient as raw text
	Unit        string  // optional ingredient unit
}

IngredientAmount represents the amount required of an ingredient

type Metadata

type Metadata = map[string]string

Metadata contains key value map of metadata

type Recipe

type Recipe struct {
	Steps    []Step   // list of steps for the recipe
	Metadata Metadata // metadata of the recipe
}

Recipe contains a cooklang defined recipe

func ParseFile

func ParseFile(fileName string) (*Recipe, error)

ParseFile parses a cooklang recipe file and returns the recipe or an error

func ParseStream

func ParseStream(s io.Reader) (*Recipe, error)

ParseStream parses a cooklang recipe text stream and returns the recipe or an error

func ParseString

func ParseString(s string) (*Recipe, error)

ParseString parses a cooklang recipe string and returns the recipe or an error

Example
recipe := `>> servings: 6

Make 6 pizza balls using @tipo zero flour{820%g}, @water{533%ml}, @salt{24.6%g} and @fresh yeast{1.6%g}. Put in a #fridge for ~{2%days}.

Set #oven to max temperature and heat #pizza stone{} for about ~{40%minutes}.

Make some tomato sauce with @chopped tomato{3%cans} and @garlic{3%cloves} and @dried oregano{3%tbsp}. Put on a #pan and leave for ~{15%minutes} occasionally stirring.

Make pizzas putting some tomato sauce with #spoon on top of flattened dough. Add @fresh basil{18%leaves}, @parma ham{3%packs} and @mozzarella{3%packs}.

Put in an #oven for ~{4%minutes}.`
r, _ := cooklang.ParseString(recipe)
j, _ := json.MarshalIndent(r, "", "  ")
fmt.Println(string(j))
Output:

{
  "Steps": [
    {
      "Directions": "Make 6 pizza balls using tipo zero flour, water, salt and fresh yeast. Put in a fridge for 2 days.",
      "Timers": [
        {
          "Name": "",
          "Duration": 2,
          "Unit": "days"
        }
      ],
      "Ingredients": [
        {
          "Name": "tipo zero flour",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 820,
            "QuantityRaw": "820",
            "Unit": "g"
          }
        },
        {
          "Name": "water",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 533,
            "QuantityRaw": "533",
            "Unit": "ml"
          }
        },
        {
          "Name": "salt",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 24.6,
            "QuantityRaw": "24.6",
            "Unit": "g"
          }
        },
        {
          "Name": "fresh yeast",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 1.6,
            "QuantityRaw": "1.6",
            "Unit": "g"
          }
        }
      ],
      "Cookware": [
        {
          "IsNumeric": false,
          "Name": "fridge",
          "Quantity": 1,
          "QuantityRaw": ""
        }
      ],
      "Comments": null
    },
    {
      "Directions": "Set oven to max temperature and heat pizza stone for about 40 minutes.",
      "Timers": [
        {
          "Name": "",
          "Duration": 40,
          "Unit": "minutes"
        }
      ],
      "Ingredients": [],
      "Cookware": [
        {
          "IsNumeric": false,
          "Name": "oven",
          "Quantity": 1,
          "QuantityRaw": ""
        },
        {
          "IsNumeric": false,
          "Name": "pizza stone",
          "Quantity": 1,
          "QuantityRaw": ""
        }
      ],
      "Comments": null
    },
    {
      "Directions": "Make some tomato sauce with chopped tomato and garlic and dried oregano. Put on a pan and leave for 15 minutes occasionally stirring.",
      "Timers": [
        {
          "Name": "",
          "Duration": 15,
          "Unit": "minutes"
        }
      ],
      "Ingredients": [
        {
          "Name": "chopped tomato",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 3,
            "QuantityRaw": "3",
            "Unit": "cans"
          }
        },
        {
          "Name": "garlic",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 3,
            "QuantityRaw": "3",
            "Unit": "cloves"
          }
        },
        {
          "Name": "dried oregano",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 3,
            "QuantityRaw": "3",
            "Unit": "tbsp"
          }
        }
      ],
      "Cookware": [
        {
          "IsNumeric": false,
          "Name": "pan",
          "Quantity": 1,
          "QuantityRaw": ""
        }
      ],
      "Comments": null
    },
    {
      "Directions": "Make pizzas putting some tomato sauce with spoon on top of flattened dough. Add fresh basil, parma ham and mozzarella.",
      "Timers": [],
      "Ingredients": [
        {
          "Name": "fresh basil",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 18,
            "QuantityRaw": "18",
            "Unit": "leaves"
          }
        },
        {
          "Name": "parma ham",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 3,
            "QuantityRaw": "3",
            "Unit": "packs"
          }
        },
        {
          "Name": "mozzarella",
          "Amount": {
            "IsNumeric": true,
            "Quantity": 3,
            "QuantityRaw": "3",
            "Unit": "packs"
          }
        }
      ],
      "Cookware": [
        {
          "IsNumeric": false,
          "Name": "spoon",
          "Quantity": 1,
          "QuantityRaw": ""
        }
      ],
      "Comments": null
    },
    {
      "Directions": "Put in an oven for 4 minutes.",
      "Timers": [
        {
          "Name": "",
          "Duration": 4,
          "Unit": "minutes"
        }
      ],
      "Ingredients": [],
      "Cookware": [
        {
          "IsNumeric": false,
          "Name": "oven",
          "Quantity": 1,
          "QuantityRaw": ""
        }
      ],
      "Comments": null
    }
  ],
  "Metadata": {
    "servings": "6"
  }
}
Example (ToString)
recipeIn := `>> servings: 6

Make 6 pizza balls using @tipo zero flour{820%g}, @water{533%ml}, @salt{24.6%g} and @fresh yeast{1.6%g}. Put in a #fridge for ~{2%days}.

Set #oven to max temperature and heat #pizza stone{} for about ~{40%minutes}.

Make some tomato sauce with @chopped tomato{3%cans} and @garlic{3%cloves} and @dried oregano{3%tbsp}. Put on a #pan and leave for ~{15%minutes} occasionally stirring.

Make pizzas putting some tomato sauce with #spoon on top of flattened dough. Add @fresh basil{18%leaves}, @parma ham{3%packs} and @mozzarella{3%packs}.

Put in an #oven for ~{4%minutes}.`
r, _ := cooklang.ParseString(recipeIn)
fmt.Print(r)
Output:

>> servings: 6

Make 6 pizza balls using tipo zero flour, water, salt and fresh yeast. Put in a fridge for 2 days.

Set oven to max temperature and heat pizza stone for about 40 minutes.

Make some tomato sauce with chopped tomato and garlic and dried oregano. Put on a pan and leave for 15 minutes occasionally stirring.

Make pizzas putting some tomato sauce with spoon on top of flattened dough. Add fresh basil, parma ham and mozzarella.

Put in an oven for 4 minutes.

func (Recipe) String added in v0.1.5

func (r Recipe) String() string

type Step

type Step struct {
	Directions  string       // step directions as plain text
	Timers      []Timer      // list of timers in the step
	Ingredients []Ingredient // list of ingredients used in the step
	Cookware    []Cookware   // list of cookware used in the step
	Comments    []string     // list of comments
}

Step represents a recipe step

type Timer

type Timer struct {
	Name     string  // name of the timer
	Duration float64 // duration of the timer
	Unit     string  // time unit of the duration
}

Timer represents a time duration

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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