diff --git a/hy/macros.py b/hy/macros.py index fa43d18..43e4fb0 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -20,6 +20,7 @@ from hy.models.expression import HyExpression from hy.models.string import HyString +from hy.models.dict import HyDict from hy.models.list import HyList _hy_macros = {} @@ -47,6 +48,11 @@ def process(tree): ntree.replace(tree) return ntree + if isinstance(tree, HyDict): + obj = HyDict({process(x): process(tree[x]) for x in tree}) + obj.replace(tree) + return obj + if isinstance(tree, HyList): obj = HyList([process(x) for x in tree]) obj.replace(tree) diff --git a/hy/models/dict.py b/hy/models/dict.py index 7fd9315..ea0af14 100644 --- a/hy/models/dict.py +++ b/hy/models/dict.py @@ -25,4 +25,10 @@ class HyDict(HyObject, dict): """ HyDict (just a dict) """ - pass + + def replace(self, other): + for x in self: + self[x].replace(other) + x.replace(other) + + HyObject.replace(self, other) diff --git a/site/app.hy b/site/app.hy index 7b4b852..d59fea9 100644 --- a/site/app.hy +++ b/site/app.hy @@ -11,3 +11,5 @@ (route "/" [] (render-template "index.html")) + +(post-route "/test" [] (render-template "index.html")) diff --git a/site/shim.py b/site/shim.py index 938a6b7..c213f9b 100644 --- a/site/shim.py +++ b/site/shim.py @@ -4,22 +4,41 @@ import hy # NOQA from hy.models.expression import HyExpression from hy.models.symbol import HySymbol +from hy.models.string import HyString +from hy.models.list import HyList +from hy.models.dict import HyDict from hy.macros import macro -@macro("route") -def route_macro(tree): - """ Simple routing macro """ - +def router(tree, rkwargs=None): tree.pop(0) path = tree.pop(0) tree.insert(0, HySymbol("fn")) + route = HyExpression([HySymbol(".route"), + HySymbol("app"), + path]) + + if rkwargs: + route = HyExpression([HySymbol("kwapply"), + route, + HyDict({HyString("methods"): rkwargs})]) + return HyExpression([HySymbol("decorate_with"), - HyExpression([HySymbol(".route"), - HySymbol("app"), - path]), tree]) + route, + tree]) + + +@macro("route") +def route_macro(tree): + return router(tree) + + +@macro("post_route") +def route_macro(tree): + return router(tree, rkwargs=HyList([HyString("POST")])) + from app import app