Example #1
0
    def test_can_pickle(self):
        c = newConfig(read_files=False)
        s = pickle.dumps(c, pickle.HIGHEST_PROTOCOL)

        assert_that(pickle.loads(s), is_(c))
Example #2
0
def main(argv=None):
    """ Main program routine """

    as_main = False
    if argv is None:
        as_main = True
        print( 'plasTeX version %s' % __version__, file=sys.stderr )
        argv = sys.argv
    xml_conf_context = xmlconfig.file('configure.zcml', package=plasTeX)
    # Parse the command line options
    config = newConfig()
    try:
        opts, args = config.getopt(argv[1:])
    except Exception as msg:
        log.error(msg)
        print( config.usage(), file=sys.stderr )
        sys.exit(1)

    if not args:
        print( config.usage(), file=sys.stderr )
        sys.exit(1)

    tex_file = args.pop(0)

    # Create document instance that output will be put into
    document = plasTeX.TeXDocument(config=config)

    # Instantiate the TeX processor and parse the document
    tex = TeX(document, file=tex_file)

    # Populate variables for use later
    if config['document']['title']:
        document.userdata['title'] = config['document']['title']
    jobname = document.userdata['jobname'] = tex.jobname
    cwd = document.userdata['working-dir'] = os.getcwd()
    rname = config['general']['renderer']

    # Load aux files for cross-document references
    pauxname = '%s.paux' % jobname
    for dirname in [cwd] + config['general']['paux-dirs']:
        for fname in glob.glob(os.path.join(dirname, '*.paux')):
            if os.path.basename(fname) == pauxname:
                continue
            document.context.restore(fname, rname)

    # Parse the document
    tex.parse()

    # Set up TEXINPUTS to include the current directory for the renderer
    os.environ['TEXINPUTS'] = '%s%s%s%s' % (os.getcwd(), os.pathsep,
                                         os.environ.get('TEXINPUTS',''), os.pathsep)

    # Change to specified directory to output to
    outdir = config['files']['directory']
    if outdir:
        outdir = string.Template(outdir).substitute({'jobname':jobname})
        if not os.path.isdir(outdir):
            os.makedirs(outdir)
        log.info('Directing output files to directory: %s.', outdir)
        os.chdir(outdir)


    # Write expanded source file
    #sourcefile = '%s.source' % jobname
    #open(sourcefile,'w').write(document.source.encode('utf-8'))

    # Write XML dump
    if config['general']['xml']:
        outfile = '%s.xml' % jobname
        with codecs.open(outfile,'w',encoding='utf-8') as f:
            f.write(document.toXML())

    # Load the renderer. If we do this after we chdir,
    # and there is a sys.path problem, then we may wind up
    # not being able to import the renderer. OTOH, if we do
    # it before the chdir, the renderer might not find its data files,
    # resulting in a bad render.
    # At least doing it after is an obvious failure
    try:
        Renderer = dottedname.resolve( 'plasTeX.Renderers.%s.Renderer' % rname )
    except ImportError:
        print('Could not import renderer "%s"   Make sure that it is installed correctly, and can be imported by Python.' % rname,
              file=sys.stderr)
        import traceback
        traceback.print_exc()
        sys.exit(1)

    # Apply renderer
    Renderer().render(document)

    if as_main:
        print("")
Example #3
0
    def test_can_pickle(self):
        c = newConfig( read_files=False )
        s = pickle.dumps( c, pickle.HIGHEST_PROTOCOL )

        assert_that( pickle.loads( s ), is_( c ) )
Example #4
0
def main(argv=None):
    """ Main program routine """

    as_main = False
    if argv is None:
        as_main = True
        print('plasTeX version %s' % __version__, file=sys.stderr)
        argv = sys.argv
    xml_conf_context = xmlconfig.file('configure.zcml', package=plasTeX)
    # Parse the command line options
    config = newConfig()
    try:
        opts, args = config.getopt(argv[1:])
    except Exception as msg:
        log.error(msg)
        print(config.usage(), file=sys.stderr)
        sys.exit(1)

    if not args:
        print(config.usage(), file=sys.stderr)
        sys.exit(1)

    tex_file = args.pop(0)

    # Create document instance that output will be put into
    document = plasTeX.TeXDocument(config=config)

    # Instantiate the TeX processor and parse the document
    tex = TeX(document, file=tex_file)

    # Populate variables for use later
    if config['document']['title']:
        document.userdata['title'] = config['document']['title']
    jobname = document.userdata['jobname'] = tex.jobname
    cwd = document.userdata['working-dir'] = os.getcwd()
    rname = config['general']['renderer']

    # Load aux files for cross-document references
    pauxname = '%s.paux' % jobname
    for dirname in [cwd] + config['general']['paux-dirs']:
        for fname in glob.glob(os.path.join(dirname, '*.paux')):
            if os.path.basename(fname) == pauxname:
                continue
            document.context.restore(fname, rname)

    # Parse the document
    tex.parse()

    # Set up TEXINPUTS to include the current directory for the renderer
    os.environ['TEXINPUTS'] = '%s%s%s%s' % (
        os.getcwd(), os.pathsep, os.environ.get('TEXINPUTS', ''), os.pathsep)

    # Change to specified directory to output to
    outdir = config['files']['directory']
    if outdir:
        outdir = string.Template(outdir).substitute({'jobname': jobname})
        if not os.path.isdir(outdir):
            os.makedirs(outdir)
        log.info('Directing output files to directory: %s.', outdir)
        os.chdir(outdir)

    # Write expanded source file
    #sourcefile = '%s.source' % jobname
    #open(sourcefile,'w').write(document.source.encode('utf-8'))

    # Write XML dump
    if config['general']['xml']:
        outfile = '%s.xml' % jobname
        with codecs.open(outfile, 'w', encoding='utf-8') as f:
            f.write(document.toXML())

    # Load the renderer. If we do this after we chdir,
    # and there is a sys.path problem, then we may wind up
    # not being able to import the renderer. OTOH, if we do
    # it before the chdir, the renderer might not find its data files,
    # resulting in a bad render.
    # At least doing it after is an obvious failure
    try:
        Renderer = dottedname.resolve('plasTeX.Renderers.%s.Renderer' % rname)
    except ImportError:
        print(
            'Could not import renderer "%s"   Make sure that it is installed correctly, and can be imported by Python.'
            % rname,
            file=sys.stderr)
        import traceback
        traceback.print_exc()
        sys.exit(1)

    # Apply renderer
    Renderer().render(document)

    if as_main:
        print("")