summaryrefslogtreecommitdiff
path: root/Setup.hs
diff options
context:
space:
mode:
authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2008-02-09 03:20:42 +0000
committerfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2008-02-09 03:20:42 +0000
commit446a964a190edf094871cd4bb4b045e469446223 (patch)
tree106c01a40bfe59056306e4a0d7fcbd1912f6ce07 /Setup.hs
parentc150e84d3c74555d7024c9b0c23a76ef13e73335 (diff)
Incorporated templates phase into Setup.hs and improved templates.
+ Removed templates/Makefile and fillTemplates.pl. + Removed 'templates' Makefile target. + Added postConf hook to Setup.hs that fills the templates. + Use compressed javascripts for S5 and ASCIIMathML templates. + Revised test suite. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1216 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'Setup.hs')
-rw-r--r--Setup.hs84
1 files changed, 83 insertions, 1 deletions
diff --git a/Setup.hs b/Setup.hs
index 9a994af67..2470a527d 100644
--- a/Setup.hs
+++ b/Setup.hs
@@ -1,2 +1,84 @@
import Distribution.Simple
-main = defaultMain
+import Distribution.Simple.Setup
+import Distribution.PackageDescription
+import Distribution.Simple.LocalBuildInfo
+import System.FilePath (combine, joinPath, takeFileName)
+import System.Directory (getDirectoryContents, removeFile)
+import System.IO (readFile, writeFile)
+import Control.Monad (foldM)
+import Data.List (isPrefixOf)
+
+main = defaultMainWithHooks myHooks
+
+myHooks = defaultUserHooks { postConf = myPostConf, postClean = myPostClean }
+
+pandocPath = combine "Text" "Pandoc"
+
+-- Builds Text/Pandoc/ASCIIMathML.hs, Text/Pandoc/Writers/S5.hs, and
+-- Text/Pandoc/Writers/DefaultHeaders.hs from templates and data.
+myPostConf :: Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ()
+myPostConf _ configFlags pkgDescription buildInfo = do
+ putStrLn "Generating source files from templates..."
+ fillAsciiMathMLTemplate
+ fillS5WriterTemplate
+ fillDefaultHeadersTemplate
+
+-- Fill templateFile with data in dataFiles and write to outputFile.
+fillTemplate :: [FilePath] -> FilePath -> FilePath -> IO ()
+fillTemplate dataFiles templateFile outputFile = do
+ template <- readFile (combine "templates" templateFile)
+ filled <- foldM processFile template $ map (combine "templates") dataFiles
+ writeTemplate (combine pandocPath outputFile) filled
+
+fillAsciiMathMLTemplate :: IO ()
+fillAsciiMathMLTemplate =
+ fillTemplate ["ASCIIMathML.js.comment", "ASCIIMathML.js.packed"] "ASCIIMathML.hs" "ASCIIMathML.hs"
+
+fillS5WriterTemplate :: IO ()
+fillS5WriterTemplate =
+ let s5Path = joinPath ["ui", "default"]
+ files = map (combine s5Path) ["slides.js.comment", "slides.js.packed", "s5-core.css",
+ "framing.css", "pretty.css", "opera.css", "outline.css", "print.css"]
+ in fillTemplate files "S5.hs" (combine "Writers" "S5.hs")
+
+fillDefaultHeadersTemplate :: IO ()
+fillDefaultHeadersTemplate = do
+ files <- getDirectoryContents (combine "templates" "headers") >>=
+ return . map (combine "headers") . filter (\x -> not (x `elem` [".",".."]))
+ fillTemplate files "DefaultHeaders.hs" (combine "Writers" "DefaultHeaders.hs")
+
+-- Post-clean: remove the files generated from templates.
+myPostClean :: Args -> CleanFlags -> PackageDescription -> Maybe LocalBuildInfo -> IO ()
+myPostClean _ _ _ _ = do
+ putStrLn "Removing source files generated from templates:"
+ removeGeneratedFile $ joinPath [pandocPath, "ASCIIMathML.hs"]
+ removeGeneratedFile $ joinPath [pandocPath, "Writers", "S5.hs"]
+ removeGeneratedFile $ joinPath [pandocPath, "Writers", "DefaultHeaders.hs"]
+
+-- Remove file and print message.
+removeGeneratedFile :: FilePath -> IO ()
+removeGeneratedFile fpath = do
+ putStrLn $ " " ++ fpath
+ removeFile fpath
+
+-- Write the filled template file and print an explanatory message.
+writeTemplate :: FilePath -> String -> IO ()
+writeTemplate outfile contents = do
+ putStrLn $ " " ++ outfile
+ writeFile outfile contents
+
+-- Read contents of fpath and insert in template replacing @fpath@.
+processFile :: String -> FilePath -> IO String
+processFile template fpath = do
+ contents <- readFile fpath >>= return . show
+ return $ substitute ("@" ++ takeFileName fpath ++ "@") contents template
+
+-- Replace each occurrence of one sublist in a list with another.
+substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
+substitute _ _ [] = []
+substitute [] _ lst = lst
+substitute target replacement lst =
+ if target `isPrefixOf` lst
+ then replacement ++ (substitute target replacement $ drop (length target) lst)
+ else (head lst):(substitute target replacement $ tail lst)
+