Ejemplo n.º 1
0
    def render_table(self, props):
        num_cols = props["data"]["numCols"]
        num_rows = props["data"]["numRows"]
        with_header = props["data"].get("withHeader", False)
        cells = props["data"]["cells"]
        table = DOM.create_element("table")
        if with_header:
            start_row = 1
            thead = DOM.create_element("thead")
            DOM.append_child(table, thead)
            tr = DOM.create_element("tr")
            DOM.append_child(thead, tr)
            for col_idx in range(num_cols):
                th = DOM.create_element("th")
                DOM.append_child(tr, th)
                try:
                    content_state = cells[0][col_idx]
                except IndexError:
                    continue
                try:
                    content = DOM.parse_html(
                        self.exporter.render(content_state))
                except etree.ParserError:
                    continue
                if content.text or len(content):
                    DOM.append_child(th, content)
        else:
            start_row = 0

        if not with_header or num_rows > 1:
            tbody = DOM.create_element("tbody")
            DOM.append_child(table, tbody)

        for row_idx in range(start_row, num_rows):
            tr = DOM.create_element("tr")
            DOM.append_child(tbody, tr)
            for col_idx in range(num_cols):
                td = DOM.create_element("td")
                DOM.append_child(tr, td)
                try:
                    content_state = cells[row_idx][col_idx]
                except IndexError:
                    continue
                try:
                    content = DOM.parse_html(
                        self.exporter.render(content_state))
                except etree.ParserError:
                    continue
                if content.text or len(content):
                    DOM.append_child(td, content)

        return table
Ejemplo n.º 2
0
    def render_table(self, props):
        num_cols = props['data']['numCols']
        num_rows = props['data']['numRows']
        with_header = props['data'].get('withHeader', False)
        cells = props['data']['cells']
        table = DOM.create_element('table')
        if with_header:
            start_row = 1
            thead = DOM.create_element('thead')
            DOM.append_child(table, thead)
            tr = DOM.create_element('tr')
            DOM.append_child(thead, tr)
            for col_idx in range(num_cols):
                th = DOM.create_element('th')
                DOM.append_child(tr, th)
                try:
                    content_state = cells[0][col_idx]
                except IndexError:
                    continue
                content = DOM.parse_html(self.exporter.render(content_state))
                if content.text or len(content):
                    DOM.append_child(th, content)
        else:
            start_row = 0

        if not with_header or num_rows > 1:
            tbody = DOM.create_element('tbody')
            DOM.append_child(table, tbody)

        for row_idx in range(start_row, num_rows):
            tr = DOM.create_element('tr')
            DOM.append_child(tbody, tr)
            for col_idx in range(num_cols):
                td = DOM.create_element('td')
                DOM.append_child(tr, td)
                try:
                    content_state = cells[row_idx][col_idx]
                except IndexError:
                    continue
                content = DOM.parse_html(self.exporter.render(content_state))
                if content.text or len(content):
                    DOM.append_child(td, content)

        return table
Ejemplo n.º 3
0
    def render_media(self, props):
        media_props = props["media"]
        media_type = media_props.get("type", "picture")
        rendition = media_props["renditions"].get("original") or media_props["renditions"]["viewImage"]
        alt_text = media_props.get("alt_text") or ""
        desc = media_props.get("description_text")
        if media_type == "picture":
            embed_type = "Image"
            elt = DOM.create_element("img", {"src": rendition["href"], "alt": alt_text}, props["children"])
        elif media_type == "video":
            embed_type = "Video"
            elt = DOM.create_element(
                "video",
                {"control": "control", "src": rendition["href"], "alt": alt_text, "width": "100%", "height": "100%"},
                props["children"],
            )
        elif media_type == "audio":
            embed_type = "Audio"
            elt = DOM.create_element(
                "audio",
                {"control": "control", "src": rendition["href"], "alt": alt_text, "width": "100%", "height": "100%"},
                props["children"],
            )
        else:
            logger.error("Invalid or not implemented media type: {media_type}".format(media_type=media_type))
            return None

        content = DOM.render(elt)

        if desc:
            content += "<figcaption>{}</figcaption>".format(desc)

        # we need to retrieve the key, there is not straightforward way to do it
        # so we find the key in entityMap with a corresponding value
        embed_key = next(
            k for k, v in self.content_state["entityMap"].items() if v["data"].get("media") == props["media"]
        )

        # <dummy_tag> is needed for the comments, because a root node is necessary
        # it will be removed during rendering.
        embed = DOM.parse_html(
            dedent(
                """\
            <dummy_tag><!-- EMBED START {embed_type} {{id: "editor_{key}"}} -->
            <figure>{content}</figure>
            <!-- EMBED END {embed_type} {{id: "editor_{key}"}} --></dummy_tag>"""
            ).format(embed_type=embed_type, key=embed_key, content=content)
        )

        return embed
