Rand Stats

MermaidJS::Grammar

zef:antononcube

MermaidJS::Grammar

Raku package with a grammar for Mermaid-JS diagram specs.

Languages and formats Mermaid-JS is translated to:

A very similar Raku package is "Graphviz::DOT::Grammar", [AAp1].


Usage examples

Here is a Mermaid-JS spec:

my $spec = q:to/END/;
flowchart TD
  A[Start] --> B{Decide}
  B -->|Yes| C[Do thing]
  B -->|No| D[Stop]
END
flowchart TD
  A[Start] --> B{Decide}
  B -->|Yes| C[Do thing]
  B -->|No| D[Stop]

Translate to Raku:

use MermaidJS::Grammar;
$spec ==> mermaid-js-interpret
# {edges => [{from => A, label => (Any), to => B, type => -->} {from => B, label => Yes, to => C, type => -->} {from => B, label => No, to => D, type => -->}], nodes => [{label => Start, name => A, type => rect} {label => Decide, name => B, type => rhombus} {label => Do thing, name => C, type => rect} {label => Stop, name => D, type => rect}], styles => []}

Translate to Graphviz DOT:

$spec ==> mermaid-js-interpret(a=>'DOT')
digraph G {
  "A" [label="Start", shape=box];
  "B" [label="Decide", shape=box];
  "C" [label="Do thing", shape=box];
  "D" [label="Stop", shape=box];
  "A" -> "B";
  "B" -> "C" [label="Yes"];
  "B" -> "D" [label="No"];
}

CLI

The package provides the Command Line Interface (CLI) script from-mermaid-js. Here is its usage message:

from-mermaid-js --help
# Usage:
#   from-mermaid-js.raku <text> [-t|--to=<Str>] [-o|--output=<Str>] -- Converts Mermaid JS language texts or files into Graphviz DOT, JSON, Mathematica, PlantUML, or Raku files.
#   
#     <text>               Input file or Mermaid-JS spec.
#     -t|--to=<Str>        Format to convert to. (One of 'json', 'mathematica', 'dot', 'plantuml', 'raku', or 'Whatever'.) [default: 'Whatever']
#     -o|--output=<Str>    Output file; if an empty string then the result is printed to stdout. [default: '']

References

[AAp1] Anton Antonov, Graphviz::DOT::Grammar. Raku package, (2024), GitHub/antononcube.