misc/libfreetype/src/tools/docmaker/formatter.py
author koda
Mon, 25 Apr 2011 01:46:54 +0200
changeset 5172 88f2e05288ba
permissions -rw-r--r--
aaand let's add freetype as well while we are at it other smaller changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
#  Formatter (c) 2002, 2004, 2007, 2008 David Turner <david@freetype.org>
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
from sources import *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
from content import *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
from utils   import *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
# This is the base Formatter class.  Its purpose is to convert
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
# a content processor's data into specific documents (i.e., table of
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
# contents, global index, and individual API reference indices).
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
# You need to sub-class it to output anything sensible.  For example,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
# the file tohtml.py contains the definition of the HtmlFormatter sub-class
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
# used to output -- you guessed it -- HTML.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
class  Formatter:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
    def  __init__( self, processor ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
        self.processor   = processor
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
        self.identifiers = {}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
        self.chapters    = processor.chapters
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
        self.sections    = processor.sections.values()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
        self.block_index = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
        # store all blocks in a dictionary
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
        self.blocks = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
        for section in self.sections:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
            for block in section.blocks.values():
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
                self.add_identifier( block.name, block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
                # add enumeration values to the index, since this is useful
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
                for markup in block.markups:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
                    if markup.tag == 'values':
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
                        for field in markup.fields:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
                            self.add_identifier( field.name, block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
        self.block_index = self.identifiers.keys()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
        self.block_index.sort( index_sort )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
    def  add_identifier( self, name, block ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
        if self.identifiers.has_key( name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
            # duplicate name!
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
            sys.stderr.write(                                           \
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
               "WARNING: duplicate definition for '" + name + "' in " + \
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
               block.location() + ", previous definition in " +         \
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
               self.identifiers[name].location() + "\n" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
        else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
            self.identifiers[name] = block
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
    #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
    #  Formatting the table of contents
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
    #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
    def  toc_enter( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
    def  toc_chapter_enter( self, chapter ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
    def  toc_section_enter( self, section ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
    def  toc_section_exit( self, section ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
    def  toc_chapter_exit( self, chapter ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
    def  toc_index( self, index_filename ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
    def  toc_exit( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
    def  toc_dump( self, toc_filename = None, index_filename = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
        output = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
        if toc_filename:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
            output = open_output( toc_filename )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
        self.toc_enter()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
        for chap in self.processor.chapters:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
            self.toc_chapter_enter( chap )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
            for section in chap.sections:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
                self.toc_section_enter( section )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
                self.toc_section_exit( section )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
            self.toc_chapter_exit( chap )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
        self.toc_index( index_filename )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
        self.toc_exit()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
        if output:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
            close_output( output )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
    #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
    #  Formatting the index
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
    #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
    def  index_enter( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
    def  index_name_enter( self, name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
    def  index_name_exit( self, name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
    def  index_exit( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
    def  index_dump( self, index_filename = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
        output = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
        if index_filename:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
            output = open_output( index_filename )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
        self.index_enter()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
        for name in self.block_index:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
            self.index_name_enter( name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
            self.index_name_exit( name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
        self.index_exit()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
        if output:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
            close_output( output )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
    #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
    #  Formatting a section
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
    #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   133
    def  section_enter( self, section ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
    def  block_enter( self, block ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   138
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   139
    def  markup_enter( self, markup, block = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
    def  field_enter( self, field, markup = None, block = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
    def  field_exit( self, field, markup = None, block = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
    def  markup_exit( self, markup, block = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
    def  block_exit( self, block ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
    def  section_exit( self, section ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
        pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
    def  section_dump( self, section, section_filename = None ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   158
        output = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   159
        if section_filename:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   160
            output = open_output( section_filename )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   161
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   162
        self.section_enter( section )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   163
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   164
        for name in section.block_names:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   165
            block = self.identifiers[name]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   166
            self.block_enter( block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   167
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   168
            for markup in block.markups[1:]:   # always ignore first markup!
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   169
                self.markup_enter( markup, block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   170
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   171
                for field in markup.fields:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   172
                    self.field_enter( field, markup, block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   173
                    self.field_exit( field, markup, block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   174
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   175
                self.markup_exit( markup, block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   176
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   177
            self.block_exit( block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   178
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   179
        self.section_exit( section )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   180
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   181
        if output:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   182
            close_output( output )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   183
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   184
    def  section_dump_all( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   185
        for section in self.sections:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   186
            self.section_dump( section )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   187
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   188
# eof