Mermaid CLI - how do you escape characters? - diagramming

I'm using the Mermaid CLI to generate a flowchart (http://knsv.github.io/mermaid/flowchart.html). It works great, but I can't figure out how to get special characters (percent signs, parenthesis, etc) working as text within a node.
For illustration purposes here is a sample flowchart definition for Mermaid (filename is example.mermaid):
graph TD
question1{Gas tank less than 1/8?}
action1[Fill tank to 100%]
question1-- Yes -->action1
When I run mermaid on that file, I get this error (it blows up on the percent sign):
My-MacBook-Pro:mermaid mark$ mermaid example.mermaid
Error: Parse error on line 3:
...on1[Fill tank to 100%]question1-- Yes -
-----------------------^
Expecting 'QUOTE', 'TAG_END', 'TAG_START', 'MULT', 'EQUALS', 'PLUS', 'DOT', 'BRKT', 'COLON', 'ALPHA', 'COMMA', 'NUM', 'CLICK', 'CLASS', 'CLASSDEF', 'LINKSTYLE', 'STYLE', 'PIPE', 'THICK_ARROW_OPEN', 'THICK_ARROW_CROSS', 'THICK_ARROW_CIRCLE', 'THICK_ARROW_POINT', 'DOTTED_ARROW_OPEN', 'DOTTED_ARROW_CROSS', 'DOTTED_ARROW_CIRCLE', 'DOTTED_ARROW_POINT', 'ARROW_OPEN', 'ARROW_CROSS', 'ARROW_CIRCLE', 'ARROW_POINT', '==', '-.', '--', 'MINUS', 'DIAMOND_STOP', 'DIAMOND_START', 'PE', 'PS', 'SQE', 'SQS', 'end', 'subgraph', 'NEWLINE', 'TAGSTART', 'TAGEND', 'DIR', 'SPACE', 'GRAPH', 'EOF', 'SEMI', got 'PCT'
../dist/mermaid.full.js:14712 in parseError
../dist/mermaid.full.js:14782 in parse
../dist/mermaid.full.js:13260
../dist/mermaid.full.js:16846
../dist/mermaid.full.js:16889
phantomjs://webpage.evaluate():23 in executeInPage
phantomjs://webpage.evaluate():29
phantomjs://webpage.evaluate():29
PHANTOM ERROR: TypeError: 'null' is not an object (evaluating 'element.setAttribute')
TRACE:
-> /usr/local/lib/node_modules/mermaid/lib/phantomscript.js: 149 (in function resolveSVGElement)
-> /usr/local/lib/node_modules/mermaid/lib/phantomscript.js: 69
I tried escaping the percent sign, like this:
action1[Fill tank to 100&]
But then I get the same error on the semicolon. Any thoughts on how I can escape those characters to make it work? Thanks!

Use quotation marks "" to enclose your text and escape special characters, e.g. in your example:
graph TD
question1{"Gas tank less than 1/8?"}
action1["Fill tank to 100%"]
question1-- Yes -->action1
will produce this diagram:
This is now documented in the official documentation.

