#!/usr/bin/env runhaskell -- This script is in the public domain; do as you want. -- Eric Kow 2007-01-06 -- Making the print version of the Haskell tutorial wikibook can be a -- slight pain, so it's nice to automate it. Note that to use this script, -- you'll need to have a copy of MVS -- Setting it up -- ------------- -- mkdir haskell-wikibook -- cp PrintVersion.hs haskell-wikibook -- cd haskell-wikibook -- mvs login -u your_user_name -p your_password -l en -d wikibooks.org -- Running it -- ---------- -- cd haskell-wikibook -- ./PrintVersion.hs -- mvs commit -m "update print version" Haskell/Print_version.wiki module Main where import Data.List (isInfixOf) import Data.Maybe (mapMaybe) import System.Process (runCommand, waitForProcess) main :: IO () main = withMvs "login" $ do ps <- extractFrom navFile -- parts cs <- mapM (extractFrom.partPath) ps -- chapters writeFile printVersionFile $ toPrintVersion ps cs where extractFrom f = withMvs ("update " ++ f) $ extract `fmap` readFile f withMvs cmd' a = do let cmd = "mvs " ++ cmd' x <- waitForProcess =<< runCommand cmd x `seq` a -- we're being sloppy, and don't care what mvs does navFile, printVersionFile :: FilePath navFile = "Template:Haskell_navigation.wiki" printVersionFile = "Haskell/Print_version.wiki" partPath :: String -> FilePath partPath s = "Template:Haskell_chapter/" ++ map tweak s ++ ".wiki" where tweak ' ' = '_' tweak c = c -- --------------------------------------------------------------------- -- Input -- --------------------------------------------------------------------- extract :: String -> [String] extract = map degunk . filter isPC . lines degunk :: String -> String degunk = takeWhile notEnd . tail . dropWhile (/= '/') where notEnd x = x /= '|' && x /= ']' isPC :: String -> Bool isPC s = "Haskell/" `isInfixOf` s -- --------------------------------------------------------------------- -- Output -- --------------------------------------------------------------------- toPrintVersion :: [String] -> [[String]] -> String toPrintVersion parts chapters = unlines $ [ "__NOTOC__ __NOEDITSECTION__" , "{{Print version notice|Haskell|Haskell/Print_version}}" , "" , "= Table Of Contents =" ] ++ concatMap toc parts ++ (concat $ zipWith body parts chapters) where toc c = [ "== " ++ c ++ " ==" , "" , ":{{Haskell chapter/" ++ c ++ "|sep=" , ":}}" , "" ] body p cs = [ "----" , "= " ++ p ++ " =" , "----" , "" ] ++ concatMap bodyC cs bodyC c = [ "= " ++ c ++ " =" , "{{:Haskell/" ++ c ++ "}}" , "" ]