Skip to content

Commit

Permalink
Merge pull request #40 from Klafyvel/code-analysis
Browse files Browse the repository at this point in the history
WIP: Code analysis
  • Loading branch information
Klafyvel authored Sep 19, 2023
2 parents 46791dd + 942bafd commit 5f03e26
Show file tree
Hide file tree
Showing 54 changed files with 3,053 additions and 1,881 deletions.
4 changes: 1 addition & 3 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
style = "sciml"
format_docstrings = true
margin = 80
style = "blue"
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
JULIA=$(shell which julia)
TEST_PROCEDURE="import Pkg;Pkg.test()"

format:
$(JULIA) format_project.jl

test%:
$(JULIA) $* --project -e $(TEST_PROCEDURE) 2&> test-$*.log

testall: test+lts test+beta test+release

clean:
rm test*.log

.PHONY: format, test, testall, clean
18 changes: 14 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,31 @@ version = "0.3.0"
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
HypertextLiteral = "ac1192a8-f4b3-4bfe-ba22-af5b92cd3ab2"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[weakdeps]
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[extensions]
TimeZonesExt = "TimeZones"

[compat]
AbstractTrees = "0.4"
OrderedCollections = "1"
SnoopPrecompile = "1"
HypertextLiteral = "0.9"
SnoopPrecompile = "1"
TimeZones = "1"
julia = "1"

[extras]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Gumbo = "708ec375-b3d6-5a57-a7ce-8257bf98657a"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
pandoc_jll = "c5432543-76ad-5c9d-82bf-db097047a5e2"

