Skip to content

Commit

Permalink
Add pygments and SPARQL execute
Browse files Browse the repository at this point in the history
  • Loading branch information
epoz committed Dec 16, 2023
1 parent 324c5f0 commit cfdc897
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/app/schpiel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,44 @@
from .config import SCHPIEL_PATH, SCHPIEL_TOKEN
from .main import app, templates
from markdown_it import MarkdownIt
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from markupsafe import Markup
import os
from .lode import can_lode
from pydantic import BaseModel
from urllib.parse import quote


class CustomHtmlFormatter(HtmlFormatter):
def __init__(self, url=None, **options):
super().__init__(**options)
self.url = url

def wrap(self, source):
for i, t in source:
yield i, t + "<br>"
yield i + 1, f'<a style="margin-top: 1vh" href="{self.url}"><svg style="color: #34d8e4" xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-play-btn" viewBox="0 0 16 16"><path d="M6.79 5.093A.5.5 0 0 0 6 5.5v5a.5.5 0 0 0 .79.407l3.5-2.5a.5.5 0 0 0 0-.814l-3.5-2.5z"/><path d="M0 4a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V4zm15 0a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V4z"/></svg></a>'


def pygments_highlighter(md):
def _highlight(self, tokens, idx, options, env):
token = tokens[idx]
lang = token.info.strip() if token.info else "text"
code = token.content.strip()
url = f"/sparql#query={quote(code)}"
try:
lexer = get_lexer_by_name(lang, stripall=True)
except Exception:
lexer = get_lexer_by_name("text")
if lang == "sparql":
formatter = CustomHtmlFormatter(url=url, noclasses=True)
else:
formatter = HtmlFormatter()
return highlight(code, lexer, formatter)

md.add_render_rule("fence", _highlight)


def sanitize_path(path):
Expand Down Expand Up @@ -59,6 +93,7 @@ def schpiel(request: Request, background_tasks: BackgroundTasks, pagename: str):
if filepath_ == f"{pagename}.md":
md_content = open(filepath).read()
md = MarkdownIt()
pygments_highlighter(md)
contents = md.render(md_content)
return templates.TemplateResponse(
"schpiel.html", {"request": request, "contents": Markup(contents)}
Expand Down

0 comments on commit cfdc897

Please sign in to comment.