Podświetlenie składni formatu TreeStructInfo - CodeMirror i Highlight.js
Wprowadzenie
Aby ułatwić edycję i przeglądanie plików TreeStructInfo, przygotowano specjalne definicje podświetlania składni dla dwóch popularnych bibliotek: CodeMirror oraz Highlight.js. Dzięki nim format staje się bardziej czytelny, co poprawia komfort pracy z jego strukturą – szczególnie podczas edycji atrybutów, węzłów oraz referencji.
Podświetlenie składni uwzględnia m.in.:
- nagłówki
treestructinfo
, - atrybuty
attr
,ref attr
, - węzły
node
,ref node
,end node
, - wartości tekstowe i numeryczne,
- komentarze (
::
), - wielolinijkowe wartości.
Kod dla CodeMirror 5
CodeMirror.defineMode("treestructinfo", function() {
return {
startState: function() {
return { inString: false };
},
token: function(stream, state) {
if (state.inString) {
let escaped = false;
while (!stream.eol()) {
const ch = stream.next();
if (ch === '"' && !escaped) {
state.inString = false;
break;
}
escaped = ch === '\\' && !escaped;
}
return "string";
}
if (stream.sol()) {
stream.eatSpace();
if (stream.match("::")) {
stream.skipToEnd();
return "comment";
}
if (stream.match(/treestructinfo\b/)) return "header";
if (stream.match(/end tree\b/)) return "def";
if (stream.match(/node\b/)) return "keyword";
if (stream.match(/end node\b/)) return "keyword";
if (stream.match(/ref attr\b/)) return "variable-2";
if (stream.match(/ref node\b/)) return "variable-3";
if (stream.match(/end ref node\b/)) return "variable-3";
if (stream.match(/attr\b/)) return "attribute";
}
if (stream.match(/"([^"\\]|\\.)*"?/)) {
// Full or partial string matched
if (stream.current().endsWith('"')) {
return "string";
} else {
state.inString = true;
return "string";
}
}
stream.next();
return null;
}
};
});
MIT License
Copyright (C) 2025 by Dariusz Rorat.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Opcjonalny CSS
.cm-header { color: #007acc; font-weight: bold; }
.cm-keyword { color: #d14; font-weight: bold; }
.cm-def { color: #999; }
.cm-attribute { color: #005; }
.cm-variable-2 { color: #3a3; font-style: italic; }
.cm-variable-3 { color: #f90; }
.cm-comment { color: #777; font-style: italic; }
.cm-error { background-color: #fdd; color: #a00; }
Kod dla Highlight.js 11.9
hljs.registerLanguage('treestructinfo', function(hljs) {
return {
name: 'TreeStructInfo',
case_insensitive: true,
contains: [
hljs.COMMENT('::', '$'),
{
begin: /\b(ref attr|attr)\b\s+\w+\s+"/,
returnBegin: true,
contains: [
{
className: 'keyword',
begin: /\b(ref attr|attr)\b/
},
{
className: 'attribute',
begin: /\w+/,
relevance: 0
},
{
className: 'string',
begin: /"/,
end: /"/,
contains: [
{ begin: /\\"/ }
]
}
]
},
{
className: 'keyword',
begin: /\bref attr\b/
},
{
className: 'attribute',
begin: /\b\w+\b/,
relevance: 0
},
{
className: 'keyword',
begin: /\b(treestructinfo|end tree|ref node|end ref node|node|end node)\b/
},
{
className: 'string',
begin: /"/,
end: /"/,
contains: [{ begin: /\\"/ }]
},
{
className: 'number',
begin: /\b(\d+(\.\d+)?|0x[0-9a-fA-F]+)\b/
}
]
};
});
BSD 3-Clause License
Copyright (c) 2025, Dariusz Rorat.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Przykład formatu TreeStructInfo 2.0
Podświetlenie w CodeMirror
Podświetlenie w HighlightJS
:: Example TreeStructInfo 2.0
treestructinfo "2.0" name "Test Sample Tree"
:: Various root tree attributes
attr StringAttr "Example String Attribute"
attr BoolAttr "true"
attr IntBoolAttr "1"
attr IntAttr "42"
attr FloatAttr "3.1415"
attr EngineeringAttr "1.23E+03"
attr BinaryDataAttr "54726565537472756374496E666F202D"
attr Base64DataAttr "U29tZSBiYXNlNjQgZGF0YQ=="
attr MultilineDataAttr "First Line"
"Second Line"
node Coords
attr DecCoords "100,120"
attr HexCoords "0xA0,0x80"
attr OctCoords "0o200,0o100"
attr BinCoords "0b10110011,0b11001101"
end node
node Meta
attr Author "John Doe"
ref attr SharedNote
ref node ParentRefNode
end node
node Items
node ItemA
attr Price "12,50 zł"
attr Available "false"
end node
node ItemB
attr Price "20,00 zł"
attr Available "true"
end node
end node
end tree
:: Ref attributes and ref nodes must be defined after end tree
:: Ref attrubites must be defined before ref nodes
ref attr SharedNote "This is a shared note used across the tree."
:: Child ref node must by defined before parent ref node
ref node ChildRefNode
attr Name "This Is Child Ref Node"
node ChildNode
attr ExampleValue "123456"
end node
end ref node
ref node ParentRefNode
attr Name "This Is Parent Ref Node"
node Child
attr ChildExampleAttr "This is child node attr"
ref attr SharedNote
end node
ref node ChildRefNode
end ref node
Podczas korzystania z podświetlania składni przy użyciu CodeMirror lub Highlight.js, warto upewnić się, że wybrany motyw spełnia wymagania kontrastu kolorów zgodne ze standardem WCAG AA. Niektóre motywy (zwłaszcza ciemne lub niestandardowe) mogą powodować zbyt niski kontrast tekstu względem tła, co utrudnia czytelność osobom z wadami wzroku. Zalecam przetestowanie wybranego stylu z użyciem narzędzi do oceny dostępności.
Podsumowanie
Dzięki integracji z CodeMirror i Highlight.js, edycja plików TreeStructInfo może odbywać się z wygodą porównywalną do edytorów kodu. To rozwiązanie szczególnie przydatne w aplikacjach webowych, narzędziach testujących i przeglądarkach konfiguracji, gdzie ważna jest przejrzystość i poprawna interpretacja struktury danych. Udostępnione reguły można łatwo dostosować lub rozbudować w zależności od potrzeb projektu.
Strona formatu TreeStructInfo:
https://tsinfo.4programmers.net/pl/index.htm
Moja aplikacja online do testowania formatu:
http://www.dariuszrorat.ugu.pl/aplikacje/treestructinfo-tester
Kody źródłowe do pobrania:
http://www.dariuszrorat.ugu.pl/uploads/media/2025/07/tsinfo.zip