Skip to content

Commit

Permalink
add hy lisp
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfast committed Sep 25, 2024
1 parent e01f149 commit a46c08b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/midgy/language/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class Python(Markdown, type="text/x-python", language="ipython3"):
html="midgy.types:HTML",
markdown="midgy.types:Markdown",
javascript="midgy.types:Script",
graphviz="midgy.types:DOT",
dot="midgy.types:DOT",
graphviz="midgy.types:Dot",
dot="midgy.types:Dot",
md="midgy.types:Markdown",
lisp="midgy.types:Hy.eval",
hy="midgy.types:Hy.eval"
)
fenced_code_blocks: list = field(
default_factory=["python", "python3", "ipython3", "ipython", ""].copy
Expand Down
56 changes: 54 additions & 2 deletions src/midgy/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from functools import wraps
# all of these types should have magic counterparts
from functools import cached_property, wraps
from midgy.tangle import get_markdown_it


def enforce_cls(callable):
Expand Down Expand Up @@ -65,13 +67,23 @@ class Markdown(String):
def _repr_markdown_(self):
return self

def to_html(self):
from IPython import get_ipython

shell = get_ipython()
if shell:
from midgy._magics import get_environment

return HTML(shell.tangle.parser.parser.render(self))
return HTML(get_markdown_it().render(self))


class Mermaid(String):
def _repr_markdown_(self):
return f"""```mermaid\n{self}```"""


Md = Markdown
Md = Markdown


class SVG(HTML):
Expand All @@ -92,3 +104,43 @@ def _repr_svg_(self):
return self.graphviz()._repr_image_svg_xml()
except (ModuleNotFoundError, ImportError):
pass


class Hy(String):
@cached_property
def shell(self):
import hy.repl

class InteractiveHy(hy.repl.HyCompile):

def __init__(self, *a, **kw):
from IPython import get_ipython

shell = get_ipython()
# we'll need to scope this for non interactive versions
if shell:
main = __import__("__main__")
else:
raise TypeError("only interactive shells are supported for hy at the moment")
super().__init__(main, vars(main))
self.hy_compiler = hy.repl.HyASTCompiler(__import__(__name__))

def __call__(self, src, *args, **kwargs):
import builtins

exec, eval = super().__call__(src, *args, **kwargs)
builtins.exec(exec, vars(self.module), vars(self.module))
return builtins.eval(eval, vars(self.module), vars(self.module))

@classmethod
def execute(cls, code):
return cls()(code)

return InteractiveHy

def _eval(self):
return self.shell.execute(self)

@classmethod
def eval(cls, code):
return cls(code)._eval()
2 changes: 1 addition & 1 deletion src/midgy/weave.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def weave(
quick_doctest(self.source)

data = {"text/x-python": self.out}
from IPython.display import display
if weave and self.tokens[0].map[0] == int(magic):
from IPython.display import display

if html:
output = self.parser.parser.render(
Expand Down

0 comments on commit a46c08b

Please sign in to comment.