Documentation
¶
Overview ¶
Package macros holds the sysml-* AsciiDoc macros (requirements MC-1..25), 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: send a model to a SysML service, 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"
"os"
"github.com/Open-MBEE/OpenSysML/client/opensysml"
asciidoctor "github.com/rolling-robot/asciidoctorg"
"github.com/rolling-robot/sysml-publisher/internal/servicetest"
"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()
addr, stop, err := servicetest.Service()
if err != nil {
log.Fatal(err)
}
defer stop()
client, err := opensysml.Dial(addr)
if err != nil {
log.Fatal(err)
}
content, err := os.ReadFile("../testdata/external/vehicles.sysml")
if err != nil {
log.Fatal(err)
}
parsed, err := client.ParseDocuments(ctx, []opensysml.Document{
opensysml.Source("../testdata/external/vehicles.sysml", string(content)),
})
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 "Boat"</span>.</p> </div>
Index ¶
- Constants
- func Attributes(id string) asciidoctor.Attributes
- func FormatValue(v opensysml.Value) string
- func MermaidText(n asciidoctor.Node) (text string, ok bool)
- func Passthrough(html string) string
- func ViewSVG(n asciidoctor.Node) (svg []byte, ok bool)
- type BlockFunc
- type Call
- func (c *Call) Attr(name string) string
- func (c *Call) Context() context.Context
- func (c *Call) Element() (Element, error)
- func (c *Call) Evaluate(subject Element, expression string) (opensysml.Value, error)
- func (c *Call) Link(id, html string) string
- func (c *Call) Model() *sysml.Model
- func (c *Call) Name(id string) (string, error)
- func (c *Call) Parent() asciidoctor.Node
- func (c *Call) Resolve(name string) (Element, error)
- func (c *Call) Warn(message string)
- type Element
- type InlineFunc
- type Pages
- type Registry
- type Rendering
- type Views
- type Warn
- type Warning
Examples ¶
Constants ¶
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).
const AttrFig = "sysml-fig"
AttrFig is the attribute of the block a figure is put in: the qualified name of the element the figure is of. As links are anchor nodes (AttrLink) and a view an image block (AttrView), a figure is a pass block the document's converter writes verbatim; a converter for another output can tell the figure by AttrFig and take its Mermaid source from MermaidText.
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>).
const AttrMermaid = "sysml-mermaid"
AttrMermaid is the attribute of the block a figure is put in: the figure's Mermaid source, for an output that renders Mermaid itself.
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.
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.
const CatalogRole = "sysml-catalog"
CatalogRole is the role of the block holding the catalog table, for styling.
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.
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.
const DocRole = "sysml-doc"
DocRole is the role of the block the documentation is put in, for styling.
const FailedRole = "sysml-failed"
FailedRole is the class of the placeholder a failing macro leaves on the page (MC-19), for styling.
const Fig = "sysml-fig"
Fig is the block macro sysml-fig::[<element>][], replaced, when the target is an interface or a part, by a Mermaid figure of it (MC-25): an interface's figure shows the parts on its ends and the parts between them and their common ancestor, each end's feature, and one edge per pair of ends; a part's figure shows its owned ports, and, for every connection they are on, the other ends' features and their owner parts. The figures are drawn by the site's mermaid.js in the look of OpenSysML's Mermaid interconnection form. Without a target the page's element is used; a target is read as sysml-name reads it (MC-8, MC-15). A resolvable target that is neither an interface nor a part is written out as the macro text, no warning raised. The figure draws exactly what the ends' chains pass through; the model is read the way sysml-ifaces reads it, the connections indexed once for every page (MC-3).
const FigRole = "sysml-fig"
FigRole is the role of the block a figure is put in, for styling.
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.
const IfaceEndsRole = "sysml-iface-ends"
IfaceEndsRole is the role of the list, for styling.
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.
const IfacesRole = "sysml-ifaces"
IfacesRole is the role of the list, for styling.
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.
const MembersListRole = "sysml-members-list"
MembersListRole is the role of the list, for styling.
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).
const MetadataTableRole = "sysml-metadata-table"
MetadataTableRole is the role of the block holding the tables, for styling.
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).
const NoValue = "(no value)"
NoValue is shown for an attribute that has no value.
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).
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.
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).
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).
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).
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 ¶
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 MermaidText ¶ added in v0.5.0
func MermaidText(n asciidoctor.Node) (text string, ok bool)
MermaidText is the Mermaid source of a figure sysml-fig put in, for a converter that renders Mermaid itself: n is the pass block, carrying AttrFig. ok is false for any other node.
func Passthrough ¶
Passthrough is finished HTML as AsciiDoc that substitutions leave alone, for list items and table cells, whose text AsciiDoc would otherwise read as markup.
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 ¶
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 ¶
Context is the rendering's context: pass it to model reads, which stop when it is cancelled.
func (*Call) Element ¶
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 ¶
Evaluate evaluates expression with element subject as its subject, so a feature reads that element's value (sysml-value, MC-14).
func (*Call) Link ¶
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) Name ¶
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.
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 ¶
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 ¶
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"
"os"
"github.com/Open-MBEE/OpenSysML/client/opensysml"
asciidoctor "github.com/rolling-robot/asciidoctorg"
"github.com/rolling-robot/sysml-publisher/internal/servicetest"
"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()
addr, stop, err := servicetest.Service()
if err != nil {
log.Fatal(err)
}
defer stop()
client, err := opensysml.Dial(addr)
if err != nil {
log.Fatal(err)
}
content, err := os.ReadFile("../testdata/external/vehicles.sysml")
if err != nil {
log.Fatal(err)
}
parsed, err := client.ParseDocuments(ctx, []opensysml.Document{
opensysml.Source("../testdata/external/vehicles.sysml", string(content)),
})
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 "Boat"</span>.</p> </div>
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).