Ejemplo n.º 1
0
 def testTabular(self):
     tex_input = r'\begin{tabular}{lll} \hline a & b & c \\[0.4in] 1 & 2 & 3 \end{tabular}'
     s = TeX()
     s.input(tex_input)
     output = s.parse()
     source = normalize(output.source)
     assert_that( source, is_(tex_input))
Ejemplo n.º 2
0
 def testMathCal(self):
     input = r'a $ \mathcal A $ b'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input == source, '"%s" != "%s"' % (input, source)
Ejemplo n.º 3
0
 def testStringCommand(self):
     class figurename(Command): unicode = 'Figure'
     s = TeX()
     s.input(r'\figurename')
     s.ownerDocument.context['figurename'] = figurename
     output = [x for x in s]
     assert output[0].unicode == 'Figure', output
Ejemplo n.º 4
0
 def testTabular(self):
     input = r'\begin{tabular}{lll} \hline a & b & c \\[0.4in] 1 & 2 & 3 \end{tabular}'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input == source, '"%s" != "%s"' % (input, source)
Ejemplo n.º 5
0
 def testNewcommand(self):
     s = TeX()
     s.input(r'\newcommand\mycommand[2][optional]{\itshape{#1:#2}}\newcommand{ \foo }{{this is foo}}\mycommand[hi]{{\foo}!!!}')
     res = [x for x in s]
     text = [x for x in res if x.nodeType == Node.TEXT_NODE]
     expected = list('hi:this is foo!!!')
     assert text == expected, '%s != %s' % (text, expected)
Ejemplo n.º 6
0
def test_cleveref():
    tex = TeX()
    tex.input(r'''
\documentclass{article}
\usepackage{cleveref}
\newtheorem{thm}{TheoRem}
\begin{document}
\section{Foo}\label{sec}
\begin{figure}
  \caption{Test}
  \label{fig}
\end{figure}
\subsection{Bar}\label{subsec}
\begin{thm}
  \label{thm}
\end{thm}
\begin{equation}
  x = y\label{eq}
\end{equation}
\Cref{sec}\Cref{fig}\Cref{subsec}\Cref{thm}\Cref{eq}
\cref{sec}\cref{fig}\cref{subsec}\cref{thm}\cref{eq}
\end{document}
''')

    p = tex.parse()

    assert ["Section", "Figure", "Section", "TheoRem", "Equation"] == [x.refname() for x in p.getElementsByTagName("Cref")]
    assert ["section", "figure", "section", "theoRem", "eq."] == [x.refname() for x in p.getElementsByTagName("cref")]
    def testLocalCatCodes(self):
        """ Make sure that category codes are local """
        class code(Macro):
            args = 'self:nox'
            def parse(self, tex):
                self.ownerDocument.context.catcode('#',11)
                self.ownerDocument.context.catcode('&',11)
                self.ownerDocument.context.catcode('^',11)
                self.ownerDocument.context.catcode('_',11)
                self.ownerDocument.context.catcode('$',11)
                return Macro.parse(self, tex)
        s = TeX()
        s.input(r'\code{this # is $ some & nasty _ text}&_2')
        s.ownerDocument.context['code'] = code
        tokens = [x for x in s]

        tok = type(tokens[0])
        cs = type(s.ownerDocument.createElement('code'))
        assert tok is cs, '"%s" != "%s"' % (tok, cs)

        assert not [x.catcode for x in tokens[0].attributes['self'] if x.catcode not in [10,11]], \
               'All codes should be 10 or 11: %s' % [x.code for x in tokens[0]]

        tok = type(tokens[-3])
        cs = type(s.ownerDocument.createElement('active::&'))
        assert tok is cs, '"%s" != "%s"' % (tok, cs)
Ejemplo n.º 8
0
class WordPressLatexPost(WordPressPost):
    """Encapsulate a LaTeX Post to Wordpress
    """
    
    def __init__(self, latex):
        """
        
        Arguments:
        - `latex`:
        """
        WordPressPost.__init__(self)

        self._tex = TeX()
        self._renderer = WPRenderer()

        self._tex.input(latex)
        self._document = self._tex.parse()

        # Before rendering, let's pass through and extract the images
        self.images = findImageNodes(self._document)
        
    def render(self):
        """Render this post
        """
        # Have to encode the unicode string to print it correctly.
        self.description = self._renderer.render(self._document).encode('utf-8')
        self.title = self._document.userdata['title'].textContent.encode('utf-8')
        self.tags = self._document.userdata['tags'].textContent.encode('utf-8')
