Documentation
¶
Overview ¶
Package code provides syntax-highlighted source code widgets.
Highlighting is performed server-side via Chroma (Apache 2.0). The generated markup uses CSS classes that map to Material design color tokens, so themes switch with the rest of the page without re-rendering.
This package is not part of the default widget library because Chroma's lexer set adds several MB to the binary. Import it explicitly:
import "github.com/microbus-io/bespa/code"
See ATTRIBUTIONS.md for upstream licensing.
Index ¶
- Variables
- type BytesWidget
- type CodeBlockWidget
- func (wgt *CodeBlockWidget) Draw(w io.Writer, r *http.Request) (err error)
- func (wgt *CodeBlockWidget) WithFrame(on bool) *CodeBlockWidget
- func (wgt *CodeBlockWidget) WithLanguage(name string) *CodeBlockWidget
- func (wgt *CodeBlockWidget) WithLineNumbers(on bool) *CodeBlockWidget
- func (wgt *CodeBlockWidget) WithMaxHeight(css string) *CodeBlockWidget
- func (wgt *CodeBlockWidget) WithMaxRows(rows int) *CodeBlockWidget
- type CodeFactory
- type InputWidget
- type Widget
Constants ¶
This section is empty.
Variables ¶
var Any = factory.Any
Function aliases
var Bytes = factory.Bytes
var HTML = factory.HTML
var HTMLUnsafe = factory.HTMLUnsafe
var Many = factory.Many
var Tag = factory.Tag
var Text = factory.Text
Functions ¶
This section is empty.
Types ¶
type BytesWidget ¶
type BytesWidget = widget.BytesWidget
type CodeBlockWidget ¶
type CodeBlockWidget struct {
*widget.WidgetBase[*CodeBlockWidget]
// contains filtered or unexported fields
}
CodeBlockWidget renders a block of source code with syntax highlighting.
func (*CodeBlockWidget) WithFrame ¶
func (wgt *CodeBlockWidget) WithFrame(on bool) *CodeBlockWidget
WithFrame toggles the surrounding border and tinted background. On by default. Pass false for a bare, transparent code block — useful when embedding inside a Card or other container that already provides the frame.
func (*CodeBlockWidget) WithLanguage ¶
func (wgt *CodeBlockWidget) WithLanguage(name string) *CodeBlockWidget
WithLanguage sets the language used to tokenize the code. Use any name recognized by Chroma (e.g. "go", "javascript", "python", "rust", "sql", "json", "yaml", "html", "css", "bash"). The full list lives at https://github.com/alecthomas/chroma/tree/master/lexers/embedded. An empty string falls back to auto-detection.
func (*CodeBlockWidget) WithLineNumbers ¶
func (wgt *CodeBlockWidget) WithLineNumbers(on bool) *CodeBlockWidget
WithLineNumbers toggles line-number rendering in the left gutter. Off by default.
func (*CodeBlockWidget) WithMaxHeight ¶
func (wgt *CodeBlockWidget) WithMaxHeight(css string) *CodeBlockWidget
WithMaxHeight caps the visible height of the code block at the given CSS length, e.g. "300px", "50vh" or "calc(100vh - 50px)". Content beyond the cap scrolls vertically. Empty removes the cap.
func (*CodeBlockWidget) WithMaxRows ¶
func (wgt *CodeBlockWidget) WithMaxRows(rows int) *CodeBlockWidget
WithMaxRows caps the visible height of the code block at the given number of lines. Content beyond that scrolls vertically. Set to 0 to remove the cap.
type CodeFactory ¶
type CodeFactory struct{}
CodeFactory aggregates the widget constructors of this package.
func (CodeFactory) CodeBlock ¶
func (f CodeFactory) CodeBlock(code string) *CodeBlockWidget
CodeBlock creates a new widget that renders a syntax-highlighted code block. Highlighting runs server-side on every Draw; for cheap, plain <pre><code> output without highlighting use basic.PlainCodeBlock. Without WithLanguage the lexer is auto-detected from the code; if detection fails, the code renders unhighlighted.
type InputWidget ¶
type InputWidget = widget.InputWidget