macros

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package macros holds the sysml-* AsciiDoc macros (requirements MC-1..21), which put facts from a SysML v2 model into AsciiDoc text: names, docs, members, metadata, evaluated values. Each macro lives in its own file; Register adds them all to an asciidoctorg instance.

The package is usable from other modules. It reads the model through package sysml, and links to the host's pages through Pages:

m, err := sysml.Open(ctx, client, parsed) // an OpenSysML-parsed model
ad, err := asciidoctor.New()
r, err := macros.Register(ctx, ad, m, pages, warn)
err = r.Inline("my-macro", func(c *macros.Call) (string, error) { ... })
html, err := ad.Convert(body, &asciidoctor.Options{Attributes: macros.Attributes(elementID)})

Macros of the caller's own (Registry.Inline, Registry.Block) get a Call: the page's element, the target, name lookup, links, evaluation and warnings, the way the sysml-* macros have them; an error they return leaves a placeholder and a warning instead of stopping the conversion.

Example

Using the macros from another module: parse a model with OpenSysML, open it with sysml.Open, register the macros with the host's pages, and convert AsciiDoc for an element. A failing macro leaves a placeholder and a warning.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Open-MBEE/OpenSysML/client/opensysml"
	asciidoctor "github.com/rolling-robot/asciidoctorg"
	"github.com/rolling-robot/sysml-publisher/macros"
	"github.com/rolling-robot/sysml-publisher/sysml"
)

// pages is a host's pages: the file of each element that has one, by id.
type pages map[string]string

func (p pages) Href(from, to string) (string, bool) {
	href, ok := p[to]
	return href, ok
}

func main() {
	ctx := context.Background()
	client, err := opensysml.New()
	if err != nil {
		log.Fatal(err)
	}
	parsed, err := client.ParseFiles(ctx, []string{"../testdata/external/vehicles.sysml"})
	if err != nil {
		log.Fatal(err)
	}
	m, err := sysml.Open(ctx, client, parsed)
	if err != nil {
		log.Fatal(err)
	}
	defer m.Close()

	// The host decides which elements have pages, by id.
	car, err := m.Props(ctx, "Vehicles::Car", "name")
	if err != nil {
		log.Fatal(err)
	}
	wheel, err := m.Props(ctx, "Vehicles::Wheel", "name")
	if err != nil {
		log.Fatal(err)
	}
	site := pages{car.Element.ID: "car.html", wheel.Element.ID: "wheel.html"}

	ad, err := asciidoctor.New()
	if err != nil {
		log.Fatal(err)
	}
	defer ad.Close()
	warn := func(w macros.Warning) { fmt.Println("warning:", w) }
	if _, err := macros.Register(ctx, ad, m, site, warn); err != nil {
		log.Fatal(err)
	}

	src := "sysml-name:[] weighs sysml-value:mass[], on sysml-name:Wheel[]s.\n\n" +
		"sysml-members-list::Wheel[]\n\n" +
		"Missing: sysml-name:Boat[]."
	out, err := ad.Convert(src, &asciidoctor.Options{Attributes: macros.Attributes(car.Element.ID)})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(out)
}
Output:
warning: Vehicles::Car: sysml-name:Boat[]: the model has no element named "Boat"
<div class="paragraph">
<p>Car weighs 1200, on <a href="wheel.html">Wheel</a>s.</p>
</div>
<div class="ulist sysml-members-list">
<ul>
<li>
<p>front</p>
</li>
<li>
<p>rear</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Missing: <span class="sysml-failed">sysml-name:Boat[]: the model has no element named &#34;Boat&#34;</span>.</p>
</div>

Index

Examples

Constants

View Source
const AttrElement = "sysml-element"

AttrElement is the document attribute naming the page's element by id, set by the renderer on every representation it converts (MC-5).

View Source
const AttrLink = "sysml-link"

AttrLink is the attribute of the anchor nodes macros link with (MC-18): the id of the element whose page the link goes to. A link between pages is an anchor node of type link, converted by the document's converter like any link: html5 writes <a href>, and a converter for another output can tell these links by AttrLink and write its own (Confluence's <ac:link>).

View Source
const AttrView = "sysml-view"

