Esempio n. 1
0
def raw_debish_lex(input_string):
    '''Used internally to generate a series of lexer states.'''
    end = 0
    yield 'start', None, 0
    result = tag(input_string, tags)
    taglist = result[1]
    for tag_type, start, end, dummy in taglist:
        yield tag_type, input_string[start:end], start
    yield 'end', None, end
Esempio n. 2
0
def raw_debish_lex(input_string):
    '''Used internally to generate a series of lexer states.'''
    end = 0
    yield 'start', None, 0
    result = tag(input_string, tags)
    taglist = result[1]
    for tag_type, start, end, dummy in taglist:
        yield tag_type, input_string[start:end], start
    yield 'end', None, end
Esempio n. 3
0
def main(cmdline):
    """main(cmdline) -- process cmdline as if it were sys.argv"""

    # parse options/files
    options = []
    optvalues = {}
    for opt in cmdline[1:]:
        if opt.startswith('-'):
            if ':' in opt:
                k, v = tuple(opt.split(':', 1))
                optvalues[k] = v
                options.append(k)
            else:
                options.append(opt)
        else:
            break
    files = cmdline[len(options)+1:]

    ### create converting object

    verbose = ('-v' in options)

    # load fontifier
    if '-marcs' in options:
        # use mxTextTool's tagging engine as fontifier
        from mx.TextTools import tag
        from mx.TextTools.Examples.Python import python_script
        tagfct = lambda text, tag=tag, pytable=python_script: tag(
            text, pytable)[1]
        print "Py2HTML: using Marc's tagging engine"
    else:
        # load Just's fontifier
        try:
            import PyFontify
            if PyFontify.__version__ < '0.3':
                raise ImportError
            tagfct = PyFontify.fontify
        except ImportError:
            print """
Sorry, but this script needs the PyFontify.py module version 0.3;
You can download it from Just's homepage at
URL: http://starship.python.net/~just/
"""
            sys.exit()

    if '-format' in options:
        format = optvalues['-format']
    else:
        # use default
        format = 'html'

    if '-mode' in options:
        mode = optvalues['-mode']
    else:
        # use default
        mode = 'color'

    c = PrettyPrint(tagfct, format, mode)
    convert = c.file_filter

    ### start working

    if '-title' in options:
        c.title = optvalues['-title']

    if '-bgcolor' in options:
        c.bgcolor = optvalues['-bgcolor']

    if '-header' in options:
        try:
            f = open(optvalues['-header'])
            c.header = f.read()
            f.close()
        except IOError:
            if verbose:
                print 'IOError: header file not found'

    if '-footer' in options:
        try:
            f = open(optvalues['-footer'])
            c.footer = f.read()
            f.close()
        except IOError:
            if verbose:
                print 'IOError: footer file not found'

    if '-URL' in options:
        c.replace_URLs = True

    if '-' in options:
        convert(sys.stdin, sys.stdout)
        sys.exit()

    if '-h' in options:
        print __doc__
        sys.exit()

    if not files:
        # Turn URL processing on
        c.replace_URLs = True
        # Try CGI processing...
        import cgi, urllib, urlparse, os
        form = cgi.FieldStorage()
        if 'script' not in form:
            # Ok, then try pathinfo
            if 'PATH_INFO' not in os.environ:
                if INPUT_FORM:
                    redirect_to(INPUT_FORM)
                else:
                    sys.stdout.write('Content-Type: text/html\r\n\r\n')
                    write_html_error('Missing Parameter',
                        'Missing script=URL field in request')
                sys.exit(1)
            url = os.environ['PATH_INFO'][1:] # skip the leading slash
        else:
            url = form['script'].value
        sys.stdout.write('Content-Type: text/html\r\n\r\n')
        scheme, host, path, params, query, frag = urlparse.urlparse(url)
        if not host:
            scheme = 'http'
            host = os.environ.get('HTTP_HOST', 'localhost')
            url = urlparse.urlunparse((scheme, host, path, params, query, frag))
        #print url; sys.exit()
        network = urllib.URLopener()
        try:
            tempfile, headers = network.retrieve(url)
        except IOError, reason:
            write_html_error('Error opening "%s"' % url,
                'The given URL could not be opened. Reason: %s' % str(reason))
            sys.exit(1)
        f = open(tempfile,'rb')
        c.title = url
        c.footer = __cgifooter__
        convert(f, sys.stdout)
        f.close()
        network.close()
        sys.exit()
