gensection

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package gensection provides a reusable engine for marker-based generated-section rules. A Directive implementation supplies rule-specific validation and content generation, while the Engine handles marker parsing, YAML extraction, content comparison, and fix (replacement) logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectIgnoredLines

func CollectIgnoredLines(f *lint.File, startPrefix, endMarker string) map[int]bool

CollectIgnoredLines returns a set of 1-based line numbers inside fenced code blocks or HTML blocks, where markers should be ignored.

func EnsureTrailingNewline

func EnsureTrailingNewline(s string) string

EnsureTrailingNewline appends \n if s does not already end with \n.

func ExtractColumnsRaw

func ExtractColumnsRaw(rawMap map[string]any) map[string]any

ExtractColumnsRaw removes and returns the "columns" key from rawMap.

func ExtractContent

func ExtractContent(f *lint.File, mp MarkerPair) string

ExtractContent returns the content between markers as a string.

func MakeDiag

func MakeDiag(ruleID, ruleName, filePath string, line int, message string) lint.Diagnostic

MakeDiag creates an error diagnostic at the given line using the provided rule ID and name.

func ParseColumnConfig

func ParseColumnConfig(raw map[string]any) map[string]ColumnConfig

ParseColumnConfig parses the raw YAML columns map into ColumnConfig entries.

func ParseYAMLBody

func ParseYAMLBody(
	filePath string, mp MarkerPair, ruleID, ruleName string,
) (map[string]any, []lint.Diagnostic)

ParseYAMLBody unmarshals the YAML body of a marker pair.

func ReplaceContent

func ReplaceContent(f *lint.File, mp MarkerPair, content string) []byte

ReplaceContent replaces the content between markers with new content.

func SplitLines

func SplitLines(source []byte) [][]byte

SplitLines splits source into lines (like bytes.Split but returns [][]byte).

func ValidateStringParams

func ValidateStringParams(
	filePath string, line int, rawMap map[string]any, ruleID, ruleName string,
) (map[string]string, []lint.Diagnostic)

ValidateStringParams checks that all values in rawMap are strings.

Types

type ColumnConfig

type ColumnConfig struct {
	MaxWidth int    // maximum width for the column content
	Wrap     string // "truncate" (default) or "br"
}

ColumnConfig holds per-column width and wrapping configuration.

type Directive

type Directive interface {
	// Name returns the directive/rule name used in markers
	// (e.g., "catalog"). Markers are derived as:
	//   start: "<!-- " + Name()
	//   end:   "<!-- /" + Name() + " -->"
	Name() string

	// RuleID returns the lint rule ID (e.g., "MDS019").
	RuleID() string

	// RuleName returns the lint rule name (e.g., "catalog").
	RuleName() string

	// Validate checks directive-specific parameters. Returns
	// diagnostics for invalid params.
	Validate(filePath string, line int, params map[string]string,
		columns map[string]ColumnConfig) []lint.Diagnostic

	// Generate produces the expected content between markers.
	// Returns content and any diagnostics.
	Generate(f *lint.File, filePath string, line int,
		params map[string]string,
		columns map[string]ColumnConfig) (string, []lint.Diagnostic)
}

Directive defines a generated-section rule that produces content from markers.

type Engine

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

Engine orchestrates Check/Fix using a registered Directive.

func NewEngine

func NewEngine(d Directive) *Engine

NewEngine creates a new Engine for the given Directive.

func (*Engine) Check

func (e *Engine) Check(f *lint.File) []lint.Diagnostic

Check scans the file for marker pairs and returns diagnostics.

func (*Engine) Fix

func (e *Engine) Fix(f *lint.File) []byte

Fix regenerates content for all marker pairs.

type MarkerPair

type MarkerPair struct {
	StartLine   int // 1-based line of start marker
	EndLine     int // 1-based line of end marker
	ContentFrom int // 1-based line of the first content line
	ContentTo   int // 1-based line of the last content line
	FirstLine   string
	YAMLBody    string
}

MarkerPair holds the line numbers and parsed content of a start/end marker pair.

func FindMarkerPairs

func FindMarkerPairs(
	f *lint.File,
	startPrefix, endMarker, ruleID, ruleName string,
) ([]MarkerPair, []lint.Diagnostic)

FindMarkerPairs scans the file for start/end marker pairs, skipping markers inside code blocks or HTML blocks. The startPrefix and endMarker are derived from the directive name.

type ParsedDirective

type ParsedDirective struct {
	Name    string
	Params  map[string]string
	Columns map[string]ColumnConfig
}

ParsedDirective holds the parsed directive from a marker pair.

func ParseDirective

func ParseDirective(
	filePath string, mp MarkerPair, ruleID, ruleName string,
) (*ParsedDirective, []lint.Diagnostic)

ParseDirective extracts the YAML parameters from a marker pair.

Jump to

Keyboard shortcuts

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