Last active
July 14, 2016 08:20
-
-
Save benjamintanweihao/992a874417a3e3afd66fe966ca94aaf0 to your computer and use it in GitHub Desktop.
This is my first attempt at reproducing Html. Not working though.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Html do | |
| defmacro markup(do: inner) do | |
| {:ok, buffer} = start_buffer | |
| quote do | |
| unquote(inner) | |
| end | |
| buffer = get_buffer | |
| stop_buffer | |
| buffer | |
| end | |
| defmacro tag(name, do: inner) do | |
| IO.puts "OHAI" | |
| quote do | |
| IO.puts put_buffer "<#{unquote(name)}>" | |
| unquote(inner) | |
| IO.puts put_buffer "</#{unquote(name)}>" | |
| end | |
| end | |
| # TODO: These should be defp's? | |
| def start_buffer do | |
| IO.puts "start buffer" | |
| Agent.start_link(fn -> [] end, name: __MODULE__) | |
| end | |
| def put_buffer(buffer) do | |
| IO.puts "OHAI" | |
| Agent.get_and_update(__MODULE__, {&[buffer|&1], &[buffer|&1]}) | |
| end | |
| def get_buffer do | |
| Agent.get(__MODULE__, &(&1)) | |
| end | |
| def stop_buffer do | |
| Agent.stop(__MODULE__) | |
| end | |
| end | |
| defmodule Foo do | |
| import Html | |
| m = markup do | |
| tag :div do | |
| tag :h1 do | |
| tag :text do | |
| end | |
| end | |
| tag :article do | |
| tag :h2 do | |
| tag :text do | |
| end | |
| end | |
| end | |
| end | |
| end | |
| IO.puts "====================" | |
| IO.inspect m | |
| IO.puts "====================" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment