bbcode.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. """
  2. pygments.formatters.bbcode
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. BBcode formatter.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.formatter import Formatter
  9. from pygments.util import get_bool_opt
  10. __all__ = ['BBCodeFormatter']
  11. class BBCodeFormatter(Formatter):
  12. """
  13. Format tokens with BBcodes. These formatting codes are used by many
  14. bulletin boards, so you can highlight your sourcecode with pygments before
  15. posting it there.
  16. This formatter has no support for background colors and borders, as there
  17. are no common BBcode tags for that.
  18. Some board systems (e.g. phpBB) don't support colors in their [code] tag,
  19. so you can't use the highlighting together with that tag.
  20. Text in a [code] tag usually is shown with a monospace font (which this
  21. formatter can do with the ``monofont`` option) and no spaces (which you
  22. need for indentation) are removed.
  23. Additional options accepted:
  24. `style`
  25. The style to use, can be a string or a Style subclass (default:
  26. ``'default'``).
  27. `codetag`
  28. If set to true, put the output into ``[code]`` tags (default:
  29. ``false``)
  30. `monofont`
  31. If set to true, add a tag to show the code with a monospace font
  32. (default: ``false``).
  33. """
  34. name = 'BBCode'
  35. aliases = ['bbcode', 'bb']
  36. filenames = []
  37. def __init__(self, **options):
  38. Formatter.__init__(self, **options)
  39. self._code = get_bool_opt(options, 'codetag', False)
  40. self._mono = get_bool_opt(options, 'monofont', False)
  41. self.styles = {}
  42. self._make_styles()
  43. def _make_styles(self):
  44. for ttype, ndef in self.style:
  45. start = end = ''
  46. if ndef['color']:
  47. start += '[color=#%s]' % ndef['color']
  48. end = '[/color]' + end
  49. if ndef['bold']:
  50. start += '[b]'
  51. end = '[/b]' + end
  52. if ndef['italic']:
  53. start += '[i]'
  54. end = '[/i]' + end
  55. if ndef['underline']:
  56. start += '[u]'
  57. end = '[/u]' + end
  58. # there are no common BBcodes for background-color and border
  59. self.styles[ttype] = start, end
  60. def format_unencoded(self, tokensource, outfile):
  61. if self._code:
  62. outfile.write('[code]')
  63. if self._mono:
  64. outfile.write('[font=monospace]')
  65. lastval = ''
  66. lasttype = None
  67. for ttype, value in tokensource:
  68. while ttype not in self.styles:
  69. ttype = ttype.parent
  70. if ttype == lasttype:
  71. lastval += value
  72. else:
  73. if lastval:
  74. start, end = self.styles[lasttype]
  75. outfile.write(''.join((start, lastval, end)))
  76. lastval = value
  77. lasttype = ttype
  78. if lastval:
  79. start, end = self.styles[lasttype]
  80. outfile.write(''.join((start, lastval, end)))
  81. if self._mono:
  82. outfile.write('[/font]')
  83. if self._code:
  84. outfile.write('[/code]')
  85. if self._code or self._mono:
  86. outfile.write('\n')