Skip to content

Commit

Permalink
Improve highlighting in new web ui
Browse files Browse the repository at this point in the history
Markdown url syntax is now supported. Typos are fixed. Cmake is ported.
  • Loading branch information
jart committed Nov 6, 2024
1 parent fdfdb13 commit d25fa3a
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 23 deletions.
Binary file modified llamafile/server/www/chatbot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
261 changes: 261 additions & 0 deletions llamafile/server/www/highlight_cmake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Copyright 2024 Mozilla Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const CMAKE_KEYWORDS = new Set([
'block',
'else',
'elseif',
'endblock',
'endforeach',
'endfunction',
'endif',
'endmacro',
'endwhile',
'foreach',
'function',
'if',
'macro',
'while',
]);

class HighlightCmake extends Highlighter {

static NORMAL = 0;
static BACKSLASH = 1;
static WORD = 2;
static WORD_SPACE = 3;
static DOLLAR = 4;
static VAR = 5;
static COMMENT = 6;
static DQUOTE = 7;
static DQUOTE_BACKSLASH = 8;
static DQUOTE_DOLLAR = 9;
static DQUOTE_VAR = 10;

constructor(delegate) {
super(delegate);
this.spaces = 0;
this.word = '';
}

feed(input) {
for (let i = 0; i < input.length; i += this.delta) {
this.delta = 1;
let c = input[i];
switch (this.state) {

case HighlightCmake.NORMAL:
if (!isascii(c) || isalpha(c) || c == '_') {
this.state = HighlightCmake.WORD;
this.word += c;
} else if (c == '#') {
this.state = HighlightCmake.COMMENT;
this.push("span", "comment");
this.append('#');
} else if (c == '"') {
this.state = HighlightCmake.DQUOTE;
this.push("span", "string");
this.append('"');
} else if (c == '$') {
this.state = HighlightCmake.DOLLAR;
} else if (c == '\\') {
this.state = HighlightCmake.BACKSLASH;
this.push("span", "escape");
this.append('\\');
} else {
this.append(c);
}
break;

case HighlightCmake.BACKSLASH:
this.append(c);
this.pop();
this.state = HighlightCmake.NORMAL;
break;

case HighlightCmake.WORD:
if (!isascii(c) || isalnum(c) || c == '_') {
this.word += c;
} else {
this.spaces = 0;
this.epsilon(HighlightCmake.WORD_SPACE);
}
break;

case HighlightCmake.WORD_SPACE:
if (c == ' ') {
++this.spaces;
} else if (c == '(') {
if (CMAKE_KEYWORDS.has(this.word.toLowerCase())) {
this.push("span", "keyword");
this.append(this.word);
this.pop();
} else {
this.push("span", "builtin");
this.append(this.word);
this.pop();
}
this.word = '';
for (let i = 0; i < this.spaces; ++i)
this.append(' ');
this.epsilon(HighlightCmake.NORMAL);
} else {
this.append(this.word);
this.word = '';
for (let i = 0; i < this.spaces; ++i)
this.append(' ');
this.epsilon(HighlightCmake.NORMAL);
}
break;

case HighlightCmake.COMMENT:
this.append(c);
if (c == '\n') {
this.pop();
this.state = HighlightCmake.NORMAL;
}
break;

case HighlightCmake.DOLLAR:
if (c == '{') {
this.state = HighlightCmake.VAR;
} else if (c == '$') {
this.append('$');
} else {
this.append('$');
this.epsilon(HighlightCmake.NORMAL);
}
break;

case HighlightCmake.VAR:
if (isalnum(c) || c == '_') {
this.word += c;
} else if (c == '}') {
this.append("${");
this.push("span", "var");
this.append(this.word);
this.pop();
this.append('}');
this.word = '';
this.state = HighlightCmake.NORMAL;
} else {
this.append("${");
this.append(this.word);
this.word = '';
this.epsilon(HighlightCmake.NORMAL);
}
break;

case HighlightCmake.DQUOTE:
if (c == '"') {
this.append('"');
this.pop();
this.state = HighlightCmake.NORMAL;
} else if (c == '\\') {
this.append('\\');
this.state = HighlightCmake.DQUOTE_BACKSLASH;
} else if (c == '$') {
this.state = HighlightCmake.DQUOTE_DOLLAR;
} else {
this.append(c);
}
break;

case HighlightCmake.DQUOTE_BACKSLASH:
this.append(c);
this.state = HighlightCmake.DQUOTE;
break;

case HighlightCmake.DQUOTE_DOLLAR:
if (c == '{') {
this.state = HighlightCmake.DQUOTE_VAR;
} else if (c == '$') {
this.append('$');
} else {
this.append('$');
this.epsilon(HighlightCmake.DQUOTE);
}
break;

case HighlightCmake.DQUOTE_VAR:
if (isalnum(c) || c == '_') {
this.word += c;
} else if (c == '}') {
this.append("${");
this.push("span", "var");
this.append(this.word);
this.pop();
this.append('}');
this.word = '';
this.state = HighlightCmake.DQUOTE;
} else {
this.append("${");
this.append(this.word);
this.word = '';
this.epsilon(HighlightCmake.DQUOTE);
}
break;

default:
throw new Error('Invalid state');
}
}
}

flush() {
switch (this.state) {
case HighlightCmake.WORD:
this.append(this.word);
this.word = '';
break;
case HighlightCmake.WORD_SPACE:
this.append(this.word);
this.word = '';
for (let i = 0; i < this.spaces; ++i)
this.append(' ');
break;
case HighlightCmake.DOLLAR:
this.append('$');
break;
case HighlightCmake.DQUOTE_DOLLAR:
this.append('$');
this.pop();
break;
case HighlightCmake.DQUOTE_VAR:
this.append("${");
this.append(this.word);
this.pop();
this.word = '';
break;
case HighlightCmake.VAR:
this.append("${");
this.append(this.word);
this.word = '';
break;
case HighlightCmake.DQUOTE:
case HighlightCmake.COMMENT:
case HighlightCmake.BACKSLASH:
case HighlightCmake.DQUOTE_BACKSLASH:
this.pop();
break;
default:
break;
}
this.state = HighlightCmake.NORMAL;
this.delegate.flush();
this.delta = 1;
}
}

