Skip to content

Commit d7d4311

Browse files
javier-godoypaodb
authored andcommitted
feat: use short license header
1 parent f8e6262 commit d7d4311

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

src/main/resources/META-INF/resources/frontend/code-viewer.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import "./prism.js";
2828
@customElement("code-viewer")
2929
export class CodeViewer extends LitElement {
3030

31+
private __license : Element[] = [];
32+
3133
createRenderRoot() {
3234
return this;
3335
}
@@ -101,6 +103,12 @@ pre[class*="language-"] {
101103
.token.namespace {
102104
opacity: 0.7;
103105
}
106+
107+
.token.license a {
108+
color: #f8f8f2;
109+
opacity: 0.7;
110+
text-decoration: underline;
111+
}
104112
105113
.token.property,
106114
.token.tag,
@@ -187,16 +195,54 @@ pre[class*="language-"] {
187195
// Wait for LitElement to finish updating the DOM before higlighting
188196
await self.updateComplete;
189197
var code = self.querySelector("code") as HTMLElement;
190-
198+
var text = self.removeLicense(this.responseText);
191199
code.setAttribute("class", "language-" + language);
192-
code.innerHTML = self.escapeHtml(this.responseText);
200+
code.innerHTML = self.escapeHtml(text);
193201

194202
(window as any).Prism.highlightAllUnder(self);
203+
self.__license.reverse().forEach(e=>self.querySelector('pre code')?.prepend(e));
195204
}};
196205
xhr.open('GET', sourceUrl, true);
197206
xhr.send();
198207
}
199208

209+
removeLicense(text: string) : string {
210+
this.__license = [];
211+
do {
212+
//parse license header
213+
if (!text.startsWith('/*-')) break;
214+
let end = text.indexOf('*/');
215+
if (end<0) break;
216+
217+
let pos = text.indexOf('#%L');
218+
if (pos<0 || end<pos) break;
219+
220+
let license = text.substring(pos+3,end).split('%%');
221+
if (license.length<3) break;
222+
license[1]=license[1].trim().replace(/\*/g,'').trim();
223+
license[2]=license[2].trim().replace(/\*/g,'').trim();
224+
225+
if (license[1].indexOf('\n')>0) break;
226+
227+
let newSpan = () => {
228+
let span = document.createElement('span');
229+
span.className='token comment license';
230+
this.__license.push(span);
231+
return span;
232+
}
233+
234+
if (license[2].startsWith('Licensed under the Apache License, Version 2.0')) {
235+
newSpan().innerText='//'+license[1]+'\n';
236+
newSpan().innerHTML = '//Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License, Version 2.0</a>\n';
237+
}
238+
239+
if (license.length) {
240+
text = text.substring(end+2).trimStart();
241+
}
242+
} while (0);
243+
return text;
244+
}
245+
200246
escapeHtml(unsafe: string) {
201247
return unsafe
202248
.replace(/&/g, "&amp;")

0 commit comments

Comments
 (0)