/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: John Arbash Meinel
  • Date: 2007-08-08 14:34:56 UTC
  • mfrom: (2685 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2688.
  • Revision ID: john@arbash-meinel.com-20070808143456-wa00r4p8r146p689
[merge] bzr.dev 2685

Show diffs side-by-side

added added

removed removed

Lines of Context:
274
274
        s = s[:-1]
275
275
        return s
276
276
 
277
 
    def get_help_text(self, additional_see_also=None):
 
277
    def get_help_text(self, additional_see_also=None, plain=True,
 
278
                      see_also_as_links=False):
278
279
        """Return a text string with help for this command.
279
280
        
280
281
        :param additional_see_also: Additional help topics to be
281
282
            cross-referenced.
 
283
        :param plain: if False, raw help (reStructuredText) is
 
284
            returned instead of plain text.
 
285
        :param see_also_as_links: if True, convert items in 'See also'
 
286
            list to internal links (used by bzr_man rstx generator)
282
287
        """
283
288
        doc = self.help()
284
289
        if doc is None:
285
290
            raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
286
291
 
 
292
        # Extract the summary (purpose) and sections out from the text
 
293
        purpose,sections = self._get_help_parts(doc)
 
294
 
 
295
        # If a custom usage section was provided, use it
 
296
        if sections.has_key('Usage'):
 
297
            usage = sections.pop('Usage')
 
298
        else:
 
299
            usage = self._usage()
 
300
 
 
301
        # The header is the purpose and usage
287
302
        result = ""
288
 
        result += 'usage: %s\n' % self._usage()
289
 
 
 
303
        result += ':Purpose: %s\n' % purpose
 
304
        if usage.find('\n') >= 0:
 
305
            result += ':Usage:\n%s\n' % usage
 
306
        else:
 
307
            result += ':Usage:   %s\n' % usage
 
308
        result += '\n'
 
309
 
 
310
        # Add the options
 
311
        options = option.get_optparser(self.options()).format_option_help()
 
312
        if options.startswith('Options:'):
 
313
            result += ':' + options
 
314
        elif options.startswith('options:'):
 
315
            # Python 2.4 version of optparse
 
316
            result += ':Options:' + options[len('options:'):]
 
317
        else:
 
318
            result += options
 
319
        result += '\n'
 
320
 
 
321
        # Add the description, indenting it 2 spaces
 
322
        # to match the indentation of the options
 
323
        if sections.has_key(None):
 
324
            text = sections.pop(None)
 
325
            text = '\n  '.join(text.splitlines())
 
326
            result += ':%s:\n  %s\n\n' % ('Description',text)
 
327
 
 
328
        # Add the custom sections (e.g. Examples). Note that there's no need
 
329
        # to indent these as they must be indented already in the source.
 
330
        if sections:
 
331
            labels = sorted(sections.keys())
 
332
            for label in labels:
 
333
                result += ':%s:\n%s\n\n' % (label,sections[label])
 
334
 
 
335
        # Add the aliases, source (plug-in) and see also links, if any
290
336
        if self.aliases:
291
 
            result += 'aliases: '
 
337
            result += ':Aliases:  '
292
338
            result += ', '.join(self.aliases) + '\n'
293
 
 
294
 
        result += '\n'
295
 
 
296
339
        plugin_name = self.plugin_name()
297
340
        if plugin_name is not None:
298
 
            result += '(From plugin "%s")' % plugin_name
299
 
            result += '\n\n'
300
 
 
301
 
        result += doc
302
 
        if result[-1] != '\n':
303
 
            result += '\n'
304
 
        result += '\n'
305
 
        result += option.get_optparser(self.options()).format_option_help()
 
341
            result += ':From:     plugin "%s"\n' % plugin_name
306
342
        see_also = self.get_see_also(additional_see_also)
307
343
        if see_also:
308
 
            result += '\nSee also: '
309
 
            result += ', '.join(see_also)
310
 
            result += '\n'
 
344
            if not plain and see_also_as_links:
 
345
                see_also_links = []
 
346
                for item in see_also:
 
347
                    if item == 'topics':
 
348
                        # topics doesn't have an independent section
 
349
                        # so don't create a real link
 
350
                        see_also_links.append(item)
 
351
                    else:
 
352
                        # Use a reST link for this entry
 
353
                        see_also_links.append("`%s`_" % (item,))
 
354
                see_also = see_also_links
 
355
            result += ':See also: '
 
356
            result += ', '.join(see_also) + '\n'
 
357
 
 
358
        # If this will be rendered as plan text, convert it
 
359
        if plain:
 
360
            import bzrlib.help_topics
 
361
            result = bzrlib.help_topics.help_as_plain_text(result)
311
362
        return result
312
363
 
 
364
    @staticmethod
 
365
    def _get_help_parts(text):
 
366
        """Split help text into a summary and named sections.
 
367
 
 
368
        :return: (summary,sections) where summary is the top line and
 
369
            sections is a dictionary of the rest indexed by section name.
 
370
            A section starts with a heading line of the form ":xxx:".
 
371
            Indented text on following lines is the section value.
 
372
            All text found outside a named section is assigned to the
 
373
            default section which is given the key of None.
 
374
        """
 
375
        def save_section(sections, label, section):
 
376
            if len(section) > 0:
 
377
                if sections.has_key(label):
 
378
                    sections[label] += '\n' + section
 
379
                else:
 
380
                    sections[label] = section
 
381
            
 
382
        lines = text.rstrip().splitlines()
 
383
        summary = lines.pop(0)
 
384
        sections = {}
 
385
        label,section = None,''
 
386
        for line in lines:
 
387
            if line.startswith(':') and line.endswith(':') and len(line) > 2:
 
388
                save_section(sections, label, section)
 
389
                label,section = line[1:-1],''
 
390
            elif label != None and len(line) > 1 and not line[0].isspace():
 
391
                save_section(sections, label, section)
 
392
                label,section = None,line
 
393
            else:
 
394
                if len(section) > 0:
 
395
                    section += '\n' + line
 
396
                else:
 
397
                    section = line
 
398
        save_section(sections, label, section)
 
399
        return summary, sections
 
400
 
313
401
    def get_help_topic(self):
314
402
        """Return the commands help topic - its name."""
315
403
        return self.name()
316
404
 
317
405
    def get_see_also(self, additional_terms=None):
318
 
        """Return a list of help topics that are related to this ommand.
 
406
        """Return a list of help topics that are related to this command.
319
407
        
320
408
        The list is derived from the content of the _see_also attribute. Any
321
409
        duplicates are removed and the result is in lexical order.