Creating Custom Syntax Highlighting in Quarto
Code
Quarto
xml
When I tried to write a summary in quarto of the theoratical programming lecture, I wanted to include syntaxt highligthing for so called ram programs:
<!-- ````ram{code-line-numbers=false}
# f(x, y, z) = z + x ⋅ y
0 R3 ← 1
# Begin der Schleife
1 IF R1 = 0 GOTO 3
2 R2 ← R2 + R0
3 R1 ← R1 - R3
4 GOTO 1
# Ende der Schleife
5 R0 ← R2
6 STOP
```-->to create costom syntax highlighing on has to define it inside an xml file.
here are some other examples of establish laguages: https://github.com/KDE/syntax-highlighting/tree/master/data/syntax
I made a very simple and easy to change file
ram.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language>
<language name="RAM" version="1" kateversion="5.62" section="RAM" extensions="*.ram;*.ram" mimetype="text/ram;" author="Daniel Schwarzenbach" license="MIT">
<highlighting>
<list name="Operators">
<item>←</item>
<item>=</item>
<item><</item>
<item>></item>
<item>≥</item>
<item>≤</item>
</list>
<list name="Keywords">
<item>IF</item>
<item>GOTO</item>
<item>STOP</item>
</list>
<contexts>
<context attribute="Normal Text" lineEndContext="#stay" name="Normal">
<DetectSpaces attribute="Normal Text" />
<keyword attribute="Keyword" context="#stay" String="Keywords" />
<RegExpr attribute="Register" context="#stay" String="R+[0-9]+" />
<RegExpr attribute="LineNumber" context="#stay" String="[0-9]{1,3} " />
<RegExpr attribute="Number" context="#stay" String="[0-9]+" />
<RegExpr attribute="Comment" String="#.*$"/>
<RegExpr attribute="Operator" String="\+|-|="/>
<keyword attribute="Operator" context="#stay" String="Operators" />
</context>
</contexts>
<itemDatas>
<itemData name="Normal Text" defStyleNum="dsNormal" />
<itemData name="Keyword" defStyleNum="dsKeyword" />
<itemData name="Operator" defStyleNum="dsFunction" />
<itemData name="Register" defStyleNum="dsDataType" />
<itemData name="LineNumber" defStyleNum="dsNormal" />
<itemData name="Number" defStyleNum="dsBaseN" />
<itemData name="Comment" defStyleNum="dsComment" />
</itemDatas>
</highlighting>
</language>finaly one has to include this file inside the quarto settings:
syntax-definitions:
- ram.xmlnow your custom syntax highlighting should be working
# f(x, y, z) = z + x ⋅ y
0 R3 ← 1
# Begin der Schleife
1 IF R1 = 0 GOTO 3
2 R2 ← R2 + R0
3 R1 ← R1 - R3
4 GOTO 1
# Ende der Schleife
5 R0 ← R2
6 STOP