1 # $Id: module-infobox.rb,v 1.22 2012/04/17 02:49:40 machan Exp $
2
3 require 'tmdoc/tmstd'
4 require 'tmdoc/tmstd/docbook'
5 require 'tmdoc/constant'
6 require 'tmdoc/model/document'
7 require 'tmdoc/transformer/document-into-docbook/node'
8 require 'tmdoc/transformer/document-into-docbook/link'
9 require 'tmdoc/transformer/localizable-string'
10
11
12 module TmDoc
13
14 module Transformer::DocumentIntoDocBook
15
16 module Module
17
18 module InfoBox
19
20 module_function
21
22 def transform(md_infobox, module_id, show_line_num, show_code)
23 ASSERT.kind_of md_infobox, MDA::InfoBox::Abstract
24 ASSERT.kind_of module_id, String
25 ASSERT.boolean show_line_num
26 ASSERT.boolean show_code
27
28 contents = md_infobox.rows.map { |md_row|
29 transform_row(md_row, module_id, show_line_num, show_code)
30 }.compact
31 return nil if contents.empty?
32
33 DBOOKEX.informal_table('ll', :colsep => 0, :rowsep => 1) {
34 contents
35 }
36 end
37
38
39 def transform_row(md_row, module_id, show_line_num, show_code)
40 ASSERT.kind_of md_row, MDA::InfoBox::Row
41 ASSERT.kind_of module_id, String
42 ASSERT.boolean show_line_num
43 ASSERT.boolean show_code
44
45 entry_rows = md_row.entry_rows
46 return nil if entry_rows.empty?
47
48 row_label, show_entry_row_label, show_above_module =
49 case md_row
50 when MDM::InfoBox::Row::AboveModule
51 [TLS::LAB_A_SUBMODULE_OF, true, true]
52 when MDM::InfoBox::Row::BelowModules
53 [TLS::LAB_SUBMODULES, true, false]
54 when MDM::InfoBox::Row::SiblingModules
55 [TLS::LAB_SIBLING_MODULES, true, false]
56 when MDM::InfoBox::Row::ExtendeeModules
57 [TLS::LAB_EXTENDEES, false, true]
58 when MDM::InfoBox::Row::ExtenderModules
59 [TLS::LAB_EXTENDERS, true, true]
60 when MDM::InfoBox::Row::IncludeeModules
61 [TLS::LAB_INCLUDEES, false, true]
62 when MDM::InfoBox::Row::IncluderModules
63 [TLS::LAB_INCLUDERS, true, true]
64 when MDM::InfoBox::Row::Superclass
65 [TLS::LAB_SUPERCLASS, false, true]
66 when MDM::InfoBox::Row::Subclasses
67 [TLS::LAB_SUBCLASSES, false, true]
68 when MDM::InfoBox::Row::SiblingClasses
69 [TLS::LAB_SIBLING_CLASSES, false, true]
70 when MDM::InfoBox::Row::Properties
71 [TLS::LAB_PROPERTIES, true, false]
72 when MDM::InfoBox::Row::Locations
73 [TLS::LAB_LOCATIONS, false, false]
74 else
75 ASSERT.abort "Unknown row class: %s", md_row.class.to_s
76 end
77
78 DBOOK.row {
79 [
80 DBOOK.entry(:valign => 'top') {
81 [ DBOOK.text(row_label) ]
82 },
83
84 DBOOKEX.entry_table('rlll') {
85 entry_rows.map { |entry_row|
86 EntryRow.transform(
87 entry_row,
88 module_id,
89 show_entry_row_label,
90 show_above_module,
91 show_line_num,
92 show_code
93 )
94 }
95 }
96 ]
97 }
98 end
99
100
101 module EntryRow
102
103 class NamedEntryRow < TmStd::Lsm::Product::Abstract
104 attr_reader :label,
105 :name_contents,
106 :opt_contents_1,
107 :opt_contents_2
108
109
110 def initialize(
111 label, name_contents, list_of_opt_contents = []
112 )
113 ASSERT.kind_of label, String
114 ASSERT.kind_of name_contents, Array
115 ASSERT.kind_of list_of_opt_contents, Array
116 ASSERT.assert list_of_opt_contents.length <= 2
117
118 opt_contents_1, opt_contents_2 = list_of_opt_contents
119 ASSERT.opt_kind_of opt_contents_1, Array
120 ASSERT.opt_kind_of opt_contents_2, Array
121
122 @label = label
123 @name_contents = name_contents
124 @opt_contents_1 = opt_contents_1
125 @opt_contents_2 = opt_contents_2
126 end
127 end
128
129
130
131 module_function
132
133 def transform(
134 md_entry_row, module_id,
135 show_label, show_above_module, show_line_num, show_code
136 )
137 ASSERT.kind_of md_entry_row, MDA::InfoBox::EntryRow::Abstract
138 ASSERT.kind_of module_id, String
139 ASSERT.boolean show_label
140 ASSERT.boolean show_above_module
141 ASSERT.boolean show_line_num
142 ASSERT.boolean show_code
143
144 db_row =
145 case md_entry_row
146 when MDA::InfoBox::EntryRow::NamedEntryRow
147 transform_named_entry_row(
148 md_entry_row, module_id,
149 show_label, show_above_module, show_line_num
150 )
151 when MDM::InfoBox::EntryRow::Location
152 transform_location(
153 md_entry_row, show_code
154 )
155 else
156 ASSERT.abort md_entry_row.class.to_s
157 end
158
159 ASSERT.kind_of db_row, TmStd::DocBook::Node::Row::Element
160 end
161
162
163 def transform_named_entry_row(
164 md_entry_row, module_id,
165 show_label, show_above_module, show_line_num
166 )
167 ASSERT.kind_of md_entry_row, MDA::InfoBox::EntryRow::Abstract
168 ASSERT.kind_of module_id, String
169 ASSERT.boolean show_label
170 ASSERT.boolean show_above_module
171 ASSERT.boolean show_line_num
172
173 name = md_entry_row.name
174 row =
175 case md_entry_row
176 when MDM::InfoBox::EntryRow::Module,
177 MDM::InfoBox::EntryRow::Class
178 transform_module(
179 md_entry_row, show_label, show_above_module
180 )
181 when MDA::InfoBox::EntryRow::Property
182 md_property_row = md_entry_row
183
184 label = TLS::HASH_OF_TAG_TO_LABEL[md_property_row.tag]
185 ASSERT.kind_of(label, String,
186 "Unknown tag: <%s> in md_property: <%s>",
187 md_property_row.tag, md_property_row.to_s
188 )
189 md_link_to_line =
190 if show_line_num
191 md_property_row.a_link_to_line
192 else
193 nil
194 end
195
196 args = [
197 label,
198 name,
199 module_id,
200 md_link_to_line,
201 md_property_row.uniq_num,
202 md_property_row.max_uniq_num
203 ]
204
205 case md_property_row
206 when MDM::InfoBox::EntryRow::Constant
207 transform_constant(
208 label, name, module_id, md_link_to_line
209 )
210 when MDM::InfoBox::EntryRow::Alias
211 transform_alias *(args << md_property_row.orig_name)
212 when MDM::InfoBox::EntryRow::Attribute
213 transform_attribute *(args << md_property_row.accessor)
214 when MDM::InfoBox::EntryRow::Method
215 transform_method *(args << md_property_row.num_of_args)
216 else
217 ASSERT.abort md_property_row.class.to_s
218 end
219 else
220 ASSERT.abort md_entry_row.class.to_s
221 end
222 ASSERT.kind_of row, NamedEntryRow
223
224 db_row = DBOOK.row {
225 [
226 DBOOK.entry {
227 if show_label
228 [ DBOOK.text(row.label + ':') ]
229 else
230 []
231 end
232 },
233
234 DBOOK.entry { row.name_contents },
235
236 DBOOK.entry {
237 if row.opt_contents_1
238 row.opt_contents_1
239 else
240 []
241 end
242 },
243
244 DBOOK.entry {
245 if row.opt_contents_2
246 row.opt_contents_2
247 else
248 []
249 end
250 }
251 ]
252 }
253
254 ASSERT.kind_of db_row, TmStd::DocBook::Node::Row::Element
255 end
256
257
258 def transform_module(md_entry_row, show_label, show_above_module)
259 ASSERT.kind_of md_entry_row, MDA::InfoBox::EntryRow::Abstract
260 ASSERT.boolean show_label
261 ASSERT.boolean show_above_module
262
263 md_link_to_module = md_entry_row.a_link_to_module
264 above_path = md_link_to_module.above_path
265 name = md_link_to_module.name
266
267 md_link_to_above_module =
268 if show_above_module &&
269 md_link_to_module.a_link_to_above_module
270 md_link_to_module.a_link_to_above_module
271 else
272 nil
273 end
274
275
276 NamedEntryRow.new(
277 if md_entry_row.kind_of?(MDM::InfoBox::EntryRow::Class)
278 TLS::CLASS
279 else
280 TLS::MODULE
281 end,
282
283 [
284 Link.transform_module(
285 Id.transform_module(above_path, name),
286 Link.display_name_of_module(md_link_to_module, name)
287 )
288 ],
289
290 [
291 if md_link_to_above_module
292 [ DBOOK.text('(') ]
293 else
294 nil
295 end,
296
297 if md_link_to_above_module
298 [
299 Link.transform_link_to_module(
300 md_link_to_above_module
301 ),
302
303 DBOOK.text(')')
304 ]
305 else
306 nil
307 end
308 ]
309 )
310 end
311
312
313 def transform_constant(label, name, module_id, md_link_to_line)
314 ASSERT.kind_of label, String
315 ASSERT.kind_of name, String
316 ASSERT.kind_of module_id, String
317 ASSERT.opt_kind_of md_link_to_line, MDL::Line
318
319 id = Id.transform_property module_id, name
320
321 NamedEntryRow.new(
322 label,
323
324 [
325 Link.transform_constant(id, name)
326 ],
327
328 transform_line_num(md_link_to_line)
329 )
330 end
331
332
333 def transform_alias(
334 label, name, module_id, md_link_to_line, uniq_num, max_uniq_num,
335 orig_name
336 )
337 ASSERT.kind_of label, String
338 ASSERT.kind_of name, String
339 ASSERT.kind_of module_id, String
340 ASSERT.opt_kind_of md_link_to_line, MDL::Line
341 ASSERT.kind_of uniq_num, Integer
342 ASSERT.kind_of max_uniq_num, Integer
343 ASSERT.kind_of orig_name, String
344
345 id = Id.transform_property module_id, name, uniq_num
346
347 NamedEntryRow.new(
348 label,
349
350 [
351 Link.transform_alias(id, name, uniq_num, max_uniq_num),
352
353 DBOOK.raw_text('⇐'),
354
355 DBOOK.literal(orig_name)
356 ],
357
358 transform_line_num(md_link_to_line)
359 )
360 end
361
362
363 def transform_attribute(
364 label, name, module_id, md_link_to_line, uniq_num, max_uniq_num,
365 accessor
366 )
367 ASSERT.kind_of label, String
368 ASSERT.kind_of name, String
369 ASSERT.kind_of module_id, String
370 ASSERT.opt_kind_of md_link_to_line, MDL::Line
371 ASSERT.kind_of uniq_num, Integer
372 ASSERT.kind_of max_uniq_num, Integer
373 ASSERT.kind_of accessor, String
374
375 id = Id.transform_property module_id, name, uniq_num
376
377 NamedEntryRow.new(
378 label,
379
380 [
381 Link.transform_attribute(id, name, uniq_num, max_uniq_num),
382
383 DBOOK.text(format("[%s]", accessor))
384 ],
385
386 transform_line_num(md_link_to_line)
387 )
388 end
389
390
391 def transform_method(
392 label, name, module_id, md_link_to_line, uniq_num, max_uniq_num,
393 num_of_args
394 )
395 ASSERT.kind_of label, String
396 ASSERT.kind_of name, String
397 ASSERT.kind_of module_id, String
398 ASSERT.opt_kind_of md_link_to_line, MDL::Line
399 ASSERT.kind_of uniq_num, Integer
400 ASSERT.kind_of max_uniq_num, Integer
401 ASSERT.kind_of num_of_args, Integer
402
403 id = Id.transform_property module_id, name, uniq_num
404
405 NamedEntryRow.new(
406 label,
407
408 [
409 Link.transform_method(id, name, uniq_num, max_uniq_num)
410 ] + (
411 if num_of_args == 0
412 []
413 else
414 [ DBOOK.text(format(" / %d", num_of_args)) ]
415 end
416 ),
417
418 transform_line_num(md_link_to_line)
419 )
420 end
421
422
423 def transform_line_num(md_link_to_line)
424 ASSERT.opt_kind_of md_link_to_line, MDL::Line
425
426 if md_link_to_line && md_link_to_line.line_num >= 1
427 [
428 [ DBOOK.raw_text('—') ],
429
430 [ Link.transform_line(md_link_to_line) ]
431 ]
432 else
433 []
434 end
435 end
436
437
438 def transform_location(md_entry_row, show_code)
439 ASSERT.kind_of md_entry_row, MDA::InfoBox::EntryRow::Abstract
440 ASSERT.boolean show_code
441
442 db_row = DBOOK.row {
443 [
444 Link.transform_lines(md_entry_row.links_to_line, show_code),
445
446 DBOOK.text('in'),
447
448 Link.transform_file(md_entry_row.a_link_to_file, show_code)
449 ].map { |contents| DBOOK.entry { [ contents ] } }
450 }
451
452 ASSERT.kind_of db_row, TmStd::DocBook::Node::Row::Element
453 end
454 end
455
456 end
457
458 end # TmDoc::Transformer::DocumentIntoDocBook::Module
459
460 end # TmDoc::Transformer::DocumentIntoDocBook
461
462 end # TmDoc