Last active
February 23, 2021 07:10
-
-
Save mathematicalcoffee/e4f25350449e6004014f to your computer and use it in GitHub Desktop.
Pandoc extension to insert the metadata of a file as headings of levels 1, 2 and 3. This is so that if you have (say) each chapter of a thesis in its own file with its own title block, you can still compile the lot together to the one final thesis. See the file for more details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env runhaskell | |
-- Filter that adds the metadata block as h1, h2, h3 (title, author, date). | |
-- | |
-- Usage:: | |
-- chmod +x ./chapter.hs | |
-- | |
-- # (where each chapter is in its own md file and I want to compile them | |
-- # into one document preserving the title/author/date for each chapter): | |
-- | |
-- for f in Chapter*.md; do \ | |
-- pandoc --filter ./chapter.hs $f; \ | |
-- echo ""; \ | |
-- done | pandoc --standalone | |
-- | |
-- As a one-liner for easy copy/paste:: | |
-- | |
-- for f in Chapter*.md; do pandoc --filter ./chapter.hs $f; echo ""; done | pandoc --standalone | |
-- | |
-- Add your pandoc switches as necessary. For example I'd have a file | |
-- title.txt with the title block for the entire document and add that in | |
-- to the last pandoc command to set the metadata for the entire document. | |
-- | |
-- See [1] for writing pandoc extensions. | |
-- This script is hosted on gist [2]. | |
-- [1]: http://johnmacfarlane.net/pandoc/scripting.html | |
-- [2]: https://gist.github.com/mathematicalcoffee/e4f25350449e6004014f | |
-- Amy Chan, May 2014. | |
import Text.Pandoc.JSON | |
import Data.List ( intercalate ) | |
main :: IO () | |
main = toJSONFilter insertMeta | |
where insertMeta (Pandoc meta xs) = Pandoc meta xs' | |
where xs' = [ Header 1 ("",["title"],[]) (docTitle meta) | |
, Header 2 ("",["author"],[]) (intercalate [Str ";", Space] (docAuthors meta)) | |
, Header 3 ("",["date"],[]) (docDate meta) ] ++ xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you write this:
Can you explain how that works, exactly? I can't seem to achieve your result. If I add a
title.txt
to the last pandoc command, pandoc completely ignores input passed to it via the pipe and only parses thetitle.txt
file. (More info on approaches I've tried here)