Class: Betterdocs::Generator::Markdown
- Inherits:
-
Object
- Object
- Betterdocs::Generator::Markdown
show all
- Includes:
- ConfigShortcuts, FileUtils, Term::ANSIColor
- Defined in:
- lib/betterdocs/generator/markdown.rb
Instance Method Summary
collapse
#api_base_url, #config, #project_name, #section, #sections
Constructor Details
#initialize(only: nil) ⇒ Markdown
Returns a new instance of Markdown.
12
13
14
|
# File 'lib/betterdocs/generator/markdown.rb', line 12
def initialize(only: nil)
only and @only = Regexp.new(only)
end
|
Instance Method Details
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/betterdocs/generator/markdown.rb', line 33
def configure_for_creation
infobar.puts color(40, "Setting asset_host to #{Betterdocs::Global.asset_host.inspect}.")
Betterdocs.rails.configuration.action_controller.asset_host = Betterdocs::Global.asset_host
options = {
host: Betterdocs::Global.api_host,
protocol: Betterdocs::Global.api_protocol
}
infobar.puts color(40, "Setting default_url_options to #{options.inspect}.")
Betterdocs.rails.application.routes.default_url_options = options
self
end
|
#create_assets ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/betterdocs/generator/markdown.rb', line 73
def create_assets
Infobar(total: config.assets.size)
config.each_asset do |src, dst|
infobar.progress(
message: " Asset #{File.basename(src).inspect} %c/%t in %te ETA %e @%E ",
force: true
)
mkdir_p File.dirname(dst)
cp Betterdocs.rails.root.join(src), dst
end
infobar.finish message: ' %t assets created in %te, completed @%E '
infobar.newline
self
end
|
#create_readme(dirname) ⇒ Object
64
65
66
67
68
69
70
71
|
# File 'lib/betterdocs/generator/markdown.rb', line 64
def create_readme(dirname)
name = 'README.md'
cd dirname do
infobar.puts color(40, "Creating readme.")
render_to name, readme_template, binding
end
self
end
|
#create_sections(dirname) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/betterdocs/generator/markdown.rb', line 45
def create_sections(dirname)
Infobar(total: sections.size)
cd dirname do
for section in sections.values
infobar.progress(
message: " Section #{section.name.to_s.inspect} %c/%t in %te ETA %e @%E ",
force: true
)
if @only
@only =~ section.name or next
end
render_to "sections/#{section.name}.md", section_template, section.instance_eval('binding')
end
end
infobar.finish message: ' %t sections created in %te, completed @%E '
infobar.newline
self
end
|
#default_templates_directory ⇒ Object
113
114
115
|
# File 'lib/betterdocs/generator/markdown.rb', line 113
def default_templates_directory
File.join File.dirname(__FILE__), 'markdown', 'templates'
end
|
#fail_while_rendering(template, exception) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/betterdocs/generator/markdown.rb', line 90
def fail_while_rendering(template, exception)
message = color(
231,
on_color( 124, " *** ERROR #{exception.class}: #{exception.message.inspect} in template ***")
)
unless Betterdocs::Dsl::Controller::Action::Response::Error === exception
infobar.puts color(88, on_color(136, template))
end
infobar.puts color(136, (%w[Location:] + exception.backtrace) * "\n"),
message
exit 1
end
|
#generate ⇒ Object
16
17
18
19
20
21
22
|
# File 'lib/betterdocs/generator/markdown.rb', line 16
def generate
if dir = config.output_directory.full?
generate_to dir
else
fail "Specify an output_directory in your configuration!"
end
end
|
#generate_to(dirname) ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/betterdocs/generator/markdown.rb', line 24
def generate_to(dirname)
configure_for_creation
prepare_dir dirname
create_sections(dirname)
create_readme dirname
create_assets
self
end
|
#prepare_dir(dirname) ⇒ Object
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/betterdocs/generator/markdown.rb', line 144
def prepare_dir(dirname)
dirname.present? or raise ArgumentError,
"#{dirname.inspect} should be an explicite output dirname"
begin
stat = File.stat(dirname)
if stat.directory?
rm_rf Dir[dirname.to_s + '/**/*']
else
raise ArgumentError, "#{dirname.inspect} is not a directory"
end
rescue Errno::ENOENT
end
mkdir_p "#{dirname}/sections"
self
end
|
#provide_template(template_subpath) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/betterdocs/generator/markdown.rb', line 122
def provide_template(template_subpath)
if templates_directory = config.full?(:templates_directory)
path = File.expand_path(template_subpath, templates_directory)
File.file?(path) and return read_template(path)
end
path = File.expand_path(template_subpath, default_templates_directory)
File.file?(path) and return read_template(path)
message = "#{template_subpath.inspect} missing"
infobar.puts " *** #{message}"
"[#{message}]"
end
|
#read_template(filename) ⇒ Object
117
118
119
120
|
# File 'lib/betterdocs/generator/markdown.rb', line 117
def read_template(filename)
infobar.puts color(40, "Now reading #{filename.inspect}.")
File.read(filename)
end
|
#render_to(filename, template, binding) ⇒ Object
103
104
105
106
107
108
109
110
111
|
# File 'lib/betterdocs/generator/markdown.rb', line 103
def render_to(filename, template, binding)
File.open(filename, 'w') do |output|
rendered = ERB.new(template, trim_mode: '-').result(binding)
output.write rendered
end
self
rescue => e
fail_while_rendering(template, e)
end
|