Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output for docs #4746

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/crystal/tools/doc/constant.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ class Crystal::Doc::Constant
def formatted_value
Highlighter.highlight value.to_s
end

def to_json(builder : JSON::Builder)
builder.object do
builder.field "value", value.try(&.to_s)
builder.field "doc", doc
builder.field "summary", formatted_summary
end
end
end
17 changes: 15 additions & 2 deletions src/compiler/crystal/tools/doc/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ class Crystal::Doc::Generator
end

if filename
body = doc(program_type, File.read(filename))
raw_body = File.read(filename)
body = doc(program_type, raw_body)
else
raw_body = ""
body = ""
end

File.write File.join(@output_dir, "index.html"), MainTemplate.new(body, types, repository_name)

main_index = Main.new(raw_body, Type.new(self, @program), repository_name)
File.write File.join(@output_dir, "index.json"), main_index
end

def copy_files
Expand Down Expand Up @@ -350,7 +355,15 @@ class Crystal::Doc::Generator
filename[@base_dir.size..-1]
end

record RelativeLocation, filename : String, line_number : Int32, url : String?
record RelativeLocation, filename : String, line_number : Int32, url : String? do
def to_json(builder : JSON::Builder)
builder.object do
builder.field "filename", filename
builder.field "line_number", line_number
builder.field "url", url
end
end
end
SRC_SEP = "src#{File::SEPARATOR}"

def relative_locations(type)
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/crystal/tools/doc/macro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,19 @@ class Crystal::Doc::Macro
def must_be_included?
@generator.must_include? @macro
end

def to_json(builder : JSON::Builder)
builder.object do
builder.field "id", id
builder.field "html_id", html_id
builder.field "name", name
builder.field "doc", doc
builder.field "summary", formatted_summary
builder.field "abstract", abstract?
builder.field "args", args
builder.field "args_string", args_to_s
builder.field "source_link", source_link
builder.field "def", self.macro
end
end
end
15 changes: 15 additions & 0 deletions src/compiler/crystal/tools/doc/main.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Crystal::Doc
record Main, body : String, program : Type, repository_name : String do
def to_s(io : IO)
to_json(io)
end

def to_json(builder : JSON::Builder)
builder.object do
builder.field "repository_name", repository_name
builder.field "body", body
builder.field "program", program
end
end
end
end
16 changes: 16 additions & 0 deletions src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,20 @@ class Crystal::Doc::Method
def has_args?
[email protected]? || @def.block_arg || @def.yields
end

def to_json(builder : JSON::Builder)
builder.object do
builder.field "id", id
builder.field "html_id", html_id
builder.field "name", name
builder.field "doc", doc
builder.field "summary", formatted_summary
builder.field "abstract", abstract?
builder.field "args", args
builder.field "args_string", args_to_s
builder.field "source_link", source_link
builder.field "source_link", source_link
builder.field "def", self.def
end
end
end
41 changes: 41 additions & 0 deletions src/compiler/crystal/tools/doc/to_json.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Crystal::Arg
def to_json(builder : JSON::Builder)
builder.object do
builder.field "name", name
builder.field "doc", doc
builder.field "default_value", default_value.to_s
builder.field "external_name", external_name.to_s
builder.field "restriction", restriction.to_s
end
end
end

class Crystal::Def
def to_json(builder : JSON::Builder)
builder.object do
builder.field "name", name
builder.field "args", args
builder.field "double_splat", double_splat
builder.field "splat_index", splat_index
builder.field "yields", yields
builder.field "block_arg", block_arg
builder.field "return_type", return_type.to_s
builder.field "visibility", visibility.to_s
builder.field "body", body.to_s
end
end
end

class Crystal::Macro
def to_json(builder : JSON::Builder)
builder.object do
builder.field "name", name
builder.field "args", args
builder.field "double_splat", double_splat
builder.field "splat_index", splat_index
builder.field "block_arg", block_arg
builder.field "visibility", visibility.to_s
builder.field "body", body.to_s
end
end
end
66 changes: 64 additions & 2 deletions src/compiler/crystal/tools/doc/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ class Crystal::Doc::Type
end

def alias_definition
alias_def = @type.as(AliasType).aliased_type
alias_def = @type.as?(AliasType).try(&.aliased_type)
alias_def
end

def formatted_alias_definition
type_to_html alias_definition
type_to_html alias_definition.as(Crystal::Type)
end

@types : Array(Type)?
Expand Down Expand Up @@ -772,4 +772,66 @@ class Crystal::Doc::Type
end

delegate to_s, inspect, to: @type

def to_json(builder : JSON::Builder)
builder.object do
builder.field "html_id", html_id
builder.field "path", path
builder.field "kind", kind
builder.field "full_name", full_name
builder.field "name", name
builder.field "abstract", abstract?
builder.field "superclass" { superclass.try(&.to_json_simple(builder)) || builder.scalar(nil) }
builder.field "ancestors" do
builder.array do
ancestors.each &.to_json_simple(builder)
end
end
builder.field "locations", locations
builder.field "repository_name", repository_name
builder.field "program", program?
builder.field "enum", enum?
builder.field "alias", alias?
builder.field "aliased", alias_definition.to_s
builder.field "const", const?
builder.field "constants", constants
builder.field "included_modules" do
builder.array do
included_modules.each &.to_json_simple(builder)
end
end
builder.field "extended_modules" do
builder.array do
extended_modules.each &.to_json_simple(builder)
end
end
builder.field "subclasses" do
builder.array do
subclasses.each &.to_json_simple(builder)
end
end
builder.field "including_types" do
builder.array do
including_types.each &.to_json_simple(builder)
end
end
builder.field "namespace" { namespace.try(&.to_json_simple(builder)) || builder.scalar(nil) }
builder.field "doc", doc
builder.field "summary", formatted_summary
builder.field "class_methods", class_methods
builder.field "constructors", constructors
builder.field "instance_methods", instance_methods
builder.field "macros", macros
builder.field "types", types
end
end

def to_json_simple(builder : JSON::Builder)
builder.object do
builder.field "html_id", html_id
builder.field "kind", kind
builder.field "full_name", full_name
builder.field "name", name
end
end
end