Highlighter.REGISTRY['cmake'] = HighlightCmake;
36 changes: 18 additions & 18 deletions llamafile/server/www/highlight_make.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ const MAKE_BUILTINS = new Set([
'words',
]);

function is_automatic_variable(c) {
switch (c) {
case '@':
case '%':
case '<':
case '?':
case '^':
case '+':
case '|':
case '*':
return true;
default:
return false;
}
}

class HighlightMake extends Highlighter {

static NORMAL = 0;
Expand All @@ -99,6 +83,22 @@ class HighlightMake extends Highlighter {
static VARIABLE = 6;
static BACKSLASH = 7;

static is_automatic_variable(c) {
switch (c) {
case '@':
case '%':
case '<':
case '?':
case '^':
case '+':
case '|':
case '*':
return true;
default:
return false;
}
}

constructor(delegate) {
super(delegate);
this.word = '';
Expand Down Expand Up @@ -154,7 +154,7 @@ class HighlightMake extends Highlighter {
break;

case HighlightMake.DOLLAR:
if (isdigit(c) || is_automatic_variable(c)) {
if (isdigit(c) || HighlightMake.is_automatic_variable(c)) {
this.push("span", "var");
this.append(c);
this.pop();
Expand Down Expand Up @@ -191,7 +191,7 @@ class HighlightMake extends Highlighter {
c == '@' || //
c == '_') {
this.word += c;
} else if (c == '$' && this.word.empty()) {
} else if (c == '$' && !this.word) {
this.state = HighlightMake.DOLLAR;
this.append('$');
} else if (c == ')' || //
Expand Down
Loading

0 comments on commit d25fa3a

Please sign in to comment.