Ejemplo n.º 1
0
def main():
    app = Application()
    try:
        run(app)
    except KeyboardInterrupt:
        if utils.get_debugging():
            app.exception_dialog()
        sys.exit(1)
Ejemplo n.º 2
0
def main():
    app = Application()
    try:
        run(app)
    except KeyboardInterrupt:
        if utils.get_debugging():
            app.exception_dialog()
        sys.exit(1)
Ejemplo n.º 3
0
    if url:
        context = URIContext(url)
    elif infile:
        url = infile
        context = URIContext(url)
    else:
        # BOGOSITY: reading from stdin
        context = URIContext("file:/index.html")
    context.app = app
    paper = printing.paper.PaperInfo(settings.papersize,
                                     margins=settings.margins,
                                     rotation=settings.orientation)
    if tabstop and tabstop > 0:
        paper.TabStop = tabstop
    if utils.get_debugging('paper'):
        paper.dump()
    # create the writer & parser
    fontsize, leading = settings.get_fontsize()
    w = PSWriter.PSWriter(
        outfp,
        title or None,
        url or '',
        #varifamily='Palatino',
        paper=paper,
        settings=settings)
    ctype = "text/html"
    mod = app.find_type_extension("printing.filetypes", ctype)
    if not mod.parse:
        sys.exit("cannot load printing support for " + ctype)
    p = mod.parse(w, settings, context)
