Skip to content

Commit

Permalink
[enh] implement _repr_html_ of ipython notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
Psycojoker committed Jan 13, 2015
1 parent 79a4e24 commit 619c8db
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion redbaron.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pygments.token import Comment, Text, String, Keyword, Name, Operator
from pygments.lexer import RegexLexer, bygroups
from pygments.lexers import PythonLexer
from pygments.formatters import Terminal256Formatter
from pygments.formatters import Terminal256Formatter, HtmlFormatter

import baron
import baron.path
Expand Down Expand Up @@ -335,6 +335,24 @@ def __str__(self):
return to_return
return "%s" % [x.__repr__() for x in self.data]

def _repr_html_(self):
def __repr_html(self):
# string addition is slow (and makes copies)
yield u"<table>"
yield u"<tr><th>Index</th><th>node</th></tr>"
for num, item in enumerate(self):
yield u"<tr>"
yield u"<td>"
yield str(num)
yield u"</td>"
yield u"<td>"
yield item._repr_html_()
yield u"</td>"
yield u"</tr>"
yield "</table>"
return u''.join(__repr_html(self))


def help(self, deep=2, with_formatting=False):
for num, i in enumerate(self.data):
sys.stdout.write(str(num) + " -----------------------------------------------------\n")
Expand Down Expand Up @@ -883,6 +901,10 @@ def __str__(self):
else:
return self.dumps()

def _repr_html_(self):
return highlight(self.dumps(), PythonLexer(encode="Utf-8"),
HtmlFormatter(noclasses=True, encoding="UTf-8"))

def copy(self):
# XXX not very optimised but at least very simple
return Node.from_fst(self.fst())
Expand Down Expand Up @@ -1277,6 +1299,23 @@ def __repr__(self):
id(self.parent)
)

def _repr_html_(self):
def __repr_html(self):
# string addition is slow (and makes copies)
yield u"<table>"
yield u"<tr><th>Index</th><th>node</th></tr>"
for num, item in enumerate(self):
yield u"<tr>"
yield u"<td>"
yield str(num)
yield u"</td>"
yield u"<td>"
yield item._repr_html_()
yield u"</td>"
yield u"</tr>"
yield "</table>"
return u''.join(__repr_html(self))

def __str__(self):
to_return = ""
for number, value in enumerate(self.data):
Expand Down Expand Up @@ -1941,6 +1980,10 @@ class EndlNode(Node):
def __repr__(self):
return repr(baron.dumps([self.fst()]))

def _repr_html_(self):
return highlight(self.__repr__(), PythonLexer(encode="Utf-8"),
HtmlFormatter(noclasses=True, encoding="UTf-8"))


class ExceptNode(CodeBlockNode):
def __setattr__(self, key, value):
Expand Down

0 comments on commit 619c8db

Please sign in to comment.