예제 #1
0
 def testComment(self):
     tokens = [x for x in TeX().input('line % comment').itertokens()]
     expected = [
         Letter('l'),
         Letter('i'),
         Letter('n'),
         Letter('e'),
         Space(' ')
     ]
     assert tokens == expected, '%s != %s' % (tokens, expected)
예제 #2
0
    def testExercises(self):
        """ Exercises in the TeX book """
        # 8.4
        tokens = [x for x in TeX().input(r' $x^2$~  \TeX  ^^C').itertokens()]
        expected = [
            MathShift('$'),
            Letter('x'),
            Superscript('^'),
            Other('2'),
            MathShift('$'),
            EscapeSequence('active::~'),
            Space(' '),
            EscapeSequence('TeX'),
            Other('\x03')
        ]
        assert tokens == expected, '%s != %s' % (tokens, expected)

        # 8.5
        tokens = [x for x in TeX().input('Hi!\n\n\n').itertokens()]
        expected = [
            Letter('H'),
            Letter('i'),
            Other('!'),
            Space(' '),
            EscapeSequence('par')
        ]
        assert tokens == expected, '%s != %s' % (tokens, expected)

        # 8.6
        tokens = [
            x for x in TeX().input(r'^^B^^BM^^A^^B^^C^^M^^@\M ').itertokens()
        ]
        expected = [
            Other('\x02'),
            Other('\x02'),
            Letter('M'),
            Other('\x01'),
            Other('\x02'),
            Other('\x03'),
            Space(' '),
            EscapeSequence('M')
        ]
        assert tokens == expected, '%s != %s' % (tokens, expected)
예제 #3
0
    def testNumberSection(self):
        t = TeX()
        t.input(r'''
\documentclass{article}
\begin{document}
\section{}
\section{}
\number\thesection
\end{document}
''')
        assert t.parse().textContent.strip() == '2'
예제 #4
0
    def testRomanNumeralSection(self):
        t = TeX()
        t.input(r'''
\documentclass{article}
\begin{document}
\section{}
\section{}
\romannumeral\thesection
\end{document}
''')
        assert t.parse().textContent.strip() == 'ii'
예제 #5
0
 def testTokens(self):
     tokens = [x for x in TeX().input(r'{\hskip 36 pt}').itertokens()]
     expected = [BeginGroup('{'), 
                 EscapeSequence('hskip'), 
                 Other('3'),
                 Other('6'),
                 Space(' '),
                 Letter('p'), 
                 Letter('t'), 
                 EndGroup('}')]
     assert tokens == expected, '%s != %s' % (tokens, expected)
예제 #6
0
 def testParagraph(self):
     tokens = [x for x in TeX().input('1\n   2\n   \n   3\n').itertokens()]
     expected = [
         Other('1'),
         Space(' '),
         Other('2'),
         Space(' '),
         EscapeSequence('par'),
         Other('3'),
         Space(' ')
     ]
     assert tokens == expected, '%s != %s' % (tokens, expected)
예제 #7
0
    def testDimenParameters(self):
        t = TeX()
        t.input(r'\newdimen\foo\foo=\hsize')
        t.parse()
        foo = t.ownerDocument.context['foo'].value
        hsize = t.ownerDocument.context['hsize'].value
        assert foo == hsize, '"%s" != "%s"' % (foo, hsize)

        t = TeX()
        t.input(r'\newdimen\foo\foo=7.6\hsize')
        t.parse()
        foo = t.ownerDocument.context['foo'].value
        hsize = t.ownerDocument.context['hsize'].value
        assert foo == (7.6 * hsize), '"%s" != "%s"' % (foo, 7.6 * hsize)

        t = TeX()
        t.input(r'\newdimen\foo\foo=-4\hsize')
        t.parse()
        foo = t.ownerDocument.context['foo'].value
        hsize = t.ownerDocument.context['hsize'].value
        assert foo == (-4 * hsize), '"%s" != "%s"' % (foo, (-4 * hsize))
예제 #8
0
파일: Numbers.py 프로젝트: we-taper/plastex
 def testGlueParameters(self):
     t = TeX()
     t.input(r'\newskip\foo\foo=\baselineskip')
     t.parse()
     foo = t.ownerDocument.context['foo'].value
     baselineskip = t.ownerDocument.context['baselineskip'].value
     assert foo == baselineskip, '"%s" != "%s"' % (foo, baselineskip)
     
     t = TeX()
     t.input(r'\newskip\foo\foo=7.6\baselineskip')
     t.parse()
     foo = t.ownerDocument.context['foo'].value
     baselineskip = t.ownerDocument.context['baselineskip'].value
     assert foo == (7.6*baselineskip), '"%s" != "%s"' % (foo, 7.6*baselineskip)
     
     t = TeX()
     t.input(r'\newskip\foo\foo=-4\baselineskip')
     t.parse()
     foo = t.ownerDocument.context['foo'].value
     baselineskip = t.ownerDocument.context['baselineskip'].value
     assert foo == (-4*baselineskip), '"%s" != "%s"' % (foo, (-4*baselineskip))
