Show More
@@ -5,10 +5,10 b' Authors : MinRK, gregcaporaso, dannystaple' | |||||
5 | from os.path import exists, isfile, splitext, abspath, join, isdir |
|
5 | from os.path import exists, isfile, splitext, abspath, join, isdir | |
6 | from os import walk, sep |
|
6 | from os import walk, sep | |
7 |
|
7 | |||
8 | from IPython.core.display import DisplayObject |
|
8 | from IPython.core.display import DisplayObject, TextDisplayObject | |
9 |
|
9 | |||
10 | __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', |
|
10 | __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument', | |
11 | 'FileLink', 'FileLinks'] |
|
11 | 'FileLink', 'FileLinks', 'Code'] | |
12 |
|
12 | |||
13 |
|
13 | |||
14 | class Audio(DisplayObject): |
|
14 | class Audio(DisplayObject): | |
@@ -555,3 +555,32 b' class FileLinks(FileLink):' | |||||
555 | for dirname, subdirs, fnames in walked_dir: |
|
555 | for dirname, subdirs, fnames in walked_dir: | |
556 | result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes) |
|
556 | result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes) | |
557 | return '\n'.join(result_lines) |
|
557 | return '\n'.join(result_lines) | |
|
558 | ||||
|
559 | ||||
|
560 | class Code(TextDisplayObject): | |||
|
561 | def __init__(self, data=None, url=None, filename=None, language=None): | |||
|
562 | self.language = language | |||
|
563 | super().__init__(data=data, url=url, filename=filename) | |||
|
564 | ||||
|
565 | def _get_lexer(self): | |||
|
566 | if self.language: | |||
|
567 | from pygments.lexers import get_lexer_by_name | |||
|
568 | return get_lexer_by_name(self.language) | |||
|
569 | elif self.filename: | |||
|
570 | from pygments.lexers import get_lexer_for_filename | |||
|
571 | return get_lexer_for_filename(self.filename) | |||
|
572 | else: | |||
|
573 | from pygments.lexers import guess_lexer | |||
|
574 | return guess_lexer(self.data) | |||
|
575 | ||||
|
576 | def _repr_html_(self): | |||
|
577 | from pygments import highlight | |||
|
578 | from pygments.formatters import HtmlFormatter | |||
|
579 | fmt = HtmlFormatter() | |||
|
580 | style = '<style>{}</style>'.format(fmt.get_style_defs('.output_html')) | |||
|
581 | return style + highlight(self.data, self._get_lexer(), fmt) | |||
|
582 | ||||
|
583 | def _repr_latex_(self): | |||
|
584 | from pygments import highlight | |||
|
585 | from pygments.formatters import LatexFormatter | |||
|
586 | return highlight(self.data, self._get_lexer(), LatexFormatter()) |
@@ -175,3 +175,7 b' def test_recursive_FileLinks():' | |||||
175 | def test_audio_from_file(): |
|
175 | def test_audio_from_file(): | |
176 | path = pjoin(dirname(__file__), 'test.wav') |
|
176 | path = pjoin(dirname(__file__), 'test.wav') | |
177 | display.Audio(filename=path) |
|
177 | display.Audio(filename=path) | |
|
178 | ||||
|
179 | def test_code_from_file(): | |||
|
180 | c = display.Code(filename=__file__) | |||
|
181 | assert c._repr_html_().startswith('<style>') |
General Comments 0
You need to be logged in to leave comments.
Login now