Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using $source$ inside Markdown files #219

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions support/shake/app/Shake/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,26 @@ buildMarkdown refs modname input output = do
| length modname > 24 = '…':reverse (take 24 (reverse modname))
| otherwise = modname

variables =
[ ("title", title)
, ("source", permalink)
]

addVars :: (Text -> String -> b -> b) -> b -> b
addVars f a = foldl' (\a (k, v) -> f k v a) a variables

mStr = MetaString . Text.pack
patchMeta
= Meta
. Map.insert "title" (mStr title)
. Map.insert "source" (mStr permalink)
SquidDev marked this conversation as resolved.
Show resolved Hide resolved
. addVars (\k v -> Map.insert k (mStr v))
. Map.insert "bibliography" (mStr bibliographyName)
. Map.insert "link-citations" (MetaBool True)
. unMeta

(markdown, references) <- liftIO do
contents <- Text.readFile input
-- Perform a basic variable substitution on the file. We don't really want to use
-- Pandoc's full templating system, as the $xyz$ variable syntax conflicts with LaTeX.
contents <- addVars (\k v -> Text.replace ("$" <> k <> "$") (Text.pack v)) <$> Text.readFile input
either (fail . show) pure =<< runIO do
Pandoc meta markdown <- readMarkdown def { readerExtensions = getDefaultExtensions "markdown" } [(input, contents)]
let pandoc = Pandoc (patchMeta meta) markdown
Expand Down