Documentation
¶
Overview ¶
Package renderers provides different renderers for Cooklang recipes.
Package renderers provides different renderers for Cooklang recipes.
This package includes renderers for various output formats:
- CooklangRenderer: Renders recipes back to Cooklang format
- MarkdownRenderer: Renders recipes as Markdown
- HTMLRenderer: Renders recipes as HTML
- PrintRenderer: Renders recipes as print-optimized HTML
- JSONLDRenderer: Renders recipes as Schema.org JSON-LD for SEO
Example usage:
recipe, _ := cooklang.ParseFile("recipe.cook")
// Use the default renderers
html := renderers.Default.HTML.RenderRecipe(recipe)
markdown := renderers.Default.Markdown.RenderRecipe(recipe)
// For JSON-LD (SEO structured data)
jsonLD, _ := renderers.Default.JSONLD.RenderRecipeJSON(recipe, nil)
Index ¶
- Variables
- func NewCooklangRenderer() cooklang.RecipeRenderer
- func NewHTMLRenderer() cooklang.RecipeRenderer
- func NewMarkdownRenderer() cooklang.RecipeRenderer
- func NewPrintRenderer() cooklang.RecipeRenderer
- func ParseDurationToISO8601(duration string) string
- type AggregateRating
- type CooklangRenderer
- type HTMLRenderer
- type JSONLDOptions
- type JSONLDRenderer
- func (jr JSONLDRenderer) RenderRecipe(recipe *cooklang.Recipe, opts *JSONLDOptions) map[string]interface{}
- func (jr JSONLDRenderer) RenderRecipeJSON(recipe *cooklang.Recipe, opts *JSONLDOptions) (string, error)
- func (jr JSONLDRenderer) RenderRecipeScriptTag(recipe *cooklang.Recipe, opts *JSONLDOptions) (string, error)
- type MarkdownRenderer
- type PrintRenderer
- type VideoObject
Constants ¶
This section is empty.
Variables ¶
var ( // Default renderers that can be used directly Default = struct { Cooklang CooklangRenderer Markdown MarkdownRenderer HTML HTMLRenderer Print PrintRenderer JSONLD JSONLDRenderer }{ Cooklang: CooklangRenderer{}, Markdown: MarkdownRenderer{}, HTML: HTMLRenderer{}, Print: PrintRenderer{}, JSONLD: JSONLDRenderer{}, } )
All default renderer instances for convenience
var DefaultCooklangRenderer = CooklangRenderer{}
DefaultCooklangRenderer is the default instance of CooklangRenderer
var DefaultHTMLRenderer = HTMLRenderer{}
DefaultHTMLRenderer is the default instance of HTMLRenderer
var DefaultJSONLDRenderer = JSONLDRenderer{}
DefaultJSONLDRenderer is the default instance of JSONLDRenderer.
var DefaultMarkdownRenderer = MarkdownRenderer{}
DefaultMarkdownRenderer is the default instance of MarkdownRenderer
var DefaultPrintRenderer = PrintRenderer{}
DefaultPrintRenderer is the default instance of PrintRenderer
Functions ¶
func NewCooklangRenderer ¶
func NewCooklangRenderer() cooklang.RecipeRenderer
NewCooklangRenderer creates a new Cooklang renderer
func NewHTMLRenderer ¶
func NewHTMLRenderer() cooklang.RecipeRenderer
NewHTMLRenderer creates a new HTML renderer
func NewMarkdownRenderer ¶
func NewMarkdownRenderer() cooklang.RecipeRenderer
NewMarkdownRenderer creates a new Markdown renderer
func NewPrintRenderer ¶
func NewPrintRenderer() cooklang.RecipeRenderer
NewPrintRenderer creates a new print-optimized HTML renderer
func ParseDurationToISO8601 ¶ added in v1.0.3
ParseDurationToISO8601 converts human-readable duration strings to ISO 8601 format. This is used internally to convert prep_time and total_time fields.
Supported input formats:
- "5 minutes", "5 min", "5m"
- "1 hour", "1 hr", "1h"
- "1 hour 30 minutes", "1h 30m"
- "90 minutes" (converted to "PT1H30M")
- "2 hours" → "PT2H"
- "30 seconds", "30 sec", "30s"
Returns empty string if the input cannot be parsed.
Examples:
ParseDurationToISO8601("15 minutes") // "PT15M"
ParseDurationToISO8601("1 hour") // "PT1H"
ParseDurationToISO8601("1h 30m") // "PT1H30M"
ParseDurationToISO8601("90 minutes") // "PT1H30M"
ParseDurationToISO8601("2 hours 15 min") // "PT2H15M"
Types ¶
type AggregateRating ¶ added in v1.0.3
type AggregateRating struct {
// RatingValue is the average rating value.
// Required for the rating to be included.
RatingValue float64
// RatingCount is the total number of ratings.
// Required for the rating to be included.
RatingCount int
// BestRating is the highest possible rating value.
// Defaults to 5 if not specified.
BestRating float64
// WorstRating is the lowest possible rating value.
// Defaults to 1 if not specified.
WorstRating float64
}
AggregateRating represents the aggregate rating of a recipe based on multiple user reviews. This is used to display star ratings in Google search results.
Example:
rating := &renderers.AggregateRating{
RatingValue: 4.5,
RatingCount: 42,
}
type CooklangRenderer ¶
type CooklangRenderer struct{}
CooklangRenderer renders recipes in the original Cooklang format
func (CooklangRenderer) RenderRecipe ¶
func (cr CooklangRenderer) RenderRecipe(recipe *cooklang.Recipe) string
type HTMLRenderer ¶
type HTMLRenderer struct{}
HTMLRenderer renders recipes in HTML format
func (HTMLRenderer) RenderRecipe ¶
func (hr HTMLRenderer) RenderRecipe(recipe *cooklang.Recipe) string
type JSONLDOptions ¶ added in v1.0.3
type JSONLDOptions struct {
// URL is the canonical URL of the recipe page.
// Maps to Schema.org "url" property.
URL string
// DateModified is the last modification date of the recipe.
// Maps to Schema.org "dateModified" property.
DateModified *time.Time
// DatePublished overrides the recipe.Date field.
// Maps to Schema.org "datePublished" property.
DatePublished *time.Time
// Images provides full URLs for recipe images, overriding or supplementing
// the recipe.Images field which typically contains relative paths.
// Maps to Schema.org "image" property.
Images []string
// AggregateRating provides rating data from your application.
// Maps to Schema.org "aggregateRating" property.
AggregateRating *AggregateRating
// Video provides video information for the recipe.
// Maps to Schema.org "video" property.
Video *VideoObject
// Keywords provides additional keywords beyond the recipe tags.
// These are merged with recipe.Tags.
// Maps to Schema.org "keywords" property.
Keywords []string
// RecipeCategory overrides or supplements the recipe category.
// Examples: "Cocktail", "Appetizer", "Main course", "Dessert"
// Maps to Schema.org "recipeCategory" property.
RecipeCategory string
// AuthorURL provides a URL for the author's profile page.
// When set, the author is rendered as a Person object with a URL.
AuthorURL string
}
JSONLDOptions allows customization of the JSON-LD output with application-specific data that may not be available in the cooklang Recipe itself.
All fields are optional. When nil or zero-valued, the corresponding JSON-LD property is either omitted or derived from the cooklang.Recipe fields.
type JSONLDRenderer ¶ added in v1.0.3
type JSONLDRenderer struct{}
JSONLDRenderer renders recipes as Schema.org Recipe JSON-LD structured data. JSON-LD (JavaScript Object Notation for Linked Data) is the recommended format by Google for embedding structured data in web pages to enable rich search results.
The renderer produces output conforming to the Schema.org Recipe specification: https://schema.org/Recipe
Example usage:
recipe, _ := cooklang.ParseFile("margarita.cook")
renderer := renderers.JSONLDRenderer{}
// Get JSON-LD as a map for further manipulation
data := renderer.RenderRecipe(recipe, nil)
// Get JSON-LD as a formatted JSON string
jsonStr, _ := renderer.RenderRecipeJSON(recipe, nil)
// Get a complete <script> tag ready for HTML embedding
scriptTag, _ := renderer.RenderRecipeScriptTag(recipe, &renderers.JSONLDOptions{
URL: "https://example.com/recipes/margarita",
AggregateRating: &renderers.AggregateRating{
RatingValue: 4.5,
RatingCount: 42,
},
})
func NewJSONLDRenderer ¶ added in v1.0.3
func NewJSONLDRenderer() JSONLDRenderer
NewJSONLDRenderer creates a new JSON-LD renderer. This is a convenience function that returns a configured JSONLDRenderer instance.
func (JSONLDRenderer) RenderRecipe ¶ added in v1.0.3
func (jr JSONLDRenderer) RenderRecipe(recipe *cooklang.Recipe, opts *JSONLDOptions) map[string]interface{}
RenderRecipe returns a JSON-LD object as a map for flexible manipulation. This is useful when you need to modify the output before serialization.
The returned map follows the Schema.org Recipe specification with these properties:
- @context: Always "https://schema.org"
- @type: Always "Recipe"
- name: From recipe.Title
- description: From recipe.Description
- author: From recipe.Author (as Person object)
- image: From opts.Images or recipe.Images
- recipeYield: From recipe.Servings
- prepTime: From recipe.PrepTime (converted to ISO 8601 duration)
- totalTime: From recipe.TotalTime (converted to ISO 8601 duration)
- recipeCategory: From opts.RecipeCategory or recipe.Metadata["category"]
- recipeCuisine: From recipe.Cuisine
- keywords: From recipe.Tags merged with opts.Keywords
- recipeIngredient: Array of ingredient strings
- recipeInstructions: Array of HowToStep objects
- tool: Array of cookware/tool names
- datePublished: From opts.DatePublished or recipe.Date
- dateModified: From opts.DateModified
- url: From opts.URL
- aggregateRating: From opts.AggregateRating
- video: From opts.Video
Parameters:
- recipe: The parsed Cooklang recipe to render
- opts: Optional configuration (can be nil for defaults)
Returns:
- map[string]interface{}: The JSON-LD object as a map
func (JSONLDRenderer) RenderRecipeJSON ¶ added in v1.0.3
func (jr JSONLDRenderer) RenderRecipeJSON(recipe *cooklang.Recipe, opts *JSONLDOptions) (string, error)
RenderRecipeJSON returns JSON-LD as a formatted (indented) JSON string. This is suitable for debugging or when you need the raw JSON output.
Parameters:
- recipe: The parsed Cooklang recipe to render
- opts: Optional configuration (can be nil for defaults)
Returns:
- string: The JSON-LD as an indented JSON string
- error: Any error during JSON marshaling
Example:
recipe, _ := cooklang.ParseFile("recipe.cook")
renderer := renderers.JSONLDRenderer{}
jsonStr, err := renderer.RenderRecipeJSON(recipe, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(jsonStr)
func (JSONLDRenderer) RenderRecipeScriptTag ¶ added in v1.0.3
func (jr JSONLDRenderer) RenderRecipeScriptTag(recipe *cooklang.Recipe, opts *JSONLDOptions) (string, error)
RenderRecipeScriptTag returns a complete HTML <script> tag with JSON-LD content. This is ready to be embedded directly in an HTML page's <head> section.
The output format is:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Recipe",
...
}
</script>
Parameters:
- recipe: The parsed Cooklang recipe to render
- opts: Optional configuration (can be nil for defaults)
Returns:
- string: The complete script tag with JSON-LD content
- error: Any error during JSON marshaling
Example:
recipe, _ := cooklang.ParseFile("recipe.cook")
renderer := renderers.JSONLDRenderer{}
scriptTag, err := renderer.RenderRecipeScriptTag(recipe, &renderers.JSONLDOptions{
URL: "https://example.com/recipes/my-recipe",
})
if err != nil {
log.Fatal(err)
}
// Embed scriptTag in your HTML template
type MarkdownRenderer ¶
type MarkdownRenderer struct{}
MarkdownRenderer renders recipes in Markdown format
func (MarkdownRenderer) RenderRecipe ¶
func (mr MarkdownRenderer) RenderRecipe(recipe *cooklang.Recipe) string
type PrintRenderer ¶
type PrintRenderer struct{}
PrintRenderer renders recipes as print-optimized HTML designed to fit on a single page. It includes embedded CSS for clean printing without browser chrome or interactive elements.
func (PrintRenderer) RenderRecipe ¶
func (pr PrintRenderer) RenderRecipe(recipe *cooklang.Recipe) string
type VideoObject ¶ added in v1.0.3
type VideoObject struct {
// Name is the title of the video.
Name string
// Description is a description of the video.
Description string
// ThumbnailURL is the URL to a thumbnail image for the video.
ThumbnailURL string
// ContentURL is the URL to the actual video file.
ContentURL string
// EmbedURL is the URL to an embeddable player for the video.
EmbedURL string
// UploadDate is the date the video was uploaded.
UploadDate *time.Time
// Duration is the video duration in ISO 8601 format (e.g., "PT1M30S").
Duration string
}
VideoObject represents a video associated with the recipe. This enables video rich results in search engines.