AttrView is the attribute of the block a view is put in (MC-21): the view's qualified name. As links are anchor nodes (AttrLink), a view is an ordinary node the document's converter writes: a diagram an image block, whose target is the SVG as a data: URI, a table view a table. html5 writes the diagram as <img src="data:...">; a converter for another output can tell the block by AttrView and write the picture its own way (Confluence's <ac:image> with an attachment), taking the SVG from ViewSVG.

View Source
const Catalog = "sysml-catalog"

Catalog is the block macro sysml-catalog::[], replaced, on a view's page, by a table of that view's exposed elements: one row per element of the exposed tree that carries the view's filter metadata, with a Name column and one column per metadata def attribute (MC-24). The macro takes no target or attributes: the exposed root and the filter come solely from the page's view element, so the user never re-states them.

View Source
const CatalogRole = "sysml-catalog"

CatalogRole is the role of the block holding the catalog table, for styling.

View Source
const CompositeText = "[composite]"

CompositeText is shown for an attribute whose value is composite: one made of attributes of its own, which has no single value to show.

View Source
const Doc = "sysml-doc"

Doc is the block macro sysml-doc::[<element>][], replaced by the doc comments of the page's element, or of the element the target names (read as sysml-name reads it), each parsed as AsciiDoc (MC-10, MC-15). Macros in them still describe the page's element.

View Source
const DocRole = "sysml-doc"

DocRole is the role of the block the documentation is put in, for styling.

View Source
const FailedRole = "sysml-failed"

FailedRole is the class of the placeholder a failing macro leaves on the page (MC-19), for styling.

View Source
const IfaceEnds = "sysml-iface-ends"

IfaceEnds is the block macro sysml-iface-ends::[<element>][], replaced by a bullet list of the ends of an interface (or any connection): each end's feature, engine.air, its segments linked to their pages (MC-17). The source end comes first when there is one. Without a target the page's element is used; a target is read as sysml-name reads it (MC-8, MC-15). The attribute depth (named, or the first positional one) shows the last depth segments of the feature's qualified path, joined by " :: " (1: air, 2: engine :: air), all the whole path (Model :: aircraft :: engine :: air). The attribute name says how each segment is named: short (the default) uses the element's short name when it has one, falling back to its name; long always uses the name. Names are shown without single quotes.

View Source
const IfaceEndsRole = "sysml-iface-ends"

IfaceEndsRole is the role of the list, for styling.

View Source
const Ifaces = "sysml-ifaces"

Ifaces is the block macro sysml-ifaces::[<element>][], replaced by a bullet list of the interfaces and connections the target's owned ports are on (MC-23): each item one connection, by name, -> , and the distinct part(s) on the other ends. Without a target the page's element is used; a target is read as sysml-name reads it (MC-8, MC-15). The model is searched once, a reverse index of ports to connections built on first use and shared by every page (MC-3). The attribute name says how the connection and the partner parts are named, as sysml-iface-ends' name does: short (the default) uses each element's short name when it has one, else its name; long always the name.

View Source
const IfacesRole = "sysml-ifaces"

IfacesRole is the role of the list, for styling.

View Source
const MembersList = "sysml-members-list"

MembersList is the block macro sysml-members-list::[<element or kind>][], replaced by a bullet list of the owned members of the page's element (MC-12). Without a target every member is listed, each with its types in parentheses; with an element, only the members typed by, subclassing or subsetting it; with an element kind (partUsage), only members of that kind.

View Source
const MembersListRole = "sysml-members-list"

MembersListRole is the role of the list, for styling.

View Source
const MetadataTable = "sysml-metadata-table"

MetadataTable is the block macro sysml-metadata-table::<metadata def>[], replaced by a table of the attributes of that metadata on the page's element: each attribute's name and value, the metadata def's value where the element binds none (MC-13).

View Source
const MetadataTableRole = "sysml-metadata-table"

MetadataTableRole is the role of the block holding the tables, for styling.

View Source
const Name = "sysml-name"

Name is the inline macro sysml-name:[<element>], replaced by an element's name (MC-8): the page's element without a target, else the element the target names (MC-15), such as sysml-name:Model::AircraftPart[] or sysml-name:ap[]. A target may also name metadata applied to an element and its attributes: sysml-name:@m[] gives the metadata def's name (m), sysml-name:@m::owner[] the attribute's (owner), for the page's element or, with a path before the @, another one (wing::@m::owner).

View Source
const NoValue = "(no value)"

NoValue is shown for an attribute that has no value.

View Source
const QualifiedName = "sysml-qualified-name"

QualifiedName is the inline macro sysml-qualified-name:[<element>], replaced by the qualified name of the page's element, or of the element the target names, read as sysml-name reads it (MC-9, MC-15).

View Source
const Rep = "sysml-rep"

Rep is the inline macro sysml-rep:[<element>][<language>], replaced by the body of the textual representation in that language that the element owns (MC-16): the page's element without a target, else the element the target names, read as sysml-name reads it (MC-8, MC-15). The body is inserted as plain text, not read as AsciiDoc.

View Source
const ShortName = "sysml-short-name"

ShortName is the inline macro sysml-short-name:[<element>], replaced by the short name of the page's element, or of the element the target names (read as sysml-name reads it), or by nothing when it has none (MC-11, MC-15).

View Source
const Value = "sysml-value"

Value is the inline macro sysml-value:<feature>[], replaced by the value of that feature, units included (MC-14). The target names a feature, not an expression: a feature of the page's element (mass, motor.power), a qualified name (Values::custom::count) or a name relative to the page's element (custom::count). It is evaluated with the page's element as its subject. A target [<element>::]@<metadata>::<attribute> gives the value of an attribute of metadata applied to an element (@Info::owner), read as sysml-metadata-table reads it (MC-14, MC-13).

View Source
const View = "sysml-view"

View is the block macro sysml-view::[<view>][], replaced by a view of the model as OpenSysML renders it (MC-21): a diagram as a picture, a table view as a table. Without a target it shows the page's element, which must then be a view; a target names the view as sysml-name does (MC-15). Views are rendered by the host's Views (Registry.SetViews).

View Source
const ViewRole = "sysml-view"

ViewRole is the role of the block a view is put in, for styling.

Variables

This section is empty.

Functions

func Attributes

func Attributes(id string) asciidoctor.Attributes

Attributes are the document attributes a representation is converted with for the element id: on its page, macros describe that element (MC-2).

func FormatValue

func FormatValue(v opensysml.Value) string

FormatValue is a value as sysml-value shows it (MC-14): a quantity as magnitude and unit symbol (5 kg), a string without quotes, an enumeration literal as written, NoValue for none and CompositeText for a composite value. The text is not escaped.

func Passthrough

func Passthrough(html string) string

Passthrough is finished HTML as AsciiDoc that substitutions leave alone, for list items and table cells, whose text AsciiDoc would otherwise read as markup.

func ViewSVG

func ViewSVG(n asciidoctor.Node) (svg []byte, ok bool)

ViewSVG is the SVG of a diagram sysml-view put in, for a converter that writes it elsewhere than in the page (an attachment): n is the image block, carrying AttrView. ok is false for any other node.

Types

type BlockFunc

type BlockFunc func(c *Call, parent asciidoctor.StructuralNode) (asciidoctor.StructuralNode, error)

BlockFunc makes the block that replaces a block macro, created under parent (asciidoctor.CreateOpenBlock, CreateList, ...), or nil to drop the macro line. An error is the macro failing, as for InlineFunc.

type Call

type Call struct {
	// Page is the element whose page is converted (MC-2).
	Page Element
	// Target is the target as written, unescaped; "" when there is none.
	Target string
	// Macro is the macro as written (my-mass:engine[]), as warnings name it.
	Macro string
	// contains filtered or unexported fields
}

Call is one use of a custom macro in a representation converted for an element: what the macro's function gets.

func (*Call) Attr

func (c *Call) Attr(name string) string

Attr is the attribute name as written (named, or positional when Inline or Block named its position), trimmed; "" when it is not given.

func (*Call) Context

func (c *Call) Context() context.Context

Context is the rendering's context: pass it to model reads, which stop when it is cancelled.

func (*Call) Element

func (c *Call) Element() (Element, error)

Element is the element the macro is about: the page's element without a target, else the element the target names, read as sysml-name reads it (MC-8, MC-15), metadata forms ([<element>::]@<metadata>[::<attribute>]) included.

func (*Call) Evaluate

func (c *Call) Evaluate(subject Element, expression string) (opensysml.Value, error)

Evaluate evaluates expression with element subject as its subject, so a feature reads that element's value (sysml-value, MC-14).

func (c *Call) Link(id, html string) string

Link is html linked to the page of element id from the page being converted (MC-18): unlinked when id has no page, is the page's own element, or html is empty. html must be escaped already. The link is an anchor node with AttrLink, written by the document's converter; when it cannot be converted, html is returned unlinked with a warning.

func (*Call) Model

func (c *Call) Model() *sysml.Model

Model is the model the macros read.

func (*Call) Name

func (c *Call) Name(id string) (string, error)

Name is the element's name as sysml-name shows it, sysml.UnnamedName when it has none.

func (*Call) Parent

func (c *Call) Parent() asciidoctor.Node

Parent is the node the macro is converted in.

func (*Call) Resolve

func (c *Call) Resolve(name string) (Element, error)

Resolve is the element name names, read from the page's element as every macro reads a name (MC-15): a qualified name, a name inside the page's element or an element around it, a library qualified name, or a name only one element has; each segment a name or a short name.

func (*Call) Warn

func (c *Call) Warn(message string)

Warn reports a problem without failing the macro (MC-7): the warning names the page and the macro, so message should not.

type Element

type Element struct {
	ID string
	// QualifiedName is its qualified name, or its positional name (M::@2)
	// when it has no name; the subject Evaluate takes.
	QualifiedName string
}

Element is an element a macro is converted for or names.

type InlineFunc

type InlineFunc func(c *Call) (string, error)

InlineFunc makes the HTML that replaces an inline macro. The HTML is written out as is: escape model text (html.EscapeString). An error is the macro failing: a placeholder takes its place and a warning is reported (MC-19).

type Pages

type Pages interface {
	// Href is the link, relative to the page of element from, to the page
	// of element to (both by id); ok is false when to has no page.
	Href(from, to string) (href string, ok bool)
}

Pages is how the host places elements on pages, for the links macros write (MC-18, MC-12, MC-17). A nil Pages links nothing.

type Registry

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

Registry is the macros registered on one asciidoctorg instance for one rendering, as Register returns it. Inline and Block add macros of the caller's own that work like the sysml-* ones (MC-20): they read the same model, name elements the same way (MC-15), link through the same Pages, and a failing one leaves a placeholder and a warning (MC-19).

func Register

func Register(ctx context.Context, ad *asciidoctor.Asciidoctor, m *sysml.Model, pages Pages, warn Warn) (*Registry, error)

Register adds every sysml-* macro to ad. The macros read the model m (DS-4), link to the pages pages gives, report warnings to warn and stop reading and evaluating when ctx is cancelled. ctx is the context of the rendering ad is registered for; register again for another. m must stay open while ad converts. The Registry adds macros of the caller's own (MC-20).

func (*Registry) Block

func (r *Registry) Block(name string, fn BlockFunc, positional ...string) error

Block adds the block macro name::[<target>][<attributes>], on a line of its own. positional names the positional attributes, in order. The macro is active once Block returns.

func (*Registry) Inline

func (r *Registry) Inline(name string, fn InlineFunc, positional ...string) error

Inline adds the inline macro name:[<target>][<attributes>], whose target is optional and may hold quoted names with spaces, as sysml-name's does. positional names the positional attributes, in order (Attr reads them). The macro is active once Inline returns.

Example

A macro of your own works like the sysml-* ones: Element reads the target as sysml-name does (here the short name W), Link links to the host's pages, FormatValue shows values as sysml-value does, and an error leaves a placeholder and a warning instead of stopping the conversion.

package main

import (
	"context"
	"fmt"
	"html"
	"log"

	"github.com/Open-MBEE/OpenSysML/client/opensysml"
	asciidoctor "github.com/rolling-robot/asciidoctorg"
	"github.com/rolling-robot/sysml-publisher/macros"
	"github.com/rolling-robot/sysml-publisher/sysml"
)

// pages is a host's pages: the file of each element that has one, by id.
type pages map[string]string

func (p pages) Href(from, to string) (string, bool) {
	href, ok := p[to]
	return href, ok
}

func main() {
	ctx := context.Background()
	client, err := opensysml.New()
	if err != nil {
		log.Fatal(err)
	}
	parsed, err := client.ParseFiles(ctx, []string{"../testdata/external/vehicles.sysml"})
	if err != nil {
		log.Fatal(err)
	}
	m, err := sysml.Open(ctx, client, parsed)
	if err != nil {
		log.Fatal(err)
	}
	defer m.Close()
	car, err := m.Props(ctx, "Vehicles::Car", "name")
	if err != nil {
		log.Fatal(err)
	}
	wheel, err := m.Props(ctx, "Vehicles::Wheel", "name")
	if err != nil {
		log.Fatal(err)
	}
	site := pages{car.Element.ID: "car.html", wheel.Element.ID: "wheel.html"}

	ad, err := asciidoctor.New()
	if err != nil {
		log.Fatal(err)
	}
	defer ad.Close()
	r, err := macros.Register(ctx, ad, m, site, func(w macros.Warning) { fmt.Println("warning:", w) })
	if err != nil {
		log.Fatal(err)
	}

	// my-mass:[<element>]: the element's mass, its name linked to its page.
	err = r.Inline("my-mass", func(c *macros.Call) (string, error) {
		el, err := c.Element()
		if err != nil {
			return "", err
		}
		v, err := c.Evaluate(el, "mass")
		if err != nil {
			return "", err
		}
		name, err := c.Name(el.ID)
		if err != nil {
			return "", err
		}
		return c.Link(el.ID, html.EscapeString(name)) + " weighs " + html.EscapeString(macros.FormatValue(v)), nil
	})
	if err != nil {
		log.Fatal(err)
	}

	// On the page of Wheel, so Car's name links to its page.
	out, err := ad.Convert("my-mass:Car[].\n\nmy-mass:Boat[].", &asciidoctor.Options{Attributes: macros.Attributes(wheel.Element.ID)})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(out)
}
Output:
warning: Vehicles::Wheel: my-mass:Boat[]: the model has no element named "Boat"
<div class="paragraph">
<p><a href="car.html">Car</a> weighs 1200.</p>
</div>
<div class="paragraph">
<p><span class="sysml-failed">my-mass:Boat[]: the model has no element named &#34;Boat&#34;</span>.</p>
</div>

func (*Registry) SetViews

func (r *Registry) SetViews(v Views)

SetViews makes v render the views of sysml-view (MC-21). Without it, the macro fails (MC-19).

type Rendering

type Rendering struct {
	// SVG is a diagram, as a standalone SVG document; nil for a table.
	SVG []byte
	// Table is a table view's cells, row by row, the header row first.
	Table [][]string
}

Rendering is a rendered view: a diagram or a table.

type Views

type Views interface {
	// Render renders the view with that qualified name. Its error is the
	// macro's message (MC-19): it says what went wrong, not which macro.
	Render(ctx context.Context, qualifiedName string) (*Rendering, error)
}

Views renders the views sysml-view shows (MC-21).

type Warn

type Warn func(Warning)

Warn receives a warning from a macro. A nil Warn drops warnings.

type Warning

type Warning struct {
	// Page is the qualified name (or positional name, M::@2) of the element
	// the macro was converted for.
	Page string
	// Macro is the macro as written: sysml-value:position[].
	Macro string
	// Message says what is wrong, without the page or the macro.
	Message string
	// Location is where the macro is written in the model, file:line, or
	// empty when unknown. Macros leave it empty: they see the
	// representation's text, not its file; the renderer fills it in.
	Location string
}

Warning is a problem a macro reports while still producing output, such as a value it cannot show, or a macro that failed and left a placeholder (MC-19). Its parts are kept apart so the receiver can tell repeats without parsing the text (MC-7: one warning per page and macro).

func (Warning) String

func (w Warning) String() string

String is the warning as reported: "<location>: <page>: <macro>: <message>", without the location when it is unknown.

Jump to

Keyboard shortcuts

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