Ejemplo n.º 9
0
 def testTabular(self):
     input = r'\begin{tabular}{lll} \hline a & b & c \\[0.4in] 1 & 2 & 3 \end{tabular}'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input == source, '"%s" != "%s"' % (input, source)
Ejemplo n.º 10
0
    def testParentage(self):
        class myenv(Environment):
            pass

        class foo(Command):
            args = 'self'

        s = TeX()
        s.ownerDocument.context.importMacros(locals())
        s.input(r'\begin{myenv}\foo{TEST}\end{myenv}')
        output = s.parse()

        myenv = output[0]
        assert myenv.nodeName == 'myenv', myenv.nodeName

        foo = output[0][0]
        assert foo.nodeName == 'foo', foo.nodeName

        children = output[0][0].childNodes
        fooself = output[0][0].attributes['self']
        assert children is fooself

        # Check parents
        assert fooself[0].parentNode is fooself, fooself[0].parentNode
        assert foo.parentNode is myenv, foo.parentNode
        assert myenv.parentNode is output, myenv.parentNode
        assert output.parentNode is None, output.parentNode

        # Check owner document
        assert fooself[0].ownerDocument is output
        assert foo.ownerDocument is output
        assert myenv.ownerDocument is output
        assert output.ownerDocument is output
Ejemplo n.º 11
0
 def testNewcommand(self):
     s = TeX()
     s.input(r'\newcommand\mycommand[2][optional]{\itshape{#1:#2}}\newcommand{ \foo }{{this is foo}}\mycommand[hi]{{\foo}!!!}')
     res = [x for x in s]
     text = [x for x in res if x.nodeType == Node.TEXT_NODE]
     expected = list('hi:this is foo!!!')
     assert text == expected, '%s != %s' % (text, expected)
Ejemplo n.º 12
0
    def testParentage(self):
        class myenv(Environment):
            pass
        class foo(Command):
            args = 'self'
        s = TeX()
        s.ownerDocument.context.importMacros(locals())
        s.input(r'\begin{myenv}\foo{TEST}\end{myenv}')
        output = s.parse()

        myenv = output[0]
        assert myenv.nodeName == 'myenv', myenv.nodeName

        foo = output[0][0]
        assert foo.nodeName == 'foo', foo.nodeName

        children = output[0][0].childNodes
        fooself = output[0][0].attributes['self']
        assert children is fooself

        # Check parents
        assert fooself[0].parentNode is fooself, fooself[0].parentNode
        assert foo.parentNode is myenv, foo.parentNode
        assert myenv.parentNode is output, myenv.parentNode
        assert output.parentNode is None, output.parentNode

        # Check owner document
        assert fooself[0].ownerDocument is output
        assert foo.ownerDocument is output
        assert myenv.ownerDocument is output
        assert output.ownerDocument is output
Ejemplo n.º 13
0
 def testStringCommand(self):
     class figurename(Command): unicode = 'Figure'
     s = TeX()
     s.input(r'\figurename')
     s.ownerDocument.context['figurename'] = figurename
     output = [x for x in s]
     assert output[0].unicode == 'Figure', output
Ejemplo n.º 14
0
 def testTokenArgument(self):
     s = TeX()
     s.input(r'\foo a ')
     arg = s.readArgument(type='Tok')
     assert arg == 'foo'
     arg = s.readArgument(type='Tok')
     assert arg == 'a'
Ejemplo n.º 15
0
 def testMath(self):
     tex_input = r'a $ x^{y_3} $ b'
     s = TeX()
     s.input(tex_input)
     output = s.parse()
     source = normalize(output.source)
     assert_that( source, is_(tex_input))
