Skip to content

Commit

Permalink
add lua filter for links to fix #43
Browse files Browse the repository at this point in the history
  • Loading branch information
zkamvar committed Jul 29, 2021
1 parent aeb4af0 commit b914a35
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sandpaper
Title: Create and Curate Carpentries Lessons
Version: 0.0.0.9033
Version: 0.0.0.9034
Authors@R: c(
person(given = "Zhian N.",
family = "Kamvar",
Expand Down
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# sandpaper 0.0.0.9034

NEW FEATURES
------------

- Authors can now cross-link between files within the lesson as they appear in
the lesson instead of trying to guess how the link would appear on the
website. For example, if you wanted to reference `learners/setup.md` in
`episodes/introduction.md`, you would write `[setup](../learners/setup.md)`
and it will be automatically converted to the correct URL in the website
(#43). This is still backwards compatible with the previous iteration of
writing the flattened link (as it would appear on the website).

# sandpaper 0.0.0.9033

NEW FEATURES
Expand Down
51 changes: 49 additions & 2 deletions inst/rmarkdown/lua/lesson.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Set (list)
for _, l in ipairs(list) do set[l] = true end
return set
end

local blocks = Set{
"callout",
"objectives",
Expand Down Expand Up @@ -272,7 +273,53 @@ handle_our_divs = function(el)
return el
end

-- Flatten relative links for HTML output
--
-- 1. removes any leading ../[folder]/ from the links to prepare them for the
-- way the resources appear in the HTML site
-- 2. renames all local Rmd and md files to .html
--
-- NOTE: it _IS_ possible to use variable expansion with this so that we can
-- configure this to do specific things (e.g. decide how we want the
-- architecture of the final site to be)
-- function expand (s)
-- s = s:gsub("$(%w+)", function(n)
-- return _G[n] -- substitute with global variable
-- end)
-- return s
-- end
flatten_links = function(el)
local pat = "^%.%./"
local tgt;
if el.target == nil then
tgt = el.src
else
tgt = el.target
end
-- Flatten local redirects, e.ge. ../episodes/link.md goes to link.md
tgt,_ = tgt:gsub(pat.."episodes/", "")
tgt,_ = tgt:gsub(pat.."learners/", "")
tgt,_ = tgt:gsub(pat.."instructors/", "")
tgt,_ = tgt:gsub(pat.."profiles/", "")
tgt,_ = tgt:gsub(pat, "")
-- rename local markdown/Rmarkdown
-- link.md goes to link.html
-- link.md#section1 goes to link.html#section1
if text.sub(tgt, 1, 5) ~= "http" then
tgt,_ = tgt:gsub("%.R?md(#[%S]+)$", ".html%1")
tgt,_ = tgt:gsub("%.R?md$", ".html")
end
if el.target == nil then
el.src = tgt;
else
el.target = tgt;
end
return el
end

return {
{Meta = get_timings},
{Div = handle_our_divs}
{Meta = get_timings},
{Link = flatten_links},
{Image = flatten_links},
{Div = handle_our_divs}
}
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/render_html.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
,Div ("",["challenge"],[])
[Header 2 ("",[],[]) [Str "Challenge"]
,Para [Str "How",Space,Str "do",Space,Str "you",Space,Str "write",Space,Str "markdown",Space,Str "divs?"]
,Para [Str "This",Space,Link ("",[],[]) [Str "link",Space,Str "should",Space,Str "be",Space,Str "transformed"] ("Setup.html","")]
,Para [Str "This",Space,Link ("",[],[]) [Str "too"] ("Setup.html#windows-setup","windows setup")]
,Para [Image ("",[],[("alt","alt text")]) [Str "link",Space,Str "should",Space,Str "be",Space,Str "transformed"] ("fig/Setup.png","fig:")]
,Div ("",["solution"],[])
[Header 2 ("write-now",[],[]) [Str "Write",Space,Str "now"]
,Para [Str "just",Space,Str "write",Space,Str "it,",Space,Str "silly."]]]
Expand Down Expand Up @@ -96,6 +99,12 @@
<div class="section level2">
<h2>Challenge</h2>
<p>How do you write markdown divs?</p>
<p>This <a href="Setup.html">link should be transformed</a></p>
<p>This <a href="Setup.html#windows-setup" title="windows setup">too</a></p>
<div class="figure">
<img src="fig/Setup.png" alt="alt text" alt="" />
<p class="caption">link should be transformed</p>
</div>
</div>
<div id="write-now" class="section level2 solution">
<h2>Write now</h2>
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ ex <- c("---",
"",
"How do you write markdown divs?",
"",
"This [link should be transformed](../learners/Setup.md)",
"",
"This [too](../learners/Setup.md#windows-setup 'windows setup')",
"",
"![link should be transformed](../episodes/fig/Setup.png){alt='alt text'}",
"",
"::: solution",
"",
"# Write now",
Expand Down

0 comments on commit b914a35

Please sign in to comment.