Skip to content

Commit 55f6534

Browse files
authored
Add Painless language support (#521)
1 parent 5c1d039 commit 55f6534

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

docs/testing/custom-highlighters.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,40 @@ FROM books
112112
| SORT book_no
113113
| LIMIT 5;
114114
```
115+
116+
## Painless
117+
118+
```painless
119+
int i = (int)5L;
120+
Map m = new HashMap();
121+
HashMap hm = (HashMap)m;
122+
```
123+
124+
```painless
125+
ZonedDateTime zdt1 =
126+
ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));
127+
ZonedDateTime zdt2 =
128+
ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));
129+
130+
if (zdt1.isAfter(zdt2)) {
131+
// handle condition
132+
}
133+
```
134+
135+
```painless
136+
if (doc.containsKey('start') && doc.containsKey('end')) {
137+
138+
if (doc['start'].size() > 0 && doc['end'].size() > 0) {
139+
140+
ZonedDateTime start = doc['start'].value;
141+
ZonedDateTime end = doc['end'].value;
142+
long differenceInMillis = ChronoUnit.MILLIS.between(start, end);
143+
144+
// handle difference in times
145+
} else {
146+
// handle fields without values
147+
}
148+
} else {
149+
// handle index with missing fields
150+
}
151+
```

src/Elastic.Markdown/Assets/hljs.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ hljs.registerLanguage('eql', function() {
6868
}
6969
})
7070

71+
hljs.registerLanguage('painless', function() {
72+
return {
73+
case_insensitive: true, // language is case-insensitive
74+
keywords: {
75+
keyword: 'where sequence sample untill and or not in in~',
76+
literal: ['false','true','null'],
77+
'subst': 'add between cidrMatch concat divide endsWith indexOf length modulo multiply number startsWith string stringContains substring subtract'
78+
},
79+
contains: [
80+
hljs.QUOTE_STRING_MODE,
81+
hljs.C_LINE_COMMENT_MODE,
82+
{
83+
scope: "operator", // (pathname: path1/path2/dothis) color #ab5656
84+
match: /(?:<|<=|==|:|!=|>=|>|like~?|regex~?)/,
85+
},
86+
{
87+
scope: "punctuation", // (pathname: path1/path2/dothis) color #ab5656
88+
match: /(?:!?\[|\]|\|)/,
89+
},
90+
NUMBER,
91+
92+
]
93+
}
94+
})
95+
96+
7197
hljs.registerLanguage('esql', function() {
7298
return {
7399
case_insensitive: true, // language is case-insensitive

src/Elastic.Markdown/Assets/markdown/code.css

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,21 @@
118118

119119
.hljs-meta,
120120
.hljs-name,
121-
.hljs-type,
122-
.hljs-symbol,
123121
.hljs-bullet,
124122
.hljs-addition,
125-
.hljs-variable,
126123
.hljs-template-tag,
127124
.hljs-template-variable {
128125
color: var(--color-yellow-50)
129126
}
130127

128+
.hljs-type,
129+
.hljs-symbol {
130+
color: var(--color-teal-50)
131+
}
132+
.hljs-variable {
133+
color: var(--color-purple-50)
134+
}
135+
131136
.hljs-comment,
132137
.hljs-quote,
133138
.hljs-deletion {
@@ -155,7 +160,7 @@
155160
color: var(--color-pink-50)
156161
}
157162
.hljs-number {
158-
color: var(--color-teal-50)
163+
color: var(--color-red-50)
159164
}
160165

161166
.hljs-emphasis {

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public override bool Close(BlockProcessor processor, Block block)
8585
"console-response" => "json",
8686
"console-result" => "json",
8787
"terminal" => "bash",
88+
"painless" => "java",
8889
_ => codeBlock.Language
8990
};
9091
if (!string.IsNullOrEmpty(codeBlock.Language) && !CodeBlock.Languages.Contains(codeBlock.Language))

0 commit comments

Comments
 (0)