##// END OF EJS Templates
Merge pull request #10978 from takluyver/display-code...
Thomas Kluyver -
r24233:b9e079f6 merge
parent child Browse files
Show More
@@ -6,10 +6,10 b' from html import escape as html_escape'
6 from os.path import exists, isfile, splitext, abspath, join, isdir
6 from os.path import exists, isfile, splitext, abspath, join, isdir
7 from os import walk, sep, fsdecode
7 from os import walk, sep, fsdecode
8
8
9 from IPython.core.display import DisplayObject
9 from IPython.core.display import DisplayObject, TextDisplayObject
10
10
11 __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
11 __all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
12 'FileLink', 'FileLinks']
12 'FileLink', 'FileLinks', 'Code']
13
13
14
14
15 class Audio(DisplayObject):
15 class Audio(DisplayObject):
@@ -557,3 +557,52 b' class FileLinks(FileLink):'
557 for dirname, subdirs, fnames in walked_dir:
557 for dirname, subdirs, fnames in walked_dir:
558 result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
558 result_lines += self.terminal_display_formatter(dirname, fnames, self.included_suffixes)
559 return '\n'.join(result_lines)
559 return '\n'.join(result_lines)
560
561
562 class Code(TextDisplayObject):
563 """Display syntax-highlighted source code.
564
565 This uses Pygments to highlight the code for HTML and Latex output.
566
567 Parameters
568 ----------
569 data : str
570 The code as a string
571 url : str
572 A URL to fetch the code from
573 filename : str
574 A local filename to load the code from
575 language : str
576 The short name of a Pygments lexer to use for highlighting.
577 If not specified, it will guess the lexer based on the filename
578 or the code. Available lexers: http://pygments.org/docs/lexers/
579 """
580 def __init__(self, data=None, url=None, filename=None, language=None):
581 self.language = language
582 super().__init__(data=data, url=url, filename=filename)
583
584 def _get_lexer(self):
585 if self.language:
586 from pygments.lexers import get_lexer_by_name
587 return get_lexer_by_name(self.language)
588 elif self.filename:
589 from pygments.lexers import get_lexer_for_filename
590 return get_lexer_for_filename(self.filename)
591 else:
592 from pygments.lexers import guess_lexer
593 return guess_lexer(self.data)
594
595 def __repr__(self):
596 return self.data
597
598 def _repr_html_(self):
599 from pygments import highlight
600 from pygments.formatters import HtmlFormatter
601 fmt = HtmlFormatter()
602 style = '<style>{}</style>'.format(fmt.get_style_defs('.output_html'))
603 return style + highlight(self.data, self._get_lexer(), fmt)
604
605 def _repr_latex_(self):
606 from pygments import highlight
607 from pygments.formatters import LatexFormatter
608 return highlight(self.data, self._get_lexer(), LatexFormatter())
@@ -183,3 +183,7 b' def test_recursive_FileLinks():'
183 def test_audio_from_file():
183 def test_audio_from_file():
184 path = pjoin(dirname(__file__), 'test.wav')
184 path = pjoin(dirname(__file__), 'test.wav')
185 display.Audio(filename=path)
185 display.Audio(filename=path)
186
187 def test_code_from_file():
188 c = display.Code(filename=__file__)
189 assert c._repr_html_().startswith('<style>')
General Comments 0
You need to be logged in to leave comments. Login now