Ejemplo n.º 16
0
    def __init__(self,
                 fn,
                 imdir="static/images",
                 imurl="",
                 fp=None,
                 extra_filters=None,
                 latex_string=None,
                 add_wrap=False,
                 fix_plastex_optarg_bug=True,
                 abox=None,
                 imurl_fmt=None,
                 verbose=False):
        '''
        fn            = tex filename (should end in .tex)
        imdir         = directory where images are to be stored
        imurl         = web root for images
        fp            = file object (optional) - used instead of open(fn), if provided
        extra_filters = dict with key=regular exp, value=function for search-replace, for
                        post-processing of XHTML output
        latex_string  = latex string (overrides fp and fn)
        add_wrap      = if True, then assume latex is partial, and add preamble and postfix
        fix_plastex_optarg_bug = if True, then filter the input latex to fix the plastex bug 
                                 triggered e.g. by \begin{edXchapter} and \begin{edXsection}
                                 being placed with no empty newline inbetween
        abox          = (class) use this in place of AnswerBox
        imurl_fmt     = (str) image url format expression
        verbose       = if True, then do verbose logging
        '''

        if fn.endswith('.tex'):
            ofn = fn[:-4] + '.xhtml'
        else:
            ofn = fn + ".xhtml"

        self.input_fn = fn
        self.output_fn = ofn
        self.fp = fp
        self.latex_string = latex_string
        self.add_wrap = add_wrap
        self.verbose = verbose
        self.renderer = MyRenderer(imdir,
                                   imurl,
                                   extra_filters,
                                   abox,
                                   imurl_fmt=imurl_fmt,
                                   verbose=verbose)
        self.fix_plastex_optarg_bug = fix_plastex_optarg_bug

        # Instantiate a TeX processor and parse the input text
        tex = TeX()
        tex.ownerDocument.config['files']['split-level'] = -100
        tex.ownerDocument.config['files']['filename'] = self.output_fn
        tex.ownerDocument.config['general']['theme'] = 'plain'

        plasTeXconfig.add_section('logging')
        plasTeXconfig['logging'][''] = CRITICAL

        self.tex = tex
        if not self.verbose:
            tex.disableLogging()
    def testLocalCatCodes(self):
        """ Make sure that category codes are local """
        class code(Macro):
            args = 'self:nox'
            def parse(self, tex):
                self.ownerDocument.context.catcode('#',11)
                self.ownerDocument.context.catcode('&',11)
                self.ownerDocument.context.catcode('^',11)
                self.ownerDocument.context.catcode('_',11)
                self.ownerDocument.context.catcode('$',11)
                return Macro.parse(self, tex)
        s = TeX()
        s.input('\code{this # is $ some & nasty _ text}&_2')
        s.ownerDocument.context['code'] = code
        tokens = [x for x in s]

        tok = type(tokens[0])
        cs = type(s.ownerDocument.createElement('code'))
        assert tok is cs, '"%s" != "%s"' % (tok, cs)

        assert not [x.catcode for x in tokens[0].attributes['self'] if x.catcode not in [10,11]], \
               'All codes should be 10 or 11: %s' % [x.code for x in tokens[0]]

        tok = type(tokens[-3])
        cs = type(s.ownerDocument.createElement('active::&'))
        assert tok is cs, '"%s" != "%s"' % (tok, cs)
Ejemplo n.º 18
0
 def testMath(self):
     input = r'a $ x^{y_3} $ b'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input == source, '"%s" != "%s"' % (input, source)
Ejemplo n.º 19
0
 def testSimpleNewEnvironment(self):
     s = TeX()
     s.input(r"\newenvironment{myenv}{\it}{}\begin{myenv}hi\end{myenv}")
     for key, value in list(self.macros.items()):
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     assert type(output[2]) == s.ownerDocument.context["it"]
     assert output[-3:-1] == ["h", "i"]
Ejemplo n.º 20
0
def test_numberwithin():
    s = TeX()
    s.input(DOC)
    output = s.parse()
    equations =output.getElementsByTagName('equation')
    assert equations[0].ref.textContent == '1.1'
    assert equations[1].ref.textContent == '2.1'
    assert equations[2].ref.textContent == '2.2'
Ejemplo n.º 21
0
def test_numberwithin():
    s = TeX()
    s.input(DOC)
    output = s.parse()
    equations = output.getElementsByTagName('equation')
    assert equations[0].ref.textContent == '1.1'
    assert equations[1].ref.textContent == '2.1'
    assert equations[2].ref.textContent == '2.2'
Ejemplo n.º 22
0
 def testSimpleNewEnvironment(self):
     s = TeX()
     s.input(r'\newenvironment{myenv}{\it}{}\begin{myenv}hi\end{myenv}')
     for key, value in self.macros.items():
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     assert type(output[2]) == s.ownerDocument.context['it']
     assert output[-3:-1] == ['h','i']
