Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Add syntax highlighting to code blocks #110

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9fe2ceb
Remove the code effect
bkjohnson Sep 3, 2021
08b6f1f
Render highlighted text on slide w/ placeholder
bkjohnson Sep 4, 2021
904015c
Remove screen from the tuple
bkjohnson Sep 4, 2021
98982e7
Refactor into function for rendering entire block
bkjohnson Sep 4, 2021
9a6452e
Render highlighted block line by line
bkjohnson Sep 4, 2021
9b195dd
Avoid going out of range on the slides index
bkjohnson Sep 4, 2021
abadc0f
Add pygments to requires list
bkjohnson Sep 6, 2021
cf796e1
Switch parsers and don't use raw_text prop
bkjohnson Sep 6, 2021
5f9a6c8
Add padded box around code
bkjohnson Sep 6, 2021
01f243a
Remove now-unused _code effect
bkjohnson Sep 6, 2021
b76a280
Add to code_blocks directly instead of effects
bkjohnson Sep 7, 2021
f866e19
Refactor relationship between Slide and Slideshow
bkjohnson Sep 7, 2021
62ed159
Replace manual incrementing w/ enumerate func
bkjohnson Sep 7, 2021
e8f8e55
Reorder imports & rename variable
bkjohnson Sep 7, 2021
c6041b8
Refactor has_code to be based on code_blocks
bkjohnson Sep 7, 2021
952e692
Use a custom asciimatics renderer instead
bkjohnson Sep 7, 2021
f0d1405
Remove extra whitespace
bkjohnson Sep 7, 2021
c5ce9b2
Add colour & bg args back in
bkjohnson Sep 7, 2021
d564b3c
Reflect syntax highlighting support in README
bkjohnson Sep 7, 2021
4db2276
Add md language to code example
bkjohnson Sep 7, 2021
972eccf
Guess the language if code block doesn't specify
bkjohnson Sep 7, 2021
0675d97
Don't try to guess the language
bkjohnson Sep 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions present/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,6 @@ def _base(screen, element, row, fg_color, bg_color, attr=0):
return [base]


def _code(screen, element, row):
code = Print(
screen,
Text(element.render()),
row,
colour=Screen.COLOUR_WHITE,
bg=Screen.COLOUR_BLACK,
transparent=False,
)

return [code]


def _codio(screen, element, row):
codio = Print(
screen,
Expand Down
8 changes: 6 additions & 2 deletions present/slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ def pad(s, fill=" "):
lines.insert(0, top)
lines.append(bottom)

return "\n".join(lines)
return lines

@property
def size(self):
return len(self.obj["text"].splitlines())

def render(self):
def lang(self):
return self.obj["info"]

def padded_lines(self):
return self.pad(self.obj["text"])


Expand Down Expand Up @@ -407,6 +410,7 @@ def __init__(self, elements=None):
self.effect = None
self.fg_color = 0
self.bg_color = 7
self.code_blocks = []

_elements = []

Expand Down
55 changes: 44 additions & 11 deletions present/slideshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
from asciimatics.effects import Print
from asciimatics.event import KeyboardEvent
from asciimatics.exceptions import ResizeScreenError, StopApplication
from asciimatics.parsers import AnsiTerminalParser
from asciimatics.strings import ColouredText

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import Terminal256Formatter

from .effects import (
_reset,
_base,
_code,
_codio,
_image,
_fireworks,
Expand All @@ -23,11 +28,33 @@
)


def render_code_block(screen, block, row):
# Divide the width by 3
column = int(screen.dimensions[1] / 3)
lexer = get_lexer_by_name(block.lang())

for cur_row, line in enumerate(block.padded_lines(), start=row):
coded_text = highlight(line, lexer, Terminal256Formatter())
text = ColouredText(
coded_text,
AnsiTerminalParser(),
)
screen.paint(
text,
column,
cur_row,
colour_map=text.colour_map,
)
bkjohnson marked this conversation as resolved.
Show resolved Hide resolved


class Slide(Scene):
def __init__(self, show, effects, fg_color, bg_color):
def __init__(self, show, slide):
self.show = show
self.fg_color = fg_color
self.bg_color = bg_color
self.fg_color = slide.fg_color
self.bg_color = slide.bg_color
self.code_blocks = slide.code_blocks
effects = show.build_scene(slide)
bkjohnson marked this conversation as resolved.
Show resolved Hide resolved

super(Slide, self).__init__(effects)

def _reset(self):
Expand Down Expand Up @@ -93,7 +120,7 @@ def __enter__(self):
def __exit__(self, type, value, traceback):
self.screen.close()

def get_effects(self, slide):
def build_scene(self, slide):
effects = []
transparent = True
elements = slide.elements
Expand All @@ -112,7 +139,8 @@ def get_effects(self, slide):
pad = 2
for e in elements:
if e.type == "code":
effects.extend(_code(self.screen, e, row))
# Add the element + row to the slide's code_blocks list
slide.code_blocks.append((e, row))
pad = 4
elif e.type == "codio":
effects.extend(_codio(self.screen, e, row))
Expand Down Expand Up @@ -140,12 +168,9 @@ def play(
repeat=True,
allow_int=False,
):
self.reset = [Slide(self, _reset(self.screen), 7, 0)]
self.reset = [Scene(_reset(self.screen))]

self.slides = [
Slide(self, self.get_effects(slide), slide.fg_color, slide.bg_color)
for slide in self.slides
]
self.slides = [Slide(self, slide) for slide in self.slides]
bkjohnson marked this conversation as resolved.
Show resolved Hide resolved

# Initialise the Screen for animation.
self.screen.set_scenes(
Expand Down Expand Up @@ -189,6 +214,14 @@ def play(

a = time.time()
self.screen.draw_next_frame(repeat=repeat)

if self.current_slide < len(self.slides) and (
code_blocks := self.slides[self.current_slide].code_blocks
):
for tpl in code_blocks:
block_code, row = tpl
render_code_block(self.screen, block_code, row)
bkjohnson marked this conversation as resolved.
Show resolved Hide resolved

if self.screen.has_resized():
if stop_on_resize:
self.screen._scenes[self.screen._scene_index].exit()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"Click>=7.0",
"mistune>=2.0.0a4",
"pyfiglet>=0.8.post1",
"Pygments>=2.10.0",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pygments is the library that adds highlighting to the code. It has different styles/themes, but I'm leaving it on the default.

"PyYAML>=5.3.1",
]
dev_requires = ["black>=20.8b1", "Sphinx>=2.2.1"]
Expand Down