From 5ff242b4c3e23c791f52676f0ac998b984ba1852 Mon Sep 17 00:00:00 2001 From: Dave Powers Date: Thu, 17 Oct 2024 20:44:51 -0400 Subject: [PATCH 1/3] Add field to docs.json denoting available aliases - read JavaScript file defining slug/alias mapping - parse aliases object as Ruby hash - add alias key to JSON output with value or null --- lib/docs/core/manifest.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/docs/core/manifest.rb b/lib/docs/core/manifest.rb index b7c2e00d..a043cb5d 100644 --- a/lib/docs/core/manifest.rb +++ b/lib/docs/core/manifest.rb @@ -20,6 +20,15 @@ module Docs if doc.options[:attribution].is_a?(String) json[:attribution] = doc.options[:attribution].strip end + + # parse doc aliases from JS file as Ruby hash + entry_file = File.open("assets/javascripts/models/entry.js") + data = entry_file.read + aliases = eval data.split("ALIASES = ").last.split(";").first + + # set alias value + json["alias"] = aliases[json["slug"].to_sym] + result << json end end From ed116eb795d3b45f78b4b2348ea5b4d758fc7e3b Mon Sep 17 00:00:00 2001 From: Dave Powers Date: Thu, 17 Oct 2024 21:10:00 -0400 Subject: [PATCH 2/3] Update and fix manifest test --- lib/docs/core/manifest.rb | 2 +- test/files/docs.json | 2 +- test/lib/docs/core/manifest_test.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/docs/core/manifest.rb b/lib/docs/core/manifest.rb index a043cb5d..2a1f084c 100644 --- a/lib/docs/core/manifest.rb +++ b/lib/docs/core/manifest.rb @@ -27,7 +27,7 @@ module Docs aliases = eval data.split("ALIASES = ").last.split(";").first # set alias value - json["alias"] = aliases[json["slug"].to_sym] + json["alias"] = aliases[json["slug"].try(:to_sym)] result << json end diff --git a/test/files/docs.json b/test/files/docs.json index bd9146b5..7f795c43 100644 --- a/test/files/docs.json +++ b/test/files/docs.json @@ -1 +1 @@ -[{"name":"CSS","slug":"css","type":"mdn","release":null,"mtime":1420139788,"db_size":3460507},{"name":"DOM","slug":"dom","type":"mdn","release":null,"mtime":1420139789,"db_size":11399128},{"name":"DOM Events","slug":"dom_events","type":"mdn","release":null,"mtime":1420139790,"db_size":889020},{"name":"HTML","slug":"html~5","type":"mdn","version":"5","mtime":1420139791,"db_size":1835647},{"name":"HTML","slug":"html~4","type":"mdn","version":"4","mtime":1420139790,"db_size":1835646},{"name":"HTTP","slug":"http","type":"rfc","release":null,"mtime":1420139790,"db_size":183083},{"name":"JavaScript","slug":"javascript","type":"mdn","release":null,"mtime":1420139791,"db_size":4125477}] +[{"name":"CSS","slug":"css","type":"mdn","release":null,"mtime":1420139788,"db_size":3460507,"alias":null},{"name":"DOM","slug":"dom","type":"mdn","release":null,"mtime":1420139789,"db_size":11399128,"alias":null},{"name":"DOM Events","slug":"dom_events","type":"mdn","release":null,"mtime":1420139790,"db_size":889020,"alias":null},{"name":"HTML","slug":"html~5","type":"mdn","version":"5","mtime":1420139791,"db_size":1835647,"alias":null},{"name":"HTML","slug":"html~4","type":"mdn","version":"4","mtime":1420139790,"db_size":1835646,"alias":null},{"name":"HTTP","slug":"http","type":"rfc","release":null,"mtime":1420139790,"db_size":183083,"alias":null},{"name":"JavaScript","slug":"javascript","type":"mdn","release":null,"mtime":1420139791,"db_size":4125477,"alias":"js"}] diff --git a/test/lib/docs/core/manifest_test.rb b/test/lib/docs/core/manifest_test.rb index 636885cd..42c053af 100644 --- a/test/lib/docs/core/manifest_test.rb +++ b/test/lib/docs/core/manifest_test.rb @@ -64,7 +64,7 @@ class ManifestTest < Minitest::Spec it "includes the doc's meta representation" do json = manifest.as_json assert_equal 1, json.length - assert_equal "{\"name\"=>\"Test\", \"db_size\"=>776533, :attribution=>\"foo\"}", json[0].to_s + assert_equal "{\"name\"=>\"Test\", \"db_size\"=>776533, :attribution=>\"foo\", \"alias\"=>nil}", json[0].to_s end end From 050022bfb7bee46e4615fa5ede4bd2194c47f2ba Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Sun, 17 Nov 2024 11:09:13 +0100 Subject: [PATCH 3/3] Move docs_aliases to class App --- assets/javascripts/app/config.js.erb | 1 + assets/javascripts/models/entry.js | 45 ++++------------------------ lib/app.rb | 36 ++++++++++++++++++++++ lib/docs/core/manifest.rb | 10 +------ 4 files changed, 43 insertions(+), 49 deletions(-) diff --git a/assets/javascripts/app/config.js.erb b/assets/javascripts/app/config.js.erb index 56ce2652..04bc8cba 100644 --- a/assets/javascripts/app/config.js.erb +++ b/assets/javascripts/app/config.js.erb @@ -1,6 +1,7 @@ app.config = { db_filename: 'db.json', default_docs: <%= App.default_docs.to_json %>, + docs_aliases: <%= App.docs_aliases.to_json %>, docs_origin: '<%= App.docs_origin %>', env: '<%= App.environment %>', history_cache_size: 10, diff --git a/assets/javascripts/models/entry.js b/assets/javascripts/models/entry.js index 79c81342..98822bc9 100644 --- a/assets/javascripts/models/entry.js +++ b/assets/javascripts/models/entry.js @@ -2,14 +2,15 @@ app.models.Entry = class Entry extends app.Model { static applyAliases(string) { - if (Entry.ALIASES.hasOwnProperty(string)) { - return [string, Entry.ALIASES[string]]; + const aliases = app.config.docs_aliases; + if (aliases.hasOwnProperty(string)) { + return [string, aliases[string]]; } else { const words = string.split("."); for (let i = 0; i < words.length; i++) { var word = words[i]; - if (Entry.ALIASES.hasOwnProperty(word)) { - words[i] = Entry.ALIASES[word]; + if (aliases.hasOwnProperty(word)) { + words[i] = aliases[word]; return [string, words.join(".")]; } } @@ -17,43 +18,7 @@ app.models.Entry = class Entry extends app.Model { return string; } - static ALIASES = { - angular: "ng", - "angular.js": "ng", - "backbone.js": "bb", - "c++": "cpp", - coffeescript: "cs", - crystal: "cr", - elixir: "ex", - javascript: "js", - julia: "jl", - jquery: "$", - "knockout.js": "ko", - kubernetes: "k8s", - less: "ls", - lodash: "_", - löve: "love", - marionette: "mn", - markdown: "md", - matplotlib: "mpl", - modernizr: "mdr", - "moment.js": "mt", - openjdk: "java", - nginx: "ngx", - numpy: "np", - pandas: "pd", - postgresql: "pg", - python: "py", - "ruby.on.rails": "ror", - ruby: "rb", - rust: "rs", - sass: "scss", - tensorflow: "tf", - typescript: "ts", - "underscore.js": "_", - }; // Attributes: name, type, path - constructor() { super(...arguments); this.text = Entry.applyAliases(app.Searcher.normalizeString(this.name)); diff --git a/lib/app.rb b/lib/app.rb index e23b241c..939eab47 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -33,6 +33,42 @@ class App < Sinatra::Application set :default_docs, %w(css dom html http javascript) set :news_path, File.join(root, assets_prefix, 'javascripts', 'news.json') + set :docs_aliases, { + 'angular' => 'ng', + 'angular.js' => 'ng', + 'backbone.js' => 'bb', + 'c++' => 'cpp', + 'coffeescript' => 'cs', + 'crystal' => 'cr', + 'elixir' => 'ex', + 'javascript' => 'js', + 'julia' => 'jl', + 'jquery' => '$', + 'knockout.js' => 'ko', + 'kubernetes' => 'k8s', + 'less' => 'ls', + 'lodash' => '_', + 'löve' => 'love', + 'marionette' => 'mn', + 'markdown' => 'md', + 'matplotlib' => 'mpl', + 'modernizr' => 'mdr', + 'moment.js' => 'mt', + 'openjdk' => 'java', + 'nginx' => 'ngx', + 'numpy' => 'np', + 'pandas' => 'pd', + 'postgresql' => 'pg', + 'python' => 'py', + 'ruby.on.rails' => 'ror', + 'ruby' => 'rb', + 'rust' => 'rs', + 'sass' => 'scss', + 'tensorflow' => 'tf', + 'typescript' => 'ts', + 'underscore.js' => '_', + } + set :csp, false require 'docs' diff --git a/lib/docs/core/manifest.rb b/lib/docs/core/manifest.rb index 2a1f084c..134bb9e3 100644 --- a/lib/docs/core/manifest.rb +++ b/lib/docs/core/manifest.rb @@ -20,15 +20,7 @@ module Docs if doc.options[:attribution].is_a?(String) json[:attribution] = doc.options[:attribution].strip end - - # parse doc aliases from JS file as Ruby hash - entry_file = File.open("assets/javascripts/models/entry.js") - data = entry_file.read - aliases = eval data.split("ALIASES = ").last.split(";").first - - # set alias value - json["alias"] = aliases[json["slug"].try(:to_sym)] - + json[:alias] = App.docs_aliases[json["slug"].try(:to_sym)] result << json end end