Exemple #1
0
def PyToHTML(inputfilename, output):
    """Convert a Python file to an HTML file"""
    
    #~Open Files

    input  = open(inputfilename, 'r')
    #output = open(outputfilename, 'w')
    
    #~Make sure Token class is ready, then Parse into Tokens @N
    #~Parse calls us back and returns a list of our results
    
    Token_ClassInit()
    tokens = Parse(input, Token)
    
    #~Write out the html prefix, with title @N
    #~Write out all the Tokens as html @N
    #~Write out the html suffix    
    #~Write out the cross reference
        
    output.write(html_prefix(inputfilename))
    if Token.autosplit:
        output.write(pymacro.pymax_process('@startsplit'))
    for t in tokens:
        output.write(t.asHTML())    
    output.write(html_suffix())
    if Token.autosplit:
        output.write(pymacro.pymax_process('@endsplit'))
    if not pymacro.TestFlag('noindex'):
        output.write(Token.xref.asHTML())
Exemple #2
0
def main(argv):
    "for command line invocation - gimme sys.argv[1:]"
    if not len(argv): 
        print __doc__   # usage info
        return
    
    for arg in argv:
        if arg[0] == '-':   # command line - convert to @
            s = pymacro.pymax_process('@SetFlag{'+arg[1:]+'}')
            if s:           # report any output
                print s
        else:
            PyToHTML(arg, arg+".html")
Exemple #3
0
    def asHTML(self):
        """ render this Token asHTML """

        s = self.isSpecial # well, isn't that special?
        if s:
            if not self.preprocessed:
                s = autohtml(pymacro.pymax_process(escapes4html(s)))
            if 0:   # doesn't work as well as hoped - dedents come too late
                t = self.indentlevel + 1
                if self.indentlevel:
                    s = '<ul>' * t + s + '</ul>' * t
            return html_wrap(self.leading, self.trailing, s)
            
        s = ""
        # put out the line numbers
        if (self.token or self.type==DEDENT) and self.isAfterNewLine:
            s = ('<a name="%d">%4d</a>    ' % (self.line, self.line))

        type = self.type
        token = self.token
        if type > RESERVED:
            type = RESERVED
        wrap = gWraps.get(type,None)

        # check for strings with embedded newlines
        if type == STRING and string.count(token, '\n') > 1:
            t = escapes4html(string.join(
                               string.split(token, '\n'), '\n        '))
            if wrap:
                t = wrap % t
            s = s + t + '\n'
        else:
            t = escapes4html(token)
            if wrap:
                t = wrap % t
            s = s + t
        return s