[targets]
test = ["Test"]
test = ["AbstractTrees", "Aqua", "Gumbo", "JET", "JSON", "Test", "pandoc_jll"]
95 changes: 48 additions & 47 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ end;
md_path = joinpath(@__DIR__, "src", "1.0-specification.md")
ast = norg(s)
function mk_toc(ast)
toc_tree = filter(!isnothing, [mk_toc(ast, c) for c in children(ast.root)])
return toc_tree = filter(!isnothing, [mk_toc(ast, c) for c in children(ast.root)])
end
function mk_toc(ast, node)
c = children(node)
if !Norg.AST.is_heading(node)
nothing
else
h, node_children... = c
else
h, node_children... = c
level = Norg.AST.heading_level(node)
(title=Norg.Codegen.textify(ast, h),
level = level,
children=filter([mk_toc(ast, c) for c in node_children]) do c
(
title=Norg.Codegen.textify(ast, h),
level=level,
children=filter([mk_toc(ast, c) for c in node_children]) do c
if isnothing(c)
false
elseif c.level >= 3
Expand All @@ -31,16 +32,14 @@ function mk_toc(ast, node)
else
true
end
end
end,
)
end
end
toc = mk_toc(ast)
function mk_html_toc(toc_elem)
href = "#"*"h$(toc_elem.level)-"*Norg.Codegen.idify(toc_elem.title)
lis = [
@htl("<li>$(mk_html_toc(t))</li>") for t in toc_elem.children
]
href = "#" * "h$(toc_elem.level)-" * Norg.Codegen.idify(toc_elem.title)
lis = [@htl("<li>$(mk_html_toc(t))</li>") for t in toc_elem.children]

@htl """<a href=$href>$(toc_elem.title)</a>
<ul>
Expand All @@ -53,52 +52,54 @@ lis = [@htl("<li>$(mk_html_toc(c))</li>") for c in toc]
toc_html = @htl """<ul>$lis</ul>"""

open(md_path, "w") do f
write(f, """This is an automated rendering of the [norg specification](https://github.com/nvim-neorg/norg-specs) using Norg.jl.
write(
f,
"""This is an automated rendering of the [norg specification](https://github.com/nvim-neorg/norg-specs) using Norg.jl.
# Table of contents
""")
# Table of contents
""",
)
write(f, "```@raw html\n")
write(f, string(toc_html))
write(f, "\n")
write(f, string(norg(Norg.HTMLTarget(), s)))
write(f, "\n```")
end

DocMeta.setdocmeta!(Norg, :DocTestSetup, :(using Norg); recursive = true)
DocMeta.setdocmeta!(Norg, :DocTestSetup, :(using Norg); recursive=true)

makedocs(;
modules = [Norg],
authors = "Hugo Levy-Falk <hugo@klafyvel.me> and contributors",
repo = "https://github.com/Klafyvel/Norg.jl/blob/{commit}{path}#{line}",
sitename = "Norg.jl",
format = Documenter.HTML(;
prettyurls = true,
canonical = "https://klafyvel.github.io/Norg.jl",
edit_link = "main",
assets = String[]),
pages = [
"Home" => "index.md",
"Specification" => "1.0-specification.md",
"Internals" => [
"How parsing works" => "internals.md",
"Private API" => [
"internals/kinds.md"
"internals/tokens.md"
"internals/scanners.md"
"internals/match.md"
"internals/parser.md"
"Code generation" => [
"internals/codegen/index.md"
"Targets" => [
"internals/codegen/html.md"
"internals/codegen/json.md"
]
modules=[Norg],
authors="Hugo Levy-Falk <hugo@klafyvel.me> and contributors",
repo="https://github.com/Klafyvel/Norg.jl/blob/{commit}{path}#{line}",
sitename="Norg.jl",
format=Documenter.HTML(;
prettyurls=true,
canonical="https://klafyvel.github.io/Norg.jl",
edit_link="main",
assets=String[],
),
pages=[
"Home" => "index.md",
"Specification" => "1.0-specification.md",
"Internals" => [
"How parsing works" => "internals.md",
"Private API" => [
"internals/kinds.md"
"internals/tokens.md"
"internals/scanners.md"
"internals/match.md"
"internals/parser.md"
"Code generation" => [
"internals/codegen/index.md"
"Targets" => [
"internals/codegen/html.md"
"internals/codegen/json.md"
]
]
]
])
],
],
],
)


deploydocs(;
repo = "github.com/Klafyvel/Norg.jl",
devbranch = "main")
deploydocs(; repo="github.com/Klafyvel/Norg.jl", devbranch="main")
21 changes: 21 additions & 0 deletions ext/TimeZonesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module TimeZonesExt
using Dates, TimeZones, Norg

@static if VERSION v"1.9"
Norg.HAS_TIMEZONES_CAPABILITIES = true
end

function parse_timezone(::Val{:extension}, w)
timezone = nothing
try
timezone = TimeZone(w)
catch e
if e isa ArgumentError
@warn "Unable to process timezone" w
else
rethrow(e)
end
end
return timezone
end
end
32 changes: 32 additions & 0 deletions format_project.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Pkg: Pkg
Pkg.add("JuliaFormatter")
using TOML, JuliaFormatter
format(".")
projecttoml = TOML.parsefile("Project.toml")
const _project_key_order = [
"name",
"uuid",
"keywords",
"license",
"desc",
"deps",
"weakdeps",
"extensions",
"compat",
"extras",
"targets",
]
function project_key_order(key::String)
return something(
findfirst(x -> x == key, _project_key_order), length(_project_key_order) + 1
)
end

function print_project(io, dict)
return TOML.print(io, dict; sorted=true, by=key -> (project_key_order(key), key))
end

open("Project.toml", "w") do io
@info "whoh" io
write(io, sprint(print_project, projecttoml))
end
20 changes: 14 additions & 6 deletions src/Norg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Path to the Norg semantics specification.
"""
const NORG_SEMANTICS_PATH = joinpath(NORG_SPEC_ROOT, "1.0-semantics.norg")

@static if VERSION < v"1.9"
HAS_TIMEZONES_CAPABILITIES = true
else
HAS_TIMEZONES_CAPABILITIES = false
end

using AbstractTrees

include("kind.jl")
Expand All @@ -62,7 +68,6 @@ include("semantics/timestamps.jl")
include("codegen.jl")
using .Codegen


"""
norg([codegentarget, ] s)
Expand All @@ -83,8 +88,8 @@ julia> norg(HTMLTarget(), "* Hello world!")
<div class="norg"><section id="section-h1-hello-world"><h1 id="h1-hello-world">Hello world&#33;</h1></section><section class="footnotes"><ol></ol></section></div>
```
"""
norg(s)= parse_norg(tokenize(s))
norg(t::T, s) where {T <: Codegen.CodegenTarget} = codegen(t, norg(s))
norg(s) = parse_norg(tokenize(s))
norg(t::T, s) where {T<:Codegen.CodegenTarget} = codegen(t, norg(s))

"""
Easily parse Norg string to an AST. This can be used in *e.g.* Pluto notebooks,
Expand All @@ -105,12 +110,12 @@ NorgDocument
└─ Example
```
"""
macro norg_str(s, t ...)
norg(s)
macro norg_str(s, t...)
return norg(s)
end

function Base.show(io::IO, ::MIME"text/html", ast::AST.NorgDocument)
print(io, codegen(HTMLTarget(), ast))
return print(io, codegen(HTMLTarget(), ast))
end

using SnoopPrecompile
Expand Down Expand Up @@ -202,4 +207,7 @@ end
export HTMLTarget, JSONTarget
export @norg_str, norg

if !isdefined(Base, :get_extension)
include("../ext/TimeZonesExt.jl")
end
end
4 changes: 1 addition & 3 deletions src/assets/norg_line_ending.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@
All the UTF-8 characters that Norg specifies as a whitespace.
"""
const NORG_LINE_ENDING = String[
string(Char(0x000A)),
string(Char(0x000D)),
String([Char(0x000D), Char(0x000A)]),
string(Char(0x000A)), string(Char(0x000D)), String([Char(0x000D), Char(0x000A)])
]
Loading

0 comments on commit 5f03e26

Please sign in to comment.