Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 28 lines (23 sloc) 1.04 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
-- Usage: a paragraph containing just [My page](!subst)
-- will be replaced by the contents of My page.
--
-- Limitations: it is assumed that My page is
-- formatted with markdown, and contains no metadata.

module Subst (plugin) where

import Data.FileStore (retrieve)
import Text.Pandoc (defaultParserState, readMarkdown)
import Network.Gitit.ContentTransformer (inlinesToString)
import Network.Gitit.Interface
import Network.Gitit.Framework (filestoreFromConfig)

plugin :: Plugin
plugin = mkPageTransformM substituteIntoBlock

substituteIntoBlock :: [Block] -> PluginM [Block]
substituteIntoBlock (Para (Link ref ("!subst", _):_ ):xs) =
     do let target = inlinesToString ref ++ ".page"
        cfg <- askConfig
        let fs = filestoreFromConfig cfg
        article <- liftIO (retrieve fs target Nothing)
        let (Pandoc _ content) = readMarkdown defaultParserState article
        (content ++) `fmap` substituteIntoBlock xs
substituteIntoBlock (x:xs) = (x:) `fmap` substituteIntoBlock xs
substituteIntoBlock [] = return []
Something went wrong with that request. Please try again.