Exemple #4
0
    def __init__(self, type, token, srow, scol, erow, ecol):
        
        #~ Pick up our token info

        self.type = type
        self.token= token
        self.line = srow
        # scol, erow, ecol, not in use
            
        #~ Stitch up our linked list

        self.next     = None
        self.previous = Token.previous
        if Token.previous:
            Token.previous.next = self
        Token.previous = self

        #~ Remember our level info        
        self.indentlevel  = Token.indentlevel
        self.parenlevel   = Token.parenlevel
        self.bracketlevel = Token.bracketlevel
        self.bracelevel   = Token.bracelevel

        #~ Do all of the level tracking maintenance
        if type == INDENT:
            Token.indentlevel = Token.indentlevel + 1
        elif type == DEDENT:
            Token.indentlevel = Token.indentlevel - 1
        elif type == OP:
            if token == '(':
                Token.parenlevel = Token.parenlevel + 1
            elif token == ')':
                Token.parenlevel = Token.parenlevel - 1
            elif token == '[':
                Token.bracketlevel = Token.bracketlevel + 1
            elif token == ']':
                Token.bracketlevel = Token.bracketlevel - 1
            elif token == '{':
                Token.bracelevel = Token.bracelevel + 1
            elif token == '}':
                Token.bracelevel = Token.bracelevel - 1

        #~ Figure out if we're white space, for future reference

        self.isWhite = type in (WHITE, INDENT, DEDENT, NEWLINE, NL)

        #~ Worried about newline:
        #~ isNewLine means we're a single literal new line character,
        #~ but isAfterNewLine means previous item could have also been comment, etc.

        self.isAfterNewLine = Token.afternewline
        self.isNewLine = type == NL or type == NEWLINE
        
        #~ Set up afternewline for the Next token
        Token.afternewline = token and token[-1] == '\n'

        #~ Looking for first string on line as possible special comment

        self.isFirstString = 0
        if type == STRING and Token.firstonline:
            if not (self.parenlevel or self.bracketlevel or self.bracelevel):
                self.isFirstString = 1

        #~ For automatic formatting without explicit ~, add it in,
        #~ and preserve existing formatting by adding @@N (<br>) directives
        if Token.autoformat:
            if Token.firstonline:
                if self.isFirstString and len(token) > 2:
                    if token[0] == token[1]:
                        if token[3] <> '~':
                            token = token[0:3] + '~' + token[3:]
                    elif token[1] <> '~':
                        token = token[0] + '~' + token[1:]
                    token = string.replace(token, "\n", "@N\n")
                elif type == COMMENT:
                    if token[1] <> '~' and token[1] <> '-':
                        token = '#~'+token[1:]
                    token = string.replace(token, "\n", "@N\n")
        
        #~ Set up firstline for the Next token
        if Token.afternewline or type == DEDENT or type == INDENT:
            Token.firstonline = 1
        elif type <> WHITE:
            Token.firstonline = 0
            
        #~ Special defaults
        
        self.isSpecial = 0
        self.preprocessed = 0

        #~ Quick hack for division lines - beware of #- with other stuff

        if type == COMMENT:
            if len(token) > 1 and token[1] == '-':
                self.isSpecial = "<hr>"
                self.preprocessed = 1
                self.token = ""

        #~Look for specially marked boxes of your favorite serial
        # this isn't quite as brutal as it looks

        if type == COMMENT and len(token) > 2:
            if token[1] == '~': 
                self.isSpecial = token[2:]
            elif token[1] == '@':  # early v late execution
                self.isSpecial = pymacro.pymax_process(token[1:])
                self.preprocessed = 1
                self.token = ""
        elif self.isFirstString and len(token) > 2:
            if token[1] == '~':
                self.isSpecial = token[2:-1]
            elif token[0] == token[1] and token[3] == '~':
                self.isSpecial = token[4:-3]                
            elif (token == 
'~this is a test'):    # this is a test to make sure we don't grab this
                pass

        """~
Worrying about the extra blank lines.

If we look backward and ignore any white space
(including INDENT/DEDENT/NEWLINE/NL etc.)
and the first thing we find is also 'special',
then tell him to forget trailing wrapping,
and tell me to forget leading wrapping.
"""

        if self.isSpecial:
            # drop leading space, just because
            if self.isSpecial[0] == ' ':    
                self.isSpecial = self.isSpecial[1:]
            
            self.leading = 1
            self.trailing = 1
            previous = self.previous
            while previous:
                if previous.isWhite:
                    previous.type = WHITE
                    previous.token = '' # clear 
                    previous = previous.previous
                else:
                    if previous.isSpecial:
                        previous.trailing = 0
                        self.leading = 0
                    break

        """~
Also look forward to get rid of extra white space/new lines following special.
Handled by looking back from newline instead.

"""

        if self.isNewLine:  # look back for last special
            previous = self.previous 
            while previous:
                if previous.isWhite:
                    previous = previous.previous
                else:
                    if previous.isSpecial:
                        self.token = ''
                        # walk back forward looking for white
                        previous = previous.next
                        while not previous is self:
                            if previous.type == WHITE:
                                previous.token = ''
                            previous = previous.next
                    break

        #~ Maintain symbol cross reference for the summary Index

        if type == NAME:
            Token.xref(token, srow)

        #~ Trim the extra lines and white space at the end of the program

        if type == ENDMARKER:
            previous = self.previous 
            while previous:
                if previous.isWhite:
                    previous.type = WHITE
                    previous.token = ''
                    previous = previous.previous
                else:
                    break
Exemple #5
0
def nested(s):
    """ keep on expanding """
    while string.find(s, '@') >= 0:
        s = pymacro.pymax_process(s)
    return s