Ejemplo n.º 4
0
 def test_parse_html(self):
     self.assertEqual(
         DOM.render_debug(DOM.parse_html("<p><span>Test text</span></p>")),
         "<p><span>Test text</span></p>",
     )
Ejemplo n.º 5
0
def Embed(props):
    """
    Inspired by: https://github.com/wagtail/wagtail/blob/master/wagtail/wagtailembeds/rich_text.py
    """
    return DOM.parse_html(embed_to_frontend_html(props['url']))
Ejemplo n.º 6
0
 def _render_embed(self, props):
     elt = DOM.parse_html(props['data']['html'])
     return DOM.create_element('div', {'class': 'embed-block'}, elt)
Ejemplo n.º 7
0
 def test_parse_html(self):
     self.assertEqual(DOM.render(DOM.parse_html('<p><span>Test text</span></p>')), '<p><span>Test text</span></p>')
Ejemplo n.º 8
0
 def test_parse_html(self):
     self.assertEqual(DOM.render_debug(DOM.parse_html('<p><span>Test text</span></p>')), '<p><span>Test text</span></p>')
Ejemplo n.º 9
0
 def render(self, props):
     return DOM.parse_html(embed_to_frontend_html(props['url']))
Ejemplo n.º 10
0
    def render_media(self, props):
        media_props = props['media']
        media_type = media_props.get('type', 'picture')
        rendition = media_props['renditions'].get(
            'original') or media_props['renditions']['viewImage']
        alt_text = media_props.get('alt_text') or ''
        desc = media_props.get('description_text')
        if media_type == 'picture':
            embed_type = "Image"
            elt = DOM.create_element('img', {
                'src': rendition['href'],
                'alt': alt_text
            }, props['children'])
        elif media_type == 'video':
            embed_type = "Video"
            elt = DOM.create_element(
                'video', {
                    'control': 'control',
                    'src': rendition['href'],
                    'alt': alt_text,
                    'width': '100%',
                    'height': '100%'
                }, props['children'])
        elif media_type == 'audio':
            embed_type = "Audio"
            elt = DOM.create_element(
                'audio', {
                    'control': 'control',
                    'src': rendition['href'],
                    'alt': alt_text,
                    'width': '100%',
                    'height': '100%'
                }, props['children'])
        else:
            logger.error(
                "Invalid or not implemented media type: {media_type}".format(
                    media_type=media_type))
            return None

        content = DOM.render(elt)

        if desc:
            content += "<figcaption>{}</figcaption>".format(desc)

        # we need to retrieve the key, there is not straightforward way to do it
        # so we find the key in entityMap with a corresponding value
        embed_key = next(k for k, v in self.content_state['entityMap'].items()
                         if v['data'].get('media') == props['media'])

        # <dummy_tag> is needed for the comments, because a root node is necessary
        # it will be removed during rendering.
        embed = DOM.parse_html(
            dedent("""\
            <dummy_tag><!-- EMBED START {embed_type} {{id: "editor_{key}"}} -->
            <figure>{content}</figure>
            <!-- EMBED END {embed_type} {{id: "editor_{key}"}} --></dummy_tag>"""
                   ).format(embed_type=embed_type,
                            key=embed_key,
                            content=content))

        return embed