From 51a55b25dca2aa955e507a1613e4914001848b0f Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Fri, 9 Jul 2021 20:30:54 +0200 Subject: [PATCH] Webconfig: Read metadata from themes This readds the preferred background and url, all read from "# url:" and "# preferred_background:" comments in the .theme file. --- share/tools/web_config/js/controllers.js | 18 +++++++++++++- share/tools/web_config/webconfig.py | 30 ++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/share/tools/web_config/js/controllers.js b/share/tools/web_config/js/controllers.js index ed995bae0..9258a7cf8 100644 --- a/share/tools/web_config/js/controllers.js +++ b/share/tools/web_config/js/controllers.js @@ -81,14 +81,30 @@ controllers.controller("colorsController", function($scope, $http) { /* Array of FishColorSchemes */ $scope.colorSchemes = []; + isValidColor = function(col) { + // Check if preferred_background is actually a valid color + var s = new Option().style; + s.color = col; + return !!s.color; + } + $scope.getThemes = function() { $http.get("colors/").then(function(arg) { for (var scheme of arg.data) { var currentScheme = { "name": "Current", "colors":[], "preferred_background": "black" }; currentScheme["name"] = scheme["theme"]; var data = scheme["colors"]; + if (scheme["preferred_background"]) { + if (isValidColor(scheme["preferred_background"])) { + currentScheme["preferred_background"] = scheme["preferred_background"]; + } + } + if (scheme["url"]) currentScheme["url"] = scheme["url"]; + for (var i in data) { - currentScheme[data[i].name] = data[i].color; + if (isValidColor(data[i].color)) { + currentScheme[data[i].name] = data[i].color; + } } $scope.colorSchemes.push(currentScheme); } diff --git a/share/tools/web_config/webconfig.py b/share/tools/web_config/webconfig.py index dc8538cb7..2d9441e75 100755 --- a/share/tools/web_config/webconfig.py +++ b/share/tools/web_config/webconfig.py @@ -941,7 +941,23 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): else: with open(path) as f: out = f.read() + extrainfo = {} for line in out.split("\n"): + # Ignore empty lines + if not line: continue + # Lines starting with "#" can contain metadata. + if line.startswith("#"): + if not ":" in line: continue + key, value = line.split(":", maxsplit=1) + key = key.strip("# '") + value = value.strip(" '\"") + # Only use keys we know + if not key in ["name", "preferred_background", "url"]: continue + if key == "preferred_background": + if not value in named_colors and not value.startswith("#"): + value = "#" + value + extrainfo[key] = value + for match in re.finditer(r"^fish_color_(\S+) ?(.*)", line): color_name, color_value = [x.strip() for x in match.group(1, 2)] @@ -960,7 +976,7 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): color_desc = descriptions.get(color_name, "") result.append([color_name, color_desc, parse_color("")]) - return result + return result, extrainfo def do_get_functions(self): out, err = run_fish_cmd("functions") @@ -1297,15 +1313,19 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): # Construct our colorschemes. # Add the current scheme first, then the default. # The rest in alphabetical order. - output = [{ "theme": "Current", "colors": self.do_get_colors()}, - { "theme": "fish default", "colors": self.do_get_colors("themes/fish default.theme")}] + curcolors, curinfo = self.do_get_colors() + defcolors, definfo = self.do_get_colors("themes/fish default.theme") + curinfo.update({ "theme": "Current", "colors": curcolors}) + definfo.update({ "theme": "fish default", "colors": defcolors}) + output = [curinfo, definfo] paths = sorted(glob.iglob("themes/*.theme"), key=str.casefold) for p in paths: # Strip ".theme" suffix and path theme = os.path.basename(p)[:-6] if any(theme == d["theme"] for d in output): continue - out = self.do_get_colors(p) - output.append({ "theme": theme, "colors": out}) + out, outinfo = self.do_get_colors(p) + outinfo.update({ "theme": theme, "colors": out }) + output.append(outinfo) print(len(output)) elif p == "/functions/": output = self.do_get_functions()