示例#1
0
def text_array_to_html(text_arr):
    """Take a numpy.ndarray containing strings, and convert it into html.

    If the ndarray contains a single scalar string, that string is converted to
    html via our sanitized markdown parser. If it contains an array of strings,
    the strings are individually converted to html and then composed into a table
    using make_table. If the array contains dimensionality greater than 2,
    all but two of the dimensions are removed, and a warning message is prefixed
    to the table.

    Args:
      text_arr: A numpy.ndarray containing strings.

    Returns:
      The array converted to html.
    """
    if not text_arr.shape:
        # It is a scalar. No need to put it in a table, just apply markdown
        return plugin_util.markdown_to_safe_html(text_arr.item())
    warning = ""
    if len(text_arr.shape) > 2:
        warning = plugin_util.markdown_to_safe_html(WARNING_TEMPLATE %
                                                    len(text_arr.shape))
        text_arr = reduce_to_2d(text_arr)
    table = plugin_util.markdowns_to_safe_html(
        text_arr.reshape(-1),
        lambda xs: make_table(np.array(xs).reshape(text_arr.shape)),
    )
    return warning + table
示例#2
0
def text_array_to_html(text_arr, enable_markdown):
    """Take a numpy.ndarray containing strings, and convert it into html.

    If the ndarray contains a single scalar string, that string is converted to
    html via our sanitized markdown parser. If it contains an array of strings,
    the strings are individually converted to html and then composed into a table
    using make_table. If the array contains dimensionality greater than 2,
    all but two of the dimensions are removed, and a warning message is prefixed
    to the table.

    Args:
      text_arr: A numpy.ndarray containing strings.
      enable_markdown: boolean, whether to enable Markdown

    Returns:
      The array converted to html.
    """
    if not text_arr.shape:
        # It is a scalar. No need to put it in a table.
        if enable_markdown:
            return plugin_util.markdown_to_safe_html(text_arr.item())
        else:
            return plugin_util.safe_html(text_arr.item())
    warning = ""
    if len(text_arr.shape) > 2:
        warning = plugin_util.markdown_to_safe_html(WARNING_TEMPLATE %
                                                    len(text_arr.shape))
        text_arr = reduce_to_2d(text_arr)
    if enable_markdown:
        table = plugin_util.markdowns_to_safe_html(
            text_arr.reshape(-1),
            lambda xs: make_table(np.array(xs).reshape(text_arr.shape)),
        )
    else:
        # Convert utf-8 bytes to str. The built-in np.char.decode doesn't work on
        # object arrays, and converting to an numpy chararray is lossy.
        decode = lambda bs: bs.decode("utf-8") if isinstance(bs, bytes) else bs
        text_arr_str = np.array([decode(bs) for bs in text_arr.reshape(-1)
                                 ]).reshape(text_arr.shape)
        table = plugin_util.safe_html(make_table(text_arr_str))
    return warning + table
示例#3
0
 def test_sanitization_can_have_collateral_damage(self):
     inputs = ['<table title="*chuckles* ', "I'm in danger", '<table>">']
     combine = lambda xs: "".join(xs)
     actual = plugin_util.markdowns_to_safe_html(inputs, combine)
     expected = "<table></table>"
     self.assertEqual(actual, expected)
示例#4
0
 def test_sanitizes_combination_result(self):
     inputs = ["safe"]
     combine = lambda xs: "<script>alert('unsafe!')</script>%s" % xs[0]
     actual = plugin_util.markdowns_to_safe_html(inputs, combine)
     expected = "&lt;script&gt;alert('unsafe!')&lt;/script&gt;<p>safe</p>"
     self.assertEqual(actual, expected)
示例#5
0
 def test_simple(self):
     inputs = ["0", "*1*", "**2**"]
     combine = lambda xs: "<br>".join(xs)
     actual = plugin_util.markdowns_to_safe_html(inputs, combine)
     expected = "<p>0</p><br><p><em>1</em></p><br><p><strong>2</strong></p>"
     self.assertEqual(actual, expected)