vinayak-mehta / present

A terminal-based presentation tool with colors and effects.
https://present.readthedocs.io
Apache License 2.0
4.33k stars 164 forks source link

Local deveopment problems + syntax highlighting feature #109

Closed bkjohnson closed 3 years ago

bkjohnson commented 3 years ago

I'd like to make a PR to add syntax highlighting for code blocks (like #101 mentions). I've gotten this working in a sample file, but I'm having a hard time running the project from source without any errors, so I can't test my changes.

Local development problems

Any added documentation to the Contributing section would be greatly appreciated. Here is what I have been doing:

pip install ".[dev]"
python3 present/__main__.py sample.md

I'm using python 3.8.10 on Linux Mint 20.1

Markdown file

# present

A terminal-based presentation tool with colors and effects

```bash
$ pip install present

Code blocks

An explanation for the code below:

import os

print(os.getcwd())
import shutil

columns, rows = shutil.get_terminal_size()


The first problem I'm having is that the local file is importing `present` from the system, rather than the local package. I can get rid of that error in a hackish way by having `from cli import cli` in the main file, and removing the `.` prefix from all of the other local imports. However, I then get an error log ending with:

File "/home/me/.local/lib/python3.8/site-packages/asciimatics/renderers.py", line 118, in _convert_images for line in image.split("\n"): AttributeError: 'Markdown' object has no attribute 'split'


I'm comfortable writing python scripts, but I don't have a lot of experience in a full python development environment, and I'm guessing that lack of knowledge is what's causing me problems. I've tried a virtual environment as well and had no luck.

---

## Code for syntax highlighting

I haven't been able to incorporate this into the actual codebase because I can't get it to run from source, but here is my sample file:

**sample.py**
`````python
from mistune import escape, create_markdown, HTMLRenderer
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import terminal256

class HighlightRenderer(HTMLRenderer):
    def block_code(self, code, lang=None):
        if lang:
            lexer = get_lexer_by_name(lang, stripall=True)
            formatter = terminal256.Terminal256Formatter()
            return highlight(code, lexer, formatter)
        return escape(code)

markdown = create_markdown(renderer=HighlightRenderer())

print(
    markdown(
        """
```python
import os

print(os.getcwd())

""" ) )



The output in the terminal:

![image](https://user-images.githubusercontent.com/2008881/131618727-5737dfaa-706c-4d0a-9df7-2a05116916c3.png)

If I can solve my local development problems I'd be happy to test this out and hopefully make a PR.
bkjohnson commented 3 years ago

I figured out the development environment by setting up the virtual environment properly and then running this from the project directory:

pip3 install -e ./

I'm working on trying to make the feature happen now.