-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathoptions_list_markdownizer.rb
55 lines (49 loc) · 1.56 KB
/
options_list_markdownizer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class OptionsListMarkdownizer
# Builds markdownized option list of a Gem::Command
def call(command)
ui = Gem::SilentUI.new
Gem::DefaultUserInteraction.use_ui ui do
# Invoke the Ruby options parser by asking for help. Otherwise the
# options list in the parser will never be initialized.
command.show_help
end
parser = command.send(:parser)
options = ''
helplines = parser.summarize
helplines.each do |helpline|
break if (helpline =~ /Arguments/) || (helpline =~ /Summary/)
next if helpline.strip == ''
helpline = markdownize_options helpline
if helpline =~ /^\s{10,}(.*)/
options = options[0..-2] + " #{$1}\n"
else
if helpline =~ /^(.+)\s{2,}(.*)/
helpline = "#{$1} - #{$2}"
end
if helpline =~ /options/i
options += "\n### #{helpline.strip.delete_suffix(":")}\n\n"
else
options += "* #{helpline.strip}\n"
end
end
end
options
end
# Marks options mentioned on the given summary line
def markdownize_options(line)
# Mark options up as code (also prevents change of -- to –)
option_line_re = /^(\s*)((?:--[^\s]+|-[^\s])(?:, (?:--[^\s]+|-[^\s]))*(?: [A-Za-z_\[\],]+)*)(\s{3,})(.+)/
if option_line_re =~ line
line.sub(option_line_re) do |m|
"#{$1}`#{$2}`#{$3}" +
markdownize_inline_options($4)
end
else
markdownize_inline_options(line)
end
end
private
def markdownize_inline_options(line)
line.gsub(/(?<=[\s\/])(--[\w\[\]\-]+|-\w)/, '`\1`')
end
end