Ejemplo n.º 23
0
    def testEquation(self):
        tex_input = r'\sqrt{\pi ^{3}}'
        s = TeX()
        s.input(tex_input)

        output = s.parse()
        source = normalize(output.source)
        assert_that( source, is_(tex_input))
Ejemplo n.º 24
0
 def testXYMatrix(self):
     input = r'\xymatrix{A \ar[d]^b \ar[r]^a &B\ar[d]^c\\ C \ar[r]^d &D}'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input.replace(" ", "") == source.replace(
         " ", ""), '"%s" != "%s"' % (input, source)
Ejemplo n.º 25
0
 def testLabelStar(self):
     s = TeX()
     s.input(r'\section{hi} text \section*{bye\label{two}}')
     output = s.parse()
     one = output[0]
     two = output[-1]
     assert one.id == 'two', one.id
     assert two.id != 'two', two.id
Ejemplo n.º 26
0
 def testXYMatrix2(self):
     input = r'\xymatrix{A \ar@{->>}[rd] \ar@{^{(}->}[r]&B \ar@{.>}[d]&C \ar@{_{(}->}[l]\ar@{->>}[ld]\\&D}'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input.replace(" ", "") == source.replace(
         " ", ""), '"%s" != "%s"' % (input, source)
Ejemplo n.º 27
0
    def testEquation(self):
        tex_input = r'\sqrt{\pi ^{3}}'
        s = TeX()
        s.input(tex_input)

        output = s.parse()
        source = normalize(output.source)
        assert_that(source, is_(tex_input))
Ejemplo n.º 28
0
 def testIfDim(self):
     s = TeX()
     s.input(
         r'\ifdim -5 pt > 2in bye\else text\fi\ifdim 2mm = 2 mm one\else two\fi'
     )
     output = ''.join([x for x in s]).strip()
     expected = 'textone'
     assert output == expected, '"%s" != "%s"' % (output, expected)
Ejemplo n.º 29
0
    def parse(self):
        self.doc = plasTeX.TeXDocument(config=self.config)
        self.tex = TeX(self.doc, file=self.filename)
        self.tex.parse()

        renderer = EpubRenderer()
        renderer.chapterName = self.name
        renderer.render(self.doc)
Ejemplo n.º 30
0
 def testLabel(self):
     s = TeX()
     s.input(r'\section{hi\label{one}} text \section{bye\label{two}}')
     output = s.parse()
     one = output[0]
     two = output[-1]
     assert one.id == 'one', one.id
     assert two.id == 'two', two.id
Ejemplo n.º 31
0
 def testSimpleNewEnvironment(self):
     s = TeX()
     s.input(r'\newenvironment{myenv}{\it}{}\begin{myenv}hi\end{myenv}')
     for key, value in self.macros.items():
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     assert type(output[2]) == s.ownerDocument.context['it']
     assert output[-3:-1] == ['h', 'i']
Ejemplo n.º 32
0
 def testDef(self):
     s = TeX()
     s.input(
         r'\def\mymacro#1#2;#3\endmacro{this #1 is #2 my #3 command}\mymacro{one}x;y\endmacro morestuff'
     )
     output = [x for x in s]
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list('this one is x my y commandmorestuff'), text
Ejemplo n.º 33
0
 def testXYMatrix3(self):
     input = r'\xymatrix{A \ar[r]^u_d \ar[rd]^u_d &B \ar[d]^u_d &C \ar[l]^u_d \ar[ld]^u_d\\&D}'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input.replace(" ", "") == source.replace(
         " ", ""), '"%s" != "%s"' % (input, source)