Esempio n. 4
0
def main(cmdline):

    """main(cmdline) -- process cmdline as if it were sys.argv"""
    # parse options/files
    options = []
    optvalues = {}
    for o in cmdline[1:]:
        if o[0] == "-":
            if ":" in o:
                k, v = tuple(string.split(o, ":"))
                optvalues[k] = v
                options.append(k)
            else:
                options.append(o)
        else:
            break
    files = cmdline[len(options) + 1 :]

    ### create converting object

    # load fontifier
    if "-marcs" in options:
        # use mxTextTool's tagging engine as fontifier
        from mx.TextTools import tag
        from mx.TextTools.Examples.Python import python_script

        tagfct = lambda text, tag=tag, pytable=python_script: tag(text, pytable)[1]
        print "Py2HTML: using Marc's tagging engine"
    else:
        # load Just's fontifier
        try:
            import PyFontify

            if PyFontify.__version__ < "0.3":
                raise ValueError
            tagfct = PyFontify.fontify
        except:
            print """
Sorry, but this script needs the PyFontify.py module version 0.3;
You can download it from Just's homepage at
URL: http://starship.python.net/~just/
	"""
            sys.exit()

    if "-format" in options:
        format = optvalues["-format"]
    else:
        # use default
        format = "html"

    if "-mode" in options:
        mode = optvalues["-mode"]
    else:
        # use default
        mode = "color"

    c = PrettyPrint(tagfct, format, mode)
    convert = c.file_filter

    ### start working

    if "-title" in options:
        c.title = optvalues["-title"]

    if "-bgcolor" in options:
        c.bgcolor = optvalues["-bgcolor"]

    if "-header" in options:
        try:
            f = open(optvalues["-header"])
            c.header = f.read()
            f.close()
        except IOError:
            if verbose:
                print "IOError: header file not found"

    if "-footer" in options:
        try:
            f = open(optvalues["-footer"])
            c.footer = f.read()
            f.close()
        except IOError:
            if verbose:
                print "IOError: footer file not found"

    if "-URL" in options:
        c.replace_URLs = 1

    if "-" in options:
        convert(sys.stdin, sys.stdout)
        sys.exit()

    if "-h" in options:
        print __doc__
        sys.exit()

    if len(files) == 0:
        # Turn URL processing on
        c.replace_URLs = 1
        # Try CGI processing...
        import cgi, urllib, urlparse, os

        form = cgi.FieldStorage()
        if not form.has_key("script"):
            # Ok, then try pathinfo
            if not os.environ.has_key("PATH_INFO"):
                if INPUT_FORM:
                    redirect_to(INPUT_FORM)
                else:
                    sys.stdout.write("Content-Type: text/html\r\n\r\n")
                    write_html_error("Missing Parameter", "Missing script=URL field in request")
                sys.exit(1)
            url = os.environ["PATH_INFO"][1:]  # skip the leading slash
        else:
            url = form["script"].value
        sys.stdout.write("Content-Type: text/html\r\n\r\n")
        scheme, host, path, params, query, frag = urlparse.urlparse(url)
        if not host:
            scheme = "http"
            if os.environ.has_key("HTTP_HOST"):
                host = os.environ["HTTP_HOST"]
            else:
                host = "localhost"
            url = urlparse.urlunparse((scheme, host, path, params, query, frag))
            # print url; sys.exit()
        network = urllib.URLopener()
        try:
            tempfile, headers = network.retrieve(url)
        except IOError, reason:
            write_html_error('Error opening "%s"' % url, "The given URL could not be opened. Reason: %s" % str(reason))
            sys.exit(1)
        f = open(tempfile, "rb")
        c.title = url
        c.footer = __cgifooter__
        convert(f, sys.stdout)
        f.close()
        network.close()
        sys.exit()
