1111require 'rugged'
1212require 'redcarpet'
1313require 'redcarpet/compat'
14+ require 'parallel'
1415require 'thread'
1516
1617# Markdown expects the old redcarpet compat API, so let's tell it what
@@ -118,69 +119,41 @@ def format_examples!(data, version)
118119 def generate_doc_for ( version )
119120 index = Rugged ::Index . new
120121 read_subtree ( index , version , option_version ( version , 'input' , '' ) )
122+
121123 data = parse_headers ( index , version )
122- data
124+ examples = format_examples! ( data , version )
125+ [ data , examples ]
123126 end
124127
125128 def process_project ( versions )
126- nversions = versions . size
127- output = Queue . new
128- pipes = { }
129- versions . each do |version |
130- # We don't need to worry about joining since this process is
131- # going to die immediately
132- read , write = IO . pipe
133- pid = Process . fork do
134- read . close
135-
136- data = generate_doc_for ( version )
137- examples = format_examples! ( data , version )
138-
139- Marshal . dump ( [ version , data , examples ] , write )
140- write . close
141- end
142-
143- pipes [ pid ] = read
144- write . close
145- end
146-
147- print "Generating documentation [0/#{ nversions } ]\r "
148-
149- # This may seem odd, but we need to keep reading from the pipe or
150- # the buffer will fill and they'll block and never exit. Therefore
151- # we can't rely on Process.wait to tell us when the work is
152- # done. Instead read from all the pipes concurrently and send the
153- # ruby objects through the queue.
154- Thread . abort_on_exception = true
155- pipes . each do |pid , read |
156- Thread . new do
157- result = read . read
158- output << Marshal . load ( result )
159- end
160- end
161-
162- for i in 1 ..nversions
163- version , data , examples = output . pop
164-
129+ nversions = versions . count
130+ Parallel . each_with_index ( versions , finish : -> ( version , index , result ) do
131+ data , examples = result
165132 # There's still some work we need to do serially
166133 tally_sigs! ( version , data )
167134 force_utf8 ( data )
168135
136+ puts "Adding documentation for #{ version } [#{ index } /#{ nversions } ]"
137+
169138 # Store it so we can show it at the end
170139 @head_data = data if version == 'HEAD'
171140
172- yield i , nversions , version , data , examples if block_given?
141+ yield index , version , result if block_given?
142+
143+ end ) do |version , index |
144+ puts "Generating documentation for #{ version } [#{ index } /#{ nversions } ]"
145+ generate_doc_for ( version )
173146 end
174147 end
175148
176- def generate_docs ( options )
149+ def generate_docs
177150 output_index = Rugged ::Index . new
178151 write_site ( output_index )
179152 @tf = File . expand_path ( File . join ( File . dirname ( __FILE__ ) , 'docurium' , 'layout.mustache' ) )
180153 versions = get_versions
181154 versions << 'HEAD'
182155 # If the user specified versions, validate them and overwrite
183- if !( vers = ( options [ :for ] || [ ] ) ) . empty?
156+ if !( vers = ( @cli_options [ :for ] || [ ] ) ) . empty?
184157 vers . each do |v |
185158 next if versions . include? ( v )
186159 puts "Unknown version #{ v } "
@@ -189,8 +162,9 @@ def generate_docs(options)
189162 versions = vers
190163 end
191164
192- process_project ( versions ) do |i , version , data , examples |
193- @repo . write ( data . to_json , :blob )
165+ process_project ( versions ) do |i , version , result |
166+ data , examples = result
167+ sha = @repo . write ( data . to_json , :blob )
194168
195169 print "Generating documentation [#{ i } /#{ versions . count } ]\r "
196170
@@ -314,7 +288,7 @@ def initialize(warning, type, identifier, opts = {})
314288
315289 def message
316290 msg = self . _message
317- msg . shift % msg . map { |a | self . send ( a ) . to_s } if msg . kind_of? ( Array )
291+ msg . kind_of? ( Array ) ? msg . shift % msg . map { |a | self . send ( a ) . to_s } : msg
318292 end
319293 end
320294
0 commit comments