How to use the taguette.convert function in taguette

To help you get started, we’ve selected a few taguette examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github remram44 / taguette / tests.py View on Github external
body = (
            b"\n"
            b"\n  \n  <title>Test</title>\n\n"
            b"<h1>Example</h1><p>This is an <a>example</a> text document.\n"
            b"It should be <blink>converted</blink>.</p>\n\n"
            b"<p>It has another paragraph <strong>here</strong>, "
            b"images: <img src="\&quot;here.png\&quot;" width="\&quot;50\&quot;"> "
            b"<img width="\&quot;30\&quot;" src="\&quot;/over/there.png\&quot;" title="\&quot;important\&quot;"> "
            b"<img class="\&quot;a\&quot;" src="\&quot;http://and/the/last/one.png\&quot;">, and "
            b"links: <a href="\&quot;here\&quot;">1</a> "
            b"<a href="\&quot;/over/there\&quot;" title="\&quot;important\&quot;">2</a> "
            b"<a class="\&quot;a\&quot;" href="\&quot;http://and/the/last/one\&quot;">3</a></p>\n"
            b"\n"
        )
        with mock.patch('tornado.process.Subprocess', object()):
            body = await convert.to_html(body, 'text/html', 'test.html',
                                         self.config)
        self.assertEqual(
            body,
            "<h1>Example</h1><p>This is an example text document.\n"
            "It should be converted.</p>\n\n"
github remram44 / taguette / taguette / web.py View on Github external
async def post(self, project_id):
        project = self.get_project(project_id)

        name = self.get_body_argument('name')
        description = self.get_body_argument('description')
        file = self.request.files['file'][0]
        content_type = file.content_type
        filename = secure_filename(file.filename)

        try:
            body = await convert.to_html_chunks(file.body, content_type,
                                                filename)
        except convert.ConversionError as err:
            self.set_status(400)
            self.send_json({
                'error': str(err),
            })
        else:
            doc = database.Document(
                name=name,
                description=description,
                filename=filename,
                project=project,
                contents=body,
            )
            self.db.add(doc)
            self.db.flush()  # Need to flush to get doc.id
github remram44 / taguette / taguette / web / api.py View on Github external
self.set_status(403)
            return self.send_json({'error': "Unauthorized"})
        try:
            name = self.get_body_argument('name')
            validate.document_name(name)
            description = self.get_body_argument('description')
            validate.document_description(description)
            try:
                file = self.request.files['file'][0]
            except (KeyError, IndexError):
                raise MissingArgumentError('file')
            content_type = file.content_type
            filename = validate.filename(file.filename)

            try:
                body = await convert.to_html_chunks(
                    file.body, content_type, filename,
                    self.application.config,
                )
            except convert.ConversionError as err:
                self.set_status(400)
                return self.send_json({
                    'error': str(err),
                })
            else:
                doc = database.Document(
                    name=name,
                    description=description,
                    filename=filename,
                    project=project,
                    contents=body,
                )
github remram44 / taguette / taguette / web / export.py View on Github external
sheet.set_column(1, 1, 15.0)
            sheet.set_column(2, 2, 15.0)
            sheet.set_column(3, 3, 80.0)
            row = 1
            for hl in highlights:
                tags = hl.tags
                assert all(isinstance(t, database.Tag) for t in tags)
                if tags:
                    tags = [t.path for t in tags]
                else:
                    tags = ['']
                for tag_path in tags:
                    sheet.write(row, 0, str(hl.id))
                    sheet.write(row, 1, hl.document.name)
                    sheet.write(row, 2, tag_path)
                    sheet.write(row, 3, convert.html_to_plaintext(hl.snippet))
                    row += 1
            workbook.close()
            with open(filename, 'rb') as fp:
                chunk = fp.read(4096)
                self.write(chunk)
                while len(chunk) == 4096:
                    chunk = fp.read(4096)
                    if chunk:
                        self.write(chunk)
            return self.finish()
        finally:
            shutil.rmtree(tmp)
github remram44 / taguette / taguette / web / export.py View on Github external
def init_PROM_EXPORT(w):
    for e in convert.html_to_extensions:
        PROM_EXPORT.labels(w, e).inc(0)
github remram44 / taguette / taguette / web / export.py View on Github external
async def wrapper(self, *args):
        ext = args[-1]
        ext = ext.lower()
        name, html = wrapped(self, *args)
        try:
            mimetype, contents = convert.html_to(
                html, ext,
                self.application.config,
            )
        except convert.UnsupportedFormat:
            self.set_status(404)
            self.set_header('Content-Type', 'text/plain')
            return self.finish("Unsupported format: %s" % ext)
        self.set_header('Content-Type', mimetype)
        if name:
            self.set_header('Content-Disposition',
                            'attachment; filename="%s.%s"' % (name, ext))
        else:
            self.set_header('Content-Disposition', 'attachment')
        for chunk in await contents:
            self.write(chunk)
        return self.finish()
github remram44 / taguette / taguette / web / export.py View on Github external
async def wrapper(self, *args):
        ext = args[-1]
        ext = ext.lower()
        name, html = wrapped(self, *args)
        try:
            mimetype, contents = convert.html_to(
                html, ext,
                self.application.config,
            )
        except convert.UnsupportedFormat:
            self.set_status(404)
            self.set_header('Content-Type', 'text/plain')
            return self.finish("Unsupported format: %s" % ext)
        self.set_header('Content-Type', mimetype)
        if name:
            self.set_header('Content-Disposition',
                            'attachment; filename="%s.%s"' % (name, ext))
        else:
            self.set_header('Content-Disposition', 'attachment')
        for chunk in await contents:
            self.write(chunk)
        return self.finish()
    return wrapper
github remram44 / taguette / taguette / web.py View on Github external
async def wrapper(self, *args):
        args, ext = args[:-1], args[-1]
        ext = ext.lower()
        name, html = wrapped(self, *args)
        mimetype, contents = convert.html_to(html, ext)
        self.set_header('Content-Type', mimetype)
        self.set_header('Content-Disposition',
                        'attachment; filename="%s.%s"' % (name, ext))
        for chunk in await contents:
            self.write(chunk)
        self.finish()
    return wrapper