-
Notifications
You must be signed in to change notification settings - Fork 4
/
mkreadme
executable file
·78 lines (67 loc) · 1.76 KB
/
mkreadme
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
dst_file = 'README.md'
src_file = 'README.md.raw'
lines = Dir["plugins/zero/*"].
map{|f| File::read f}.
join("\n\n")
# list of regular aliases
$aliases = Hash[
lines.
scan(/alias ([^ =]*)=['"]?([^\n']*)['"]?/)
]
# list of global aliases
$galiases = Hash[
lines.
scan(/alias -g ([^ =]*)=(?:['"]?)([^\n']*)(?:['"]?)/)
]
# find all comments that match te documentation lines
# ie: use:, usage:, desc:
docs = lines.
# find all comments...
scan(/(?<=\n# ).*/).join("\n").
# ... that match use: or usage: or desc:
scan(/(?:use|usage|desc): [^\n]*(?:\n[ ]{4,}[^\n]+)*/)
# generate the description string
def doccy_desc(d)
d.sub(/^desc:[ ]*/,'').gsub(/\n[ ]*/,' ')
end
def doccy_use(d)
lines = d.sub(/^use:[ ]*/,'').gsub(/\n[ ]*/,"\n").split("\n")
def in_md( orig, r, nu )
return "%-30s # %-30s" % [orig, orig.gsub(r,nu)]
end
def is_alias( use )
$aliases.each do |k,v|
r = Regexp.new("^"+Regexp.escape(k)+"( |$)")
return in_md(use, r, v+" ") if use =~ r
end
nil
end
def is_galias( use )
$galiases.each do |k,v|
mr = Regexp.new(" "+Regexp.escape(k)+"( |$)")
return in_md(use, mr, " "+v+" ") if use =~ mr
end
nil
end
lines.map do |use|
is_alias(use) || is_galias(use) || use
end.compact.join("\n")
end
def doccy_usage(d)
d.sub(/^usage:[ ]*/,'').gsub(/\n[ ]*/,"\n")
end
def md_code(b, indent = 0)
b.split("\n").map do |line|
(" "*(indent+1))+line
end.join("\n")
end
md_docs = (docs.map do |d| case d
when /^desc: /; "\n* "+doccy_desc(d)+"\n"
when /^usage: /; md_code( doccy_usage(d), 1 )
when /^use: /; md_code( doccy_use(d), 1 )
else nil; end
end).flatten.compact
File::write dst_file,
File::read(src_file).
sub('PLUGIN_DOC', "\n"+md_docs.join("\n"))