Ejemplo n.º 34
0
def test_imager(imager_tuple, tmpdir):
    imager, ext, compiler = imager_tuple
    if ext == ".svg":
        kind = "vector-imager"
    else:
        kind = "imager"

    tmpdir = Path(str(tmpdir)) # for old pythons

    tex = TeX()
    tex.ownerDocument.config['images'][kind] = imager
    if compiler is not None:
        tex.ownerDocument.config['images'][kind.replace("imager", "compiler")] = compiler

    tex.input(r'''
    \documentclass{article}
    \AtBeginDocument{You should not be seeing this in the imager output. This
    is injected into the document with \textbackslash AtBeginDocument. The actual
    image is on the second page and the imager should get the images from
    there.}
    \begin{document}
    $a + b = x$
    \end{document}
    ''')

    renderer = Renderer()
    if kind == "imager":
        renderer['math'] = lambda node: node.image.url
    else:
        renderer['math'] = lambda node: node.vectorImage.url

    directory = os.getcwd()
    os.chdir(str(tmpdir))
    renderer.render(tex.parse())
    os.chdir(directory)

    outfile = tmpdir/'images'/('img-0001' + ext)
    root = Path(__file__).parent

    benchfile = root/'benchmarks'/"{}-{}{}".format(imager, compiler, ext)
    if not benchfile.exists():
        (root/'new').mkdir(parents=True, exist_ok=True)
        shutil.copyfile(str(outfile), str(root/'new'/benchfile.name))
        raise OSError('No benchmark file: %s' % benchfile)

    diff = cmp_img(str(benchfile.absolute()), str(outfile.absolute()))

    if (ext == '.svg' and diff > 0.0001) or diff > 0.01:
        (root/'new').mkdir(parents=True, exist_ok=True)
        shutil.copyfile(str(outfile), str(root/'new'/benchfile.name))

        if ext == '.svg':
            bench = benchfile.read_text().split('\n')
            output = outfile.read_text()
            print('SVG differences:\n','\n'.join(difflib.unified_diff(
                bench, output.split('\n'), fromfile='benchmark', tofile='output')).strip())
            print('Full svg:\n\n', output)
        assert False, 'Differences were found:\n' + str(diff)
Ejemplo n.º 35
0
 def testNewCommandWithArgs(self):
     s = TeX()
     s.input(r"\newcommand{\mycommand}[2]{#1:#2}\mycommand{foo}{bar}")
     for key, value in list(self.macros.items()):
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     assert s.ownerDocument.context["mycommand"].definition == list("#1:#2")
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list("foo:bar"), text
Ejemplo n.º 36
0
 def testNewCommandWithArgs(self):
     s = TeX()
     s.input(r'\newcommand{\mycommand}[2]{#1:#2}\mycommand{foo}{bar}')
     for key, value in self.macros.items():
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     assert s.ownerDocument.context['mycommand'].definition == list('#1:#2')
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list('foo:bar'), text
Ejemplo n.º 37
0
def latex2edx():    
    print "============================================================================="
    print "Converting latex to XHTML using PlasTeX with custom edX macros"
    print "Source file: %s" % myglob.INPUT_TEX_FILENAME
    print "============================================================================="

    # get the input latex file
    # latex_str = open(fn).read()
    latex_str = codecs.open(myglob.fn).read()
    latex_str = latex_str.replace('\r','\n')	# convert from mac format for EOL
    
    # Instantiate a TeX processor and parse the input text
    tex = TeX()
    tex.ownerDocument.config['files']['split-level'] = -100
    tex.ownerDocument.config['files']['filename'] = myglob.ofn
    tex.ownerDocument.config['general']['theme'] = 'plain'
    
    tex.input(latex_str)
    document = tex.parse()
    
    renderer = MyRenderer()
    renderer.imdir = myglob.imdir
    renderer.imurl = myglob.imurl
    renderer.imfnset = []

    #Here's where the xhtml file myglob.ofn is written
    myglob.outxhtml.append(myglob.ofn)
    renderer.render(document)
    
    print "\n======================================== IMAGE FILES"
    print renderer.imfnset or "None"
    print "========================================"
    
    #--------------------
    # read XHTML file in and extract course + problems (JMO removed course code)
    print "============================================================================="
    print "Converting XHTML into edX problems"
    print "============================================================================="

    xml = etree.parse(myglob.ofn)
    process_edXmacros(xml.getroot())

    course = xml.find('.//course')		# top-level entry for edX course - should only be one
    chapters = xml.findall('.//chapter')	# get all chapters
    if course is not None:
        raise Exception("This version of latex2edx does not handle the course tag")
    if chapters:
        raise Exception("This version of latex2edx does not handle the chapter tag")

    for problem in xml.findall('.//problem'):
        problem_to_file(problem, myglob.problemdir)

    for html in xml.findall('.//html'):
        html_to_file(html, myglob.htmldir)
    
    return ({'htmlfiles':myglob.outhtml, 'problemfiles':myglob.outproblem,
             'xhtmlfiles':myglob.outxhtml})