예제 #9
0
    def testParameters(self):
        t = TeX()
        t.input(r'\newcount\foo\foo=\tolerance')
        t.parse()
        foo = t.ownerDocument.context['foo'].value
        tolerance = t.ownerDocument.context['tolerance'].value
        assert foo == tolerance, '"%s" != "%s"' % (foo, tolerance)

        t = TeX()
        t.input(r'\newcount\foo\foo=7\tolerance')
        t.parse()
        foo = t.ownerDocument.context['foo'].value
        tolerance = t.ownerDocument.context['tolerance'].value
        assert foo == (7 * tolerance), '"%s" != "%s"' % (foo, 7 * tolerance)

        t = TeX()
        t.input(r'\newcount\foo\foo=-3\tolerance')
        t.parse()
        foo = t.ownerDocument.context['foo'].value
        tolerance = t.ownerDocument.context['tolerance'].value
        assert foo == (-3 * tolerance), '"%s" != "%s"' % (foo, -3 * tolerance)
예제 #10
0
 def testReadDimen2(self):
     # This is illegal
     #       s = TeX()
     #       s.input(r"'.77pt")
     #       i = s.readDimen()
     #       s = TeX()
     #       s.input(r'"Ccc')
     #       i = s.readDimen()
     s = TeX()
     s.input(r'-,sp')
     i = s.readDimen()
     assert i.sp == 0, i.sp
예제 #11
0
 def testReadDimen(self):
     fuzz = 1e-3
     s = TeX()
     s.input(r'3 in')
     i = s.readDimen()
     assert i.inch - 3 < fuzz, i.inch
     s = TeX()
     s.input(r'29 pc')
     i = s.readDimen()
     assert i.pc - 29 < fuzz, i.pc
     s = TeX()
     s.input(r'-.013837in')
     i = s.readDimen()
     assert i.inch - -0.013837 < fuzz, i.inch
     s = TeX()
     s.input(r'+ 42,1 dd')
     i = s.readDimen()
     assert i.dd - 42.1 < fuzz, i.dd
     s = TeX()
     s.input(r'0.mm')
     i = s.readDimen()
     assert i.mm - 0 < fuzz, i.mm
     s = TeX()
     s.input(r'123456789sp')
     i = s.readDimen()
     assert i.sp - 123456789 < fuzz, i.sp
예제 #12
0
def squash_latex(inp, out, blobs_dir, options, helper=None):
    " transforms LaTeX file"
    if helper is None:
        helper = squash_helper_base()
    if not os.path.isabs(inp): inp = osjoin(blobs_dir, inp)
    thetex = TeX()
    thetex.input(open(inp), Tokenizer=TokenizerPassThru.TokenizerPassThru)
    if isinstance(out, str):
        if not os.path.isabs(out): out = osjoin(blobs_dir, out)
        out = open(out, 'w')
    itertokens = thetex.itertokens()
    squash_recurse(out, thetex, itertokens, options, helper)
    return helper
예제 #13
0
 def invoke(self, tex):
     if self.macroMode == Base.Environment.MODE_END:
         return
     for c in tex.iterchars():
         if c is None or c == '' or c == ' ':
             continue
         else:
             tex.pushToken(c)
             if c != '[':
                 tex.pushTokens([x for x in TeX.TeX().input('[]').itertokens()])
             break
     s = ''.join(Base.verbatim.invoke(self, tex)[1:]).replace('\r', '').split('\n')
     _format(self, s)
예제 #14
0
def reparse_metadata(inp, metadata, blobs_dir, options):
    " reparse metadata of LaTeX file"
    #
    from .transform import squash_helper_reparse_metadata
    helper = squash_helper_reparse_metadata(blobs_dir, metadata, options)
    from ColDoc.latex import environments_we_wont_latex
    if metadata.environ not in environments_we_wont_latex:
        helper.input_macros_with_parameters += options['split_graphic']
    #
    if not os.path.isabs(inp): inp = osjoin(blobs_dir, inp)
    thetex = TeX()
    mydocument = thetex.ownerDocument
    mycontext = mydocument.context
    #
    # give it some context
    mycontext.loadPackage(thetex, 'article.cls', {})
    #if args.split_sections:
    #    mycontext.newcommand('section',1,r'\section{#1}')
    #    mycontext.newcommand('subsection',1,r'\subsection{#1}')
    for name in options['metadata_command']:
        d = '\\' + name + '{#1}'
        #mycontext.newcommand(name, n, d)
        n = 1
        newclass = type(name, (plasTeX.NewCommand, ), {
            'nargs': n,
            'opt': None,
            'definition': d
        })
        assert newclass.nargs == n
        mycontext.addGlobal(name, newclass)
    #
    thetex.input(open(inp), Tokenizer=TokenizerPassThru.TokenizerPassThru)
    out = io.StringIO()
    itertokens = thetex.itertokens()
    squash_recurse(out, thetex, itertokens, options, helper)
    #
    a = osjoin(os.path.dirname(inp), '.back_map.pickle')
    pickle.dump(
        helper.back_map,
        open(a, 'wb'),
    )
    #
    return helper.back_map, helper.metadata
