Documentation
¶
Index ¶
- func DefaultHTMLTemplate() string
- func FormatNames() []string
- func RegisterFormatter(name string, formatter Formatter)
- type AlignedStatement
- type CalcMarkFormatter
- type Formatter
- type HTMLFormatter
- type JSONBlock
- type JSONConvertTo
- type JSONDiagnostic
- type JSONDocument
- type JSONFormatter
- type JSONFrontmatter
- type JSONResult
- type JSONScale
- type MarkdownFormatter
- type Options
- type TemplateBlock
- type TemplateExchange
- type TemplateFrontmatter
- type TemplateGlobal
- type TemplateLine
- type TextFormatter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultHTMLTemplate ¶
func DefaultHTMLTemplate() string
DefaultHTMLTemplate returns the embedded default HTML template. Use this to inspect the template data model or as a starting point for custom templates.
func FormatNames ¶
func FormatNames() []string
FormatNames returns a sorted list of registered format names.
func RegisterFormatter ¶
RegisterFormatter adds a custom formatter to the registry. This allows third-party formatters to be registered at runtime.
Types ¶
type AlignedStatement ¶
type AlignedStatement struct {
// Source is the original source line text.
Source string
// Result is the raw, unformatted evaluation result.
// Nil if this line has no result (blank, result-comment, etc.).
Result types.Type
// Variable is the name assigned by this statement (e.g., "x" from "x = 10").
// Empty for anonymous expressions.
Variable string
// IsBlank is true for empty/whitespace-only source lines.
IsBlank bool
// IsResultLine is true for previous result comments (e.g., "# = 42", "→ 42").
// Formatters should typically skip these.
IsResultLine bool
}
AlignedStatement maps a source line to its evaluated result. This is the shared logic extracted from all formatters to prevent the recurring source-line/result-index alignment bugs.
func AlignResults ¶
func AlignResults(block *document.CalcBlock) []AlignedStatement
AlignResults maps source lines of a calc block to their corresponding evaluation results. It handles the shared alignment logic that was previously duplicated across all four formatters:
- Result lines (from previous saves) are marked but not assigned results
- Blank lines are marked but not assigned results
- Non-blank, non-result lines consume the next result from block.Results()
- Variable names are extracted from assignment expressions
type CalcMarkFormatter ¶
type CalcMarkFormatter struct{}
CalcMarkFormatter formats CalcMark documents back to CalcMark format. Useful for round-tripping and saving documents with frontmatter.
func (*CalcMarkFormatter) Extensions ¶
func (f *CalcMarkFormatter) Extensions() []string
Extensions returns the file extensions handled by this formatter.
type Formatter ¶
type Formatter interface {
// Format writes the formatted document to the writer
Format(w io.Writer, doc *document.Document, opts Options) error
// Extensions returns file extensions this formatter handles
Extensions() []string
}
Formatter formats CalcMark documents for output. All formatters must implement this interface.
func GetFormatter ¶
GetFormatter returns the appropriate formatter based on format name or filename extension. If format is specified, it takes precedence. Otherwise, the filename extension is used. Falls back to text formatter if no match is found.
type HTMLFormatter ¶
type HTMLFormatter struct{}
HTMLFormatter formats CalcMark documents as HTML. Uses an embedded template with modern styling.
func (*HTMLFormatter) Extensions ¶
func (f *HTMLFormatter) Extensions() []string
Extensions returns the file extensions handled by this formatter.
type JSONBlock ¶
type JSONBlock struct {
Type string `json:"type"`
Source []string `json:"source"`
Results []JSONResult `json:"results,omitempty"`
Error string `json:"error,omitempty"`
Diagnostics []JSONDiagnostic `json:"diagnostics,omitempty"`
HTML string `json:"html,omitempty"`
}
JSONBlock represents a single block in JSON output.
type JSONConvertTo ¶ added in v1.6.2
type JSONConvertTo struct {
System string `json:"system"`
UnitCategories []string `json:"unit_categories,omitempty"`
}
JSONConvertTo represents convert_to config in JSON output
type JSONDiagnostic ¶
type JSONDiagnostic struct {
Severity string `json:"severity"`
Code string `json:"code,omitempty"`
Message string `json:"message"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}
JSONDiagnostic represents an error or warning with position info.
type JSONDocument ¶
type JSONDocument struct {
Frontmatter *JSONFrontmatter `json:"frontmatter,omitempty"`
Blocks []JSONBlock `json:"blocks"`
}
JSONDocument represents the full document in JSON output
type JSONFormatter ¶
type JSONFormatter struct{}
JSONFormatter formats CalcMark documents as JSON. Useful for programmatic consumption and integration with other tools.
func (*JSONFormatter) Extensions ¶
func (f *JSONFormatter) Extensions() []string
Extensions returns the file extensions handled by this formatter.
type JSONFrontmatter ¶
type JSONFrontmatter struct {
Globals map[string]string `json:"globals,omitempty"`
Exchange map[string]string `json:"exchange,omitempty"`
Scale *JSONScale `json:"scale,omitempty"`
ConvertTo *JSONConvertTo `json:"convert_to,omitempty"`
}
JSONFrontmatter represents frontmatter in JSON output
type JSONResult ¶
type JSONResult struct {
Source string `json:"source"`
Value string `json:"value,omitempty"`
Type string `json:"type,omitempty"`
NumericValue *float64 `json:"numeric_value,omitempty"`
Unit string `json:"unit,omitempty"`
DateValue string `json:"date_value,omitempty"`
IsApproximate bool `json:"is_approximate,omitempty"`
IsExplicit bool `json:"is_explicit,omitempty"`
Error string `json:"error,omitempty"`
Variable string `json:"variable,omitempty"`
}
JSONResult represents a single evaluated statement's result. Value is the locale-formatted display string. Type is the CalcMark type name (e.g., "number", "currency"). NumericValue, Unit, DateValue decompose the value for programmatic consumption.
type JSONScale ¶ added in v1.6.2
type JSONScale struct {
Factor float64 `json:"factor"`
UnitCategories []string `json:"unit_categories,omitempty"`
}
JSONScale represents scale config in JSON output
type MarkdownFormatter ¶
type MarkdownFormatter struct{}
MarkdownFormatter formats CalcMark documents as Markdown. Calculation blocks are shown in code fences with results.
func (*MarkdownFormatter) Extensions ¶
func (f *MarkdownFormatter) Extensions() []string
Extensions returns the file extensions handled by this formatter.
type Options ¶
type Options struct {
Verbose bool // Show calculation steps, types, units
IncludeErrors bool // Include error details
Template string // For template-based formatters (future use)
// DisplayFormatter is the locale-aware formatter for rendering values.
// When zero-value, formatters fall back to display.Format() (en-US default).
DisplayFormatter display.Formatter
}
Options controls formatter behavior
type TemplateBlock ¶
type TemplateBlock struct {
Type string
SourceLines []TemplateLine // For calc blocks with per-line results
Error string
HTML template.HTML // For text blocks
}
TemplateBlock represents a block for template rendering
type TemplateExchange ¶
TemplateExchange represents an exchange rate for template rendering
type TemplateFrontmatter ¶
type TemplateFrontmatter struct {
Globals []TemplateGlobal
Exchange []TemplateExchange
Scale string // e.g. "2x" or "0.5x [Length, Mass]"
ConvertTo string // e.g. "imperial" or "si [Length]"
}
TemplateFrontmatter represents frontmatter for template rendering
type TemplateGlobal ¶
TemplateGlobal represents a global variable for template rendering
type TemplateLine ¶
TemplateLine represents a single source line with its result
type TextFormatter ¶
type TextFormatter struct{}
TextFormatter formats CalcMark documents as plain text. This is the primary formatter for interactive use (REPL, CLI).
func (*TextFormatter) Extensions ¶
func (f *TextFormatter) Extensions() []string
Extensions returns the file extensions handled by this formatter.