Пример #1
0
    def test_mkd_with_html5_tags(self):
        text = '<section>foo</section>'
        cp = ctypes.c_char_p(text)
        out = tempfile.TemporaryFile('r+w')
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)
        
        ret = libmarkdown.mkd_generatehtml(
            doc, ctypes.pythonapi.PyFile_AsFile(out)
        )

        self.assertNotEqual(ret, -1)

        out.seek(0)
        html = out.read()

        self.assertEqual(
            html, (
                '<p><section>foo</section></p>\n'
            )
        )

        out.close()

        libmarkdown.mkd_with_html5_tags()

        text = '<section>foo</section>'
        cp = ctypes.c_char_p(text)
        out = tempfile.TemporaryFile('r+w')
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)
        
        ret = libmarkdown.mkd_generatehtml(
            doc, ctypes.pythonapi.PyFile_AsFile(out)
        )

        self.assertNotEqual(ret, -1)

        out.seek(0)
        html = out.read()

        self.assertEqual(
            html, (
                '<section>foo</section>\n\n'
            )
        )

        out.close()
Пример #2
0
    def _test_flag(self, i, o, f):
        text = i
        cp = ctypes.c_char_p(text)

        doc = libmarkdown.mkd_string(
            cp, len(text), f
        )
        libmarkdown.mkd_compile(doc, f)

        sb = ctypes.c_char_p('')
        ret = libmarkdown.mkd_document(doc, ctypes.byref(sb))

        self.assertEqual(
            sb.value[:ret],
            o,
        )
Пример #3
0
    def test_mkd_toc(self):
        self.assertEqual(
            libmarkdown.mkd_toc.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             ctypes.POINTER(ctypes.c_char_p)),
        )

        self.assertEqual(
            libmarkdown.mkd_toc.restype,
            ctypes.c_int,
        )

        flags = libmarkdown.MKD_TOC
        text = '# header-1\n## header-2\n### header-3'
        cp = ctypes.c_char_p(text)
        sb = ctypes.c_char_p('')
        doc = libmarkdown.mkd_string(cp, len(text), flags)
        ret = libmarkdown.mkd_compile(doc, flags)

        self.assertNotEqual(ret, -1)

        ret = libmarkdown.mkd_toc(doc, ctypes.byref(sb))

        self.assertNotEqual(ret, -1)
        self.assertEqual(
            sb.value[:ret], (
                '\n '
                '<ul>\n <li><a href="#header-1">header-1</a>\n  '
                '<ul>\n  <li><a href="#header-2">header-2</a>\n   '
                '<ul>\n   <li><a href="#header-3">header-3</a>   </li>\n   '
                '</ul>\n  </li>\n  </ul>\n </li>\n </ul>\n'
            )
        )
Пример #4
0
    def test_mkd_generatecss(self):
        self.assertEqual(
            libmarkdown.mkd_generatecss.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             ctypes.POINTER(libmarkdown.FILE)),
        )

        self.assertEqual(
            libmarkdown.mkd_generatecss.restype,
            ctypes.c_int,
        )

        text = '<style>\n  *{color:red}\n</style>\n\n# header-1\n\n`test`'
        cp = ctypes.c_char_p(text)
        out = tempfile.TemporaryFile('r+w')
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        ret = libmarkdown.mkd_generatecss(
            doc, ctypes.pythonapi.PyFile_AsFile(out)
        )

        # Returns -1 event on success
        # self.assertNotEqual(ret, -1)

        out.seek(0)
        html = out.read()

        self.assertEqual(
            html, '<style>  *{color:red}</style>\n'
        )

        out.close()
Пример #5
0
    def test_mkd_css(self):
        self.assertEqual(
            libmarkdown.mkd_css.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             ctypes.POINTER(ctypes.c_char_p)),
        )

        self.assertEqual(
            libmarkdown.mkd_css.restype,
            ctypes.c_int,
        )

        text = '<style>\n  *{color:red}\n</style>\n\n# header-1\n\n`test`'
        cp = ctypes.c_char_p(text)
        sb = ctypes.c_char_p('')
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        ret = libmarkdown.mkd_css(doc, ctypes.byref(sb))

        self.assertNotEqual(ret, -1)
        self.assertEqual(
            sb.value[:ret],
            '<style>  *{color:red}</style>\n'
        )
