Skip to content

Commit

Permalink
Add Header and Link support
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed Oct 1, 2019
1 parent 3881371 commit d128cdd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
73 changes: 61 additions & 12 deletions src/Pandoc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Pandoc
import JSON
import Markdown

const PANDOC_EXECUTABLE = get(ENV, "PANDOC_JL_EXECUTABLE", "pandoc")
const PANDOC_JL_EXECUTABLE = get(ENV, "PANDOC_JL_EXECUTABLE", "pandoc")

@enum Alignment AlignLeft=1 AlignRight=2 AlignCenter=3 AlignDefault=4
@enum ListNumberStyle DefaultStyle=1 Example=2 Decimal=3 LowerRoman=4 UpperRoman=5 LowerAlpha=6 UpperAlpha=7
Expand Down Expand Up @@ -38,6 +38,7 @@ struct Attributes
return new(identifier, classes, attributes)
end
end
Attributes() = Attributes("", [], [],)

struct Citation
mode::CitationMode
Expand Down Expand Up @@ -251,13 +252,13 @@ mutable struct Document
pandoc_api_version::VersionNumber
meta::Dict{String, Any}
blocks::Vector{Element}
end

function Document(data)
pav = pandoc_api_version(data["pandoc-api-version"])
meta = data["meta"]
blocks = get_elements(data["blocks"])
return new(data, pav, meta, blocks)
end
function Document(data)
pav = pandoc_api_version(data["pandoc-api-version"])
meta = data["meta"]
blocks = get_elements(data["blocks"])
return new(data, pav, meta, blocks)
end

Base.show(io::IO, e::Unknown) = print(io, """$(typeof(e))(
Expand Down Expand Up @@ -612,15 +613,63 @@ function get_elements(blocks)
return elements
end

function run(filename)
cmd = `$PANDOC_EXECUTABLE -t json $filename`
function parse(markdown)
cmd = pipeline(`echo $markdown`, `$PANDOC_JL_EXECUTABLE -t json`)
data = read(cmd, String)
return Document(JSON.parse(data))
end

function parse_file(filename)
cmd = `$PANDOC_JL_EXECUTABLE -t json $filename`
data = read(cmd, String)
return Document(JSON.parse(data))
end

function exists()
p = run(`which $PANDOC_EXECUTABLE`, wait=false)
return success(p)
exists() = run(`which $PANDOC_JL_EXECUTABLE`, wait=false) |> success

### Convert

function Base.convert(::Type{Document}, md::Markdown.MD)

pav = v"1.17.5-4"
meta = Dict{String, Any}()
data = Dict{String, Any}()
blocks = Element[]

for e in md.content
push!(blocks, convert(Element, e))
end

return Document(data, pav, meta, blocks)

end

Base.convert(::Type{Str}, e::AbstractString) = Str(e)
Base.convert(::Type{Element}, e::AbstractString) = convert(Str, e)
Base.convert(::Type{Header}, e::Markdown.Header{V}) where V = Header(
V #= level =#,
Attributes(),
Element[x for x in e.text],
)
Base.convert(::Type{Element}, e::Markdown.Header) = convert(Header, e)

function Base.convert(::Type{Link}, e::Markdown.Link)

content = Inline[]
for s in split(e.text[1])
push!(content, convert(Str, s))
push!(content, Space())
end
pop!(content) # remove last Space()

target = Target(e.url, "")

return Link(
Attributes(),
content,
target,
)
end
Base.convert(::Type{Element}, e::Markdown.Link) = convert(Link, e)

end # module
6 changes: 6 additions & 0 deletions test/data/simple.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Headers
=======

Level 2 with an [embedded link](/url)
-------------------------------------

3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Test

using Pandoc

const Markdown = Pandoc.Markdown
using Markdown

BASE_DIR = abspath(joinpath(dirname(Base.find_package("Pandoc")), ".."))

Expand Down
24 changes: 15 additions & 9 deletions test/test_pandoc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,44 @@

end

@testset "test conversions" begin

@test typeof(convert(Pandoc.Header, Markdown.parse("Headers\n=======").content[1])) == Pandoc.Header

end

@testset "test pandoc parser" begin

@testset "docbook" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "docbook-reader.docbook"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "docbook-reader.docbook"))) == Pandoc.Document
end

@testset "creole" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "creole-reader.txt"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "creole-reader.txt"))) == Pandoc.Document
end

@testset "markdown" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "markdown-reader-more.txt"))) == Pandoc.Document
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "writer.markdown"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "markdown-reader-more.txt"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "writer.markdown"))) == Pandoc.Document
end

@testset "wiki" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "mediawiki-reader.wiki"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "mediawiki-reader.wiki"))) == Pandoc.Document
end

@testset "ipynb" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "simple.ipynb"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "simple.ipynb"))) == Pandoc.Document
end

@testset "latex" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "latex-reader.latex"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "latex-reader.latex"))) == Pandoc.Document
end

@testset "man" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "man-reader.man"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "man-reader.man"))) == Pandoc.Document
end

@testset "tables" begin
@test typeof(Pandoc.run(joinpath(@__DIR__, "data", "pipe-tables.txt"))) == Pandoc.Document
@test typeof(Pandoc.parse_file(joinpath(@__DIR__, "data", "pipe-tables.txt"))) == Pandoc.Document
end
end

0 comments on commit d128cdd

Please sign in to comment.