/modules/freetype2/src/tools/docmaker/formatter.py

http://github.com/zpao/v8monkey · Python · 188 lines · 113 code · 52 blank · 23 comment · 20 complexity · 58b93fa28b4a0ccdbf2535397f08be91 MD5 · raw file

  1. # Formatter (c) 2002, 2004, 2007, 2008 David Turner <david@freetype.org>
  2. #
  3. from sources import *
  4. from content import *
  5. from utils import *
  6. # This is the base Formatter class. Its purpose is to convert
  7. # a content processor's data into specific documents (i.e., table of
  8. # contents, global index, and individual API reference indices).
  9. #
  10. # You need to sub-class it to output anything sensible. For example,
  11. # the file tohtml.py contains the definition of the HtmlFormatter sub-class
  12. # used to output -- you guessed it -- HTML.
  13. #
  14. class Formatter:
  15. def __init__( self, processor ):
  16. self.processor = processor
  17. self.identifiers = {}
  18. self.chapters = processor.chapters
  19. self.sections = processor.sections.values()
  20. self.block_index = []
  21. # store all blocks in a dictionary
  22. self.blocks = []
  23. for section in self.sections:
  24. for block in section.blocks.values():
  25. self.add_identifier( block.name, block )
  26. # add enumeration values to the index, since this is useful
  27. for markup in block.markups:
  28. if markup.tag == 'values':
  29. for field in markup.fields:
  30. self.add_identifier( field.name, block )
  31. self.block_index = self.identifiers.keys()
  32. self.block_index.sort( index_sort )
  33. def add_identifier( self, name, block ):
  34. if self.identifiers.has_key( name ):
  35. # duplicate name!
  36. sys.stderr.write( \
  37. "WARNING: duplicate definition for '" + name + "' in " + \
  38. block.location() + ", previous definition in " + \
  39. self.identifiers[name].location() + "\n" )
  40. else:
  41. self.identifiers[name] = block
  42. #
  43. # Formatting the table of contents
  44. #
  45. def toc_enter( self ):
  46. pass
  47. def toc_chapter_enter( self, chapter ):
  48. pass
  49. def toc_section_enter( self, section ):
  50. pass
  51. def toc_section_exit( self, section ):
  52. pass
  53. def toc_chapter_exit( self, chapter ):
  54. pass
  55. def toc_index( self, index_filename ):
  56. pass
  57. def toc_exit( self ):
  58. pass
  59. def toc_dump( self, toc_filename = None, index_filename = None ):
  60. output = None
  61. if toc_filename:
  62. output = open_output( toc_filename )
  63. self.toc_enter()
  64. for chap in self.processor.chapters:
  65. self.toc_chapter_enter( chap )
  66. for section in chap.sections:
  67. self.toc_section_enter( section )
  68. self.toc_section_exit( section )
  69. self.toc_chapter_exit( chap )
  70. self.toc_index( index_filename )
  71. self.toc_exit()
  72. if output:
  73. close_output( output )
  74. #
  75. # Formatting the index
  76. #
  77. def index_enter( self ):
  78. pass
  79. def index_name_enter( self, name ):
  80. pass
  81. def index_name_exit( self, name ):
  82. pass
  83. def index_exit( self ):
  84. pass
  85. def index_dump( self, index_filename = None ):
  86. output = None
  87. if index_filename:
  88. output = open_output( index_filename )
  89. self.index_enter()
  90. for name in self.block_index:
  91. self.index_name_enter( name )
  92. self.index_name_exit( name )
  93. self.index_exit()
  94. if output:
  95. close_output( output )
  96. #
  97. # Formatting a section
  98. #
  99. def section_enter( self, section ):
  100. pass
  101. def block_enter( self, block ):
  102. pass
  103. def markup_enter( self, markup, block = None ):
  104. pass
  105. def field_enter( self, field, markup = None, block = None ):
  106. pass
  107. def field_exit( self, field, markup = None, block = None ):
  108. pass
  109. def markup_exit( self, markup, block = None ):
  110. pass
  111. def block_exit( self, block ):
  112. pass
  113. def section_exit( self, section ):
  114. pass
  115. def section_dump( self, section, section_filename = None ):
  116. output = None
  117. if section_filename:
  118. output = open_output( section_filename )
  119. self.section_enter( section )
  120. for name in section.block_names:
  121. block = self.identifiers[name]
  122. self.block_enter( block )
  123. for markup in block.markups[1:]: # always ignore first markup!
  124. self.markup_enter( markup, block )
  125. for field in markup.fields:
  126. self.field_enter( field, markup, block )
  127. self.field_exit( field, markup, block )
  128. self.markup_exit( markup, block )
  129. self.block_exit( block )
  130. self.section_exit( section )
  131. if output:
  132. close_output( output )
  133. def section_dump_all( self ):
  134. for section in self.sections:
  135. self.section_dump( section )
  136. # eof