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 option --output to crystal docs generator #4937

Merged
merged 1 commit into from
Oct 26, 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
23 changes: 22 additions & 1 deletion src/compiler/crystal/command/docs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@

class Crystal::Command
private def docs
output_directory = File.join(".", "docs")

OptionParser.parse(options) do |opts|
opts.banner = <<-'BANNER'
Usage: crystal docs [options]

Generates API documentation from inline docstrings in all Crystal files inside ./src directory.

Options:
BANNER

opts.on("--output=DIR", "-o DIR", "Set the output directory (default: #{output_directory})") do |value|
output_directory = value
end

opts.on("-h", "--help", "Show this message") do
puts opts
exit
end
end

if options.empty?
sources = [Compiler::Source.new("require", %(require "./src/**"))]
included_dirs = [] of String
Expand All @@ -20,6 +41,6 @@ class Crystal::Command
compiler.wants_doc = true
result = compiler.top_level_semantic sources

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

def initialize(@program : Program, @included_dirs : Array(String), @dir = "./doc")
@base_dir = `pwd`.chomp
def initialize(@program : Program, @included_dirs : Array(String), @output_dir : String)
@base_dir = Dir.current.chomp
@types = {} of Crystal::Type => Doc::Type
@repo_name = ""
@is_crystal_repo = false
compute_repository
end

def run
Dir.mkdir_p @dir
Dir.mkdir_p @output_dir

types = collect_subtypes(@program)

Expand All @@ -55,7 +55,7 @@ class Crystal::Doc::Generator

def generate_docs(program_type, types)
copy_files
generate_types_docs types, @dir, types
generate_types_docs types, @output_dir, types
generate_readme program_type, types
end

Expand All @@ -72,23 +72,23 @@ class Crystal::Doc::Generator
body = ""
end

File.write "#{@dir}/index.html", MainTemplate.new(body, types, repository_name)
File.write File.join(@output_dir, "index.html"), MainTemplate.new(body, types, repository_name)
end

def copy_files
Dir.mkdir_p "#{@dir}/css"
Dir.mkdir_p "#{@dir}/js"
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Dir.mkdir_p File.join(@output_dir, "css")
Dir.mkdir_p File.join(@output_dir, "js")

File.write "#{@dir}/css/style.css", StyleTemplate.new
File.write "#{@dir}/js/doc.js", JsTypeTemplate.new
File.write File.join(@output_dir, "css", "style.css"), StyleTemplate.new
File.write File.join(@output_dir, "js", "doc.js"), JsTypeTemplate.new
end

def generate_types_docs(types, dir, all_types)
types.each do |type|
if type.program?
filename = "#{dir}/toplevel.html"
filename = File.join(dir, "toplevel.html")
else
filename = "#{dir}/#{type.name}.html"
filename = File.join(dir, "#{type.name}.html")
end

File.write filename, TypeTemplate.new(type, all_types)
Expand All @@ -97,7 +97,7 @@ class Crystal::Doc::Generator

subtypes = type.types
if subtypes && !subtypes.empty?
dirname = "#{dir}/#{type.name}"
dirname = File.join(dir, type.name)
Dir.mkdir_p dirname
generate_types_docs subtypes, dirname, all_types
end
Expand Down