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 all 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ Note: You can use high resolution images and tweak the terminal font size to get

### Code blocks

To specify your language for syntax highlighting, put it on the first line after the backticks. If no language is specified, no highlighting will be applied.

<pre>
```
```python
import os

os.getcwd()
Expand Down
2 changes: 1 addition & 1 deletion examples/sample.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ columns, rows = shutil.get_terminal_size()

## Images

```
```md
![RC](images/recurse.png)
```

Expand Down
38 changes: 37 additions & 1 deletion present/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
ColourImageFile,
DynamicRenderer,
)
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

ATTRS = {
"bold": Screen.A_BOLD,
Expand All @@ -34,6 +39,35 @@
EFFECTS = ["fireworks", "explosions", "stars", "matrix", "plasma"]


class HighlightedCode(StaticRenderer):
def __init__(self, code, lang):
super(HighlightedCode, self).__init__()
self._images = [code]
self.lang = lang

@property
def rendered_text(self):
if self.lang is None:
return super().rendered_text

if len(self._plain_images) <= 0:
self._convert_images()

lexer = get_lexer_by_name(self.lang)
highlighted_block = []
colour_map = []

# We're only expecting this renderer to have one code block
for line in self._plain_images[0]:
highlighted_line = highlight(line, lexer, Terminal256Formatter())
encoded_text = ColouredText(highlighted_line, AnsiTerminalParser())

highlighted_block.append(encoded_text)
colour_map.append(encoded_text.colour_map)

return (highlighted_block, colour_map)


class Text(StaticRenderer):
def __init__(self, text):
super(Text, self).__init__()
Expand Down Expand Up @@ -139,10 +173,12 @@ def _base(screen, element, row, fg_color, bg_color, attr=0):


def _code(screen, element, row):
column = int(screen.dimensions[1] / 3)
code = Print(
screen,
Text(element.render()),
HighlightedCode(element.render(), element.lang()),
row,
column,
colour=Screen.COLOUR_WHITE,
bg=Screen.COLOUR_BLACK,
transparent=False,
Expand Down
3 changes: 3 additions & 0 deletions present/slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def pad(s, fill=" "):
def size(self):
return len(self.obj["text"].splitlines())

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

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

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