Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Add HTML syntax highlighting
Justine Tunney386313bd8e83
diff --git a/llamafile/BUILD.mk b/llamafile/BUILD.mkindex 832403e8..00aa43bd 100644--- a/llamafile/BUILD.mk+++ b/llamafile/BUILD.mk@@ -169,6 +169,7 @@ o/$(MODE)/llamafile/highlight_c_test: \o/$(MODE)/llamafile/highlight_c_test.o \o/$(MODE)/llamafile/highlight_c.o \o/$(MODE)/llamafile/is_keyword_c.o \+ o/$(MODE)/llamafile/is_keyword_js.o \o/$(MODE)/llamafile/highlight_python_test: \o/$(MODE)/llamafile/highlight_python_test.o \diff --git a/llamafile/highlight.cpp b/llamafile/highlight.cppindex 540bbacd..91f6b0b5 100644--- a/llamafile/highlight.cpp+++ b/llamafile/highlight.cpp@@ -44,6 +44,8 @@ Highlight *Highlight::create(const std::string_view &lang) {return new HighlightSql();} else if (lang == "css") {return new HighlightCss();+ } else if (lang == "html") {+ return new HighlightHtml();} else {return new HighlightPlain;}diff --git a/llamafile/highlight.h b/llamafile/highlight.hindex f38914d9..b212942f 100644--- a/llamafile/highlight.h+++ b/llamafile/highlight.h@@ -26,11 +26,14 @@#define HI_COMMENT "\033[31m" // red#define HI_MACRO "\033[35m" // magenta#define HI_ATTRIB "\033[35m" // magenta+#define HI_LINENO "\033[2m" // fade#define HI_CONTIN "\033[33m" // yellow#define HI_LABEL "\033[33m" // yellow#define HI_TYPE "\033[36m" // cyan#define HI_SELECTOR "\033[33m" // yellow#define HI_PROPERTY "\033[36m" // cyan+#define HI_TAG "\033[33m" // yellow+#define HI_INCODE "\033[1;35m" // magentatypedef const char *is_keyword_f(const char *, size_t);@@ -179,3 +182,19 @@ class HighlightCss : public Highlight {private:int t_ = 0;};++class HighlightHtml : public Highlight {+ public:+ HighlightHtml();+ ~HighlightHtml() override;+ void feed(std::string *result, std::string_view input) override;+ void flush(std::string *result) override;++ private:+ int t_ = 0;+ int i_ = 0;+ std::string name_;+ std::string closer_;+ std::string pending_;+ Highlight *highlighter_ = nullptr;+};diff --git a/llamafile/highlight_c.cpp b/llamafile/highlight_c.cppindex e78c3bc6..34b5ab1a 100644--- a/llamafile/highlight_c.cpp+++ b/llamafile/highlight_c.cpp@@ -27,6 +27,7 @@#define SLASH_SLASH 5#define SLASH_STAR 6#define SLASH_STAR_STAR 7+#define TICK 8#define BACKSLASH 64HighlightC::HighlightC(is_keyword_f *is_keyword, is_keyword_f *is_type)@@ -68,6 +69,10 @@ void HighlightC::feed(std::string *r, std::string_view input) {t_ = DQUOTE;*r += HI_STRING;*r += c;+ } else if (c == '`' && is_keyword_ == is_keyword_js) {+ t_ = TICK;+ *r += HI_STRING;+ *r += c;} else {*r += c;}@@ -153,6 +158,14 @@ void HighlightC::feed(std::string *r, std::string_view input) {}break;+ case TICK:+ *r += c;+ if (c == '`') {+ *r += HI_RESET;+ t_ = NORMAL;+ }+ break;+default:__builtin_unreachable();}diff --git a/llamafile/highlight_cobol.cpp b/llamafile/highlight_cobol.cppindex 60c3ef74..e0dd6f3e 100644--- a/llamafile/highlight_cobol.cpp+++ b/llamafile/highlight_cobol.cpp@@ -51,7 +51,7 @@ void HighlightCobol::feed(std::string *r, std::string_view input) {*r += HI_RESET;continue;} else if (col_ < 6 && isdigit(c)) {- *r += HI_LABEL;+ *r += HI_LINENO;*r += c;*r += HI_RESET;continue;diff --git a/llamafile/highlight_html.cpp b/llamafile/highlight_html.cppnew file mode 100644index 00000000..7d209582--- /dev/null+++ b/llamafile/highlight_html.cpp@@ -0,0 +1,263 @@+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-+// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi+//+// 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.++#include "highlight.h"++#define NORMAL 0+#define TAG 1+#define TAG2 2+#define TAG_EXCLAIM 3+#define TAG_EXCLAIM_HYPHEN 4+#define KEY 5+#define VAL 6+#define QUOTE 7+#define DQUOTE 8+#define COMMENT 9+#define COMMENT_HYPHEN 10+#define COMMENT_HYPHEN_HYPHEN 11+#define RELAY 12++HighlightHtml::HighlightHtml() {+}++HighlightHtml::~HighlightHtml() {+}++void HighlightHtml::feed(std::string *r, std::string_view input) {+ char c;+ for (size_t i = 0; i < input.size(); ++i) {+ c = input[i];+ switch (t_) {++ case NORMAL:+ if (c == '<') {+ t_ = TAG;+ name_.clear();+ } else {+ *r += c;+ }+ break;++ case TAG:+ if (c == '!') {+ t_ = TAG_EXCLAIM;+ } else if (c == '>' || isspace(c)) {+ *r += '<';+ *r += c;+ t_ = NORMAL;+ } else {+ *r += '<';+ *r += HI_TAG;+ *r += c;+ name_ += tolower(c);+ t_ = TAG2;+ }+ break;++ case TAG2:+ if (c == '>') {+ *r += HI_RESET;+ *r += c;+ goto OnTag;+ } else if (isspace(c)) {+ *r += c;+ t_ = KEY;+ *r += HI_ATTRIB;+ } else {+ *r += c;+ name_ += tolower(c);+ }+ break;++ case TAG_EXCLAIM:+ if (c == '-') {+ t_ = TAG_EXCLAIM_HYPHEN;+ } else {+ *r += "<!";+ *r += c;+ t_ = NORMAL;+ }+ break;++ case TAG_EXCLAIM_HYPHEN:+ if (c == '-') {+ *r += HI_COMMENT;+ *r += "<!--";+ t_ = COMMENT;+ } else {+ *r += "<!-";+ *r += c;+ t_ = NORMAL;+ }+ break;++ case COMMENT:+ *r += c;+ if (c == '-')+ t_ = COMMENT_HYPHEN;+ break;++ case COMMENT_HYPHEN:+ *r += c;+ if (c == '-') {+ t_ = COMMENT_HYPHEN_HYPHEN;+ } else {+ t_ = COMMENT;+ }+ break;++ case COMMENT_HYPHEN_HYPHEN:+ *r += c;+ if (c == '>') {+ *r += HI_RESET;+ t_ = NORMAL;+ } else {+ t_ = COMMENT;+ }+ break;++ case KEY:+ if (c == '=') {+ *r += HI_RESET;+ *r += c;+ t_ = VAL;+ } else if (c == '>') {+ *r += HI_RESET;+ *r += c;+ goto OnTag;+ } else {+ *r += c;+ }+ break;++ case VAL:+ if (isspace(c)) {+ *r += c;+ t_ = KEY;+ *r += HI_ATTRIB;+ } else if (c == '\'') {+ *r += HI_STRING;+ *r += c;+ t_ = QUOTE;+ } else if (c == '"') {+ *r += HI_STRING;+ *r += c;+ t_ = DQUOTE;+ } else if (c == '>') {+ *r += c;+ goto OnTag;+ } else {+ *r += c;+ }+ break;++ case QUOTE:+ *r += c;+ if (c == '\'') {+ *r += HI_RESET;+ t_ = VAL;+ }+ break;++ case DQUOTE:+ *r += c;+ if (c == '"') {+ *r += HI_RESET;+ t_ = VAL;+ }+ break;++ OnTag:+ t_ = NORMAL;+ if (name_ == "script") {+ pending_.clear();+ closer_ = "</script>";+ highlighter_ = Highlight::create("js");+ t_ = RELAY;+ i_ = 0;+ } else if (name_ == "style") {+ pending_.clear();+ closer_ = "</style>";+ highlighter_ = Highlight::create("css");+ t_ = RELAY;+ i_ = 0;+ }+ break;++ case RELAY:+ if (closer_[i_] == tolower(c)) {+ pending_ += c;+ if (++i_ == closer_.size()) {+ highlighter_->flush(r);+ delete highlighter_;+ highlighter_ = nullptr;+ if (closer_ == "</style>")+ pending_ = "</" HI_TAG "style" HI_RESET ">";+ else if (closer_ == "</script>")+ pending_ = "</" HI_TAG "script" HI_RESET ">";+ *r += pending_;+ t_ = NORMAL;+ i_ = 0;+ }+ } else {+ pending_ += c;+ highlighter_->feed(r, pending_);+ pending_.clear();+ i_ = 0;+ }+ break;++ default:+ __builtin_unreachable();+ }+ }+}++void HighlightHtml::flush(std::string *r) {+ switch (t_) {+ case TAG:+ *r += '<';+ break;+ case TAG_EXCLAIM:+ *r += "<!";+ break;+ case TAG_EXCLAIM_HYPHEN:+ *r += "<!-";+ break;+ case COMMENT_HYPHEN_HYPHEN:+ case COMMENT_HYPHEN:+ case COMMENT:+ case DQUOTE:+ case QUOTE:+ case TAG2:+ case KEY:+ *r += HI_RESET;+ break;+ case RELAY:+ highlighter_->feed(r, pending_);+ highlighter_->flush(r);+ delete highlighter_;+ highlighter_ = nullptr;+ break;+ default:+ break;+ }+ pending_.clear();+ closer_.clear();+ name_.clear();+ t_ = NORMAL;+}diff --git a/llamafile/highlight_markdown.cpp b/llamafile/highlight_markdown.cppindex 93448c01..362a04be 100644--- a/llamafile/highlight_markdown.cpp+++ b/llamafile/highlight_markdown.cpp@@ -28,6 +28,7 @@#define STRONG 8#define STRONG_STAR 9#define BACKSLASH 10+#define INCODE 11HighlightMarkdown::HighlightMarkdown() {}@@ -45,7 +46,6 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {case NORMAL:if (c == '`') {t_ = TICK;- *r += c;} else if (c == '*') {t_ = STAR;} else if (c == '\\') {@@ -91,10 +91,21 @@ void HighlightMarkdown::feed(std::string *r, std::string_view input) {case TICK:if (c == '`') {t_ = TICK_TICK;+ *r += '`';*r += c;} else {+ *r += HI_INCODE;+ *r += '`';+ *r += c;+ t_ = INCODE;+ }+ break;++ case INCODE:+ *r += c;+ if (c == '`') {+ *r += HI_RESET;t_ = NORMAL;- goto Normal;}break;