hyml 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hyml/__init__.py +35 -0
- hyml/core.hy +113 -0
- hyml/ext.py +84 -0
- hyml/globals.hy +30 -0
- hyml/helpers.hy +95 -0
- hyml/ipython.py +52 -0
- hyml/macros.hy +281 -0
- hyml/minimal.hy +166 -0
- hyml/specs.hy +315 -0
- hyml/template.hy +84 -0
- hyml/variables.hy +46 -0
- hyml-1.0.0.dist-info/METADATA +1167 -0
- hyml-1.0.0.dist-info/RECORD +17 -0
- hyml-1.0.0.dist-info/WHEEL +5 -0
- hyml-1.0.0.dist-info/entry_points.txt +2 -0
- hyml-1.0.0.dist-info/licenses/LICENSE +21 -0
- hyml-1.0.0.dist-info/top_level.txt +1 -0
hyml/__init__.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""HyML - XML / (X)HTML generator for Hy.
|
|
3
|
+
|
|
4
|
+
The markup macros live in Hy modules::
|
|
5
|
+
|
|
6
|
+
(require hyml.minimal *) ; ml
|
|
7
|
+
(require hyml.macros *) ; xml, xhtml, xhtml5, html4, html5
|
|
8
|
+
|
|
9
|
+
From Python, HyML source text can be rendered with :func:`render`.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import hy # noqa: F401 (makes the .hy modules importable)
|
|
13
|
+
|
|
14
|
+
__version__ = "1.0.0"
|
|
15
|
+
|
|
16
|
+
DIALECTS = ("ml", "xml", "xhtml", "xhtml5", "html4", "html5")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def render(source, dialect="ml", variables=None):
|
|
20
|
+
"""Render HyML source text to a markup string.
|
|
21
|
+
|
|
22
|
+
>>> render('(p :class "x" "Hello")', "html5")
|
|
23
|
+
'<p class=x>Hello</p>'
|
|
24
|
+
|
|
25
|
+
``variables`` is a dictionary available to unquoted code (``~name``)
|
|
26
|
+
in addition to the ``defvar`` / ``deffun`` registry.
|
|
27
|
+
"""
|
|
28
|
+
if dialect not in DIALECTS:
|
|
29
|
+
raise ValueError("dialect must be one of: " + ", ".join(DIALECTS))
|
|
30
|
+
forms = list(hy.read_many(source))
|
|
31
|
+
if dialect == "ml":
|
|
32
|
+
from hyml.minimal import parse_mnml
|
|
33
|
+
return "".join(parse_mnml(form, variables) for form in forms)
|
|
34
|
+
from hyml.macros import render as render_forms
|
|
35
|
+
return render_forms(dialect, forms, variables)
|
hyml/core.hy
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
;; -*- coding: utf-8 -*-
|
|
2
|
+
;;
|
|
3
|
+
;; Copyright (c) 2017-2026 Marko Manninen
|
|
4
|
+
;;
|
|
5
|
+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
;; of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
;; in the Software without restriction, including without limitation the rights
|
|
8
|
+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
;; copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
;; furnished to do so, subject to the following conditions:
|
|
11
|
+
;;
|
|
12
|
+
;; The above copyright notice and this permission notice shall be included in
|
|
13
|
+
;; all copies or substantial portions of the Software.
|
|
14
|
+
;;
|
|
15
|
+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
;; THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
;----------------------------------------------
|
|
24
|
+
; Helpers shared by the MiNiMaL and ML parsers
|
|
25
|
+
;----------------------------------------------
|
|
26
|
+
; ML macros render markup at macro-expansion time, so everything here
|
|
27
|
+
; must exist at compile time too
|
|
28
|
+
(eval-and-compile
|
|
29
|
+
|
|
30
|
+
(defclass HyMLError [Exception])
|
|
31
|
+
|
|
32
|
+
; global registry for variables and functions set by defvar and deffun
|
|
33
|
+
(setv variables-and-functions {})
|
|
34
|
+
|
|
35
|
+
(defn keyword? [x] (isinstance x hy.models.Keyword))
|
|
36
|
+
|
|
37
|
+
; tag expressions and spliced lists. strings are not collections here
|
|
38
|
+
(defn coll? [x] (and (isinstance x #(list tuple)) (not (isinstance x str))))
|
|
39
|
+
|
|
40
|
+
; name of the symbol heading an expression: (unquote x) -> "unquote"
|
|
41
|
+
(defn head-name [code]
|
|
42
|
+
(when (and (isinstance code hy.models.Expression)
|
|
43
|
+
code
|
|
44
|
+
(isinstance (get code 0) hy.models.Symbol))
|
|
45
|
+
(str (get code 0))))
|
|
46
|
+
|
|
47
|
+
; plain text of an atom. str of a numeric model gives its repr,
|
|
48
|
+
; and keywords should lose their colon
|
|
49
|
+
(defn ->str [x]
|
|
50
|
+
(cond (is x None) ""
|
|
51
|
+
(keyword? x) x.name
|
|
52
|
+
(isinstance x hy.models.Integer) (str (int x))
|
|
53
|
+
(isinstance x hy.models.Float) (str (float x))
|
|
54
|
+
(isinstance x hy.models.Complex) (str (complex x))
|
|
55
|
+
True (str x)))
|
|
56
|
+
|
|
57
|
+
; Hy reads h1.main.header as (. h1 main header) and .main as (. None main).
|
|
58
|
+
; join them back to the shorthand string the (x)html parsers understand
|
|
59
|
+
(defn dotted-tag [code]
|
|
60
|
+
(.join "." (gfor part (cut code 1 None)
|
|
61
|
+
(if (= (str part) "None") "" (->str part)))))
|
|
62
|
+
|
|
63
|
+
; fresh namespace for evaluating unquoted code. the registry comes first
|
|
64
|
+
; and later dictionaries override earlier ones. keys are mangled so that
|
|
65
|
+
; {"first-name" ...} is found by ~first-name. hy must be a global, or
|
|
66
|
+
; functions defined in the namespace can't run quasiquoted code later
|
|
67
|
+
(defn namespace [#* dicts]
|
|
68
|
+
(setv ns (dict :hy hy #** variables-and-functions))
|
|
69
|
+
(for [d dicts]
|
|
70
|
+
(when d
|
|
71
|
+
(for [[k v] (.items d)]
|
|
72
|
+
(setv (get ns (hy.mangle (->str k))) v))))
|
|
73
|
+
ns)
|
|
74
|
+
|
|
75
|
+
; evaluate code with macros looked up from the given module
|
|
76
|
+
(defn eval-in [code module #* dicts]
|
|
77
|
+
(hy.eval code (namespace #* dicts) :module module))
|
|
78
|
+
|
|
79
|
+
; tag name from the head of an expression. symbols and strings are names
|
|
80
|
+
; as such. ~(code) is evaluated, 'tag and `tag are unquoted
|
|
81
|
+
(defn catch-tag [code evaluate]
|
|
82
|
+
(setv head (head-name code))
|
|
83
|
+
(cond (isinstance code hy.models.Symbol) (str code)
|
|
84
|
+
(= head "unquote") (->str (evaluate (get code 1)))
|
|
85
|
+
(in head #("quote" "quasiquote")) (->str (hy.eval code))
|
|
86
|
+
(= head ".") (dotted-tag code)
|
|
87
|
+
True (->str code)))
|
|
88
|
+
|
|
89
|
+
; result of unquoted code: keywords and expressions stay as code to be
|
|
90
|
+
; parsed further, everything else becomes text
|
|
91
|
+
(defn unquoted [value]
|
|
92
|
+
(if (or (keyword? value) (isinstance value hy.models.Expression))
|
|
93
|
+
value
|
|
94
|
+
(->str value)))
|
|
95
|
+
|
|
96
|
+
; evaluate ~ and ~@ items of a tag body. spliced lists are flattened in
|
|
97
|
+
; place, recursively, because include returns a splice itself
|
|
98
|
+
(defn expand [items evaluate]
|
|
99
|
+
(setv result [])
|
|
100
|
+
(for [item items]
|
|
101
|
+
(setv head (head-name item))
|
|
102
|
+
(cond (= head "unquote-splice")
|
|
103
|
+
(.extend result (expand (evaluate (get item 1)) evaluate))
|
|
104
|
+
(= head "unquote")
|
|
105
|
+
(.append result (unquoted (evaluate (get item 1))))
|
|
106
|
+
True
|
|
107
|
+
(.append result item)))
|
|
108
|
+
result)
|
|
109
|
+
|
|
110
|
+
; quote an attribute value. & and < are left alone so that already
|
|
111
|
+
; escaped values and template placeholders keep working
|
|
112
|
+
(defn escape-attribute [value]
|
|
113
|
+
(.replace (str value) "\"" """)))
|
hyml/ext.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# ---------------------------------------------------------
|
|
3
|
+
# Copyright (c) Marko Manninen <elonmedia@gmail.com>, 2017-2026
|
|
4
|
+
# ---------------------------------------------------------
|
|
5
|
+
#
|
|
6
|
+
# Babel message extractor for Hy and HyML files.
|
|
7
|
+
#
|
|
8
|
+
# The package registers it as an entry point:
|
|
9
|
+
#
|
|
10
|
+
# [project.entry-points."babel.extractors"]
|
|
11
|
+
# hyml = "hyml.ext:babel_extract"
|
|
12
|
+
#
|
|
13
|
+
# and then in babel.cfg:
|
|
14
|
+
#
|
|
15
|
+
# [hyml: **.hyml]
|
|
16
|
+
# [hyml: **.hy]
|
|
17
|
+
#
|
|
18
|
+
# -----------------------------------------------------------------------
|
|
19
|
+
# LOCALIZATION ROUTINE
|
|
20
|
+
# -----------------------------------------------------------------------
|
|
21
|
+
# 1. extract messages:
|
|
22
|
+
# pybabel extract -F babel.cfg -o messages.pot .
|
|
23
|
+
# 2. initialize language file:
|
|
24
|
+
# pybabel init -i messages.pot -d translations -l en
|
|
25
|
+
# 3. make changes to the language file (manually or via third party app)
|
|
26
|
+
# 4. update translation messages:
|
|
27
|
+
# pybabel update -i messages.pot -d translations
|
|
28
|
+
# 5. compile translations
|
|
29
|
+
# pybabel compile -d translations
|
|
30
|
+
# -----------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
import hy
|
|
33
|
+
from hy.models import Expression, String, Symbol
|
|
34
|
+
from hy.reader import HyReader
|
|
35
|
+
|
|
36
|
+
# accepted gettext / babel keywords
|
|
37
|
+
KEYWORDS = ("_", "gettext", "ngettext", "N_", "lgettext", "lngettext", "lazy_gettext")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _reader():
|
|
41
|
+
"""Hy reader that knows the HyML reader macros.
|
|
42
|
+
|
|
43
|
+
#i"message" is the shorthand for (_ "message") from hyml.minimal, and
|
|
44
|
+
#㎖ only renders markup, so its form is read as such.
|
|
45
|
+
"""
|
|
46
|
+
reader = HyReader()
|
|
47
|
+
reader.reader_macros["i"] = lambda r, key: Expression([Symbol("_"), r.parse_one_form()])
|
|
48
|
+
reader.reader_macros["㎖"] = lambda r, key: r.parse_one_form()
|
|
49
|
+
return reader
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _messages(model, keywords):
|
|
53
|
+
"""Yield (lineno, funcname, messages, comments) for gettext calls in model."""
|
|
54
|
+
if (isinstance(model, Expression) and model and isinstance(model[0], Symbol)
|
|
55
|
+
and str(model[0]) in keywords):
|
|
56
|
+
args = model[1:]
|
|
57
|
+
strings = [arg for arg in args if isinstance(arg, String)]
|
|
58
|
+
if strings:
|
|
59
|
+
messages = tuple(str(arg) if isinstance(arg, String) else None for arg in args)
|
|
60
|
+
lineno = getattr(model, "start_line", None) or strings[0].start_line
|
|
61
|
+
# lazy_gettext is flask-babel's lazy form of gettext
|
|
62
|
+
funcname = "gettext" if str(model[0]) == "lazy_gettext" else str(model[0])
|
|
63
|
+
yield lineno, funcname, messages[0] if len(messages) == 1 else messages, []
|
|
64
|
+
if isinstance(model, tuple):
|
|
65
|
+
for child in model:
|
|
66
|
+
yield from _messages(child, keywords)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def extract_from_source(text, keywords=KEYWORDS, filename="<string>"):
|
|
70
|
+
"""Return the list of babel message tuples found in Hy source text."""
|
|
71
|
+
keywords = set(keywords)
|
|
72
|
+
return [message
|
|
73
|
+
for form in hy.read_many(text, filename=filename, reader=_reader())
|
|
74
|
+
for message in _messages(form, keywords)]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def babel_extract(fileobj, keywords=None, comment_tags=None, options=None):
|
|
78
|
+
"""Babel extraction method for Hy and HyML files."""
|
|
79
|
+
options = options or {}
|
|
80
|
+
text = fileobj.read()
|
|
81
|
+
if isinstance(text, bytes):
|
|
82
|
+
text = text.decode(options.get("encoding", "utf-8"))
|
|
83
|
+
return iter(extract_from_source(text, set(KEYWORDS) | set(keywords or ()),
|
|
84
|
+
getattr(fileobj, "name", "<string>")))
|
hyml/globals.hy
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
;; -*- coding: utf-8 -*-
|
|
2
|
+
;;
|
|
3
|
+
;; Copyright (c) 2017-2026 Marko Manninen
|
|
4
|
+
;;
|
|
5
|
+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
;; of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
;; in the Software without restriction, including without limitation the rights
|
|
8
|
+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
;; copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
;; furnished to do so, subject to the following conditions:
|
|
11
|
+
;;
|
|
12
|
+
;; The above copyright notice and this permission notice shall be included in
|
|
13
|
+
;; all copies or substantial portions of the Software.
|
|
14
|
+
;;
|
|
15
|
+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
;; THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
;------------------------------------------------------------------
|
|
24
|
+
; Backwards compatibility: ML macros (hyml.macros) used to keep
|
|
25
|
+
; their own registry here. Both parsers now share hyml.variables
|
|
26
|
+
;------------------------------------------------------------------
|
|
27
|
+
(require hyml.variables [defvar deffun])
|
|
28
|
+
(import hyml.core [HyMLError variables-and-functions])
|
|
29
|
+
|
|
30
|
+
(setv variables variables-and-functions)
|
hyml/helpers.hy
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
;; -*- coding: utf-8 -*-
|
|
2
|
+
;;
|
|
3
|
+
;; Copyright (c) 2017-2026 Marko Manninen
|
|
4
|
+
;;
|
|
5
|
+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
;; of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
;; in the Software without restriction, including without limitation the rights
|
|
8
|
+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
;; copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
;; furnished to do so, subject to the following conditions:
|
|
11
|
+
;;
|
|
12
|
+
;; The above copyright notice and this permission notice shall be included in
|
|
13
|
+
;; all copies or substantial portions of the Software.
|
|
14
|
+
;;
|
|
15
|
+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
;; THE SOFTWARE.
|
|
22
|
+
|
|
23
|
+
(import xml.dom.minidom)
|
|
24
|
+
(import hyml.core [HyMLError])
|
|
25
|
+
|
|
26
|
+
;----------------------------------------
|
|
27
|
+
; print multiple statements on new lines
|
|
28
|
+
;----------------------------------------
|
|
29
|
+
|
|
30
|
+
(defmacro println [#* args]
|
|
31
|
+
`(do ~@(gfor arg args `(print ~arg))))
|
|
32
|
+
|
|
33
|
+
;----------------------------------------------
|
|
34
|
+
; render html for the Jupyter Notebook document
|
|
35
|
+
; this is not mandatory for HyML other macros
|
|
36
|
+
; this is just for the html render in NB
|
|
37
|
+
;----------------------------------------------
|
|
38
|
+
; IPython is imported only when the expanded code runs, and the ML
|
|
39
|
+
; macros are called through hy.R so they need not be required by the user
|
|
40
|
+
|
|
41
|
+
(defmacro ml> [#* code]
|
|
42
|
+
`(hy.I.IPython/display.HTML (hy.R.hyml/minimal.ml ~@code)))
|
|
43
|
+
|
|
44
|
+
(defmacro xml> [#* code]
|
|
45
|
+
`(hy.I.IPython/display.HTML (hy.R.hyml/macros.xml ~@code)))
|
|
46
|
+
|
|
47
|
+
(defmacro xhtml> [#* code]
|
|
48
|
+
`(hy.I.IPython/display.HTML (hy.R.hyml/macros.xhtml ~@code)))
|
|
49
|
+
|
|
50
|
+
(defmacro xhtml5> [#* code]
|
|
51
|
+
`(hy.I.IPython/display.HTML (hy.R.hyml/macros.xhtml5 ~@code)))
|
|
52
|
+
|
|
53
|
+
(defmacro html4> [#* code]
|
|
54
|
+
`(hy.I.IPython/display.HTML (hy.R.hyml/macros.html4 ~@code)))
|
|
55
|
+
|
|
56
|
+
(defmacro html5> [#* code]
|
|
57
|
+
`(hy.I.IPython/display.HTML (hy.R.hyml/macros.html5 ~@code)))
|
|
58
|
+
|
|
59
|
+
;----------------------------------------------------
|
|
60
|
+
; Indent xml code
|
|
61
|
+
; Works only with well defined root node xml content!
|
|
62
|
+
;----------------------------------------------------
|
|
63
|
+
|
|
64
|
+
(defn indent [xmldoc]
|
|
65
|
+
(try
|
|
66
|
+
(setv pretty (.toprettyxml (xml.dom.minidom.parseString xmldoc)))
|
|
67
|
+
(except [e Exception]
|
|
68
|
+
(raise (HyMLError (+ "indent needs well-formed xml with a single root node: "
|
|
69
|
+
(str e))))))
|
|
70
|
+
; minidom always adds its own xml declaration. drop it and restore
|
|
71
|
+
; the original one if the document had it
|
|
72
|
+
(setv pretty (.join "\n" (cut (.splitlines pretty) 1 None)))
|
|
73
|
+
(if (.startswith xmldoc "<?xml")
|
|
74
|
+
(+ (cut xmldoc 0 (+ (.index xmldoc "?>") 2)) "\n" pretty)
|
|
75
|
+
pretty))
|
|
76
|
+
|
|
77
|
+
;-----------------------------------------------------------------------
|
|
78
|
+
; List comprehension
|
|
79
|
+
; Use ordinary list and expression (list-comp expr args) arguments order
|
|
80
|
+
; vice versa: (list-comp* args expr). This serves as a kind of for each
|
|
81
|
+
; loop where list items and variables are introdused first, and then
|
|
82
|
+
; expression to handle each of items.
|
|
83
|
+
;-----------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
; Hy 0.13 style list-comp kept for existing templates:
|
|
86
|
+
; (list-comp expr [x xs y ys] condition) -> (lfor x xs y ys :if condition expr)
|
|
87
|
+
(defmacro list-comp [expr bindings [condition None]]
|
|
88
|
+
(if (is condition None)
|
|
89
|
+
`(lfor ~@bindings ~expr)
|
|
90
|
+
`(lfor ~@bindings :if ~condition ~expr)))
|
|
91
|
+
|
|
92
|
+
(defmacro list-comp* [bindings expr [condition None]]
|
|
93
|
+
(if (is condition None)
|
|
94
|
+
`(lfor ~@bindings ~expr)
|
|
95
|
+
`(lfor ~@bindings :if ~condition ~expr)))
|
hyml/ipython.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""IPython / Jupyter extension for running Hy code in notebook cells.
|
|
3
|
+
|
|
4
|
+
There is no maintained Hy kernel, so HyML notebooks use the standard
|
|
5
|
+
Python kernel with this extension::
|
|
6
|
+
|
|
7
|
+
%load_ext hyml.ipython
|
|
8
|
+
|
|
9
|
+
%%hy
|
|
10
|
+
(require hyml.minimal *)
|
|
11
|
+
(ml (p :class "x" "Hello"))
|
|
12
|
+
|
|
13
|
+
The value of the last form of a cell is the cell's output. Macros and
|
|
14
|
+
reader macros required in one cell are available in later cells.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import hy
|
|
18
|
+
from hy.reader import HyReader
|
|
19
|
+
from IPython.core.magic import Magics, cell_magic, line_magic, magics_class
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@magics_class
|
|
23
|
+
class HyMagics(Magics):
|
|
24
|
+
|
|
25
|
+
def __init__(self, shell):
|
|
26
|
+
super().__init__(shell)
|
|
27
|
+
# one reader for the whole session, so that reader macros stay enabled
|
|
28
|
+
self.reader = HyReader()
|
|
29
|
+
|
|
30
|
+
def run(self, source):
|
|
31
|
+
module = self.shell.user_module
|
|
32
|
+
result = None
|
|
33
|
+
# forms are read one at a time, so a reader macro required by one
|
|
34
|
+
# form can be used by the next one in the same cell
|
|
35
|
+
for form in hy.read_many(source, filename="<hy-cell>", reader=self.reader):
|
|
36
|
+
result = hy.eval(form, self.shell.user_ns, module=module)
|
|
37
|
+
self.reader.reader_macros.update(vars(module).get("_hy_reader_macros", {}))
|
|
38
|
+
return result
|
|
39
|
+
|
|
40
|
+
@cell_magic
|
|
41
|
+
def hy(self, line, cell):
|
|
42
|
+
"""Run the cell as Hy code."""
|
|
43
|
+
return self.run(cell)
|
|
44
|
+
|
|
45
|
+
@line_magic("hy")
|
|
46
|
+
def hy_line(self, line):
|
|
47
|
+
"""Run the rest of the line as Hy code."""
|
|
48
|
+
return self.run(line)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def load_ipython_extension(ipython):
|
|
52
|
+
ipython.register_magics(HyMagics)
|