Documentation
¶
Index ¶
- Constants
- type ListRenderer
- type Option
- func WithAllowSafeLabels(allowSafeLabels bool) Option
- func WithAncestorClass(ancestorClass string) Option
- func WithBranchClass(branchClass string) Option
- func WithClearMatcher(clearMatcher bool) Option
- func WithCurrentAsLink(currentAsLink bool) Option
- func WithCurrentClass(currentClass string) Option
- func WithDepth(depth *int) Option
- func WithExtra(name string, value any) Option
- func WithExtras(extras map[string]any) Option
- func WithFirstClass(firstClass string) Option
- func WithLastClass(lastClass string) Option
- func WithLeafClass(leafClass string) Option
- func WithMatchingDepth(matchingDepth *int) Option
- type Options
- func (o *Options) AddExtra(name string, value any) *Options
- func (o *Options) Apply(options ...Option) *Options
- func (o *Options) Copy() *Options
- func (o *Options) Extra(name string, def ...any) any
- func (o *Options) IsStop() bool
- func (o *Options) SetAllowSafeLabels(allowSafeLabels bool) *Options
- func (o *Options) SetAncestorClass(ancestorClass string) *Options
- func (o *Options) SetBranchClass(branchClass string) *Options
- func (o *Options) SetClearMatcher(clearMatcher bool) *Options
- func (o *Options) SetCurrentAsLink(currentAsLink bool) *Options
- func (o *Options) SetCurrentClass(currentClass string) *Options
- func (o *Options) SetDepth(depth int) *Options
- func (o *Options) SetExtras(extras map[string]any) *Options
- func (o *Options) SetFirstClass(firstClass string) *Options
- func (o *Options) SetLastClass(lastClass string) *Options
- func (o *Options) SetLeafClass(leafClass string) *Options
- func (o *Options) SetMatchingDepth(matchingDepth int) *Options
- func (o *Options) Slice() []Option
- func (o *Options) SubDepth() *Options
- func (o *Options) SubMatchingDepth() *Options
- type Renderer
- type TemplateRenderer
- type Theme
Constants ¶
const MenuTemplate = "@menu/menu.html"
MenuTemplate is the constant that holds the path to the menu template file.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ListRenderer ¶
type ListRenderer struct {
// contains filtered or unexported fields
}
ListRenderer is a type that implements the Renderer interface and is responsible for rendering menus in list format. Render method of the ListRenderer type is used to render the menu and return the generated HTML string. It takes the root menu item along with optional rendering options and returns the rendered menu as a string. The options can be used to customize the rendering behavior such as depth, matching depth, CSS classes, etc. It uses the renderList method to recursively render the menu items and their children. renderList method recursively renders the menu items and their children in list format. It takes the current menu item, its children attributes, and the rendering options. It checks if the rendering should stop based on the depth option or if the current item has no children or if the display of children is disabled. If any of these conditions are met, it returns an empty string. Otherwise, it iterates over the children of the current item and recursively calls the renderItem method to render each child. It then formats the rendered children as an unordered list and returns it as a string. renderItem method renders a single menu item and its children. It takes the current menu item and the rendering options. It checks if the item should be displayed based on its display flag. It then determines the CSS classes to be applied to the item based on its current state, such as current, ancestor, first, last, etc. It creates a clone of the item's attributes, adds the CSS classes, and formats them as HTML attributes. It then determines the current item's level and formats the HTML tag for the list item accordingly. It calls the renderLink method to render the link or span element based on the item's URI and the rendering options. It then formats the rendered item along with its children as a list item and returns it as a string. renderLink method renders either a link or a span element for a menu item. It takes the current menu item and the rendering options. If the item has a non-empty URI and it's not currently considered as the current item or if the currentAsLink option is enabled, it renders a link element. Otherwise, it renders a span element. It uses the renderLabel method to render the label content for the link or span element. It then formats the rendered
func NewListRenderer ¶
func NewListRenderer(matcher menu.Matcher, options ...Option) ListRenderer
NewListRenderer creates a new instance of ListRenderer with the given matcher and options. The matcher is used to determine the current item and its ancestor. The options are used
func (ListRenderer) Render ¶
func (r ListRenderer) Render(ctx context.Context, item *menu.Item, options ...Option) (string, error)
Render renders the menu item and its children into a HTML list. It accepts a context, the menu item to render, and optional rendering options. It returns the rendered content as a string and an error if any.
type Option ¶
type Option func(*Options)
Option represents a function that modifies an *Options object.
Usage example:
func WithDepth(depth *int) Option { return func(options *Options) { if depth == nil { options.Depth = nil } else { *options.Depth = *depth } } } opt := &Options{} WithDepth(5)(opt)
func WithAllowSafeLabels ¶
WithAllowSafeLabels is a function that returns an Option for setting the AllowSafeLabels field in the Options struct.
func WithAncestorClass ¶
WithAncestorClass is a function that creates an Option to set the ancestor class in the Options struct. It takes a string parameter, ancestorClass, and returns an Option. The returned Option sets the ancestorClass field in the Options struct.
Example usage:
options := &Options{} option := WithAncestorClass("AncestorClass") option(options)
func WithBranchClass ¶
WithBranchClass is a function that creates an Option to set the BranchClass field of the Options struct. It takes in a string parameter branchClass, and returns a function that sets the BranchClass field of the Options struct to the provided value.
func WithClearMatcher ¶
WithClearMatcher is a function that returns an Option function. The Option function sets the ClearMatcher field of the Options struct to the provided value. Usage example: options := &Options{} clearMatcherOption := WithClearMatcher(true) clearMatcherOption(options)
func WithCurrentAsLink ¶
WithCurrentAsLink is a function that returns an Option, which sets the value of the CurrentAsLink field in the Options struct. The CurrentAsLink field determines whether the current node in a tree structure should be treated as a link. If currentAsLink is true, the current node will be treated as a link, otherwise, it will not be treated as a link.
Example usage: options := &Options{} opt := WithCurrentAsLink(true) opt(options)
This will set CurrentAsLink to true in the options object.
func WithCurrentClass ¶
WithCurrentClass is a function that returns an Option function. The returned Option function sets the CurrentClass field of an Options struct. Usage example: options := &Options{} WithCurrentClass("className")(options)
func WithDepth ¶
WithDepth is a function that sets the value of the Depth field in the Options struct. It takes a pointer to an int as a parameter and returns an Option function. The returned Option function updates the Options struct by assigning the value of the depth parameter to the Depth field, or setting the Depth field to nil if the depth parameter
func WithExtra ¶
WithExtra is a function that creates an Option which adds an extra value to the Options struct. The extra value is stored in the Extras map with the specified name.
Parameters:
- name: the name of the extra value.
- value: the value of the extra.
Returns:
- Option: an Option function that adds the extra value to the Options struct.
Example:
options := &Options{} extraOption := WithExtra("key", "value") extraOption(options) // Now options.Extras["key"] contains "value"
Note:
The Options struct is modified in-place by calling the Option function. To apply multiple options at once, use the Apply method of Options.
func WithExtras ¶
WithExtras is a function that returns an Option for setting the Extras field in the Options struct. It takes a map[string]any as input and sets the Extras field in the Options struct to the provided map. Usage example:
extras := map[string]any{"key1": value1, "key2": value2} withExtras := WithExtras(extras) options := &Options{} withExtras(options)
func WithFirstClass ¶
WithFirstClass returns an Option function that sets the FirstClass field of the Options struct.
func WithLastClass ¶
WithLastClass is a function that creates an Option for setting the LastClass field in the Options struct. It takes a string parameter representing the last class and returns an Option function. The returned Option function sets the LastClass field in the Options struct when called. Example usage:
WithLastClass("lastClass") // returns an Option function to set LastClass field WithLastClass("lastClass")(options) // sets LastClass field in options
func WithLeafClass ¶
WithLeafClass is a function that returns an Option to set the leafClass field of Options. It takes a string parameter representing the leaf class and returns an Option function that sets the leafClass field to the provided value. The Options type represents a set of configuration options. The leafClass field is used to specify the leaf class value. Usage:
leafClassOption := WithLeafClass("exampleLeafClass") options := &Options{} leafClassOption(options) // Alternative usage options := &Options{} options.Apply(WithLeafClass("exampleLeafClass"))
The Options type has other fields and methods that can be used to configure additional options and apply a set of options to an Options object. For more information and examples, refer to the documentation for Options and other Option functions.
func WithMatchingDepth ¶
WithMatchingDepth is a function that returns an Option for setting the matching depth of an Options object. The matching depth determines the maximum number of matching elements in the search hierarchy. If the given matchingDepth is nil, the Options' matching depth will be set to nil, meaning there is no maximum depth. Otherwise, the Options' matching depth will be set to the value of the matchingDepth pointer. If matchingDepth is greater than zero, the Options' matching depth will be decreased by one. The Options is passed by reference and modified directly.
type Options ¶
type Options struct { Depth *int `json:"depth,omitempty"` MatchingDepth *int `json:"matching_depth,omitempty"` CurrentClass string `json:"current_class,omitempty"` AncestorClass string `json:"ancestor_class,omitempty"` FirstClass string `json:"first_class,omitempty"` LastClass string `json:"last_class,omitempty"` LeafClass string `json:"leaf_class,omitempty"` BranchClass string `json:"branch_class,omitempty"` CurrentAsLink bool `json:"current_as_link,omitempty"` AllowSafeLabels bool `json:"allow_safe_labels,omitempty"` ClearMatcher bool `json:"clear_matcher,omitempty"` Extras map[string]any `json:"extras,omitempty"` }
func NewOptions ¶
NewOptions creates a new instance of Options with default values and applies the provided options. The returned Options object can be further modified using the available setter methods. Example usage: options := NewOptions(
WithDepth(3), WithCurrentClass("active"), WithExtras(map[string]any{"key": "value"}),
) myOptions := options.SetAncestorClass("ancestor") myOptions.SetFirstClass("first") myOptions.SetLastClass("last") ... Usage within a function: renderer := NewTemplateRenderer(
myTheme, myMatcher, NewOptions(WithDepth(2), WithCurrentClass("active")),
) ... renderer := NewListRenderer(
myMatcher, NewOptions(WithCurrentClass("active"), WithLeafClass("leaf")),
)
func (*Options) AddExtra ¶
AddExtra adds an extra value to the Options.Extras map.
Parameters: - name: the name of the extra value. - value: the value to be added.
Returns: - *Options: the Options object with the extra value added.
func (*Options) Apply ¶
Apply applies the given list of options to the Options object. It iterates over the list of options and calls each option passing the Options object as an argument. Returns the modified Options object.
func (*Options) Copy ¶
Copy creates a copy of the Options object. It creates a new Options object and copies the values from the original object. If Depth is not nil, it creates a new int variable and assigns the value of Depth to it. It assigns a pointer to the new int variable to the new Options object's Depth field. If MatchingDepth is not nil, it creates a new int variable and assigns the value of MatchingDepth to it. It assigns a pointer to the new int variable to the new Options object's MatchingDepth field. It clones the Extras field using the maps.Clone function and assigns the cloned map to the new Options object's Extras field. It returns a pointer to the new Options object.
func (*Options) Extra ¶
Extra returns the value of the specified extra property from the Options struct. If the property is not found, it returns the default value.
func (*Options) IsStop ¶
IsStop returns true if the depth of the Options is less than or equal to zero, indicating that the processing should stop.
func (*Options) SetAllowSafeLabels ¶
SetAllowSafeLabels sets the value of AllowSafeLabels in the Options struct and returns a pointer to the modified Options struct.
func (*Options) SetAncestorClass ¶
SetAncestorClass sets the value of the AncestorClass field in the Options struct and returns a pointer to the modified Options struct. The AncestorClass field represents the class of the ancestor node in a tree structure.
Usage:
options := &Options{} options.SetAncestorClass("ancestorClass") // Usage in Apply method options.Apply(WithAncestorClass("ancestorClass"))
Parameters:
- ancestorClass: The class of the ancestor node.
Returns:
A pointer to the modified Options struct.
func (*Options) SetBranchClass ¶
SetBranchClass sets the branch class in the Options struct and returns a pointer to Options. It takes a string parameter called branchClass. Example usage: opt := &Options{} opt.SetBranchClass("Branch") Returns: &Options{..., BranchClass: "Branch", ...}
func (*Options) SetClearMatcher ¶
SetClearMatcher sets the `ClearMatcher` field in the `Options` struct and returns a pointer to the modified struct.
Example: opts := &Options{} opts.SetClearMatcher(true)
The `SetClearMatcher` method can be used in conjunction with the `WithClearMatcher` function to set the `ClearMatcher` field in the `Options` struct using functional options. Example: opts := &Options{} opts.Apply(WithClearMatcher(true))
Parameters:
clearMatcher: A boolean value indicating whether to clear the matcher. If true, the matcher will be cleared; otherwise, it will not be cleared.
Returns:
*Options: A pointer to the modified `Options` struct.
func (*Options) SetCurrentAsLink ¶
SetCurrentAsLink sets the `currentAsLink` field of the `Options` struct and returns the modified `Options` object. Setting `currentAsLink` to `true` indicates that the current node should be rendered as a link.
func (*Options) SetCurrentClass ¶
SetCurrentClass sets the value of the CurrentClass field in the Options struct and returns the modified Options struct.
func (*Options) SetDepth ¶
SetDepth sets the `Depth` field of the `Options` struct to the specified value and returns a pointer to the `Options` struct. Parameters: - depth: An integer value representing the depth. Returns: - *Options: A pointer to the `Options` struct with the updated `Depth` field.
func (*Options) SetExtras ¶
SetExtras sets the extras map for the Options object. If the provided extras map is nil, it sets an empty map for extras. Otherwise, it clones the provided extras map and sets it as extras. Returns a pointer to the Options object.
func (*Options) SetFirstClass ¶
SetFirstClass sets the value of the FirstClass field in the Options struct and returns a pointer to the updated Options struct.
func (*Options) SetLastClass ¶
SetLastClass sets the value of the LastClass field in the Options struct and returns a pointer to the modified Options struct.
func (*Options) SetLeafClass ¶
SetLeafClass sets the value of the `LeafClass` field in the `Options` struct and returns a pointer to the modified `Options` object.
func (*Options) SetMatchingDepth ¶
SetMatchingDepth sets the value of the MatchingDepth field in the Options struct. It takes an int as a parameter, matchingDepth, and assigns the address of that int to the MatchingDepth field. It then returns the pointer to the Options struct.
func (*Options) Slice ¶
Slice returns a slice of Option functions that correspond to the current state of the Options object.
func (*Options) SubDepth ¶
SubDepth decrements the value of the Depth field in the Options struct by 1 if it is not nil. It then returns a pointer to the modified Options struct. Example usage: options.SubDepth().SubMatchingDepth()
func (*Options) SubMatchingDepth ¶
SubMatchingDepth decreases the value of o.MatchingDepth by 1 if it is not nil and greater than 0.
type Renderer ¶
type Renderer interface {
Render(ctx context.Context, item *menu.Item, options ...Option) (string, error)
}
Renderer is an interface for rendering menu items.
Usage Example:
_ Renderer = ListRenderer{}
type TemplateRenderer ¶
type TemplateRenderer struct {
// contains filtered or unexported fields
}
TemplateRenderer is a type that represents a renderer for templates. It is used to render HTML templates based on a given theme and matcher. The renderer provides options for customizing the rendering process.
func NewTemplateRenderer ¶
func NewTemplateRenderer(theme Theme, matcher menu.Matcher, options ...Option) TemplateRenderer
NewTemplateRenderer creates a new TemplateRenderer with the given theme, matcher, and options.
func (TemplateRenderer) Render ¶
func (r TemplateRenderer) Render(ctx context.Context, item *menu.Item, options ...Option) (string, error)
Render is a method of the TemplateRenderer struct that renders a menu item using the specified options and theme. It takes a context object, a pointer to a menu.Item object, and a variadic list of options as parameters. It returns a string (the rendered content) and an error (if any occurred during rendering).
The function starts by creating a copy of the options and applying the passed options to it. It then calls the HTML method of the theme to render the menu item with the specified template and data. The data passed to the template includes the context object, the menu item, the options, the matcher, and helper functions for converting attributes and classes.
If the "ClearMatcher" option is set to true, the matcher is cleared after rendering the content.
The rendered content and any error that occurred during rendering are returned as the result of the function.
type Theme ¶
Theme is an interface that provides the method HTML for generating HTML code based on a provided template and data. HTML takes a context, a template string, and data and returns a string representation of the generated HTML code. It returns an error if there was an issue generating the HTML. Example usage: theme := // create an instance of a struct that implements the Theme interface template := "my-template" data := // provide data for the template rendering html, err := theme.HTML(ctx, template, data)
if err != nil { // handle error }
// use the generated HTML code