Ejemplo n.º 4
0
def run(app):
    global logfile
    import getopt
    import paper
    import settings
    settings = settings.get_settings(app.prefs)
    # do this after loading the settings so the user can just call
    # get_settings() w/out an arg to get a usable object.
    load_rcscript()
    context = None
    help = None
    error = 0
    logfile = None
    title = ''
    url = ''
    tabstop = None
    multi = 0
    verbose = 0
    printer = None
    copies = 1
    levels = None
    outfile = None
    #
    try:
        options, args = getopt.getopt(sys.argv[1:],
                                      'mvhdcaUl:u:t:sp:o:f:C:P:T:', [
                                          'color',
                                          'copies=',
                                          'debug',
                                          'fontsize=',
                                          'footnote-anchors',
                                          'help',
                                          'images',
                                          'logfile=',
                                          'multi',
                                          'orientation=',
                                          'output=',
                                          'papersize=',
                                          'paragraph-indent=',
                                          'paragraph-skip=',
                                          'printer=',
                                          'strict-parsing',
                                          'tab-width=',
                                          'tags=',
                                          'title=',
                                          'underline-anchors',
                                          'url=',
                                          'verbose',
                                      ])
    except getopt.error as err:
        error = 1
        help = 1
        options = ()
        sys.stderr.write("option failure: %s\n" % err)
    for opt, arg in options:
        if opt in ('-h', '--help'):
            help = 1
        elif opt in ('-a', '--footnote-anchors'):
            settings.footnoteflag = not settings.footnoteflag
        elif opt in ('-i', '--images'):
            settings.imageflag = not settings.imageflag
        elif opt in ('-d', '--debug'):
            utils.set_debugging(1)
        elif opt in ('-l', '--logfile'):
            logfile = arg
        elif opt in ('-o', '--orientation'):
            settings.orientation = arg
        elif opt in ('-f', '--fontsize'):
            settings.set_fontsize(arg)
        elif opt in ('-t', '--title'):
            title = arg
        elif opt in ('-u', '--url'):
            url = arg
        elif opt in ('-U', '--underline-anchors'):
            settings.underflag = not settings.underflag
        elif opt in ('-c', '--color'):
            settings.greyscale = not settings.greyscale
        elif opt in ('-p', '--papersize'):
            settings.papersize = arg
        elif opt in ('-s', '--strict-parsing'):
            settings.strict_parsing = not settings.strict_parsing
        elif opt in ('-C', '--copies'):
            copies = int(arg)
        elif opt in ('-P', '--printer'):
            printer = arg
        elif opt in ('-T', '--tab-width'):
            tabstop = float(arg)
        elif opt in ('-m', '--multi'):
            multi = 1
        elif opt in ('-v', '--verbose'):
            verbose = verbose + 1
        elif opt == '--output':
            outfile = arg
        elif opt == '--tags':
            if not load_tag_handler(app, arg):
                error = 2
                help = 1
        elif opt == '--paragraph-indent':
            # negative indents should indicate hanging indents, but we don't
            # do those yet, so force to normal interpretation
            settings.paragraph_indent = max(float(arg), 0.0)
        elif opt == '--paragraph-skip':
            settings.paragraph_skip = max(float(arg), 0.0)
    if help:
        usage(settings)
        sys.exit(error)
    # crack open log file if given
    stderr = sys.stderr
    if logfile:
        try:
            sys.stderr = open(logfile, 'a')
        except IOError:
            sys.stderr = stderr
    utils.debug("Using Python version " + sys.version)
    # crack open the input file, or stdin
    outfp = None
    if printer:
        if copies < 1:
            copies = 1
        outfile = "|lpr -#%d -P%s" % (copies, printer)
    if args:
        infile = args[0]
        if args[1:]:
            multi = 1
        infp, outfn = open_source(infile)
        if not outfile:
            outfile = (os.path.splitext(outfn)[0] or 'index') + '.ps'
    else:
        infile = None
        infp = sys.stdin
        outfile = '-'
    #
    # open the output file
    #
    if outfile[0] == '|':
        cmd = str.strip(outfile[1:])
        outfile = '|' + cmd
        outfp = os.popen(cmd, 'w')
    elif outfile == '-':
        outfp = sys.stdout
    else:
        outfp = open(outfile, 'w')
    if outfile != '-':
        print('Outputting PostScript to', outfile)

    if url:
        context = URIContext(url)
    elif infile:
        url = infile
        context = URIContext(url)
    else:
        # BOGOSITY: reading from stdin
        context = URIContext("file:/index.html")
    context.app = app
    paper = printing.paper.PaperInfo(settings.papersize,
                                     margins=settings.margins,
                                     rotation=settings.orientation)
    if tabstop and tabstop > 0:
        paper.TabStop = tabstop
    if utils.get_debugging('paper'):
        paper.dump()
    # create the writer & parser
    fontsize, leading = settings.get_fontsize()
    w = PSWriter.PSWriter(
        outfp,
        title or None,
        url or '',
        #varifamily='Palatino',
        paper=paper,
        settings=settings)
    ctype = "text/html"
    mod = app.find_type_extension("printing.filetypes", ctype)
    if not mod.parse:
        sys.exit("cannot load printing support for " + ctype)
    p = mod.parse(w, settings, context)
    if multi:
        if args[1:]:
            xform = explicit_multi_transform(args[1:])
        else:
            xform = multi_transform(context, levels)
        p.add_anchor_transform(xform)
        p.feed(infp.read())
        docs = [(context.get_url(), 1, w.ps.get_title(), 1)]
        #
        # This relies on xform.get_subdocs() returning the list used
        # internally to accumulate subdocs.  Make a copy to go only one
        # level deep.
        #
        for url in xform.get_subdocs():
            xform.set_basedoc(url)
            while p.sgml_parser.get_depth():
                p.sgml_parser.lex_endtag(p.sgml_parser.get_stack()[0])
            try:
                infp, fn = open_source(url)
            except IOError as err:
                if verbose and outfp is not sys.stdout:
                    print("Error opening subdocument", url)
                    print("   ", err)
            else:
                new_ctype = get_ctype(app, url, infp)
                if new_ctype != ctype:
                    if verbose:
                        print("skipping", url)
                        print("  wrong content type:", new_ctype)
                    continue
                if verbose and outfp is not sys.stdout:
                    print("Subdocument", url)
                w.ps.close_line()
                if MULTI_DO_PAGE_BREAK:  # must be true for now, not sure why
                    pageend = w.ps.push_page_end()
                    context.set_url(url)
                    w.ps.set_pageno(w.ps.get_pageno() + 1)
                    w.ps.set_url(url)
                    w.ps.push_page_start(pageend)
                else:
                    context.set_url(url)
                    w.ps.set_url(url)
                pageno = w.ps.get_pageno()
                p.feed(infp.read())
                infp.close()
                title = w.ps.get_title()
                p._set_docinfo(url, pageno, title)
                spec = (url, pageno, title, xform.get_level(url))
                docs.append(spec)
    else:
        p.feed(infp.read())
    p.close()
    w.close()
Ejemplo n.º 5
0
    if url:
        context = URIContext(url)
    elif infile:
        url = infile
        context = URIContext(url)
    else:
        # BOGOSITY: reading from stdin
        context = URIContext("file:/index.html")
    context.app = app
    paper = printing.paper.PaperInfo(settings.papersize,
                                     margins=settings.margins,
                                     rotation=settings.orientation)
    if tabstop and tabstop > 0:
        paper.TabStop = tabstop
    if utils.get_debugging('paper'):
        paper.dump()
    # create the writer & parser
    fontsize, leading = settings.get_fontsize()
    w = PSWriter.PSWriter(outfp, title or None, url or '',
                          #varifamily='Palatino',
                          paper=paper, settings=settings)
    ctype = "text/html"
    mod = app.find_type_extension("printing.filetypes", ctype)
    if not mod.parse:
        sys.exit("cannot load printing support for " + ctype)
    p = mod.parse(w, settings, context)
    if multi:
        if args[1:]:
            xform = explicit_multi_transform(args[1:])
        else: