a4code

package
v0.0.197 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

README

a4code

a4code is a custom markup language that uses a Lisp-style syntax with square brackets.

Syntax

The general syntax is [tag argument content].

  • No closing tags: Unlike HTML or BBCode, a4code does not use closing tags like [/tag]. Instead, the scope of a tag is determined by the nesting of brackets [...].
  • Arguments: Tags can take arguments separated by spaces.
  • Content: The content follows the arguments and is closed by the matching ].
Examples
  • Link: [link http://example.com Link Title]
  • Bold: [b This is bold]
  • Quote: [quote This is a quote]
  • Nested: [quote [b Bold inside quote]]

Forbidden Syntax

  • Closing tags (e.g., [/b], [/link]) are invalid and will trigger a parser error.
  • Assignment style (e.g., [link=http://example.com]) is deprecated/incorrect for this parser version in favor of space-separated arguments.

Code Blocks

Code blocks are an exception and may use specific delimiters defined by the parser, but standard usage should follow the bracket structure where possible or use the specific [code]...[/code] legacy support if enabled, though [code ... ] is preferred if content permits.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConsumeCodeBlock

func ConsumeCodeBlock(s ScannerInterface) (string, error)

ConsumeCodeBlock consumes content bytes until the terminator ']' is found at the top level. It supports escaping characters with backslash. It returns the content string and error.

func GetNext

func GetNext(s ScannerInterface, endAtEqual bool) (string, error)

GetNext reads the next token from the scanner.

func GetNextArg

func GetNextArg(s ScannerInterface) (string, error)

GetNextArg reads the next argument from the scanner, handling optional quotes.

func Parse

func Parse(r io.Reader) (*ast.Root, error)

Parse reads markup from r and returns the root node.

func ParseNodes

func ParseNodes(s string) ([]ast.Node, error)

ParseNodes parses s and returns only the top-level nodes.

func ParseNodesReader

func ParseNodesReader(r io.Reader) ([]ast.Node, error)

ParseNodesReader parses r and returns only the top-level nodes.

func ParseString

func ParseString(s string) (*ast.Root, error)

ParseString parses markup from s and returns the root node.

func QuoteText

func QuoteText(username, text string, opts ...QuoteOption) string

QuoteText wraps the provided text in quote markup for the given user. Behaviour can be customised through QuoteOption values.

func SanitizeURL

func SanitizeURL(raw string) (string, bool)

SanitizeURL validates a hyperlink and returns a safe version.

func Snip

func Snip(s string, l int) string

func SnipText

func SnipText(s string, l int) string

func SnipTextWords

func SnipTextWords(s string, count int) string

func SnipWords

func SnipWords(s string, count int) string

func Stream

func Stream(r io.Reader, opts ...StreamOption) iter.Seq[ast.Node]

Stream parses markup from r and yields nodes according to the provided options.

func Substring

func Substring(s string, start, end int) (string, error)

Substring extracts a substring based on visible text length, preserving markup. start and end are indices into the *visible text*.

func ToA4Code

func ToA4Code(n ast.Node) string

ToA4Code serializes the node tree back into A4code markup.

func ToCleanText

func ToCleanText(n ast.Node) string

ToCleanText converts the AST to plain text, stripping all markup and prefixes.

func ToCode

func ToCode(n ast.Node) string

ToCode converts the AST back to a4code markup string.

func ToHTML

func ToHTML(n ast.Node) string

ToHTML converts a node tree to HTML.

func ToText

func ToText(n ast.Node) string

ToText converts the AST to plain text, stripping all markup.

Types

type QuoteOption

type QuoteOption any

QuoteOption configures behaviour of Quote.

func WithFullQuote

func WithFullQuote() QuoteOption

WithFullQuote is a backward-compatible alias for paragraph-aware quoting. Deprecated: use WithParagraphQuote instead.

func WithParagraphQuote

func WithParagraphQuote() QuoteOption

WithParagraphQuote enables paragraph aware quoting.

func WithRestrictedQuoteDepth

func WithRestrictedQuoteDepth(depth int) QuoteOption

WithRestrictedQuoteDepth sets the depth at which quotes are removed.

func WithTrimSpace

func WithTrimSpace() QuoteOption

WithTrimSpace removes surrounding whitespace from the quoted text.

func WithTruncatedQuoteDepth

func WithTruncatedQuoteDepth(depth int) QuoteOption

WithTruncatedQuoteDepth sets the depth at which quote content is removed.

type RestrictedQuoteDepth

type RestrictedQuoteDepth int

RestrictedQuoteDepth is the depth at which quotes are removed.

type ScannerInterface

type ScannerInterface interface {
	ReadByte() (byte, error)
	UnreadByte() error
}

ScannerInterface abstracts the byte reading methods used by consumeCodeBlock. This allows both bufio.Reader (in a4code2html) and the custom scanner (in parser.go) to be used.

type StreamOption

type StreamOption func(*streamOptions)

StreamOption configures Stream behaviour.

func WithAllNodes

func WithAllNodes() StreamOption

WithAllNodes yields every node encountered while parsing.

func WithDepth

func WithDepth(d int) StreamOption

WithDepth limits yielded nodes to the specified depth, where 0 is top level and -1 yields all nodes.

type TruncatedQuoteDepth

type TruncatedQuoteDepth int

TruncatedQuoteDepth is the depth at which quote content is removed.

Directories

Path Synopsis
Package a4code2html converts a small markup language into HTML or alternative formats.
Package a4code2html converts a small markup language into HTML or alternative formats.

Jump to

Keyboard shortcuts

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