Created
June 10, 2024 09:06
-
-
Save ysbaddaden/c17c5602b601813a85ce323e5dcf1539 to your computer and use it in GitHub Desktop.
crystal docs: gen search.json from index.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
def cleanup_constant(type : JSON::Any) | |
type.as_h.select! do |key, _| | |
{ | |
"id", | |
"name", | |
"value", | |
"doc", | |
"summary", | |
}.includes?(key) | |
end | |
end | |
def cleanup_method(type : JSON::Any) | |
type.as_h.select! do |key, _| | |
{ | |
"html_id", | |
"name", | |
"doc", | |
"summary", | |
"args", | |
"args_string", | |
}.includes?(key) | |
end | |
if args = type["args"]? | |
type.as_h["args"] = JSON::Any.new(args.as_a.map { |arg| arg["name"] }) | |
end | |
end | |
def cleanup_type(type : JSON::Any) | |
type.as_h.select! do |key, _| | |
{ | |
"html_id", | |
"path", | |
"kind", | |
"full_name", | |
"name", | |
"doc", | |
"summary", | |
"constants", | |
"class_methods", | |
"constructors", | |
"instance_methods", | |
"macros", | |
"types" | |
}.includes?(key) | |
end | |
type["constants"]?.try(&.as_a.each { |c| cleanup_constant(c) }) | |
type["class_methods"]?.try(&.as_a.each { |m| cleanup_method(m) }) | |
type["constructors"]?.try(&.as_a.each { |m| cleanup_method(m) }) | |
type["instance_methods"]?.try(&.as_a.each { |m| cleanup_method(m) }) | |
type["macros"]?.try(&.as_a.each { |m| cleanup_method(m) }) | |
type["types"]?.try(&.as_a.each { |t| cleanup_type(t) }) | |
type | |
end | |
json = File.open("docs/index.json") { |file| JSON.parse(file) } | |
File.write("docs/search.json", cleanup_type(json["program"]).to_json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment