Browse Source

Auto-format JS/CSS files

David Peter 1 year ago
parent
commit
8ce1eea5c3
3 changed files with 263 additions and 260 deletions
  1. 143 143
      assets/articles/index.js
  2. 104 104
      numbat-wasm/www/index.js
  3. 16 13
      numbat-wasm/www/main.css

+ 143 - 143
assets/articles/index.js

@@ -1,188 +1,188 @@
 import init, {
-  setup_panic_hook,
-  Numbat,
-  FormatType,
+    setup_panic_hook,
+    Numbat,
+    FormatType,
 } from "./pkg/numbat_wasm.js";
 
 async function main() {
-  await init();
+    await init();
 
-  setup_panic_hook();
+    setup_panic_hook();
 
-  const num_editors = 7;
-  for (var i = 1; i <= num_editors; i++) {
-    setupEditor(i);
-  }
+    const num_editors = 7;
+    for (var i = 1; i <= num_editors; i++) {
+        setupEditor(i);
+    }
 
-  // Evaluate all editors once. Do this after initialization
-  // such that we can call ace.edit(…) internally without
-  // initializing an empty editor.
-  for (var i = 1; i <= num_editors; i++) {
-    numbatEvaluate(i);
-  }
+    // Evaluate all editors once. Do this after initialization
+    // such that we can call ace.edit(…) internally without
+    // initializing an empty editor.
+    for (var i = 1; i <= num_editors; i++) {
+        numbatEvaluate(i);
+    }
 }
 
 main();
 
 function interpret(input, with_prelude) {
-  var numbat = Numbat.new(with_prelude, false, FormatType.Html);
-  if (with_prelude) {
-    numbat.interpret("use extra::astronomy");
-  }
+    var numbat = Numbat.new(with_prelude, false, FormatType.Html);
+    if (with_prelude) {
+        numbat.interpret("use extra::astronomy");
+    }
 
-  var result = numbat.interpret(input);
+    var result = numbat.interpret(input);
 
-  return result.output;
+    return result.output;
 }
 
 ace.config.set("basePath", "https://cdnjs.cloudflare.com/ajax/libs/ace/1.9.6/");
 
 ace.define(
-  "ace/mode/numbat_highlight_rules",
-  function (require, exports, module) {
-    var oop = require("ace/lib/oop");
-    var TextHighlightRules =
-      require("ace/mode/text_highlight_rules").TextHighlightRules;
-
-    var NumbatHighlightRules = function () {
-      this.$rules = {
-        start: [
-          {
-            token: "comment",
-            regex: "#.*$",
-          },
-          {
-            token: "keyword",
-            regex:
-              "\\b(?:per|to|let|fn|where|and|dimension|unit|use|long|short|both|none|print|assert|assert_eq|type|if|then|else|true|false)\\b",
-          },
-          {
-            token: "constant.numeric",
-            regex:
-              "\\b(?:[0-9]+(?:[._][0-9]+)*" +
-              "|0x[0-9A-Fa-f]+|0o[0-7]+|0b[01]+(?:_[01]+)*" +
-              "|\\.[0-9]+(?:[eE][-+]?[0-9]+)?" +
-              "|[0-9]+[eE][-+]?[0-9]+)\\b",
-          },
-          // {
-          //     token: "variable",
-          //     regex: "\\b[a-z\u00B0\u0394\u03C0\u00B5][A-Za-z0-9_\u00B0\u0394\u03C0\u00B5]*\\b"
-          // },
-          {
-            token: "support.type",
-            regex: "\\b[A-Z][A-Za-z]*\\b",
-          },
-          {
-            token: "string",
-            regex: '"[^"]*"',
-          },
-          {
-            token: "keyword.operator",
-            regex: "\\+|-|\\*|/|\\^|➞|→|:|÷|×|≤|≥|≠|<|>|²|³|\\(|\\)",
-          },
-          {
-            token: "meta.decorator",
-            regex: "@[\\w_]+",
-          },
-        ],
-      };
-    };
-
-    oop.inherits(NumbatHighlightRules, TextHighlightRules);
-
-    exports.NumbatHighlightRules = NumbatHighlightRules;
-  },
+    "ace/mode/numbat_highlight_rules",
+    function (require, exports, module) {
+        var oop = require("ace/lib/oop");
+        var TextHighlightRules =
+            require("ace/mode/text_highlight_rules").TextHighlightRules;
+
+        var NumbatHighlightRules = function () {
+            this.$rules = {
+                start: [
+                    {
+                        token: "comment",
+                        regex: "#.*$",
+                    },
+                    {
+                        token: "keyword",
+                        regex:
+                            "\\b(?:per|to|let|fn|where|and|dimension|unit|use|long|short|both|none|print|assert|assert_eq|type|if|then|else|true|false)\\b",
+                    },
+                    {
+                        token: "constant.numeric",
+                        regex:
+                            "\\b(?:[0-9]+(?:[._][0-9]+)*" +
+                            "|0x[0-9A-Fa-f]+|0o[0-7]+|0b[01]+(?:_[01]+)*" +
+                            "|\\.[0-9]+(?:[eE][-+]?[0-9]+)?" +
+                            "|[0-9]+[eE][-+]?[0-9]+)\\b",
+                    },
+                    // {
+                    //     token: "variable",
+                    //     regex: "\\b[a-z\u00B0\u0394\u03C0\u00B5][A-Za-z0-9_\u00B0\u0394\u03C0\u00B5]*\\b"
+                    // },
+                    {
+                        token: "support.type",
+                        regex: "\\b[A-Z][A-Za-z]*\\b",
+                    },
+                    {
+                        token: "string",
+                        regex: '"[^"]*"',
+                    },
+                    {
+                        token: "keyword.operator",
+                        regex: "\\+|-|\\*|/|\\^|➞|→|:|÷|×|≤|≥|≠|<|>|²|³|\\(|\\)",
+                    },
+                    {
+                        token: "meta.decorator",
+                        regex: "@[\\w_]+",
+                    },
+                ],
+            };
+        };
+
+        oop.inherits(NumbatHighlightRules, TextHighlightRules);
+
+        exports.NumbatHighlightRules = NumbatHighlightRules;
+    },
 );
 
 ace.define("ace/mode/numbat", function (require, exports, module) {
-  var oop = require("ace/lib/oop");
-  var TextMode = require("ace/mode/text").Mode;
-  var NumbatHighlightRules =
-    require("ace/mode/numbat_highlight_rules").NumbatHighlightRules;
+    var oop = require("ace/lib/oop");
+    var TextMode = require("ace/mode/text").Mode;
+    var NumbatHighlightRules =
+        require("ace/mode/numbat_highlight_rules").NumbatHighlightRules;
 
-  var Mode = function () {
-    this.HighlightRules = NumbatHighlightRules;
-  };
-  oop.inherits(Mode, TextMode);
+    var Mode = function () {
+        this.HighlightRules = NumbatHighlightRules;
+    };
+    oop.inherits(Mode, TextMode);
 
-  (function () {
-    this.lineCommentStart = "#";
+    (function () {
+        this.lineCommentStart = "#";
 
-    this.$id = "ace/mode/numbat";
-  }).call(Mode.prototype);
+        this.$id = "ace/mode/numbat";
+    }).call(Mode.prototype);
 
-  exports.Mode = Mode;
+    exports.Mode = Mode;
 });
 
 function getOutputId(id) {
-  return (id == 4 || id == 5) ? "output4and5" : "output" + id.toString();
+    return (id == 4 || id == 5) ? "output4and5" : "output" + id.toString();
 }
 
 function getCode(id) {
-  if (id == 4 || id == 5) {
-    return ace.edit("editor4").getValue() + "\n" + ace.edit("editor5").getValue();
-  } else {
-    return ace.edit("editor" + id.toString()).getValue();
-  }
+    if (id == 4 || id == 5) {
+        return ace.edit("editor4").getValue() + "\n" + ace.edit("editor5").getValue();
+    } else {
+        return ace.edit("editor" + id.toString()).getValue();
+    }
 }
 
 function numbatEvaluate(id) {
-  const code = getCode(id);
-  const output_id = getOutputId(id);
-  const with_prelude = (id == 4 || id == 5) ? false : true;
+    const code = getCode(id);
+    const output_id = getOutputId(id);
+    const with_prelude = (id == 4 || id == 5) ? false : true;
 
-  const result = interpret(code, with_prelude).replace(/\s*$/, "");
+    const result = interpret(code, with_prelude).replace(/\s*$/, "");
 
-  document.getElementById(output_id).innerHTML = result;
+    document.getElementById(output_id).innerHTML = result;
 }
 
 function debounce(func, wait, immediate) {
-  var timeout;
-  return function () {
-    var context = this,
-      args = arguments;
-    var later = function () {
-      timeout = null;
-      if (!immediate) func.apply(context, args);
+    var timeout;
+    return function () {
+        var context = this,
+            args = arguments;
+        var later = function () {
+            timeout = null;
+            if (!immediate) func.apply(context, args);
+        };
+        var callNow = immediate && !timeout;
+        clearTimeout(timeout);
+        timeout = setTimeout(later, wait);
+        if (callNow) func.apply(context, args);
     };
-    var callNow = immediate && !timeout;
-    clearTimeout(timeout);
-    timeout = setTimeout(later, wait);
-    if (callNow) func.apply(context, args);
-  };
 }
 
 function setupEditor(id) {
-  var editor = ace.edit("editor" + id.toString(), {
-    mode: "ace/mode/numbat",
-    showPrintMargin: false,
-    showGutter: true,
-    highlightActiveLine: false,
-    highlightGutterLine: false,
-    maxLines: 20,
-  });
-
-  editor.renderer.setPadding(10);
-
-  // Move cursor to the end of the input
-  const count = editor.session.getLength();
-  const lastLine = editor.session.getLine(count - 1);
-  editor.session.selection.moveCursorTo(count - 1, lastLine.length);
-
-  function evaluate() {
-    numbatEvaluate(id);
-  }
-
-  var debouncedEvaluate = debounce(evaluate, 500);
-
-  editor.getSession().on("change", debouncedEvaluate);
-
-  editor.commands.addCommand({
-    name: "evaluateCode",
-    bindKey: { win: "Ctrl-Enter", mac: "Command-Enter" },
-    exec: function (editor) {
-      numbatEvaluate(id);
-    },
-    readOnly: true,
-  });
+    var editor = ace.edit("editor" + id.toString(), {
+        mode: "ace/mode/numbat",
+        showPrintMargin: false,
+        showGutter: true,
+        highlightActiveLine: false,
+        highlightGutterLine: false,
+        maxLines: 20,
+    });
+
+    editor.renderer.setPadding(10);
+
+    // Move cursor to the end of the input
+    const count = editor.session.getLength();
+    const lastLine = editor.session.getLine(count - 1);
+    editor.session.selection.moveCursorTo(count - 1, lastLine.length);
+
+    function evaluate() {
+        numbatEvaluate(id);
+    }
+
+    var debouncedEvaluate = debounce(evaluate, 500);
+
+    editor.getSession().on("change", debouncedEvaluate);
+
+    editor.commands.addCommand({
+        name: "evaluateCode",
+        bindKey: { win: "Ctrl-Enter", mac: "Command-Enter" },
+        exec: function (editor) {
+            numbatEvaluate(id);
+        },
+        readOnly: true,
+    });
 }

+ 104 - 104
numbat-wasm/www/index.js

@@ -1,19 +1,19 @@
 import init, { setup_panic_hook, Numbat, FormatType } from "./pkg/numbat_wasm.js";
 
 async function fetch_exchange_rates() {
-  try {
-      const response = await fetch("https://numbat.dev/ecb-exchange-rates.php");
-
-      if (!response.ok) {
-          return;
-      }
-
-      const xml_content = await response.text();
-      numbat.set_exchange_rates(xml_content);
-  } catch (error) {
-      console.error("Failed to load currency exchange rates from the European Central Bank");
-      return;
-  }
+    try {
+        const response = await fetch("https://numbat.dev/ecb-exchange-rates.php");
+
+        if (!response.ok) {
+            return;
+        }
+
+        const xml_content = await response.text();
+        numbat.set_exchange_rates(xml_content);
+    } catch (error) {
+        console.error("Failed to load currency exchange rates from the European Central Bank");
+        return;
+    }
 }
 
 function create_numbat_instance() {
@@ -21,121 +21,121 @@ function create_numbat_instance() {
 }
 
 function updateUrlQuery(query) {
-  let url = new URL(window.location);
-  if (query == null) {
-    url.searchParams.delete('q');
-  } else {
-    url.searchParams.set('q', query);
-  }
-
-  history.replaceState(null, null, url);
+    let url = new URL(window.location);
+    if (query == null) {
+        url.searchParams.delete('q');
+    } else {
+        url.searchParams.set('q', query);
+    }
+
+    history.replaceState(null, null, url);
 }
 
 function interpret(input) {
-  // Skip empty lines or comments
-  var input_trimmed = input.trim();
-  if (input_trimmed === "" || (input_trimmed[0] === "#" && input_trimmed.indexOf("\n") == -1)) {
-    return;
-  }
-
-  if (input_trimmed == "clear") {
-    this.clear();
-    var output = "";
-  } else if (input_trimmed == "reset") {
-    numbat = create_numbat_instance();
-    numbat.interpret("use units::currencies");
-    combined_input = "";
-    updateUrlQuery(null);
-    this.clear();
-  } else if (input_trimmed == "list" || input_trimmed == "ls") {
-    output = numbat.print_environment();
-  } else if (input_trimmed == "list functions" || input_trimmed == "ls functions") {
-    output = numbat.print_functions();
-  } else if (input_trimmed == "list dimensions" || input_trimmed == "ls dimensions") {
-    output = numbat.print_dimensions();
-} else if (input_trimmed == "list variables" || input_trimmed == "ls variables") {
-    output = numbat.print_variables();
-} else if (input_trimmed == "list units" || input_trimmed == "ls units") {
-    output = numbat.print_units();
-  } else if (input_trimmed == "help" || input_trimmed == "?") {
-    output = numbat.help();
-  } else {
-    var result = {is_error: false};
-    if (input_trimmed.startsWith("info ")) {
-      var keyword = input_trimmed.substring(4).trim();
-      output = numbat.print_info(keyword);
-    } else {
-      result = numbat.interpret(input);
-      output = result.output;
+    // Skip empty lines or comments
+    var input_trimmed = input.trim();
+    if (input_trimmed === "" || (input_trimmed[0] === "#" && input_trimmed.indexOf("\n") == -1)) {
+        return;
     }
 
-    if (!result.is_error) {
-        combined_input += input.trim() + "⏎";
-        updateUrlQuery(combined_input);
+    if (input_trimmed == "clear") {
+        this.clear();
+        var output = "";
+    } else if (input_trimmed == "reset") {
+        numbat = create_numbat_instance();
+        numbat.interpret("use units::currencies");
+        combined_input = "";
+        updateUrlQuery(null);
+        this.clear();
+    } else if (input_trimmed == "list" || input_trimmed == "ls") {
+        output = numbat.print_environment();
+    } else if (input_trimmed == "list functions" || input_trimmed == "ls functions") {
+        output = numbat.print_functions();
+    } else if (input_trimmed == "list dimensions" || input_trimmed == "ls dimensions") {
+        output = numbat.print_dimensions();
+    } else if (input_trimmed == "list variables" || input_trimmed == "ls variables") {
+        output = numbat.print_variables();
+    } else if (input_trimmed == "list units" || input_trimmed == "ls units") {
+        output = numbat.print_units();
+    } else if (input_trimmed == "help" || input_trimmed == "?") {
+        output = numbat.help();
+    } else {
+        var result = { is_error: false };
+        if (input_trimmed.startsWith("info ")) {
+            var keyword = input_trimmed.substring(4).trim();
+            output = numbat.print_info(keyword);
+        } else {
+            result = numbat.interpret(input);
+            output = result.output;
+        }
+
+        if (!result.is_error) {
+            combined_input += input.trim() + "⏎";
+            updateUrlQuery(combined_input);
+        }
     }
-  }
 
-  return output;
+    return output;
 }
 
 const parsedTerminalHeightInPixels = parseInt(
-  getComputedStyle(document.documentElement).getPropertyValue(
-    "--terminal-height"
-  ),
-  10
+    getComputedStyle(document.documentElement).getPropertyValue(
+        "--terminal-height"
+    ),
+    10
 );
 
 function setup() {
-  $(document).ready(function() {
-    var term = $('#terminal').terminal(interpret, {
-        greetings: false,
-        name: "terminal",
-        height: parsedTerminalHeightInPixels,
-        prompt: "[[;;;prompt]>>> ]",
-        checkArity: false,
-        historySize: 200,
-        historyFilter(line) {
-          return line.trim() !== "";
-        },
-        completion(inp, cb) {
-          cb(numbat.get_completions_for(inp));
+    $(document).ready(function () {
+        var term = $('#terminal').terminal(interpret, {
+            greetings: false,
+            name: "terminal",
+            height: parsedTerminalHeightInPixels,
+            prompt: "[[;;;prompt]>>> ]",
+            checkArity: false,
+            historySize: 200,
+            historyFilter(line) {
+                return line.trim() !== "";
+            },
+            completion(inp, cb) {
+                cb(numbat.get_completions_for(inp));
+            }
+        });
+
+        // Swap out the skeleton loader with the terminal to prevent layout shifting.
+        document.getElementById("skeleton-loader").classList.add("hidden");
+        document.getElementById("terminal").classList.remove("hidden");
+
+        // evaluate expression in query string if supplied (via opensearch)
+        if (location.search) {
+            var queryParams = new URLSearchParams(location.search);
+            if (queryParams.has("q")) {
+                // feed in the query line by line, as if the user typed it in
+                for (const line of queryParams.get("q").split("⏎")) {
+                    if (line.trim().length > 0) {
+                        term.exec(line.trim() + "\n");
+                    }
+                }
+            }
         }
-      });
-
-    // Swap out the skeleton loader with the terminal to prevent layout shifting.
-    document.getElementById("skeleton-loader").classList.add("hidden");
-    document.getElementById("terminal").classList.remove("hidden");
-
-    // evaluate expression in query string if supplied (via opensearch)
-    if (location.search) {
-      var queryParams = new URLSearchParams(location.search);
-      if (queryParams.has("q")) {
-        // feed in the query line by line, as if the user typed it in
-        for (const line of queryParams.get("q").split("⏎")) {
-          if (line.trim().length > 0) {
-            term.exec(line.trim() + "\n");
-          }
-        }
-      }
-    }
-  });
+    });
 }
 
 var numbat;
 var combined_input = "";
 
 async function main() {
-  await init();
+    await init();
 
-  setup_panic_hook();
+    setup_panic_hook();
 
-  numbat = create_numbat_instance();
-  combined_input = "";
+    numbat = create_numbat_instance();
+    combined_input = "";
 
-  // Load KeyboardEvent polyfill for old browsers
-  keyboardeventKeyPolyfill.polyfill();
+    // Load KeyboardEvent polyfill for old browsers
+    keyboardeventKeyPolyfill.polyfill();
 
-  fetch_exchange_rates().then(setup);
+    fetch_exchange_rates().then(setup);
 }
 
 main();

+ 16 - 13
numbat-wasm/www/main.css

@@ -1,5 +1,5 @@
 :root {
-  --terminal-height: 550px;
+    --terminal-height: 550px;
 }
 
 html {
@@ -46,22 +46,25 @@ p.links a {
 }
 
 #skeleton-loader {
-  animation: skeleton-loading 1.5s infinite;
-  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
-  background-size: 200% 100%;
-  height: var(--terminal-height);
-  width: 100%;
+    animation: skeleton-loading 1.5s infinite;
+    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
+    background-size: 200% 100%;
+    height: var(--terminal-height);
+    width: 100%;
 }
+
 @keyframes skeleton-loading {
-  0% {
-    background-position: 200% 0;
-  }
-  100% {
-    background-position: -200% 0;
-  }
+    0% {
+        background-position: 200% 0;
+    }
+
+    100% {
+        background-position: -200% 0;
+    }
 }
+
 .hidden {
-  display: none;
+    display: none;
 }
 
 #terminal {