Пример #1
0
    def makeTexEquation(self, s, l, t):
        eqn = t[0].strip()
        if len(eqn) == 0:
            return u""

        thumb = Thumbnails(self.parser.page)

        try:
            path = thumb.getThumbPath(True)
        except IOError:
            return u"<b>{}</b>".format(_(u"Can't create thumbnails directory"))

        tex = getTexRender(path)

        try:
            image_fname = tex.makeImage(eqn)
        except IOError:
            return u"<b>{}</b>".format(_(u"Can't create image file"))

        image_path = os.path.join(Thumbnails.getRelativeThumbDir(),
                                  image_fname)
        result = u'<img src="{image}"/>'.format(
            image=image_path.replace("\\", "/"))

        return result
Пример #2
0
    def test_inline_02(self):
        from texequation.defines import KATEX_DIR_NAME
        eqn1 = "y = f1(x)"
        eqn2 = "y = f2(x)"
        text = "{{$ {eqn1} $}} {{$ {eqn2} $}}".format(eqn1=eqn1, eqn2=eqn2)

        path = os.path.join(Thumbnails.getRelativeThumbDir(), KATEX_DIR_NAME)

        result_lines = [
            '<span class="texequation-inline" id="texequation-inline-0"></span>',
            '<span class="texequation-inline" id="texequation-inline-1"></span>',
        ]

        footer_lines = [
            'var element_0 = document.getElementById("texequation-inline-0");',
            'var element_1 = document.getElementById("texequation-inline-1");',
            'katex.render("{}",'.format(eqn1),
            'katex.render("{}",'.format(eqn2),
        ]

        result = self.parser.toHtml(text)

        for line in result_lines:
            self.assertIn(line, result)

        for line in footer_lines:
            self.assertIn(line, self.parser.footer)

        full_path = os.path.join(self.parser.page.path, path)
        self.assertTrue(os.path.exists(full_path), full_path)
Пример #3
0
    def test_block_01(self):
        from texequation.defines import KATEX_DIR_NAME
        eqn = "y = f(x)"
        text = "{{$$ {eqn} $$}}".format(eqn=eqn)

        path = os.path.join(Thumbnails.getRelativeThumbDir(), KATEX_DIR_NAME)

        result_lines = [
            '<span class="texequation-block" id="texequation-block-0"></span>',
        ]

        footer_lines = [
            'var element_0 = document.getElementById("texequation-block-0");',
            'katex.render("{}",'.format(eqn),
        ]

        result = self.parser.toHtml(text)

        for line in result_lines:
            self.assertIn(line, result)

        for line in footer_lines:
            self.assertIn(line, self.parser.footer)

        full_path = os.path.join(self.parser.page.path, path)
        self.assertTrue(os.path.exists(full_path), full_path)
Пример #4
0
    def test_inline_01(self):
        eqn = u"y = f(x)"
        text = u"{{$ {eqn} $}}".format(eqn=eqn)

        path = os.path.join(Thumbnails.getRelativeThumbDir(), 'katex')

        result_lines = [
            u'<span class="texequation-inline" id="texequation-inline-0"></span>',
        ]

        footer_lines = [
            u'var element = document.getElementById("texequation-inline-0");',
            u'katex.render("{}",'.format(eqn),
        ]

        result = self.parser.toHtml(text)

        for line in result_lines:
            self.assertIn(line, result)

        for line in footer_lines:
            self.assertIn(line, self.parser.footer)

        full_path = os.path.join(self.parser.page.path, path)
        self.assertTrue(os.path.exists(full_path), full_path)
Пример #5
0
    def test_block_02(self):
        eqn1 = u"y = f1(x)"
        eqn2 = u"y = f2(x)"

        text = u"{{$$ {eqn1} $$}} {{$$ {eqn2} $$}}".format(eqn1=eqn1,
                                                           eqn2=eqn2)

        path = os.path.join(Thumbnails.getRelativeThumbDir(), 'katex')

        result_lines = [
            u'<span class="texequation-block" id="texequation-block-0"></span>',
            u'<span class="texequation-block" id="texequation-block-1"></span>',
        ]

        footer_lines = [
            u'var element = document.getElementById("texequation-block-0");',
            u'var element = document.getElementById("texequation-block-1");',
            u'katex.render("{}",'.format(eqn1),
            u'katex.render("{}",'.format(eqn2),
        ]

        result = self.parser.toHtml(text)

        for line in result_lines:
            self.assertIn(line, result)

        for line in footer_lines:
            self.assertIn(line, self.parser.footer)

        full_path = os.path.join(self.parser.page.path, path)
        self.assertTrue(os.path.exists(full_path), full_path)