Esempio n. 5
0
def main(cmdline):
    """main(cmdline) -- process cmdline as if it were sys.argv"""

    # parse options/files
    options = []
    optvalues = {}
    for opt in cmdline[1:]:
        if opt.startswith('-'):
            if ':' in opt:
                k, v = tuple(opt.split(':', 1))
                optvalues[k] = v
                options.append(k)
            else:
                options.append(opt)
        else:
            break
    files = cmdline[len(options)+1:]

    ### create converting object

    verbose = ('-v' in options)

    # load fontifier
    if '-marcs' in options:
        # use mxTextTool's tagging engine as fontifier
        from mx.TextTools import tag
        from mx.TextTools.Examples.Python import python_script
        tagfct = lambda text, tag=tag, pytable=python_script: tag(
            text, pytable)[1]
        print "Py2HTML: using Marc's tagging engine"
    else:
        # load Just's fontifier
        try:
            import PyFontify
            if PyFontify.__version__ < '0.3':
                raise ImportError
            tagfct = PyFontify.fontify
        except ImportError:
            print """
Sorry, but this script needs the PyFontify.py module version 0.3;
You can download it from Just's homepage at
URL: http://starship.python.net/~just/
"""
            sys.exit()

    if '-format' in options:
        format = optvalues['-format']
    else:
        # use default
        format = 'html'

    if '-mode' in options:
        mode = optvalues['-mode']
    else:
        # use default
        mode = 'color'

    c = PrettyPrint(tagfct, format, mode)
    convert = c.file_filter

    ### start working

    if '-title' in options:
        c.title = optvalues['-title']

    if '-bgcolor' in options:
        c.bgcolor = optvalues['-bgcolor']

    if '-header' in options:
        try:
            f = open(optvalues['-header'])
            c.header = f.read()
            f.close()
        except IOError:
            if verbose:
                print 'IOError: header file not found'

    if '-footer' in options:
        try:
            f = open(optvalues['-footer'])
            c.footer = f.read()
            f.close()
        except IOError:
            if verbose:
                print 'IOError: footer file not found'

    if '-URL' in options:
        c.replace_URLs = True

    if '-' in options:
        convert(sys.stdin, sys.stdout)
        sys.exit()

    if '-h' in options:
        print __doc__
        sys.exit()

    if not files:
        # Turn URL processing on
        c.replace_URLs = True
        # Try CGI processing...
        import cgi, urllib, urlparse, os
        form = cgi.FieldStorage()
        if 'script' not in form:
            # Ok, then try pathinfo
            if 'PATH_INFO' not in os.environ:
                if INPUT_FORM:
                    redirect_to(INPUT_FORM)
                else:
                    sys.stdout.write('Content-Type: text/html\r\n\r\n')
                    write_html_error('Missing Parameter',
                        'Missing script=URL field in request')
                sys.exit(1)
            url = os.environ['PATH_INFO'][1:]  # skip the leading slash
        else:
            url = form['script'].value
        sys.stdout.write('Content-Type: text/html\r\n\r\n')
        scheme, host, path, params, query, frag = urlparse.urlparse(url)
        if not host:
            scheme = 'http'
            host = os.environ.get('HTTP_HOST', 'localhost')
            url = urlparse.urlunparse((scheme, host, path, params, query, frag))
        #print url; sys.exit()
        network = urllib.URLopener()
        try:
            tempfile, headers = network.retrieve(url)
        except IOError as reason:
            write_html_error('Error opening "%s"' % url,
                'The given URL could not be opened. Reason: %s' % str(reason))
            sys.exit(1)
        f = open(tempfile,'rb')
        c.title = url
        c.footer = __cgifooter__
        convert(f, sys.stdout)
        f.close()
        network.close()
        sys.exit()

    if '-stdout' in options:
        filebreak = '-'*72
        for f in files:
            try:
                if len(files) > 1:
                    print filebreak
                    print 'File:', f
                    print filebreak
                convert(f, sys.stdout)
            except IOError:
                pass
    else:
        if verbose:
            print 'Py2HTML: working on',
        for f in files:
            try:
                if verbose:
                    print f,
                convert(f, f+'.html')
            except IOError:
                if verbose:
                    print '(IOError!)',
        if verbose:
            print
            print 'Done.'