Here are more examples of escaping / quoting special characters. The code
flowchart LR
A["For most symbols double quotes are enough: ```~`!##$%^*()[]{}|\/:;'?<>,.+=-_"]
B["["&quot;&lt;<br>&gt;&amp;&frac12;#35;189;"]"]
B --> C[""<<br>>&½#189;"]
produces
Using this method most unicode characters (including non-ASCII) can be included using either unicode entity or markdown #code; syntax (both ways are illustrated using the character ½ in the diagram above). See also the official documentation.
Disclaimer: I didn't test it in Mermaid CLI. Tested in markdown-viewer and Mermaid live editor.

That was an issue with early mermaid versions. I tried your example code with mermaid 0.4.0 where it renders fine. I would recommend upgrading.

Related

Monaco Editor Monarch: Tokenizing Parentheses

I am having issues with the MonarchTokensProvider. In my application i am writing some custom auto-complete and need to be able to identify opening/closing parentheses.
As such, i expect the tokenizer to tokenize '()' to '(', ')'. However using default settings from monaco, parentheses that are next to each other are grouped together.
monaco-editor version: 0.30.1
Browser: Firefox, Chrome, Safari
OS: MacOS
Playground code that reproduces the issue:
See:
https://microsoft.github.io/monaco-editor/monarch.html
Press F1 and select 'Developer: Inspect Tokens'.
Move the cursor to the parentheses at 'main()'. See that '()' is listed as a single token.
Does anyone know the regex i need to use for the parentheses?
Overall i've only seen this being used
// delimiters and operators
[/[{}()\[\]]/, '#brackets'],
You can specify what you want to be interpreted as any monarch tokenizer class you want:
// [/[{}()\[\]]/, "#brackets"],
[/\(/, "openingparenthesis"],
[/\)/, "closingparenthesis"],
[/{/, "openingcurlybrace"],
[/}/, "closingcurlybrace"],
[/\]/, "openingbracket"],
[/\[/, "closingbracket"],

Perl 6: Backslashes in transliteration (tr///)

I noticed while experimenting with tr///, that it doesn't seem to translate backslashes, even when escaped. For example,
say TR"\^/v"." given 'v^/\\';
say TR"\\^/v"." given 'v^/\\';
say TR"\ ^/v"." given 'v^/\\';
All of them output ...\ rather than what I expected, ....
There's some other weird behaviour too, like \ seemingly only escaping lowercase letters, but the docs page doesn't have much information... What exactly is the behaviour of backslashes (\) in transliteration (tr///)?
There is a bug caused by backslashes getting swallowed instead of correctly escaping things in the grammar for tr///.
say TR/\\// given '\\'
===SORRY!=== Error while compiling:
Malformed replacement part; couldn't find final /
at line 2
------> <BOL>⏏<EOL>
I have raised https://github.com/rakudo/rakudo/issues/2456 and submitted https://github.com/rakudo/rakudo/pull/2457 which fixes it.
The second part of the answer is that Perl 6 tries quite hard in some quoting constructs to only interpret \ as an escape for valid escape sequences, i.e. \n, \r, \s, \', etc. Otherwise it is left as a literal \.
I do not have an explanation for the observed problem. However, when you use the Perl 6 Str.trans method it looks like it's working as expected:
say 'v^/\\'.trans( "\\^/v" => "." );
Outputs:
....
Reference:
https://perl6advent.wordpress.com/2010/12/21/day-21-transliteration-and-beyond/

OCaml: error in list function

Hello I am working on list of function in Ocaml I am getting this error why is that?
Error: Syntax error
# let headOf lst=
match lst with
|??[ ] -> failwith "harun"
Warning 3: deprecated: ISO-Latin1 characters in identifiers
If your input encoding is ISO-LATIN-1, then what you're doing is typing non-breaking spaces sometimes. These are non-ASCII characters that look like spaces, with a character code of 160. You should remove them all and replace with ordinary spaces (character code 32).
If you're using an input system that sometimes inputs non-breaking spaces without your specifically asking for them, you should use a different input system for working with OCaml :-)
Update
In fact, my input system inputs a non-breaking space if I type Option-Space (iTerm2 on macOS 10.12.4). It looks like this:
# let f x��= 14;;
Warning 3: deprecated: ISO-Latin1 characters in identifiers
Error: Illegal character (\160)
The solution in my case is never to type Option-Space. Just type space (no option key).

Unexpected token \n in JSON when parsing with Elm.Json

Actually I'm working with Elm but I have few issues with the json parsing in this language, the error that give me the compiler is:
Err "Given an invalid JSON: Unexpected token \n in JSON at position 388"
What I need to do is this:
example
At the char_meta I want its something like this:
[("Biographical Information", [("Japanese Name", "緑谷出久"), ...]), ...]
Here the code:
Ellie link
PD: The only constant keys are character_name, lang, summary and char_meta, they keys inside of char_meta are dynamic (thats why I use keyvaluepair) and the length its always different of this array (sometimes its empty)
Thanks, hope can help me.
EDIT:
The Ellie link now redirect to the fixed code
The issue is that elm (or JS once transcoded) interprets the \n and \" sequences when parsing the string literal, and they are replaced with an actual new line and double quotes respectively, which results in invalid JSON.
If you want to have the JSON inline in the code, you need to escape the 5 \s by doubling them (\\n and \\").
This only applies for literals, you won't have the issue if you load JSON from the network for instance.

Latex escaping in MatPlotlib

There are a number of situations where it is useful to set matplotlib.rcParams['text.usetex'] = True. In this case, special LaTeX characters such as % have to be escaped in places like axis labels. Is there a function built in to matplotlib for escaping LaTeX symbols?
There is a hint of such a thing in the docs here and here, but no clear mention.
The specific problem I am trying to tackle is using a matplotlib.ticker.PercentFormatter, where a custom symbol (symbol=r'\%') must be used if rcParams['text.usetex'] is true. I am trying to add a PR that will escape the percent symbol in PercentFormatter if rcParams['text.usetex'] is enabled, but it does not make sense to only check for the percent symbol in that case, so I would like to escape the entire symbol string.