Пример #6
0
    def testTex2(self):
        from texequation.texrender import getTexRender
        thumb = Thumbnails(self.parser.page)
        texrender = getTexRender(thumb.getThumbPath(True))

        eqn1 = u"y = f(x)"
        eqn2 = u"y = e^x"
        eqn3 = u"y = \sum_{i=0}\pi"

        text = u"""бла-бла-бла
* бла-бла-бла {$ %s $} 1111
* бла-бла-бла {$ %s $} 222
* бла-бла-бла {$ %s $} 333""" % (eqn1, eqn2, eqn3)

        fname1 = texrender.getImageName(eqn1)
        fname2 = texrender.getImageName(eqn2)
        fname3 = texrender.getImageName(eqn3)

        path1 = os.path.join(Thumbnails.getRelativeThumbDir(), fname1)
        path2 = os.path.join(Thumbnails.getRelativeThumbDir(), fname2)
        path3 = os.path.join(Thumbnails.getRelativeThumbDir(), fname3)

        result_right = u'''бла-бла-бла
<ul><li>бла-бла-бла <img src="{path1}"/> 1111</li><li>бла-бла-бла <img src="{path2}"/> 222</li><li>бла-бла-бла <img src="{path3}"/> 333</li></ul>'''.format(
            path1=path1.replace("\\", "/"),
            path2=path2.replace("\\", "/"),
            path3=path3.replace("\\", "/"))

        result = self.parser.toHtml(text)

        self.assertEqual(result_right, result, result)

        full_path1 = os.path.join(self.parser.page.path, path1)
        full_path2 = os.path.join(self.parser.page.path, path2)
        full_path3 = os.path.join(self.parser.page.path, path3)

        self.assertTrue(os.path.exists(full_path1), full_path1)
        self.assertTrue(os.path.exists(full_path2), full_path2)
        self.assertTrue(os.path.exists(full_path3), full_path3)
Пример #7
0
    def testTex2 (self):
        thumb = Thumbnails (self.parser.page)
        texrender = getTexRender(thumb.getThumbPath (True))

        eqn1 = u"y = f(x)"
        eqn2 = u"y = e^x"
        eqn3 = u"y = \sum_{i=0}\pi"

        text = u"""бла-бла-бла
* бла-бла-бла {$ %s $} 1111
* бла-бла-бла {$ %s $} 222
* бла-бла-бла {$ %s $} 333""" % (eqn1, eqn2, eqn3)

        fname1 = texrender.getImageName (eqn1)
        fname2 = texrender.getImageName (eqn2)
        fname3 = texrender.getImageName (eqn3)

        path1 = os.path.join (Thumbnails.getRelativeThumbDir(), fname1)
        path2 = os.path.join (Thumbnails.getRelativeThumbDir(), fname2)
        path3 = os.path.join (Thumbnails.getRelativeThumbDir(), fname3)

        result_right = u'''бла-бла-бла
<UL><LI>бла-бла-бла <IMG SRC="{path1}"/> 1111</LI><LI>бла-бла-бла <IMG SRC="{path2}"/> 222</LI><LI>бла-бла-бла <IMG SRC="{path3}"/> 333</LI></UL>'''.format (path1=path1.replace ("\\", "/"), 
        path2=path2.replace ("\\", "/"),
        path3=path3.replace ("\\", "/"))


        result = self.parser.toHtml (text)

        self.assertEqual (result_right, result, result)

        full_path1 = os.path.join (self.parser.page.path, path1)
        full_path2 = os.path.join (self.parser.page.path, path2)
        full_path3 = os.path.join (self.parser.page.path, path3)

        self.assertTrue (os.path.exists (full_path1), full_path1)
        self.assertTrue (os.path.exists (full_path2), full_path2)
        self.assertTrue (os.path.exists (full_path3), full_path3)