Ejemplo n.º 38
0
 def testSimpleNewCommand(self):
     s = TeX()
     s.input(r"\newcommand\mycommand{\it}\mycommand")
     for key, value in list(self.macros.items()):
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     result = type(output[1])
     expected = type(s.ownerDocument.createElement("it"))
     assert result == expected, '"%s" != "%s"' % (result, expected)
Ejemplo n.º 39
0
    def testStringCommand(self):
        class figurename(Command):
            unicode = "Figure"

        s = TeX()
        s.input(r"\figurename")
        s.ownerDocument.context["figurename"] = figurename
        output = [x for x in s]
        assert output[0].unicode == "Figure", output
Ejemplo n.º 40
0
 def testVerbatim(self):
     intext = 'line one\nline    two'
     input = 'hi \\begin{verbatim}\n%s\n\\end{verbatim} bye' % intext
     s = TeX()
     s.input(input)
     output = s.parse()
     output.normalize()
     text = ''.join(output.childNodes[1].childNodes).strip()
     assert intext == text, '"%s" != "%s"' % (intext, text)
Ejemplo n.º 41
0
def simpleparse(text):
    """Parse a simple LaTeX string.
    """
    tex = TeX()
    if not isinstance(text, unicode):
        text = text.decode("utf-8")
    tex.input(text)
    doc = tex.parse()
    return process_unbr_spaces(doc.textContent)
Ejemplo n.º 42
0
def test_math_in_textrm():
    tex = TeX()
    tex.input(r'''
      \documentclass{article}
      \begin{document}
      $\textrm{$x$}$
      \end{document}
      ''')
    assert tex.parse().getElementsByTagName('math')[0].textContent == 'x'
Ejemplo n.º 43
0
 def testVerbStar(self):
     intext = r' verbatim \tt text '
     input = r'hi \verb*+%s+ bye' % intext
     s = TeX()
     s.input(input)
     output = s.parse()
     output.normalize()
     text = ''.join(output.childNodes[1].childNodes)
     assert intext == text, '"%s" != "%s"' % (intext, text)
Ejemplo n.º 44
0
 def testDef2(self):
     s = TeX()
     s.input(r"\def\row#1{(#1_1,\ldots,#1_n)}\row{{x'}}")
     output = [x for x in s]
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list('(x\'_1,,x\'_n)'), text
     assert output[0].nodeName == 'def'
     assert output[6] == '_'
     assert output[9].nodeName == 'ldots'
Ejemplo n.º 45
0
 def testVerbStar(self):
     intext = r' verbatim \tt text '
     input = r'hi \verb*+%s+ bye' % intext
     s = TeX()
     s.input(input)
     output = s.parse()
     output.normalize()
     text = ''.join(output.childNodes[1].childNodes)
     assert intext == text, '"%s" != "%s"' % (intext, text)
Ejemplo n.º 46
0
def test_charsub():
    config = base_config.copy()

    doc = TeXDocument(config=config)
    tex = TeX(doc)

    p = tex.input(r'''{``'' '---}''').parse()[0]
    p.paragraphs()
    assert p.textContent == "“” ’—"
Ejemplo n.º 47
0
 def testVerbatim(self):
     intext = 'line one\nline    two'
     input = 'hi \\begin{verbatim}\n%s\n\\end{verbatim} bye' % intext
     s = TeX()
     s.input(input)
     output = s.parse()
     output.normalize()
     text = ''.join(output.childNodes[1].childNodes).strip()
     assert intext == text, '"%s" != "%s"' % (intext, text)
Ejemplo n.º 48
0
 def testDef2(self):
     s = TeX()
     s.input(r"\def\row#1{(#1_1,\ldots,#1_n)}\row{{x'}}")
     output = [x for x in s]
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list('(x\'_1,,x\'_n)'), text
     assert output[0].nodeName == 'def'
     assert output[6] == '_'
     assert output[9].nodeName == 'ldots'
Ejemplo n.º 49
0
 def testSimpleNewCommand(self):
     s = TeX()
     s.input(r'\newcommand\mycommand{\it}\mycommand')
     for key, value in self.macros.items():
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     result = type(output[1])
     expected = type(s.ownerDocument.createElement('it'))
     assert result == expected, '"%s" != "%s"' % (result, expected)
