Skip to content

Commit

Permalink
Merge pull request #25 from MichaelHatherly/mh/lexbor
Browse files Browse the repository at this point in the history
Switch HTML parser
  • Loading branch information
MichaelHatherly authored Jul 3, 2024
2 parents 43f9142 + ea142ec commit 2729753
Show file tree
Hide file tree
Showing 37 changed files with 6,350 additions and 204 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
margin = 92
remove_extra_newlines = true
11 changes: 4 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ version = "1.3.1"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Match = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf"
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
lexbor_jll = "be8d7a73-1782-5ce7-96ff-8fff21e5e970"

[weakdeps]
CommonMark = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
Expand All @@ -27,13 +26,11 @@ HypertextTemplatesReviseExt = "Revise"

[compat]
AbstractTrees = "0.4"
EzXML = "1"
CEnum = "0.5"
InteractiveUtils = "1.6"
Logging = "1.6"
LoggingExtras = "1"
MacroTools = "0.5"
Match = "2"
PackageExtensionCompat = "1"
Random = "1.6"
TOML = "1"
lexbor_jll = "= 2.3.0"
julia = "1.6"
24 changes: 12 additions & 12 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Let's add a new template function to that `template.html` file:
<function second-template count="1::Integer" value>
<ul>
<for iter="1:count" item>
<li><julia value="item" />: <julia value /></li>
<li><span .julia=item></span>: <span .julia=value></span></li>
</for>
</ul>
</function>
Expand Down Expand Up @@ -109,9 +109,9 @@ You can use one of your template functions in another template with:

```html
<function third-template number>
<example />
<second-template count="number" value='"Hello, world!"' />
<second-template count="2 * number" value='"Goodbye, world!"' />
<example></example>
<second-template count="number" value='"Hello, world!"'></second-template>
<second-template count="2 * number" value='"Goodbye, world!"'></second-template>
</function>
```

Expand Down Expand Up @@ -141,10 +141,10 @@ be called `<base-template>` in other templates that it is used in, and
```html
<html lang="en">
<head>
<title>My Website - <julia value="props.title" /></title>
<title $julia="My Website - $(props.title)"></title>
</head>
<body>
<third-example number="props.number" />
<third-example number="props.number"></third-example>
</body>
</html>
```
Expand Down Expand Up @@ -195,7 +195,7 @@ since it uses that package internally.
<case when="[element]">
<p>
The number is actually a single element vector containing:
<julia value="element" />
<span .julia=element></span>
</p>
</case>
<case when="_">
Expand All @@ -219,7 +219,7 @@ For that we use `<slot>` tags.
<function slot-example>
<div class="...">
<h1>Header</h1>
<slot />
<slot></slot>
<hr />
<p>Footer</p>
</div>
Expand All @@ -241,9 +241,9 @@ rendered in a single template function. For that we have named slots:
<function named-slots-example>
<div class="...">
<h1>Header</h1>
<slot content />
<slot content></slot>
<hr />
<p><slot footer /></p>
<p><slot footer></slot></p>
</div>
</function>
```
Expand Down Expand Up @@ -342,12 +342,12 @@ name = "my-template"
props = ["foo", "bar"]
+++

<large-custom-template prop="foo" />
<large-custom-template prop="foo"></large-custom-template>

# My Template

This is a template that takes two props: `foo` and `bar`. Their values are
<julia value="foo" /> and <julia value="bar" /> respectively.
<span julia="foo"></span> and <span julia="bar"></span> respectively.
```


Expand Down
5 changes: 5 additions & 0 deletions gen/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[deps]
Clang = "40e3b903-d033-50b4-a0cc-940c62c95e31"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
lexbor_jll = "be8d7a73-1782-5ce7-96ff-8fff21e5e970"
82 changes: 82 additions & 0 deletions gen/generator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Pkg
import Pkg.Artifacts
import Clang.Generators
import Clang.Generators.JLLEnvs
import lexbor_jll
import JuliaFormatter

cd(@__DIR__) do
include_dir = normpath(joinpath(lexbor_jll.artifact_dir, "include"))
@assert isdir(include_dir)

options = Generators.load_options(joinpath(@__DIR__, "generator.toml"))

args = Generators.get_default_args()
push!(args, "-I$include_dir")

header_files = [joinpath(include_dir, "lexbor", "html", "html.h")]

ctx = Generators.create_context(header_files, args, options)

Generators.build!(ctx, Generators.BUILDSTAGE_NO_PRINTING)

allowed_functions = Set([
:lxb_html_document_destroy,
:lxb_html_tokenizer_tags_noi,
:lxb_tag_name_by_id_noi,
:lxb_html_tokenizer_destroy,
:lxb_html_tokenizer_create,
:lxb_html_tokenizer_init,
:lxb_html_tokenizer_callback_token_done_set_noi,
:lxb_html_tokenizer_begin,
:lxb_html_tokenizer_chunk,
:lxb_html_tokenizer_end,
:lxb_html_parser_create,
:lxb_html_parser_init,
:lxb_html_parser_destroy,
:lxb_html_parse,
:lxb_html_document_destroy,
:lxb_dom_node_text_content,
:lxb_dom_node_first_child_noi,
:lxb_dom_node_next_noi,
:lxb_dom_node_prev_noi,
:lxb_dom_element_qualified_name,
:lxb_dom_element_first_attribute_noi,
:lxb_dom_attr_value_noi,
:lxb_dom_attr_qualified_name,
:lxb_dom_element_next_attribute_noi,
])

function rewrite!(e::Expr)
if Meta.isexpr(e, :function)
name = e.args[1].args[1]
if name === :lxb_css_syntax_token
e.args[1].args[2] = :($(e.args[1].args[2])::Ptr{lxb_css_syntax_tokenizer_t})
end
if isa(name, Symbol) && name allowed_functions
e.head = :(=)
e.args = [:_, "skip-this-code"]
end
end
return e
end

function rewrite!(dag::Generators.ExprDAG)
for node in Generators.get_nodes(dag)
for expr in Generators.get_exprs(node)
rewrite!(expr)
end
end
end

rewrite!(ctx.dag)

Generators.build!(ctx, Generators.BUILDSTAGE_PRINTING_ONLY)

generated_file = joinpath(@__DIR__, "..", "src", "liblexbor_api.jl")
content = read(generated_file, String)
content = replace(content, "_ = \"skip-this-code\"" => "")
write(generated_file, content)

JuliaFormatter.format_file(generated_file)
end
Loading

0 comments on commit 2729753

Please sign in to comment.