Пример #6
0
    def test_mkd_document(self):
        self.assertEqual(
            libmarkdown.mkd_document.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             ctypes.POINTER(ctypes.c_char_p)),
        )

        self.assertEqual(
            libmarkdown.mkd_document.restype,
            ctypes.c_int,
        )

        text = '`test`'
        cp = ctypes.c_char_p(text)
        sb = ctypes.c_char_p('')
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        ret = libmarkdown.mkd_document(doc, ctypes.byref(sb), 0)

        self.assertNotEqual(ret, -1)
        self.assertEqual(
            sb.value[:ret],
            '<p><code>test</code></p>'
        )
Пример #7
0
    def test_mkd_cleanup(self):
        self.assertEqual(
            libmarkdown.mkd_cleanup.argtypes,
            (ctypes.POINTER(libmarkdown.Document),)
        )

        self.assertEqual(
            libmarkdown.mkd_cleanup.restype,
            ctypes.c_void_p,
        )

        text = '`test`'
        cp = ctypes.c_char_p(text)
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        libmarkdown.mkd_compile(doc, 0)

        self.assertEqual(doc.contents.compiled, 1)

        libmarkdown.mkd_cleanup(doc)

        self.assertNotEqual(doc.contents.compiled, 1)
Пример #8
0
    def test_mkd_dump(self):
        self.assertEqual(
            libmarkdown.mkd_dump.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             ctypes.POINTER(libmarkdown.FILE),
             ctypes.c_int,
             ctypes.c_char_p),
        )

        self.assertEqual(
            libmarkdown.mkd_dump.restype,
            ctypes.c_int,
        )

        text = (
            '# header-1\n'
            '`test`\n'
        )

        cp = ctypes.c_char_p(text)

        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        out = tempfile.TemporaryFile('r+w')

        libmarkdown.mkd_dump(
            doc, ctypes.pythonapi.PyFile_AsFile(out),
            0, ctypes.c_char_p('title')
        )

        out.seek(0)
        dump = out.read()

        self.assertEqual(
            dump, (
                'title-----[source]--+--[header, <P>, 1 line]\n'
                '                    `--[markup, <P>, 1 line]\n'
            )
        )

        out.close()
Пример #9
0
    def test_mkd_e_flags(self):
        self.assertEqual(
            libmarkdown.mkd_e_flags.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             libmarkdown.e_flags_callback)
        )

        self.assertEqual(
            libmarkdown.mkd_e_flags.restype,
            ctypes.c_void_p,
        )

        text = (
            '[a](/a.html)\n'
            '[b](/b.html)\n'
        )
        cp = ctypes.c_char_p(text)
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        @libmarkdown.e_flags_callback
        def e_target_blank(string, size, context):
            link = string[:size]
            if link == '/a.html':
                attr = ctypes.create_string_buffer('target="_blank"')
                return ctypes.addressof(attr)

        libmarkdown.mkd_e_flags(doc, e_target_blank)

        sb = ctypes.c_char_p('')
        ret = libmarkdown.mkd_document(doc, ctypes.byref(sb), 0)

        self.assertNotEqual(ret, -1)
        self.assertEqual(
            sb.value[:ret],
            '<p>'
            '<a href="/a.html" target="_blank">a</a>\n'
            '<a href="/b.html">b</a>'
            '</p>'
        )
Пример #10
0
    def test_mkd_generatetoc(self):
        self.assertEqual(
            libmarkdown.mkd_generatetoc.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             ctypes.POINTER(libmarkdown.FILE)),
        )

        self.assertEqual(
            libmarkdown.mkd_generatetoc.restype,
            ctypes.c_int,
        )

        flags = libmarkdown.MKD_TOC
        text = '# header-1\n## header-2\n### header-3'
        cp = ctypes.c_char_p(text)
        out = tempfile.TemporaryFile('r+w')
        doc = libmarkdown.mkd_string(cp, len(text), flags)
        ret = libmarkdown.mkd_compile(doc, flags)

        self.assertNotEqual(ret, -1)

        ret = libmarkdown.mkd_generatetoc(
            doc, ctypes.pythonapi.PyFile_AsFile(out)
        )

        self.assertNotEqual(ret, -1)

        out.seek(0)
        html = out.read()

        self.assertEqual(
            html, (
                '\n '
                '<ul>\n <li><a href="#header-1">header-1</a>\n  '
                '<ul>\n  <li><a href="#header-2">header-2</a>\n   '
                '<ul>\n   <li><a href="#header-3">header-3</a>   </li>\n   '
                '</ul>\n  </li>\n  </ul>\n </li>\n </ul>\n'
            )
        )

        out.close()