Ejemplo n.º 50
0
 def testDef2(self):
     s = TeX()
     s.input(r"\def\row#1{(#1_1,\ldots,#1_n)}\row{{x'}}")
     output = [x for x in s]
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list("(x'_1,,x'_n)"), text
     assert output[0].nodeName == "def"
     assert output[6] == "_"
     assert output[9].nodeName == "ldots"
Ejemplo n.º 51
0
    def runPlastex(self, item):
        if not item.course.args.veryverbose:
            plasTeX.Logging.disableLogging()

        logger.debug("PlasTeX: " + str(item.source))
        root_dir = self.get_root_dir()
        outPath = item.temp_path()
        outPaux = self.temp_path().resolve()
        inPath = root_dir / item.source

        wd = os.getcwd()

        plastex_config['files']['filename'] = item.plastex_filename_rules
        plastex_config['files']['split-level'] = item.splitlevel
        plastex_config['general']['renderer'] = 'chirun'
        plastex_config['document']['base-url'] = self.get_web_root()
        plastex_config['images']['vector-imager'] = 'none'
        plastex_config['images']['imager'] = 'none'
        self.document = TeXDocument(config=plastex_config)
        self.document.userdata['working-dir'] = '.'

        self.document.context.importMacros(vars(overrides))

        f = open(str(Path(wd) / inPath))
        TeX.processIfContent = _processIfContent
        tex = TeX(self.document, myfile=f)
        self.document.userdata['jobname'] = tex.jobname
        pauxname = os.path.join(
            self.document.userdata.get('working-dir', '.'),
            '%s.paux' % self.document.userdata.get('jobname', ''))

        for fname in glob.glob(str(outPaux / '*.paux')):
            if os.path.basename(fname) == pauxname:
                continue
            self.document.context.restore(fname, 'chirun')

        sys.excepthook = PlastexRunner.exception_handler
        tex.parse()
        f.close()

        os.chdir(str(outPath))
        self.renderer = Renderer()
        self.renderer.loadTemplates(self.document)
        self.renderer.importDirectory(
            str(Path(wd) / self.theme.source / 'plastex'))
        self.renderer.vectorImager = VectorImager(
            self.document, self.renderer.vectorImageTypes)
        self.renderer.imager = Imager(self.document, self.renderer.imageTypes)
        self.renderer.render(self.document)

        os.chdir(wd)

        original_paux_path = item.temp_path() / item.base_file.with_suffix(
            '.paux')
        collated_paux_path = self.temp_path() / (
            str(item.out_path).replace('/', '-') + '.paux')
        shutil.copyfile(str(original_paux_path), str(collated_paux_path))
Ejemplo n.º 52
0
def execute_mediawiki_parser(config):
    # data
    input_path = config["input_path"]
    output_path = config["output_path"]
    title = config["title"]
    collapse_level = int(config["collapse_level"])

    # startin process process
    f = open(input_path, "r")
    # input text must be utf-8 encoded.
    text = f.read().decode("utf-8")
    ###preparser operations
    # reading theorems
    # the preparser result is a tuple of (tex, th_dict)
    pre_export_path = ""
    if config["print_preparsed_tex"]:
        pre_export_path = output_path + ".pre"
    preparser_result = preparse_tex(text, pre_export_path)
    # saving source after preparsing
    source = preparser_result[0]
    # tex object
    tex = TeX()
    tex.input(source)
    # parsing DOM
    document = tex.parse()
    # renderer creation
    rend = MediaWikiRenderer(config)
    # inserting theorem dictionary in renderer
    rend.init_theorems(preparser_result[1])
    # inserting tikz images source in renderer
    rend.init_tikz_images(preparser_result[2], preparser_result[3])
    # starting rendering
    rend.render(document)
    # after rendering work
    # collapsing pages
    rend.tree.collapseText(collapse_level)
    # fixing refs
    rend.tree.fixReferences()
    # create index
    if config["create_index"]:
        rend.tree.createIndex(collapse_level)
        # exporting
    export = rend.tree.exportPages(output_path + ".mw", (config["export_format"], config["username"], config["userid"]))
    # check if we have to export single pages
    if config["export_single_pages"]:
        # writing single pages
        rend.tree.export_singlePages(
            config["export_format"], output_path + "_pages", (config["username"], config["userid"])
        )
        # writing debug info
    d = open(output_path + ".debug", "w")
    # used_tags
    d.write("USED TAGS:\n")
    for key in sorted(rend.used_tags):
        d.write(key + ": " + str(rend.used_tags[key]) + "\n")
    d.close()
