Skip to content

Commit d434e35

Browse files
authored
Change filename structure of generated image files (#205)
When executing markdown (Literate.markdown(...; execute=true) images where previously saved to files where the name was based on the hash of the source block. This patch changes this such that files instead follow the format {name}-{blocknumber}.(svg|png|...). Closes #204.
1 parent 9adfd9a commit d434e35

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Changed
10+
- Image filenames resulting from executing markdown files
11+
(`Literate.markdown(...; execute=true)`) have changed from a number based on
12+
the hash of the source block to the format
13+
`{name}-{blocknumber}.(svg|png|...)`. ([#204][github-204],
14+
[#205][github-205])
915

1016
## [2.13.4] - 2022-06-03
1117
### Fixed
@@ -230,6 +236,8 @@ https://discourse.julialang.org/t/ann-literate-jl/10651 for release announcement
230236
[github-194]: https://github.com/fredrikekre/Literate.jl/pull/194
231237
[github-195]: https://github.com/fredrikekre/Literate.jl/pull/195
232238
[github-197]: https://github.com/fredrikekre/Literate.jl/issues/197
239+
[github-204]: https://github.com/fredrikekre/Literate.jl/issues/204
240+
[github-205]: https://github.com/fredrikekre/Literate.jl/pull/205
233241

234242
[Unreleased]: https://github.com/fredrikekre/Literate.jl/compare/v2.13.4...HEAD
235243
[2.13.4]: https://github.com/fredrikekre/Literate.jl/compare/v2.13.3...v2.13.4

src/Literate.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,7 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
553553
# create the markdown file
554554
sb = sandbox()
555555
iomd = IOBuffer()
556-
continued = false
557-
for chunk in chunks
556+
for (chunknum, chunk) in enumerate(chunks)
558557
if isa(chunk, MDChunk)
559558
for line in chunk.lines
560559
write(iomd, line.second, '\n') # skip indent here
@@ -587,7 +586,9 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
587586
inputfile=config["literate_inputfile"],
588587
fake_source=config["literate_outputfile"],
589588
flavor=config["flavor"],
590-
image_formats=config["image_formats"])
589+
image_formats=config["image_formats"],
590+
file_prefix="$(config["name"])-$(chunknum)",
591+
)
591592
end
592593
end
593594
end
@@ -604,7 +605,7 @@ end
604605

605606
function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
606607
inputfile::String, fake_source::String,
607-
flavor::AbstractFlavor, image_formats::Vector)
608+
flavor::AbstractFlavor, image_formats::Vector, file_prefix::String)
608609
# TODO: Deal with explicit display(...) calls
609610
r, str, _ = execute_block(sb, block; inputfile=inputfile, fake_source=fake_source)
610611
# issue #101: consecutive codefenced blocks need newline
@@ -621,7 +622,7 @@ function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
621622
end
622623
for (mime, ext) in image_formats
623624
if Base.invokelatest(showable, mime, r)
624-
file = string(hash(block) % UInt32) * ext
625+
file = file_prefix * ext
625626
open(joinpath(outputdir, file), "w") do io
626627
Base.invokelatest(show, io, mime, r)
627628
end

test/runtests.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ const GITLAB_ENV = Dict(
291291
(k => nothing for k in keys(TRAVIS_ENV))...,
292292
(k => nothing for k in keys(ACTIONS_ENV))...,
293293
)
294+
294295
@testset "Literate.script" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
295296
mktempdir(@__DIR__) do sandbox
296297
cd(sandbox) do
@@ -833,9 +834,9 @@ end end
833834
markdown = read(joinpath(outdir, "inputfile.md"), String)
834835
@test occursin("```\n2\n```", markdown) # text/plain
835836
@test occursin("```\n2×2 $(Matrix{Int}):\n 1 2\n 3 4\n```", markdown) # text/plain
836-
@test occursin(r"!\[\]\(\d+\.png\)", markdown) # image/png
837-
@test occursin(r"!\[\]\(\d+\.jpeg\)", markdown) # image/jpeg
838-
@test occursin(r"!\[\]\(\d+\.svg\)", markdown) # image/svg+xml, fredrikekre/Literate.jl#182
837+
@test occursin(r"!\[\]\(inputfile-5\.png\)", markdown) # image/png
838+
@test occursin(r"!\[\]\(inputfile-6\.jpeg\)", markdown) # image/jpeg
839+
@test occursin(r"!\[\]\(inputfile-7\.svg\)", markdown) # image/svg+xml, fredrikekre/Literate.jl#182
839840
@test occursin("# MD", markdown) # text/markdown
840841
@test occursin("```@raw html\n<h1>MD</h1>\n```", markdown) # text/html
841842
@test occursin("```\nPlain\n```", markdown) # text/plain, fredrikekre/Literate#187

0 commit comments

Comments
 (0)