Пример #11
0
    def test_mkd_doc_date(self):
        self.assertEqual(
            libmarkdown.mkd_doc_date.argtypes,
            (ctypes.POINTER(libmarkdown.Document),)
        )

        self.assertEqual(
            libmarkdown.mkd_doc_date.restype,
            ctypes.c_char_p,
        )

        text = '% title\n% author\n% date\n'
        cp = ctypes.c_char_p(text)
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        v = libmarkdown.mkd_doc_date(doc)

        self.assertEqual(v, 'date')
Пример #12
0
    def test_mkd_generatehtml(self):
        self.assertEqual(
            libmarkdown.mkd_generatehtml.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             ctypes.POINTER(libmarkdown.FILE)),
        )

        self.assertEqual(
            libmarkdown.mkd_generatehtml.restype,
            ctypes.c_int,
        )

        text = '`test`'
        cp = ctypes.c_char_p(text)
        out = tempfile.TemporaryFile('r+w')
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        ret = libmarkdown.mkd_generatehtml(
            doc, ctypes.pythonapi.PyFile_AsFile(out), 0
        )

        self.assertNotEqual(ret, -1)

        out.seek(0)
        html = out.read()

        self.assertEqual(
            html, '<p><code>test</code></p>\n',
        )

        libmarkdown.mkd_cleanup(doc)

        out.close()
Пример #13
0
    def test_mkd_e_url(self):
        self.assertEqual(
            libmarkdown.mkd_e_url.argtypes,
            (ctypes.POINTER(libmarkdown.Document),
             libmarkdown.e_url_callback)
        )

        self.assertEqual(
            libmarkdown.mkd_e_url.restype,
            ctypes.c_void_p,
        )

        text = (
            '[a](/a.html)\n'
            '[b](/b.html)\n'
        )
        cp = ctypes.c_char_p(text)
        doc = libmarkdown.mkd_string(cp, len(text), 0)
        ret = libmarkdown.mkd_compile(doc, 0)

        self.assertNotEqual(ret, -1)

        # TODO get test with libc malloc/strcpy/free working.

        # base = ctypes.c_char_p('http://example.com')

        # @libmarkdown.e_url_callback
        # def e_basename(string, size, context):
        #     base = ctypes.string_at(context)
        #     link = string[:size]
        #     if base and link and link.startswith('/'):
        #         ret = libc.malloc(len(base) + size + 2)
        #         if ret > 0:
        #             libc.strcpy(ret, context)
        #             return ret
        #     return 0

        # @libmarkdown.e_free_callback
        # def e_free(string, context):
        #     if string:
        #         libc.free(string)

        # libmarkdown.mkd_e_url(doc, e_basename)
        # libmarkdown.mkd_e_data(doc, base)
        # libmarkdown.mkd_e_free(doc, e_free)

        alloc = []

        @libmarkdown.e_url_callback
        def e_basename(string, size, context):
            link = string[:size]

            if link.startswith('/'):
                abs_link = ctypes.create_string_buffer(
                    'http://example.com%s' % link
                )
                alloc.append(abs_link)
                return ctypes.addressof(abs_link)

        libmarkdown.mkd_e_url(doc, e_basename)

        sb = ctypes.c_char_p('')
        ret = libmarkdown.mkd_document(doc, ctypes.byref(sb), 0)

        self.assertNotEqual(ret, -1)
        self.assertEqual(
            sb.value[:ret],
            '<p>'
            '<a href="http://example.com/a.html">a</a>\n'
            '<a href="http://example.com/b.html">b</a>'
            '</p>'
        )