Ejemplo n.º 53
0
 def testNewCommandWithOptional(self):
     s = TeX()
     s.input(r'\newcommand{\mycommand}[2][opt]{#1:#2}\mycommand{bar}\mycommand[foo]{bar}')
     for key, value in self.macros.items():
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     assert s.ownerDocument.context['mycommand'].definition == list('#1:#2')
     assert s.ownerDocument.context['mycommand'].opt == list('opt'), '"%s" != "opt"' % (s.ownerDocument.context['mycommand'].opt)
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list('opt:barfoo:bar'), text
Ejemplo n.º 54
0
 def testDictArgument2(self):
     s = TeX()
     s.input(r'{one=1, two={\par}, three={$(x,y)$}, four=4}')
     arg = s.readArgument(type='dict')
     keys = list(arg.keys())
     keys.sort()
     expectkeys = ['four', 'one', 'three', 'two']
     assert keys == expectkeys, '"%s" != "%s"' % (keys, expectkeys)
     assert arg['one'] == '1'
     assert arg['four'] == '4'
Ejemplo n.º 55
0
def test_modify_charsub():
    config = base_config.copy()
    config["document"]["disable-charsub"] = "'"

    doc = TeXDocument(config=config)
    tex = TeX(doc)

    p = tex.input(r'''{``'' '---}''').parse()[0]
    p.paragraphs()
    assert p.textContent == "“” '—"
Ejemplo n.º 56
0
 def testNewCommandWithOptional(self):
     s = TeX()
     s.input(r'\newcommand{\mycommand}[2][opt]{#1:#2}\mycommand{bar}\mycommand[foo]{bar}')
     for key, value in self.macros.items():
         s.ownerDocument.context[key] = value
     output = [x for x in s]
     assert s.ownerDocument.context['mycommand'].definition == list('#1:#2')
     assert s.ownerDocument.context['mycommand'].opt == list('opt'), '"%s" != "opt"' % (s.ownerDocument.context['mycommand'].opt)
     text = [x for x in output if x.nodeType == Node.TEXT_NODE]
     assert text == list('opt:barfoo:bar'), text
Ejemplo n.º 57
0
 def testDictArgument2(self):
     s = TeX()
     s.input(r'{one=1, two={\par}, three={$(x,y)$}, four=4}')
     arg = s.readArgument(type='dict')
     keys = list(arg.keys())
     keys.sort()
     expectkeys = ['four','one','three','two']
     assert keys == expectkeys, '"%s" != "%s"' % (keys, expectkeys)
     assert arg['one'] == '1'
     assert arg['four'] == '4'
Ejemplo n.º 58
0
 def create_tex():
     """Create a TeX object, ready to parse a tex file."""
     tex = TeX()
     tex.disableLogging()
     tex.ownerDocument.context.loadBaseMacros()
     sys.path.append(os.path.dirname(__file__))
     tex.ownerDocument.context.loadPackage(tex, "plastex_patchedbabel")
     tex.ownerDocument.context.loadPackage(tex, "plastex_chord")
     tex.ownerDocument.context.loadPackage(tex, "plastex_songs")
     sys.path.pop()
     return tex
Ejemplo n.º 59
0
    def testLabel(self):
        s = TeX()
        s.input(r'\section{hi\label{one}} text \section{bye\label{two}}')
        output = s.parse()
        one = output[0]
        two = output[-1]

        __traceback_info__ = one.__dict__
        assert_that( one, has_property( 'id', 'one' ) )
        __traceback_info__ = two.__dict__
        assert_that( two, has_property( 'id', 'two' ) )
Ejemplo n.º 60
0
 def testSection(self):
     input = r'\section{Heading 1} foo one \subsection{Heading 2} bar two'
     s = TeX()
     s.input(input)
     output = s.parse()
     source = normalize(output.source)
     assert input == source, '"%s" != "%s"' % (input, source)
  
     input = r'\subsection{Heading 2} bar two'
     item = output[0].lastChild
     source = normalize(item.source)
     assert input == source, '"%s" != "%s"' % (input, source)