Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 50 lines (45 sloc) 1.513 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#!/usr/local/bin/runhaskell
import Text.Hastache
import Text.Hastache.Context
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
import System.Process (readProcess)
import System.Directory (setCurrentDirectory, getCurrentDirectory)
import System.FilePath ((</>))
import Data.List (span, lines, intersperse)
import Data.Char (isSpace)

main = do
    res <- hastacheFile defaultConfig "README.md.ha"
        (mkStrContext context)
    TL.writeFile "README.md" res
    where
    context "example" = MuLambdaM $ \fn -> do
        cd <- setExampleDir
        fc <- readFile $ exampleFile fn
        let { forTC = case span (\t -> trim t /= be) (lines $ trim fc) of
            (a,[]) -> a
            (_,a) -> drop 1 a }
        let explText = concat $ intersperse "\n" forTC
        
        setCurrentDirectory cd
        return $ concat [
              "```haskell\n"
            , explText
            , "\n```"
            ]

    context "runExample" = MuLambdaM $ \fn -> do
        cd <- setExampleDir
        explRes <- readProcess "runhaskell" [exampleFile fn] []
        setCurrentDirectory cd
        return $ concat [
              "```\n"
            , trim explRes
            , "\n```"
            ]

    be = "-- begin example"
    setExampleDir = do
        cd <- getCurrentDirectory
        setCurrentDirectory $ cd </> "examples"
        return cd
    exampleFile fn = decodeStr fn ++ ".hs"

trim :: String -> String
trim = f . f where f = reverse . dropWhile isSpace
Something went wrong with that request. Please try again.