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

Add --format option to docs command #6982

Merged
merged 7 commits into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion src/compiler/crystal/command/docs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
# is in `crystal/tools/doc/`

class Crystal::Command
private VALID_OUTPUT_FORMATS = ["html", "json"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pedantic: %w(html json)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really an improvement, just a different way to express this.


private def docs
output_format = "html"
output_directory = File.join(".", "docs")
canonical_base_url = nil

Expand All @@ -20,6 +23,14 @@ class Crystal::Command
opts.on("--output=DIR", "-o DIR", "Set the output directory (default: #{output_directory})") do |value|
output_directory = value
end
opts.on("-f FORMAT", "--format=FORMAT", "Set the output format [json, html] (default: #{output_format})") do |value|
if !VALID_OUTPUT_FORMATS.includes? value
STDERR.puts "Invalid format '#{value}'"
puts opts
exit
end
output_format = value
end

opts.on("--canonical-base-url=URL", "-b URL", "Set the canonical base url") do |value|
canonical_base_url = value
Expand All @@ -46,6 +57,6 @@ class Crystal::Command
compiler.wants_doc = true
result = compiler.top_level_semantic sources

Doc::Generator.new(result.program, included_dirs, output_directory, canonical_base_url).run
Doc::Generator.new(result.program, included_dirs, output_directory, output_format, canonical_base_url).run
end
end
27 changes: 24 additions & 3 deletions src/compiler/crystal/tools/doc/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Crystal::Doc::Generator
},
}

def initialize(@program : Program, @included_dirs : Array(String), @output_dir : String, @canonical_base_url : String?)
def initialize(@program : Program, @included_dirs : Array(String), @output_dir : String, @output_format : String, @canonical_base_url : String?)
@base_dir = Dir.current.chomp
@types = {} of Crystal::Type => Doc::Type
@repo_name = ""
Expand All @@ -46,14 +46,35 @@ class Crystal::Doc::Generator
types.insert 0, program_type
end

generate_docs program_type, types
if @output_format == "json"
generate_docs_json program_type, types
else
generate_docs_html program_type, types
end
end

def program_type
type(@program)
end

def generate_docs(program_type, types)
def generate_docs_json(program_type, types)
if File.file?("README.md")
filename = "README.md"
elsif File.file?("Readme.md")
filename = "Readme.md"
end

if filename
raw_body = File.read(filename)
else
raw_body = ""
end

json = Main.new(raw_body, Type.new(self, @program), repository_name)
puts json
end

def generate_docs_html(program_type, types)
copy_files
generate_types_docs types, @output_dir, types
generate_readme program_type, types
Expand Down