You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
230 lines
5.6 KiB
230 lines
5.6 KiB
|
|
#include <string> |
|
#include <cstdio> |
|
#include <unordered_map> |
|
#include <optional> |
|
|
|
#include <syntect-c.h> |
|
|
|
/* |
|
* |
|
* |
|
* |
|
* |
|
*/ |
|
|
|
class gnu_highlighter { |
|
|
|
const std::unordered_map<std::string, std::string> supported_languages { |
|
{"cpp", "cpp"}, |
|
{"C", "cpp"}, |
|
{"c", "cpp"}, |
|
{"F77", "fortran"}, |
|
{"f77", "fortran"}, |
|
{"F90", "fortran"}, |
|
{"f90", "fortran"}, |
|
{"H", "cpp"}, |
|
{"h", "cpp"}, |
|
{"ac", "m4"}, |
|
{"ada", "ada"}, |
|
{"adb", "ada"}, |
|
{"am", "makefile"}, |
|
{"applescript", "applescript"}, |
|
{"asm", "asm"}, |
|
{"autoconf", "m4"}, |
|
{"awk", "awk"}, |
|
{"bash", "sh"}, |
|
{"bat", "bat"}, |
|
{"batch", "bat"}, |
|
{"bib", "bib"}, |
|
{"bison", "bison"}, |
|
{"c", "c"}, |
|
{"caml", "caml"}, |
|
{"cbl", "cobol"}, |
|
{"cc", "cpp"}, |
|
{"changelog", "changelog"}, |
|
{"clipper", "clipper"}, |
|
{"cls", "latex"}, |
|
{"cobol", "cobol"}, |
|
{"coffee", "coffeescript"}, |
|
{"coffeescript", "coffeescript"}, |
|
{"conf", "conf"}, |
|
{"cpp", "cpp"}, |
|
{"cs", "csharp"}, |
|
{"csh", "sh"}, |
|
{"csharp", "csharp"}, |
|
{"css", "css"}, |
|
{"ctp", "php"}, |
|
{"cxx", "cpp"}, |
|
{"d", "d"}, |
|
{"desktop", "desktop"}, |
|
{"diff", "diff"}, |
|
{"dmd", "d"}, |
|
{"docbook", "xml"}, |
|
{"dtx", "latex"}, |
|
{"el", "lisp"}, |
|
{"eps", "postscript"}, |
|
{"erl", "erlang"}, |
|
{"erlang", "erlang"}, |
|
{"errors", "errors"}, |
|
{"f", "fortran"}, |
|
{"f77", "fortran"}, |
|
{"f90", "fortran"}, |
|
{"feature", "feature"}, |
|
{"fixed-fortran", "fixed-fortran"}, |
|
{"flex", "flex"}, |
|
{"fortran", "fortran"}, |
|
{"free-fortran", "fortran"}, |
|
{"glsl", "glsl"}, |
|
{"go", "go"}, |
|
{"groovy", "groovy"}, |
|
{"h", "cpp"}, |
|
{"haskell", "haskell"}, |
|
{"haxe", "haxe"}, |
|
{"hh", "cpp"}, |
|
{"hpp", "cpp"}, |
|
{"hs", "haskell"}, |
|
{"htm", "html"}, |
|
{"html", "html"}, |
|
{"hx", "haxe"}, |
|
{"hxx", "cpp"}, |
|
{"in", "makefile"}, |
|
{"ini", "desktop"}, |
|
{"islisp", "islisp"}, |
|
{"java", "java"}, |
|
{"javalog", "javalog"}, |
|
{"javascript", "javascript"}, |
|
{"js", "javascript"}, |
|
{"json", "json"}, |
|
{"kcfg", "xml"}, |
|
{"kdevelop", "xml"}, |
|
{"kidl", "xml"}, |
|
{"ksh", "sh"}, |
|
{"l", "flex"}, |
|
{"lang", "langdef"}, |
|
{"langdef", "langdef"}, |
|
{"latex", "latex"}, |
|
{"ldap", "ldap"}, |
|
{"ldif", "ldap"}, |
|
{"lex", "flex"}, |
|
{"lgt", "logtalk"}, |
|
{"lhs", "haskell_literate"}, |
|
{"lilypond", "lilypond"}, |
|
{"lisp", "lisp"}, |
|
{"ll", "flex"}, |
|
{"log", "log"}, |
|
{"logtalk", "logtalk"}, |
|
{"lsm", "lsm"}, |
|
{"lua", "lua"}, |
|
{"ly", "lilypond"}, |
|
{"m4", "m4"}, |
|
{"makefile", "makefile"}, |
|
{"manifest", "manifest"}, |
|
{"mf", "manifest"}, |
|
{"ml", "caml"}, |
|
{"mli", "caml"}, |
|
{"moc", "cpp"}, |
|
{"opa", "opa"}, |
|
{"outlang", "outlang"}, |
|
{"oz", "oz"}, |
|
{"pas", "pascal"}, |
|
{"pascal", "pascal"}, |
|
{"patch", "diff"}, |
|
{"pc", "pc"}, |
|
{"perl", "perl"}, |
|
{"php", "php"}, |
|
{"php3", "php"}, |
|
{"php4", "php"}, |
|
{"php5", "php"}, |
|
{"pkgconfig", "pc"}, |
|
{"pl", "prolog"}, |
|
{"pm", "perl"}, |
|
{"po", "po"}, |
|
{"postscript", "postscript"}, |
|
{"pot", "po"}, |
|
{"prg", "clipper"}, |
|
{"prolog", "prolog"}, |
|
{"properties", "properties"}, |
|
{"proto", "proto"}, |
|
{"protobuf", "proto"}, |
|
{"ps", "postscript"}, |
|
{"py", "python"}, |
|
{"python", "python"}, |
|
{"r", "r"}, |
|
{"rb", "ruby"}, |
|
{"rc", "xml"}, |
|
{"ruby", "ruby"}, |
|
{"s", "s"}, |
|
{"scala", "scala"}, |
|
{"scheme", "scheme"}, |
|
{"scm", "scheme"}, |
|
{"scpt", "applescript"}, |
|
{"sh", "sh"}, |
|
{"shell", "sh"}, |
|
{"sig", "sml"}, |
|
{"sl", "slang"}, |
|
{"slang", "slang"}, |
|
{"slsh", "slang"}, |
|
{"sml", "sml"}, |
|
{"spec", "spec"}, |
|
{"sql", "sql"}, |
|
{"sty", "latex"}, |
|
{"style", "style"}, |
|
{"syslog", "log"}, |
|
{"tcl", "tcl"}, |
|
{"tcsh", "sh"}, |
|
{"tex", "latex"}, |
|
{"texi", "texinfo"}, |
|
{"texinfo", "texinfo"}, |
|
{"tk", "tcl"}, |
|
{"tml", "tml"}, |
|
{"txt", "nohilite"}, |
|
{"ui", "xml"}, |
|
{"upc", "upc"}, |
|
{"vala", "vala"}, |
|
{"vbs", "vbscript"}, |
|
{"vbscript", "vbscript"}, |
|
{"xhtml", "xml"}, |
|
{"xml", "xml"}, |
|
{"xorg", "xorg"}, |
|
{"y", "bison"}, |
|
{"yacc", "bison"}, |
|
{"yy", "bison"}, |
|
{"zsh", "zsh"}, |
|
}; |
|
|
|
|
|
public: |
|
|
|
|
|
std::optional<std::string> get_lang(std::string tag) { |
|
if (supported_languages.contains(tag)) |
|
return supported_languages.at(tag); |
|
|
|
return {}; |
|
} |
|
|
|
|
|
std::string highlight(const std::string &program, |
|
const std::string &lang) { |
|
|
|
char *highlighted = syntect_to_html(lang.c_str(), "base16-ocean.light", program.c_str()); |
|
|
|
if (highlighted) { |
|
auto res = std::string(highlighted, highlighted + strlen(highlighted)); |
|
syntect_release_str(highlighted); |
|
return res; |
|
} else { |
|
spdlog::warn("Failed to highlight code block, bad language? '{}'", lang); |
|
return program; |
|
} |
|
|
|
|
|
//return oout.str(); |
|
} |
|
|
|
gnu_highlighter() { |
|
syntect_init(); |
|
} |
|
|
|
};
|
|
|