-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Codegen | ||
using ..AST | ||
|
||
abstract type CodegenTarget end | ||
|
||
function codegen end | ||
codegen(t::Type{T}, ast) where T<:CodegenTarget = codegen(t(), ast) | ||
|
||
include("codegen/html.jl") | ||
using .HTMLCodegen | ||
|
||
export codegen, HTMLTarget | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module HTMLCodegen | ||
using AbstractTrees | ||
using Hyperscript | ||
using ..AST | ||
import ..CodegenTarget | ||
import ..codegen | ||
|
||
struct HTMLTarget <: CodegenTarget end | ||
codegen(t::HTMLTarget, node::AST.Node{AST.NorgDocument}) = m("div", class="norg", [codegen(t, c) for c ∈ children(node)]) | ||
|
||
function codegen(t::HTMLTarget, node::AST.Node{AST.Paragraph}) | ||
res = [] | ||
for c in children(node) | ||
append!(res, codegen(t, c)) | ||
push!(res, m("br")) | ||
end | ||
if !isempty(res) | ||
pop!(res) # remove last <br> | ||
end | ||
m("p", res) | ||
end | ||
|
||
function codegen(t::HTMLTarget, node::AST.Node{AST.ParagraphSegment}) | ||
res = [] | ||
for c in children(node) | ||
push!(res, codegen(t, c)) | ||
end | ||
res | ||
end | ||
|
||
html_node(::AST.Node{AST.Bold}) = "b" | ||
html_node(::AST.Node{AST.Italic}) = "i" | ||
html_node(::AST.Node{AST.Underline}) = "ins" | ||
html_node(::AST.Node{AST.Strikethrough}) = "del" | ||
html_node(::AST.Node{AST.Spoiler}) = "span" | ||
html_node(::AST.Node{AST.Superscript}) = "sup" | ||
html_node(::AST.Node{AST.Subscript}) = "sub" | ||
html_node(::AST.Node{AST.InlineCode}) = "code" | ||
|
||
html_class(::AST.Node{AST.Bold}) = [] | ||
html_class(::AST.Node{AST.Italic}) = [] | ||
html_class(::AST.Node{AST.Underline}) = [] | ||
html_class(::AST.Node{AST.Strikethrough}) = [] | ||
html_class(::AST.Node{AST.Spoiler}) = ["spoiler"] | ||
html_class(::AST.Node{AST.Superscript}) = [] | ||
html_class(::AST.Node{AST.Subscript}) = [] | ||
html_class(::AST.Node{AST.InlineCode}) = [] | ||
|
||
function codegen(t::HTMLTarget, node::AST.Node{<:AST.AttachedModifier}) | ||
res = [] | ||
for c in children(node) | ||
push!(res, codegen(t, c)) | ||
end | ||
class = html_class(node) | ||
if isempty(class) | ||
m(html_node(node), res) | ||
else | ||
m(html_node(node), class=join(html_class(node), " "), res) | ||
end | ||
end | ||
|
||
codegen(::HTMLTarget, node::AST.Node{AST.Word}) = node.data.value | ||
codegen(::HTMLTarget, node::AST.Node{AST.Escape}) = node.data.value | ||
|
||
export HTMLTarget | ||
|
||
end |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,6 @@ function tokenize(input::AbstractString) | |
result | ||
end | ||
|
||
export tokenize | ||
|
||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
@testset "Test paragraphs" begin | ||
s = "Hi I am first paragraph.\n\nOh, hello there, I am second paragraph !" | ||
html = Norg.codegen(Norg.HTMLTarget, Norg.parse_norg(Norg.tokenize(s))) | ||
pars = getfield(html, :children) | ||
@test getfield(pars[1], :tag) == "p" | ||
@test getfield(pars[2], :tag) == "p" | ||
end | ||
|
||
simple_markups_nodes = [ | ||
('*', "b"), | ||
('/', "i"), | ||
('_', "ins"), | ||
('-', "del"), | ||
('!', "span"), | ||
('^', "sup"), | ||
(',', "sub"), | ||
('`', "code"), | ||
] | ||
|
||
simple_markups_class = [ | ||
('*', nothing), | ||
('/', nothing), | ||
('_', nothing), | ||
('-', nothing), | ||
('!', "spoiler"), | ||
('^', nothing), | ||
(',', nothing), | ||
('`', nothing) | ||
] | ||
|
||
@testset "Test correct markup for $m" for (m,html_node) in simple_markups_nodes | ||
s = "$(m)inner$(m)" | ||
html = Norg.codegen(Norg.HTMLTarget, Norg.parse_norg(Norg.tokenize(s))) | ||
b = first(getfield(first(getfield(html, :children)), :children)) | ||
@test getfield(b, :tag) == html_node | ||
end | ||
|
||
@testset "Test correct class for $m" for (m,html_class) in simple_markups_class | ||
s = "$(m)inner$(m)" | ||
html = Norg.codegen(Norg.HTMLTarget, Norg.parse_norg(Norg.tokenize(s))) | ||
b = first(getfield(first(getfield(html, :children)), :children)) | ||
if isnothing(html_class) | ||
@test !haskey(getfield(b, :attrs), "class") | ||
else | ||
@test haskey(getfield(b, :attrs), "class") | ||
@test getfield(b, :attrs)["class"] == html_class | ||
end | ||
end |