Пример #8
0
    def testTex1 (self):
        thumb = Thumbnails (self.parser.page)
        texrender = getTexRender(thumb.getThumbPath (True))

        eqn = u"y = f(x)"
        text = u"{$ %s $}" % (eqn)

        fname = texrender.getImageName (eqn)
        path = os.path.join (Thumbnails.getRelativeThumbDir(), fname)

        result_right = u'<IMG SRC="{0}"/>'.format (path.replace ("\\", "/"))

        result = self.parser.toHtml (text)

        self.assertEqual (result_right, result, result)

        full_path = os.path.join (self.parser.page.path, path)
        self.assertTrue (os.path.exists (full_path), full_path )
Пример #9
0
    def testTex4 (self):
        thumb = Thumbnails (self.parser.page)
        texrender = getTexRender(thumb.getThumbPath (True))

        eqn = u"y = f(x)"
        text = u"[[http://jenyay.net | {$ %s $}]]" % (eqn)

        fname = texrender.getImageName (eqn)
        path = os.path.join (Thumbnails.getRelativeThumbDir(), fname)

        result_right = u'<a href="http://jenyay.net"><img src="{0}"/></a>'.format (path.replace ("\\", "/"))

        result = self.parser.toHtml (text)

        self.assertEqual (result_right, result, result)

        full_path = os.path.join (self.parser.page.path, path)
        self.assertTrue (os.path.exists (full_path), full_path )
Пример #10
0
    def testTex1(self):
        from texequation.texrender import getTexRender
        thumb = Thumbnails(self.parser.page)
        texrender = getTexRender(thumb.getThumbPath(True))

        eqn = u"y = f(x)"
        text = u"{$ %s $}" % (eqn)

        fname = texrender.getImageName(eqn)
        path = os.path.join(Thumbnails.getRelativeThumbDir(), fname)

        result_right = u'<img src="{0}"/>'.format(path.replace("\\", "/"))

        result = self.parser.toHtml(text)

        self.assertEqual(result_right, result, result)

        full_path = os.path.join(self.parser.page.path, path)
        self.assertTrue(os.path.exists(full_path), full_path)
Пример #11
0
    def execute (self, params, content):
        """
        Запустить команду на выполнение.
        Метод возвращает текст, который будет вставлен на место команды в вики-нотации
        """
        from blockdiag.parser import ParseException
        thumb = Thumbnails(self.parser.page)
        thumbPath = thumb.getThumbPath(True)

        md5 = hashlib.md5 (content.encode ("utf-8")).hexdigest()
        fname = self._fileNameFormat.format (md5)
        imagePath = os.path.join (thumbPath, fname)

        render = DiagramRender()

        if not os.path.exists (imagePath):
            try:
                render.renderToFile (content, imagePath)
            except (ParseException, AttributeError, TypeError):
                return u"<b>{}</b>".format(_(u"Diagram parsing error"))

        return u'<img src="{}/{}"/>'.format (thumb.getRelativeThumbDir(), fname)
Пример #12
0
    def execute(self, params, content):
        """
        Запустить команду на выполнение.
        Метод возвращает текст, который будет вставлен на место команды в вики-нотации
        """
        from blockdiag.parser import ParseException
        thumb = Thumbnails(self.parser.page)
        thumbPath = thumb.getThumbPath(True)

        md5 = hashlib.md5(content.encode("utf-8")).hexdigest()
        fname = self._fileNameFormat.format(md5)
        imagePath = os.path.join(thumbPath, fname)

        render = DiagramRender()

        if not os.path.exists(imagePath):
            try:
                render.renderToFile(content, imagePath)
            except (ParseException, AttributeError, TypeError):
                return u"<b>{}</b>".format(_(u"Diagram parsing error"))

        return u'<img src="{}/{}"/>'.format(thumb.getRelativeThumbDir(), fname)
Пример #13
0
    def makeTexEquation (self, s, l, t):
        eqn = t[0].strip()
        if len (eqn) == 0:
            return u""

        thumb = Thumbnails(self.parser.page)

        try:
            path = thumb.getThumbPath (True)
        except IOError:
            return u"<b>{}</b>".format(_(u"Can't create thumbnails directory"))

        tex = getTexRender (path)

        try:
            image_fname = tex.makeImage (eqn)
        except IOError:
            return u"<b>{}</b>".format(_(u"Can't create image file"))

        image_path = os.path.join (Thumbnails.getRelativeThumbDir(), image_fname)
        result = u'<img src="{image}"/>'.format (image=image_path.replace ("\\", "/"))

        return result