Ejemplo n.º 1
0
    def run(self, parent, blocks):
        diag_blocks = []

        for block in blocks:
            block = block.strip()
            diag_blocks.append(block)
            if block.endswith("}"):
                break

        raw_block = "\n".join(diag_blocks)
        del blocks[:len(diag_blocks)]

        font_path = self.extension.getConfig('fontpath')
        output_fmt = self.extension.getConfig('format')
        diagram = draw_blockdiag(raw_block, output_fmt=output_fmt, font_path=font_path)
        if output_fmt == 'png':
            src_data = 'data:image/png;base64,{0}'.format(
                base64.b64encode(diagram)
            )
        else:
            src_data = 'data:image/svg+xml;utf8,{0}'.format(diagram)

        p = etree.SubElement(parent, 'p')
        img = etree.SubElement(p, 'img')
        img.attrib['src'] = src_data
Ejemplo n.º 2
0
 def post(self, request: Request) -> Response:
     try:
         blockdiag = request.data["markdown"]
         svg = draw_blockdiag(blockdiag, output_fmt="svg", font_antialias=True)
     except (ParseException, KeyError):
         return Response(status=status.HTTP_400_BAD_REQUEST)
     else:
         return Response(svg)
Ejemplo n.º 3
0
    def test_seqdiag(self):
        draw = draw_blockdiag(SEQ_DIAG_TXT)

        expected = '<p><img src="data:image/png;base64,{0}" /></p>'.format(
            base64.b64encode(draw))
        result = markdown(
            SEQ_DIAG_TXT,
            extensions=['markdown_blockdiag'],
        )
        self.assertEqual(expected, result)
Ejemplo n.º 4
0
    def test_svg_blockdiag(self):
        self.maxDiff = None
        draw = draw_blockdiag(BASIC_DIAG_TXT, output_fmt='svg')

        expected = '<p><img src="data:image/svg+xml;charset=utf-8,{0}" /></p>'.format(
            url_quote(draw))
        result = markdown(
            BASIC_DIAG_TXT,
            extensions=['markdown_blockdiag'],
            extension_configs={'markdown_blockdiag': {
                'format': 'svg'
            }})
        result = unescape(result).replace('&quot;', '\"')

        self.assertEqual(expected, result)
Ejemplo n.º 5
0
    def test_markdown(self):
        draw = draw_blockdiag(EXTENDED_DIAG_TXT)

        expected = '<p><img src="data:image/png;base64,{0}" /></p>'.format(
            base64.b64encode(draw))

        marcdown_doc = MARKDOWN_DOC.format(diagram=EXTENDED_DIAG_TXT)
        result = markdown(
            marcdown_doc,
            extensions=['markdown_blockdiag'],
        )

        self.assertTrue("Title" in result)
        self.assertEqual(2, result.count(expected))
        self.assertEqual(3, result.count("paragraph"))
Ejemplo n.º 6
0
    def test_font_antialias_blockdiag(self):
        self.maxDiff = None
        draw = draw_blockdiag(BASIC_DIAG_TXT, font_antialias=False)

        expected = '<p><img src="data:image/png;base64,{0}" /></p>'.format(
            base64.b64encode(draw))
        result = markdown(
            BASIC_DIAG_TXT,
            extensions=['markdown_blockdiag'],
            extension_configs={'markdown_blockdiag': {
                'fontantialias': False
            }})
        result = unescape(result).replace('&quot;', '\"')

        self.assertEqual(expected, result)