Skip to content

Commit

Permalink
Merge pull request #466 from lebedov/nbshow-colorize
Browse files Browse the repository at this point in the history
Colorize source with pygments before displaying.
  • Loading branch information
vidartf committed Apr 29, 2019
2 parents c2635db + 0509d39 commit ba18b56
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
41 changes: 38 additions & 3 deletions nbdime/prettyprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __init__(
use_git = True,
use_diff = True,
use_color = True,
language = None
):
self.out = out
if include is None:
Expand All @@ -101,6 +102,7 @@ def __init__(
self.use_git = use_git
self.use_diff = use_diff
self.use_color = use_color
self.language = language

def should_ignore_path(self, path):
starred = star_path(split_path(path))
Expand Down Expand Up @@ -610,10 +612,35 @@ def pretty_print_attachments(attachments, prefix="", config=DefaultConfig):
for name in sorted(attachments):
pretty_print_item(name, attachments[name], prefix+IND, config)

try:
from pygments import highlight
from pygments.formatters import Terminal256Formatter
from pygments.lexers import find_lexer_class_by_name
from pygments.util import ClassNotFound

def colorize_source(source, lexer_name):
try:
lexer = find_lexer_class_by_name(lexer_name)()
except ClassNotFound:
return source
formatter = Terminal256Formatter()
return highlight(source, lexer, formatter)

except ImportError as e:
def colorize_source(source, *args, **kwargs):
return source


def pretty_print_source(source, prefix="", config=DefaultConfig):
def pretty_print_source(source, prefix="", is_markdown=False, config=DefaultConfig):
pretty_print_key("source", prefix, config)
pretty_print_multiline(source, prefix+IND, config)
if not prefix.strip() and (is_markdown or config.language):
source_highlighted = colorize_source(
source,
'markdown' if is_markdown else config.language
)
else:
source_highlighted = source
pretty_print_multiline(source_highlighted, prefix+IND, config)


def pretty_print_cell(i, cell, prefix="", force_header=False, config=DefaultConfig):
Expand Down Expand Up @@ -653,9 +680,10 @@ def c():

source = cell.get("source")
if source and config.sources:
is_markdown = cell.get('cell_type', None) == 'markdown'
# Write source
c()
pretty_print_source(source, key_prefix, config)
pretty_print_source(source, key_prefix, is_markdown=is_markdown, config=config)

attachments = cell.get("attachments")
if attachments and config.attachments:
Expand Down Expand Up @@ -692,6 +720,13 @@ def pretty_print_notebook(nb, config=DefaultConfig):
"""
prefix = ""

if config.language is None:
language_info = nb.metadata.get('language_info', {})
config.language = language_info.get(
'pygments_lexer',
language_info.get('name', None)
)

if config.details:
# Write notebook header
v = "%d.%d" % (nb.nbformat, nb.nbformat_minor)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'nbformat',
'six',
'colorama',
'pygments',
'tornado',
'requests',
'GitPython!=2.1.4, !=2.1.5, !=2.1.6', # For difftool taking git refs
Expand Down

0 comments on commit ba18b56

Please sign in to comment.