Skip to content

Instantly share code, notes, and snippets.

@ysbaddaden
Created June 10, 2024 09:06
Show Gist options
  • Save ysbaddaden/c17c5602b601813a85ce323e5dcf1539 to your computer and use it in GitHub Desktop.
Save ysbaddaden/c17c5602b601813a85ce323e5dcf1539 to your computer and use it in GitHub Desktop.
crystal docs: gen search.json from index.json
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