markdown

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

README

Markdown Package (Extended Converter API)

The internal/markdown package provides an extended API for generating documentation from OPNsense configurations with configurable options and pluggable templates.

Overview

This package implements TASK-011 by providing:

  • Options struct with comprehensive configuration (Format, Template, Sections, Theme, WrapWidth, etc.)
  • Generator interface for pluggable generation strategies
  • Default implementation that wraps existing converter logic
  • Support stubs for JSON/YAML output
  • Template management for pluggable Go text/template templates
  • Backward compatibility through adapter pattern

Key Components

Generator Interface
type Generator interface {
    Generate(ctx context.Context, cfg *model.Opnsense, opts Options) (string, error)
}
Options Configuration
type Options struct {
    Format          Format                 // markdown, json, yaml
    Template        *template.Template     // Custom template
    TemplateName    string                 // Built-in template name
    Sections        []string              // Sections to include
    Theme           Theme                 // Terminal theme
    WrapWidth       int                   // Text wrapping
    EnableTables    bool                  // Table rendering
    EnableColors    bool                  // Colored output
    EnableEmojis    bool                  // Emoji icons
    Compact         bool                  // Compact format
    IncludeMetadata bool                  // Generation metadata
    CustomFields    map[string]interface{} // Template fields
}

Usage Examples

Basic Usage
// Create generator with default options
generator := markdown.NewMarkdownGenerator()
opts := markdown.DefaultOptions()

// Generate markdown
result, err := generator.Generate(ctx, cfg, opts)
Custom Configuration
// Configure options with fluent interface
opts := markdown.DefaultOptions().
    WithFormat(markdown.FormatJSON).
    WithTheme(markdown.ThemeDark).
    WithWrapWidth(80).
    WithTables(false).
    WithCustomField("version", "1.0.0")

result, err := generator.Generate(ctx, cfg, opts)
Backward Compatibility
// Use adapter for backward compatibility with existing Converter interface
adapter := markdown.NewConverterAdapter()
result, err := adapter.ToMarkdown(ctx, cfg)

// Or with custom options
customOpts := markdown.DefaultOptions().WithFormat(markdown.FormatMarkdown)
adapter := markdown.NewConverterAdapterWithOptions(customOpts)
result, err := adapter.ToMarkdown(ctx, cfg)

Supported Formats

  • Markdown - Rich terminal-rendered markdown with themes
  • JSON - Structured JSON output
  • YAML - Human-readable YAML format

Template System

The package supports pluggable Go text/template templates:

  • Built-in templates in internal/templates/
  • Custom template registration via RegisterCustomTemplate()
  • Template loading via LoadBuiltinTemplate()

Migration from Old API

The old converter.MarkdownConverter is now deprecated. Use the new API:

// Old way (deprecated)
converter := converter.NewMarkdownConverter()
result, err := converter.ToMarkdown(ctx, cfg)

// New way
generator := markdown.NewMarkdownGenerator()
opts := markdown.DefaultOptions()
result, err := generator.Generate(ctx, cfg, opts)

// Or use adapter for drop-in replacement
adapter := markdown.NewConverterAdapter()
result, err := adapter.ToMarkdown(ctx, cfg)

Testing

The package includes comprehensive tests:

go test ./internal/markdown/...

Future Enhancements

  • Template embedding from internal/templates/
  • Section-specific filtering
  • Theme customization
  • Performance optimizations
  • Additional output formats

Documentation

Overview

Package markdown provides advanced formatting and content enrichment for markdown generation.

Package markdown provides an extended API for generating markdown documentation from OPNsense configurations with configurable options.

Package markdown provides an extended API for generating markdown documentation from OPNsense configurations with configurable options.

Package markdown provides an extended API for generating markdown documentation from OPNsense configurations with configurable options.

Index

Constants

View Source
const (
	// FormatMarkdown represents markdown output format.
	FormatMarkdown = converter.FormatMarkdown
	// FormatJSON represents JSON output format.
	FormatJSON = converter.FormatJSON
	// FormatYAML represents YAML output format.
	FormatYAML = converter.FormatYAML
)
View Source
const (
	// ThemeAuto automatically detects the appropriate theme.
	ThemeAuto = converter.ThemeAuto
	// ThemeDark uses a dark terminal theme.
	ThemeDark = converter.ThemeDark
	// ThemeLight uses a light terminal theme.
	ThemeLight = converter.ThemeLight
	// ThemeNone disables styling for plain text output.
	ThemeNone = converter.ThemeNone
)

Variables

View Source
var (
	// ErrUnsupportedFormat is returned when an unsupported output format is requested.
	ErrUnsupportedFormat = converter.ErrUnsupportedFormat

	// ErrNilConfiguration is returned when the input OPNsense configuration is nil.
	ErrNilConfiguration = converter.ErrNilConfiguration
)
View Source
var ErrInvalidWrapWidth = converter.ErrInvalidWrapWidth

ErrInvalidWrapWidth indicates that the wrap width setting is invalid.

Functions

func CodeBlock

func CodeBlock(language, content string) string

CodeBlock returns the given content wrapped in a fenced markdown code block, using the specified language for syntax highlighting. If language is empty, "text" is used as the default.

func FormatBoolean

func FormatBoolean(value any) string

FormatBoolean formats a boolean value consistently using markdown checkboxes.

func FormatBooleanWithUnset

func FormatBooleanWithUnset(value any) string

FormatBooleanWithUnset formats a boolean value, showing "unset" for -1 values.

func FormatUnixTimestamp

func FormatUnixTimestamp(timestamp string) string

FormatUnixTimestamp converts a Unix timestamp string to an ISO 8601 formatted date.

func FormatValue

func FormatValue(key string, value any) string

FormatValue automatically detects the type of value and formats it appropriately. FormatValue returns a markdown-formatted string representation of the given value, choosing an appropriate format based on its type. Slices of structs are rendered as tables, other slices as lists, structs and maps as key-value pairs, and scalars as inline text or code blocks. Nil values are represented as a code block containing "nil".

func GetPowerModeDescription

func GetPowerModeDescription(mode string) string

GetPowerModeDescription converts power management mode acronyms to their full descriptions.

func IsTruthy

func IsTruthy(value any) bool

IsTruthy determines if a value represents a "true" or "enabled" state. Handles various formats: "1", "yes", "true", "on", "enabled", etc. Treats -1 as "unset" and returns false for it.

func RenderBadge

func RenderBadge(badge Badge) string

RenderBadge returns a markdown-formatted string representing the given badge, combining its icon and bolded text.

func RenderMarkdown

func RenderMarkdown(content string) (string, error)

RenderMarkdown parses and renders markdown content to HTML using goldmark. Returns the rendered HTML string or an error if rendering fails.

func Table

func Table(headers []string, rows [][]string) string

Table returns a markdown-formatted table using the provided headers and rows. If headers or rows are empty, it returns a message indicating no data is available.

func ValidateMarkdown

func ValidateMarkdown(content string) error

ValidateMarkdown checks if the provided markdown content is syntactically valid. It returns an error if the content cannot be parsed as markdown.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter provides a simplified interface for generating documentation.

func NewAdapter

func NewAdapter() (*Adapter, error)

NewAdapter creates a new adapter with the default markdown generator.

func (*Adapter) GenerateJSON

func (a *Adapter) GenerateJSON(ctx context.Context, cfg *model.OpnSenseDocument) (string, error)

GenerateJSON generates JSON documentation from an OPNsense configuration.

func (*Adapter) GenerateMarkdown

func (a *Adapter) GenerateMarkdown(
	ctx context.Context,
	cfg *model.OpnSenseDocument,
	comprehensive bool,
) (string, error)

GenerateMarkdown generates markdown documentation from an OPNsense configuration.

func (*Adapter) GenerateYAML

func (a *Adapter) GenerateYAML(ctx context.Context, cfg *model.OpnSenseDocument) (string, error)

GenerateYAML generates YAML documentation from an OPNsense configuration.

type Badge

type Badge struct {
	Icon    string
	Text    string
	Color   string
	BGColor string
}

Badge represents a colorized status badge with emoji and text.

func BadgeEnhanced

func BadgeEnhanced() Badge

BadgeEnhanced returns a Badge representing an enhanced status with a sparkle emoji and purple color scheme.

func BadgeFail

func BadgeFail() Badge

BadgeFail returns a Badge representing a failure status with a red icon and background.

func BadgeInfo

func BadgeInfo() Badge

BadgeInfo returns a Badge representing informational status with an info icon and blue color scheme.

func BadgeInsecure

func BadgeInsecure() Badge

BadgeInsecure returns a Badge representing an insecure status with a red color scheme and unlocked icon.

func BadgeSecure

func BadgeSecure() Badge

BadgeSecure returns a Badge representing a secure status with a lock icon and green color scheme.

func BadgeSuccess

func BadgeSuccess() Badge

BadgeSuccess returns a Badge representing a successful status with a checkmark icon and green colors.

func BadgeWarning

func BadgeWarning() Badge

BadgeWarning returns a Badge representing a warning status with a warning icon and yellow color scheme.

func InfoBadge

func InfoBadge() Badge

InfoBadge returns a badge indicating informational status.

func SecurityBadge

func SecurityBadge(secure, enhanced bool) Badge

SecurityBadge selects and returns a badge representing the security status. Returns an enhanced badge if enhanced is true, a secure badge if secure is true, or an insecure badge otherwise.

func StatusBadge

func StatusBadge(success bool) Badge

StatusBadge returns a success badge if success is true, or a failure badge otherwise.

func WarningBadge

func WarningBadge() Badge

WarningBadge returns a Badge representing a warning status.

type Format

type Format = converter.Format

Format represents the output format type.

type Generator

type Generator = converter.Generator

Generator interface for creating documentation in various formats.

func NewMarkdownGenerator deprecated

func NewMarkdownGenerator(logger *log.Logger, opts Options) (Generator, error)

NewMarkdownGenerator creates a new Generator that produces documentation in Markdown, JSON, or YAML formats.

Deprecated: use converter.NewMarkdownGenerator instead.

type HybridGenerator added in v1.1.0

type HybridGenerator = converter.HybridGenerator

HybridGenerator provides programmatic markdown, JSON, and YAML generation.

func NewHybridGenerator deprecated added in v1.1.0

func NewHybridGenerator(reportBuilder builder.ReportBuilder, logger *log.Logger) (*HybridGenerator, error)

NewHybridGenerator creates a new HybridGenerator with the specified builder.

Deprecated: use converter.NewHybridGenerator instead.

type Options

type Options = converter.Options

Options contains configuration options for markdown generation.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns an Options value initialized with package defaults for markdown generation. The returned Options contains sensible defaults for formatting, wrap width, and theme selection used by the markdown package.

type ReportBuilder added in v1.1.0

type ReportBuilder = builder.ReportBuilder

ReportBuilder defines the interface for building markdown reports.

type Theme

type Theme = converter.Theme

Theme represents the rendering theme for terminal output.

Jump to

Keyboard shortcuts

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