class OutputHtml(OutputBase): def __init__(self, html): """ HTML Output INPUT: - ``html`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. String containing the html fragment code. Excludes the surrounding ``<body>`` and ``<html>`` tag. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: OutputHtml('<div>Foo<b>B</b>ar</div>') OutputHtml container """ self.html = OutputBuffer(html) @classmethod def example(cls): r""" Construct a sample Html output container This static method is meant for doctests, so they can easily construct an example. OUTPUT: An instance of :class:`OutputHtml`. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: OutputHtml.example() OutputHtml container sage: OutputHtml.example().html.get_str() '<div>Hello World!</div>' """ return cls('<div>Hello World!</div>') def print_to_stdout(self): r""" Write the data to stdout. This is just a convenience method to help with debugging. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: rich_output = OutputHtml.example() sage: rich_output.print_to_stdout() <div>Hello World!</div> """ print(self.html.get_unicode()) def with_html_tag(self): r""" Return the HTML code surrounded by ``<html>`` tag This is just a convenience method. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: rich_output = OutputHtml.example() sage: rich_output.print_to_stdout() <div>Hello World!</div> sage: rich_output.with_html_tag() '<html><div>Hello World!</div></html>' """ return '<html>{0}</html>'.format(self.html.get_unicode())
class OutputHtml(OutputBase): def __init__(self, html): """ HTML Output INPUT: - ``html`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (bytes) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. String containing the html fragment code. Excludes the surrounding ``<body>`` and ``<html>`` tag. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: OutputHtml('<div>Foo<b>B</b>ar</div>') OutputHtml container """ self.html = OutputBuffer(html) # if the html is a simple wrapper of latex for mathjax rendering, then # the latex string is saved for possible latex output such as Jupyter's # pdf export of a notebook m = latex_re.match(html) if m: mathjax_string = m.group('latex') latex_string = mathjax_string.replace('<', '<') if m.group('mathstart') == r'\[' and m.group('mathend') == r'\]': self.latex = OutputBuffer('$$' + latex_string + '$$') else: self.latex = OutputBuffer('$' + latex_string + '$') else: self.latex = None @classmethod def example(cls): r""" Construct a sample Html output container This static method is meant for doctests, so they can easily construct an example. OUTPUT: An instance of :class:`OutputHtml`. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: OutputHtml.example() OutputHtml container sage: OutputHtml.example().html.get_str() '<div>Hello World!</div>' """ return cls('<div>Hello World!</div>') def print_to_stdout(self): r""" Write the data to stdout. This is just a convenience method to help with debugging. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: rich_output = OutputHtml.example() sage: rich_output.print_to_stdout() <div>Hello World!</div> """ print(self.html.get_unicode()) def with_html_tag(self): r""" Return the HTML code surrounded by ``<html>`` tag This is just a convenience method. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputHtml sage: rich_output = OutputHtml.example() sage: rich_output.print_to_stdout() <div>Hello World!</div> sage: rich_output.with_html_tag() '<html><div>Hello World!</div></html>' """ return '<html>{0}</html>'.format(self.html.get_unicode())
class OutputUnicodeArt(OutputBase): def __init__(self, unicode_art): """ Unicode Art Output Similar to :class:`OutputAsciiArt` but using the entire unicode range. INPUT: - ``unicode_art`` -- :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, a string (unicode in Python 2.x) can be passed directly which will then be converted into an :class:`~sage.repl.rich_output.buffer.OutputBuffer`. Unicode art rendered into a string. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputUnicodeArt sage: OutputUnicodeArt(u':-}') OutputUnicodeArt container """ # Internally, all buffers store bytes. Unicode is always utf-8 # encoded. if not isinstance(unicode_art, bytes): unicode_art = unicode_art.encode('utf-8') self.unicode_art = OutputBuffer(unicode_art) @classmethod def example(cls): r""" Construct a sample unicode art output container This static method is meant for doctests, so they can easily construct an example. OUTPUT: An instance of :class:`OutputUnicodeArt`. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputUnicodeArt sage: OutputUnicodeArt.example() OutputUnicodeArt container sage: print(OutputUnicodeArt.example().unicode_art.get_unicode()) ⎛-11 0 1⎞ ⎜ 3 -1 0⎟ ⎝ -1 -1 0⎠ """ return cls(u'⎛-11 0 1⎞\n' u'⎜ 3 -1 0⎟\n' u'⎝ -1 -1 0⎠') def print_to_stdout(self): """ Write the data to stdout. This is just a convenience method to help with debugging. EXAMPLES:: sage: from sage.repl.rich_output.output_catalog import OutputUnicodeArt sage: unicode_art = OutputUnicodeArt.example() sage: unicode_art.print_to_stdout() ⎛-11 0 1⎞ ⎜ 3 -1 0⎟ ⎝ -1 -1 0⎠ """ print(self.unicode_art.get_unicode())