Skip to content

Commit

Permalink
Update Documenter -> v1
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Sep 20, 2023
1 parent 688f932 commit 824fe12
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 39 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
name = "DocumenterDiagrams"
uuid = "a106ebf2-4182-4cba-90d4-44cd3cc36e85"
authors = ["Pedro Maciel Xavier <pedromxavier@poli.ufrj.br>"]
version = "0.27.0"
version = "1.0.0"

[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Kroki = "b3565e16-c1f2-4fe9-b4ab-221c88942068"
MarkdownAST = "d0879d2d-cac2-40c8-9cee-1863dc0c7391"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[compat]
Documenter = "~0.27"
Documenter = "1"
Kroki = "0.2"
TranscodingStreams = "=0.9.11"
julia = "1.6"
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterDiagrams = "a106ebf2-4182-4cba-90d4-44cd3cc36e85"

[compat]
Documenter = "~0.27"
Documenter = "1"
3 changes: 2 additions & 1 deletion docs/src/api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# DocumenterDiagrams.jl API

```@docs
DocumenterDiagrams.DiagramBlocks
DocumenterDiagrams.DiagramBlock
DocumenterDiagrams.DiagramExpander
```
74 changes: 47 additions & 27 deletions src/DocumenterDiagrams.jl
Original file line number Diff line number Diff line change
@@ -1,57 +1,77 @@
module DocumenterDiagrams

import Kroki
import Documenter.Utilities.DOM: DOM, Node
import Documenter.Utilities.Markdown2: CodeBlock
import Documenter.Utilities.MDFlatten: mdflatten
import Documenter.Expanders: ExpanderPipeline, iscode
import MarkdownAST: Node, CodeBlock
import Documenter
import Documenter.DOM
import Documenter.MDFlatten: mdflatten
import Documenter.Selectors: order, matcher, runner
import Documenter.Writers.HTMLWriter: domify
import Documenter.Writers.LaTeXWriter: Context, latex
import Documenter.HTMLWriter: DCtx, domify

# Data

"""
DiagramExpander
Similar to the `RawBlocks` expander, but generates diagrams instead.
"""
abstract type DiagramBlocks <: ExpanderPipeline end
abstract type DiagramExpander <: Documenter.Expanders.ExpanderPipeline end

struct DiagramNode
"""
DiagramBlock
"""
struct DiagramBlock <: Documenter.AbstractDocumenterBlock
codeblock::CodeBlock
format::Symbol
code::String
end

# Parsing
order(::Type{DiagramExpander}) = 10.5

order(::Type{DiagramBlocks}) = 10.5

function matcher(::Type{DiagramBlocks}, node, page, doc)
iscode(node, r"^@diagram")
function matcher(::Type{DiagramExpander}, node, page, doc)
return Documenter.iscode(node, r"^@diagram")
end

function runner(::Type{DiagramBlocks}, x, page, doc)
m = match(r"@diagram[ ](.+)$", x.language)
m === nothing && error("invalid '@diagram <format>' syntax: $(x.language)")
function runner(::Type{DiagramExpander}, node, page, doc)
block = node.element

m = match(r"@diagram[ ](.+)$", block.info)

if isnothing(m)
error("invalid '@diagram <format>' syntax: $(block.info)")
end

page.mapping[x] = DiagramNode(Symbol(m[1]), x.code)
format = Symbol(m[1])

node.element = DiagramBlock(
block, # codeblock
format, # format
block.code, # code
)

return nothing
end

# Output
function domify(::DCtx, node::Node, ::DiagramBlock)
block = node.element

raw_svg = String(Kroki.render(Kroki.Diagram(block.format, block.code), "svg"))

function mdflatten(io, node::Node, e::DiagramNode)
mdflatten(io, node, CodeBlock("@diagram $(e.format)", e.code))
return DOM.Tag(Symbol("#RAW#"))(raw_svg)
end

function domify(ctx, navnode, diag::DiagramNode)
raw_svg = String(Kroki.render(Kroki.Diagram(diag.format, diag.code), "svg"))
# function mdflatten(io, node::Node, e::DiagramBlock)
# # mdflatten(io, node, CodeBlock("@diagram $(e.format)", e.code))
# return nothing
# end

DOM.Tag(Symbol("#RAW#"))(raw_svg)
end
abstract type DiagramBuilder <: Documenter.Builder.DocumentPipeline end

order(::Type{DiagramBuilder}) = 8.1

function latex(io::Context, ::Node, diag::DiagramNode)
# TODO: Not sure about this.
# IDEA: write figure as png/pdf and _println \includegraphics since svg (default) is not very LaTeX-friendly
nothing
function runner(::Type{DiagramBuilder}, doc::Documenter.Document)
return nothing
end

end # module DocumenterDiagrams
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Documenter = "~0.27"
Documenter = "1"
julia = "1.6"
11 changes: 10 additions & 1 deletion test/docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# DocumenterDiagrams.jl (TEST)

## Diagram (TEST)

```@diagram ditaa
+------------------------+ +-------+
| DocumenterDiagrams.jl |--->| TEST |
+------------------------+ +-------+
```
```

## API (TEST)

```@docs
DocumenterDiagrams.DiagramExpander
DocumenterDiagrams.DiagramBlock
```
14 changes: 8 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ using DocumenterDiagrams
const DOCS_PATH = joinpath(@__DIR__, "docs")

function main()
@time include(joinpath(DOCS_PATH, "make.jl"))
include(joinpath(DOCS_PATH, "make.jl"))

@test isdir(joinpath(DOCS_PATH, "build"))
@test isfile(joinpath(DOCS_PATH, "build", "index.html"))
@testset "* DocumenterDiagrams.jl Test Suite *" begin
@test isdir(joinpath(DOCS_PATH, "build"))
@test isfile(joinpath(DOCS_PATH, "build", "index.html"))

html = read(joinpath(DOCS_PATH, "build", "index.html"), String)
html = read(joinpath(DOCS_PATH, "build", "index.html"), String)

@test !isnothing(match(r"\<svg.*\>.*(DocumenterDiagrams).*\<\/svg\>", html))
@test !isnothing(match(r"\<svg.*\>.*(DocumenterDiagrams).*\<\/svg\>", html))

rm(joinpath(DOCS_PATH, "build"); recursive=true, force=true)
rm(joinpath(DOCS_PATH, "build"); recursive=true, force=true)
end

return nothing
end
Expand Down

0 comments on commit 824fe12

Please sign in to comment.