diff options
author | John MacFarlane <jgm@berkeley.edu> | 2011-01-28 11:55:11 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2011-01-28 12:02:26 -0800 |
commit | 8c435578d63b4723789b5d03d36c8da19968af8e (patch) | |
tree | f0c937228ad74f775390ee5e5fd6b0b99cefb2a1 /MakeManPage.hs | |
parent | 5ba5373ec66dfbafc16e28320851d70e1be46f80 (diff) | |
download | pandoc-8c435578d63b4723789b5d03d36c8da19968af8e.tar.gz |
Refactored man pages.
* Markdown syntax description from README now goes in pandoc_markdown.5.
* Refactored man page construction functions, putting more of
the work in MakeManPages.hs.
Diffstat (limited to 'MakeManPage.hs')
-rw-r--r-- | MakeManPage.hs | 78 |
1 files changed, 66 insertions, 12 deletions
diff --git a/MakeManPage.hs b/MakeManPage.hs index f165fbd68..117c1e9f1 100644 --- a/MakeManPage.hs +++ b/MakeManPage.hs @@ -5,20 +5,65 @@ import Data.Char (toUpper) import qualified Data.ByteString as B import Control.Monad import System.FilePath +import System.Environment (getArgs) +import Text.Pandoc.Shared (normalize) +import System.Directory (getModificationTime) +import System.IO.Error (isDoesNotExistError) +import System.Time (ClockTime(..)) +import Data.Maybe (catMaybes) main = do rmContents <- liftM toString $ B.readFile "README" let (Pandoc meta blocks) = readMarkdown defaultParserState rmContents - let newBlocks = removeWrapperSect blocks - manTemplate <- liftM toString $ B.readFile - $ "man" </> "man1" </> "pandoc.1.template" + let manBlocks = removeSect [Str "Wrappers"] + $ removeSect [Str "Pandoc's",Space,Str "markdown"] blocks + let syntaxBlocks = extractSect [Str "Pandoc's",Space,Str "markdown"] blocks + args <- getArgs + let verbose = "--verbose" `elem` args + makeManPage verbose ("man" </> "man1" </> "pandoc.1") + meta manBlocks + makeManPage verbose ("man" </> "man5" </> "pandoc_markdown.5") + meta syntaxBlocks + let markdown2pdfpage = "man" </> "man1" </> "markdown2pdf.1" + modDeps <- modifiedDependencies markdown2pdfpage [markdown2pdfpage <.> "md"] + unless (null modDeps) $ do + mpdfContents <- liftM toString $ B.readFile $ markdown2pdfpage <.> "md" + templ <- liftM toString $ B.readFile $ "templates" </> "man.template" + let doc = readMarkdown defaultParserState{ stateStandalone = True } + mpdfContents + writeManPage markdown2pdfpage templ doc + when verbose $ + putStrLn $ "Created " ++ markdown2pdfpage + +makeManPage :: Bool -> FilePath -> Meta -> [Block] -> IO () +makeManPage verbose page meta blocks = do + let templ = page <.> "template" + modDeps <- modifiedDependencies page ["README", templ] + unless (null modDeps) $ do + manTemplate <- liftM toString $ B.readFile templ + writeManPage page manTemplate (Pandoc meta blocks) + when verbose $ + putStrLn $ "Created " ++ page + +writeManPage :: FilePath -> String -> Pandoc -> IO () +writeManPage page templ doc = do let opts = defaultWriterOptions{ writerStandalone = True - , writerTemplate = manTemplate } + , writerTemplate = templ } let manPage = writeMan opts $ - bottomUp (concatMap removeLinks) $ - bottomUp capitalizeHeaders $ - Pandoc meta newBlocks - B.writeFile ("man" </> "man1" </> "pandoc.1") $ fromString manPage + bottomUp (concatMap removeLinks) $ + bottomUp capitalizeHeaders doc + B.writeFile page $ fromString manPage + +-- | Returns a list of 'dependencies' that have been modified after 'file'. +modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath] +modifiedDependencies file dependencies = do + fileModTime <- catch (getModificationTime file) $ + \e -> if isDoesNotExistError e + then return (TOD 0 0) -- the minimum ClockTime + else ioError e + depModTimes <- mapM getModificationTime dependencies + let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes + return $ catMaybes modified removeLinks :: Inline -> [Inline] removeLinks (Link l _) = l @@ -32,10 +77,19 @@ capitalize :: Inline -> Inline capitalize (Str xs) = Str $ map toUpper xs capitalize x = x -removeWrapperSect :: [Block] -> [Block] -removeWrapperSect (Header 1 [Str "Wrappers"]:xs) = +removeSect :: [Inline] -> [Block] -> [Block] +removeSect ils (Header 1 x:xs) | normalize x == normalize ils = dropWhile notLevelOneHeader xs where notLevelOneHeader (Header 1 _) = False notLevelOneHeader _ = True -removeWrapperSect (x:xs) = x : removeWrapperSect xs -removeWrapperSect [] = [] +removeSect ils (x:xs) = x : removeSect ils xs +removeSect _ [] = [] + +extractSect :: [Inline] -> [Block] -> [Block] +extractSect ils (Header 1 x:xs) | normalize x == normalize ils = + bottomUp promoteHeader xs + where promoteHeader (Header n x) = Header (n-1) x + promoteHeader x = x +extractSect ils (x:xs) = extractSect ils xs +extractSect _ [] = [] + |