-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdoc-preprocessor
executable file
·115 lines (93 loc) · 2.34 KB
/
doc-preprocessor
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env ruby
require 'json'
def parseOptions(paired_args)
options = {args: []}
index = 0
while(index < ARGV.length)
arg = ARGV[index]
paired_key = paired_args[arg]
if paired_key
if index >= ARGV.length - 1
puts "Unable to find a value for the arg: '" + arg + "'"
exit 1
end
options[paired_key] = ARGV[index+1]
index += 1
elsif /--.+/ =~ arg
puts "unknown option: " + arg
else
options[:args].push arg
end
index += 1
end
options
end
def requireArgs(args, count)
if args.length != count
puts "usage: docgen [input] [output] [...options]"
exit 1
end
end
def splitSections(content)
result = {}
current = ''
content.split("\n").each do |line|
if /^#( | \w).*/ =~ line
current = line.sub(/^#/, '').strip
# current = line
result[current] = result[current] || ""
else
result[current] += "\n" + line
end
end
result.keys.each do |key|
result[key] = result[key].strip
end
result
end
paired_args = {
'--section' => :section,
'--title' => :title,
'--transforms' => :transforms
}
options = parseOptions paired_args
requireArgs options[:args], 2
file_in = options[:args][0]
file_out = options[:args][1]
input = IO.read(file_in)
result = ''
# -----
if options[:section]
sections = splitSections input
section_name = options[:section]
if sections[section_name]
result = sections[section_name]
else
puts "Unable to find section: '" + section_name + "'"
exit 1
end
else
result = input
end
if options[:title]
result = "# #{options[:title]}\n\n" + result
end
# -----
jazzy_conf = JSON.parse IO.read '.jazzy.json'
if jazzy_conf['string-replacements']
transforms = jazzy_conf['string-replacements']
transforms.each_pair do |key, value|
result = result.sub key, value
end
end
version = `/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" ReactiveReSwift/Info.plist`.strip
if version
result = result.sub '{{version}}', version
end
# -----
if result == ''
puts "Not writing output as result is empty"
exit 1
end
IO.write(file_out, result)
puts "[Processed]: #{file_out}"