예제 #15
0
 def testDoubleSuper(self):
     tokens = [x for x in TeX().input('^^I ^^A ^^@ ^^M').itertokens()]
     expected = [Other('\x01'), Space(' ')]
     assert tokens == expected, '%s != %s' % (tokens, expected)
예제 #16
0
 def testParameters(self):
     tokens = [x for x in TeX().input(r'\def\foo#1[#2]{hi}').itertokens()]
예제 #17
0
 def testActive2(self):
     t = TeX()
     t.input(r'\catcode`|=\active\catcode`/=\active \def|#1{\textbf{#1}/} \def/{\textit{the end}} |{bold text}')
     output = t.parse()
     assert output[-2].nodeName == 'textbf', output[-2].nodeName
     assert output[-1].nodeName == 'textit', output[-1].nodeName
예제 #18
0
    parser.add_argument('--add-UUID-comments',
                        '--AUC',
                        action='store_true',
                        help="add comments to mark beg/end of blobs")
    #parser.add_argument('--add-UUID','--AU',
    #                    action='store_true', help="add \\uuid{UUID} commands")
    parser.add_argument(
        '--rm-uuid',
        action='store_true',
        help="remove the first line of a blob if it is `\\uuid{...}%` ")
    parser.add_argument('--verbose', '-v', action='count', default=0)
    #
    args = parser.parse_args()
    #
    verbose = args.verbose
    assert type(verbose) == int and verbose >= 0
    if verbose > 1:
        logging.getLogger().setLevel(logging.DEBUG)
    elif verbose:
        logging.getLogger().setLevel(logging.INFO)
    else:
        logging.getLogger().setLevel(logging.WARNING)
    #
    mytex = TeX()
    #
    try:
        deblob_inator(args.root_uuid, mytex, args)
    except:
        logger.exception(__name__ + ' failed!')
        sys.exit(1)
예제 #19
0
import sys, os
from plasTeX import TeX, TeXDocument
from plasTeX.Config import config as texConfig
from plasTeX.Renderers.XHTML import Renderer

FILE = sys.argv[1]
tempdir = sys.argv[2]
texConfig['files']['split-level'] = -10
texConfig['files']['filename'] = u'index$num(0).html'
texConfig['general']['theme'] = 'minimal'
cwd = os.getcwd()
os.chdir(tempdir)
document = TeXDocument(config=texConfig)
tex = TeX.TeX(document, file=FILE)
Renderer().render(tex.parse())
os.chdir(cwd)
예제 #20
0
            unicode(node))


if __name__ == "__main__":
    inputfile = sys.argv[1]

    latexcontent = open(inputfile, 'r').read()
    latexcontent = r'''\documentclass{book}
    
    \usepackage{latex2cnxmlmod}
    \usepackage{longtable}
    \begin{document}
    %s
    \end{document}''' % latexcontent

    tex = TeX.TeX()
    tex.ownerDocument.config['files']['split-level'] = -100
    tex.ownerDocument.config['files']['filename'] = '%s.xml' % inputfile
    tex.input(latexcontent)

    document = tex.parse()
    # Render the document
    renderer = Renderer()
    renderer['chapter'] = renderer.section
    renderer['section'] = renderer.section
    renderer['subsection'] = renderer.section
    renderer['subsubsection'] = renderer.section
    renderer['par'] = renderer.par
    renderer['itemize'] = renderer.itemize
    renderer['keyconcepts'] = renderer.keyconcepts
    renderer['newwords'] = renderer.newwords
예제 #21
0
 def testRomanNumeral(self):
     t = TeX()
     t.input(r'\romannumeral5')
     p = t.parse()
     assert ''.join(p) == 'v'
예제 #22
0
 def testActive(self):
     t = TeX()
     t.input(r'\catcode`|=\active \def|#1{\bf#1} |{bold text}')
     output = t.parse()
     assert output[-1].nodeName == 'bf', output[-1].nodeName
예제 #23
0
 def testActiveSource(self):
     t = TeX()
     t.input(r'~')
     t = t.parse()
     assert t.source.strip() == '~', t.source.strip()