misc/libfreetype/src/tools/docmaker/content.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
#  Content (c) 2002, 2004, 2006, 2007, 2008, 2009
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
#    David Turner <david@freetype.org>
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
#  This file contains routines used to parse the content of documentation
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
#  comment blocks and build more structured objects out of them.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
#
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
from sources import *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
from utils import *
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
import string, re
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
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
# this regular expression is used to detect code sequences. these
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
# are simply code fragments embedded in '{' and '}' like in:
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
#    x = y + z;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
#    if ( zookoo == 2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
#    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
#      foobar();
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
#    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
#  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
# note that indentation of the starting and ending accolades must be
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
# exactly the same. the code sequence can contain accolades at greater
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
# indentation
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
re_code_start = re.compile( r"(\s*){\s*$" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
re_code_end   = re.compile( r"(\s*)}\s*$" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
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
# this regular expression is used to isolate identifiers from
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
# other text
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
re_identifier = re.compile( r'(\w*)' )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
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
# we collect macros ending in `_H'; while outputting the object data, we use
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
# this info together with the object's file location to emit the appropriate
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
# header file macro and name before the object itself
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
re_header_macro = re.compile( r'^#define\s{1,}(\w{1,}_H)\s{1,}<(.*)>' )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
#############################################################################
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
# The DocCode class is used to store source code lines.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
#   'self.lines' contains a set of source code lines that will be dumped as
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
#   HTML in a <PRE> tag.
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
#   The object is filled line by line by the parser; it strips the leading
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
#   "margin" space from each input line before storing it in 'self.lines'.
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
class  DocCode:
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  __init__( self, margin, lines ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
        self.lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
        self.words = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
        # remove margin spaces
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
        for l in lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
            if string.strip( l[:margin] ) == "":
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
                l = l[margin:]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
            self.lines.append( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
    def  dump( self, prefix = "", width = 60 ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
        lines = self.dump_lines( 0, width )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
        for l in lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
            print prefix + l
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  dump_lines( self, margin = 0, width = 60 ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
        result = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
        for l in self.lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
            result.append( " " * margin + l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
        return result
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
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
#############################################################################
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
# The DocPara class is used to store "normal" text paragraph.
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.words' contains the list of words that make up the paragraph
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
class  DocPara:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
    def  __init__( self, lines ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
        self.lines = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
        self.words = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
        for l in lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
            l = string.strip( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
            self.words.extend( string.split( l ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
    def  dump( self, prefix = "", width = 60 ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
        lines = self.dump_lines( 0, width )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
        for l in lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
            print prefix + l
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
    def  dump_lines( self, margin = 0, width = 60 ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
        cur    = ""  # current line
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
        col    = 0   # current width
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
        result = []
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
        for word in self.words:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
            ln = len( word )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
            if col > 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
                ln = ln + 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
            if col + ln > width:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
                result.append( " " * margin + cur )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
                cur = word
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
                col = len( word )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
            else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
                if col > 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
                    cur = cur + " "
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
                cur = cur + word
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
                col = col + ln
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
        if col > 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
            result.append( " " * margin + cur )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
        return result
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
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
#############################################################################
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
#  The DocField class is used to store a list containing either DocPara or
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
#  DocCode objects. Each DocField also has an optional "name" which is used
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
#  when the object corresponds to a field or value definition
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
class  DocField:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
    def  __init__( self, name, lines ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
        self.name  = name  # can be None for normal paragraphs/sources
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
        self.items = []    # list of items
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
        mode_none  = 0     # start parsing mode
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
        mode_code  = 1     # parsing code sequences
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
        mode_para  = 3     # parsing normal paragraph
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
        margin     = -1    # current code sequence indentation
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
        cur_lines  = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
        # now analyze the markup lines to see if they contain paragraphs,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
        # code sequences or fields definitions
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
        #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
        start = 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
        mode  = mode_none
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
        for l in lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
            # are we parsing a code sequence ?
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
            if mode == mode_code:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
                m = re_code_end.match( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
                if m and len( m.group( 1 ) ) <= margin:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
                    # that's it, we finished the code sequence
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   158
                    code = DocCode( 0, cur_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   159
                    self.items.append( code )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   160
                    margin    = -1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   161
                    cur_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   162
                    mode      = mode_none
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   163
                else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   164
                    # nope, continue the code sequence
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   165
                    cur_lines.append( l[margin:] )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   166
            else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   167
                # start of code sequence ?
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   168
                m = re_code_start.match( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   169
                if m:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   170
                    # save current lines
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   171
                    if cur_lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   172
                        para = DocPara( cur_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   173
                        self.items.append( para )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   174
                        cur_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   175
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   176
                    # switch to code extraction mode
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   177
                    margin = len( m.group( 1 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   178
                    mode   = mode_code
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   179
                else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   180
                    if not string.split( l ) and cur_lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   181
                        # if the line is empty, we end the current paragraph,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   182
                        # if any
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   183
                        para = DocPara( cur_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   184
                        self.items.append( para )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   185
                        cur_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   186
                    else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   187
                        # otherwise, simply add the line to the current
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   188
                        # paragraph
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   189
                        cur_lines.append( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   190
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   191
        if mode == mode_code:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   192
            # unexpected end of code sequence
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   193
            code = DocCode( margin, cur_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   194
            self.items.append( code )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   195
        elif cur_lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   196
            para = DocPara( cur_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   197
            self.items.append( para )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   198
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   199
    def  dump( self, prefix = "" ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   200
        if self.field:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   201
            print prefix + self.field + " ::"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   202
            prefix = prefix + "----"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   203
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   204
        first = 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   205
        for p in self.items:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   206
            if not first:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   207
                print ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   208
            p.dump( prefix )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   209
            first = 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   210
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   211
    def  dump_lines( self, margin = 0, width = 60 ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   212
        result = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   213
        nl     = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   214
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   215
        for p in self.items:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   216
            if nl:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   217
                result.append( "" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   218
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   219
            result.extend( p.dump_lines( margin, width ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   220
            nl = 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   221
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   222
        return result
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   223
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   224
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   225
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   226
# this regular expression is used to detect field definitions
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   227
#
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   228
re_field = re.compile( r"\s*(\w*|\w(\w|\.)*\w)\s*::" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   229
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   230
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   231
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   232
class  DocMarkup:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   233
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   234
    def  __init__( self, tag, lines ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   235
        self.tag    = string.lower( tag )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   236
        self.fields = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   237
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   238
        cur_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   239
        field     = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   240
        mode      = 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   241
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   242
        for l in lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   243
            m = re_field.match( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   244
            if m:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   245
                # we detected the start of a new field definition
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   246
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   247
                # first, save the current one
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   248
                if cur_lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   249
                    f = DocField( field, cur_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   250
                    self.fields.append( f )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   251
                    cur_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   252
                    field     = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   253
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   254
                field     = m.group( 1 )   # record field name
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   255
                ln        = len( m.group( 0 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   256
                l         = " " * ln + l[ln:]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   257
                cur_lines = [l]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   258
            else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   259
                cur_lines.append( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   260
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   261
        if field or cur_lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   262
            f = DocField( field, cur_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   263
            self.fields.append( f )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   264
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   265
    def  get_name( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   266
        try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   267
            return self.fields[0].items[0].words[0]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   268
        except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   269
            return None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   270
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   271
    def  get_start( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   272
        try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   273
            result = ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   274
            for word in self.fields[0].items[0].words:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   275
                result = result + " " + word
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   276
            return result[1:]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   277
        except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   278
            return "ERROR"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   279
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   280
    def  dump( self, margin ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   281
        print " " * margin + "<" + self.tag + ">"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   282
        for f in self.fields:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   283
            f.dump( "  " )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   284
        print " " * margin + "</" + self.tag + ">"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   285
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   286
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   287
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   288
class  DocChapter:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   289
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   290
    def  __init__( self, block ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   291
        self.block    = block
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   292
        self.sections = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   293
        if block:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   294
            self.name  = block.name
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   295
            self.title = block.get_markup_words( "title" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   296
            self.order = block.get_markup_words( "sections" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   297
        else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   298
            self.name  = "Other"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   299
            self.title = string.split( "Miscellaneous" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   300
            self.order = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   301
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   302
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   303
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   304
class  DocSection:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   305
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   306
    def  __init__( self, name = "Other" ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   307
        self.name        = name
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   308
        self.blocks      = {}
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   309
        self.block_names = []  # ordered block names in section
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   310
        self.defs        = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   311
        self.abstract    = ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   312
        self.description = ""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   313
        self.order       = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   314
        self.title       = "ERROR"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   315
        self.chapter     = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   316
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   317
    def  add_def( self, block ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   318
        self.defs.append( block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   319
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   320
    def  add_block( self, block ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   321
        self.block_names.append( block.name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   322
        self.blocks[block.name] = block
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   323
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   324
    def  process( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   325
        # look up one block that contains a valid section description
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   326
        for block in self.defs:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   327
            title = block.get_markup_text( "title" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   328
            if title:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   329
                self.title       = title
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   330
                self.abstract    = block.get_markup_words( "abstract" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   331
                self.description = block.get_markup_items( "description" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   332
                self.order       = block.get_markup_words( "order" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   333
                return
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   334
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   335
    def  reorder( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   336
        self.block_names = sort_order_list( self.block_names, self.order )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   337
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   338
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   339
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   340
class  ContentProcessor:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   341
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   342
    def  __init__( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   343
        """initialize a block content processor"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   344
        self.reset()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   345
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   346
        self.sections = {}    # dictionary of documentation sections
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   347
        self.section  = None  # current documentation section
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   348
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   349
        self.chapters = []    # list of chapters
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   350
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   351
        self.headers  = {}    # dictionary of header macros
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   352
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   353
    def  set_section( self, section_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   354
        """set current section during parsing"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   355
        if not self.sections.has_key( section_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   356
            section = DocSection( section_name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   357
            self.sections[section_name] = section
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   358
            self.section                = section
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   359
        else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   360
            self.section = self.sections[section_name]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   361
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   362
    def  add_chapter( self, block ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   363
        chapter = DocChapter( block )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   364
        self.chapters.append( chapter )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   365
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   366
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   367
    def  reset( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   368
        """reset the content processor for a new block"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   369
        self.markups      = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   370
        self.markup       = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   371
        self.markup_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   372
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   373
    def  add_markup( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   374
        """add a new markup section"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   375
        if self.markup and self.markup_lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   376
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   377
            # get rid of last line of markup if it's empty
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   378
            marks = self.markup_lines
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   379
            if len( marks ) > 0 and not string.strip( marks[-1] ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   380
                self.markup_lines = marks[:-1]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   381
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   382
            m = DocMarkup( self.markup, self.markup_lines )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   383
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   384
            self.markups.append( m )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   385
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   386
            self.markup       = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   387
            self.markup_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   388
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   389
    def  process_content( self, content ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   390
        """process a block content and return a list of DocMarkup objects
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   391
           corresponding to it"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   392
        markup       = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   393
        markup_lines = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   394
        first        = 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   395
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   396
        for line in content:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   397
            found = None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   398
            for t in re_markup_tags:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   399
                m = t.match( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   400
                if m:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   401
                    found  = string.lower( m.group( 1 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   402
                    prefix = len( m.group( 0 ) )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   403
                    line   = " " * prefix + line[prefix:]   # remove markup from line
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   404
                    break
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   405
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   406
            # is it the start of a new markup section ?
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   407
            if found:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   408
                first = 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   409
                self.add_markup()  # add current markup content
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   410
                self.markup = found
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   411
                if len( string.strip( line ) ) > 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   412
                    self.markup_lines.append( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   413
            elif first == 0:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   414
                self.markup_lines.append( line )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   415
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   416
        self.add_markup()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   417
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   418
        return self.markups
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   419
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   420
    def  parse_sources( self, source_processor ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   421
        blocks = source_processor.blocks
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   422
        count  = len( blocks )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   423
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   424
        for n in range( count ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   425
            source = blocks[n]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   426
            if source.content:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   427
                # this is a documentation comment, we need to catch
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   428
                # all following normal blocks in the "follow" list
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   429
                #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   430
                follow = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   431
                m = n + 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   432
                while m < count and not blocks[m].content:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   433
                    follow.append( blocks[m] )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   434
                    m = m + 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   435
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   436
                doc_block = DocBlock( source, follow, self )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   437
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   438
    def  finish( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   439
        # process all sections to extract their abstract, description
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   440
        # and ordered list of items
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   441
        #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   442
        for sec in self.sections.values():
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   443
            sec.process()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   444
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   445
        # process chapters to check that all sections are correctly
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   446
        # listed there
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   447
        for chap in self.chapters:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   448
            for sec in chap.order:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   449
                if self.sections.has_key( sec ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   450
                    section = self.sections[sec]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   451
                    section.chapter = chap
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   452
                    section.reorder()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   453
                    chap.sections.append( section )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   454
                else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   455
                    sys.stderr.write( "WARNING: chapter '" +          \
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   456
                        chap.name + "' in " + chap.block.location() + \
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   457
                        " lists unknown section '" + sec + "'\n" )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   458
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   459
        # check that all sections are in a chapter
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   460
        #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   461
        others = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   462
        for sec in self.sections.values():
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   463
            if not sec.chapter:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   464
                others.append( sec )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   465
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   466
        # create a new special chapter for all remaining sections
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   467
        # when necessary
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   468
        #
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   469
        if others:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   470
            chap = DocChapter( None )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   471
            chap.sections = others
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   472
            self.chapters.append( chap )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   473
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   474
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   475
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   476
class  DocBlock:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   477
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   478
    def  __init__( self, source, follow, processor ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   479
        processor.reset()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   480
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   481
        self.source  = source
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   482
        self.code    = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   483
        self.type    = "ERRTYPE"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   484
        self.name    = "ERRNAME"
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   485
        self.section = processor.section
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   486
        self.markups = processor.process_content( source.content )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   487
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   488
        # compute block type from first markup tag
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   489
        try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   490
            self.type = self.markups[0].tag
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   491
        except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   492
            pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   493
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   494
        # compute block name from first markup paragraph
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   495
        try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   496
            markup = self.markups[0]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   497
            para   = markup.fields[0].items[0]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   498
            name   = para.words[0]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   499
            m = re_identifier.match( name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   500
            if m:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   501
                name = m.group( 1 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   502
            self.name = name
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   503
        except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   504
            pass
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   505
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   506
        if self.type == "section":
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   507
            # detect new section starts
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   508
            processor.set_section( self.name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   509
            processor.section.add_def( self )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   510
        elif self.type == "chapter":
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   511
            # detect new chapter
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   512
            processor.add_chapter( self )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   513
        else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   514
            processor.section.add_block( self )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   515
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   516
        # now, compute the source lines relevant to this documentation
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   517
        # block. We keep normal comments in for obvious reasons (??)
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   518
        source = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   519
        for b in follow:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   520
            if b.format:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   521
                break
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   522
            for l in b.lines:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   523
                # collect header macro definitions
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   524
                m = re_header_macro.match( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   525
                if m:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   526
                    processor.headers[m.group( 2 )] = m.group( 1 );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   527
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   528
                # we use "/* */" as a separator
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   529
                if re_source_sep.match( l ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   530
                    break
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   531
                source.append( l )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   532
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   533
        # now strip the leading and trailing empty lines from the sources
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   534
        start = 0
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   535
        end   = len( source ) - 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   536
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   537
        while start < end and not string.strip( source[start] ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   538
            start = start + 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   539
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   540
        while start < end and not string.strip( source[end] ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   541
            end = end - 1
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   542
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   543
        if start == end and not string.strip( source[start] ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   544
            self.code = []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   545
        else:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   546
            self.code = source[start:end + 1]
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   547
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   548
    def  location( self ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   549
        return self.source.location()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   550
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   551
    def  get_markup( self, tag_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   552
        """return the DocMarkup corresponding to a given tag in a block"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   553
        for m in self.markups:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   554
            if m.tag == string.lower( tag_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   555
                return m
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   556
        return None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   557
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   558
    def  get_markup_name( self, tag_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   559
        """return the name of a given primary markup in a block"""
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   560
        try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   561
            m = self.get_markup( tag_name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   562
            return m.get_name()
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   563
        except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   564
            return None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   565
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   566
    def  get_markup_words( self, tag_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   567
        try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   568
            m = self.get_markup( tag_name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   569
            return m.fields[0].items[0].words
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   570
        except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   571
            return []
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   572
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   573
    def  get_markup_text( self, tag_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   574
        result = self.get_markup_words( tag_name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   575
        return string.join( result )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   576
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   577
    def  get_markup_items( self, tag_name ):
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   578
        try:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   579
            m = self.get_markup( tag_name )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   580
            return m.fields[0].items
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   581
        except:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   582
            return None
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   583
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   584
# eof