git.iko.soy/typeable/pandoc
on 1.9.0.2 help sign in
typeable/pandoc/MakeManPage.hs
87 lines 3.3 kB history raw
1-- Create pandoc.1 man page from README
2import Text.Pandoc
3import Data.ByteString.UTF8 (toString, fromString)
4import Data.Char (toUpper)
5import qualified Data.ByteString as B
6import Control.Monad
7import System.FilePath
8import System.Environment (getArgs)
9import Text.Pandoc.Shared (normalize)
10import System.Directory (getModificationTime)
11import System.IO.Error (isDoesNotExistError)
12import System.Time (ClockTime(..))
13import Data.Maybe (catMaybes)
15main = do
16 rmContents <- liftM toString $ B.readFile "README"
17 let (Pandoc meta blocks) = readMarkdown defaultParserState rmContents
18 let manBlocks = removeSect [Str "Wrappers"]
19 $ removeSect [Str "Pandoc's",Space,Str "markdown"] blocks
20 let syntaxBlocks = extractSect [Str "Pandoc's",Space,Str "markdown"] blocks
21 args <- getArgs
22 let verbose = "--verbose" `elem` args
23 makeManPage verbose ("man" </> "man1" </> "pandoc.1")
24 meta manBlocks
25 makeManPage verbose ("man" </> "man5" </> "pandoc_markdown.5")
26 meta syntaxBlocks
28makeManPage :: Bool -> FilePath -> Meta -> [Block] -> IO ()
29makeManPage verbose page meta blocks = do
30 let templ = page <.> "template"
31 modDeps <- modifiedDependencies page ["README", templ]
32 unless (null modDeps) $ do
33 manTemplate <- liftM toString $ B.readFile templ
34 writeManPage page manTemplate (Pandoc meta blocks)
35 when verbose $
36 putStrLn $ "Created " ++ page
38writeManPage :: FilePath -> String -> Pandoc -> IO ()
39writeManPage page templ doc = do
40 let opts = defaultWriterOptions{ writerStandalone = True
41 , writerTemplate = templ }
42 let manPage = writeMan opts $
43 bottomUp (concatMap removeLinks) $
44 bottomUp capitalizeHeaders doc
45 B.writeFile page $ fromString manPage
47-- | Returns a list of 'dependencies' that have been modified after 'file'.
48modifiedDependencies :: FilePath -> [FilePath] -> IO [FilePath]
49modifiedDependencies file dependencies = do
50 fileModTime <- catch (getModificationTime file) $
51 \e -> if isDoesNotExistError e
52 then return (TOD 0 0) -- the minimum ClockTime
53 else ioError e
54 depModTimes <- mapM getModificationTime dependencies
55 let modified = zipWith (\dep time -> if time > fileModTime then Just dep else Nothing) dependencies depModTimes
56 return $ catMaybes modified
58removeLinks :: Inline -> [Inline]
59removeLinks (Link l _) = l
60removeLinks x = [x]
62capitalizeHeaders :: Block -> Block
63capitalizeHeaders (Header 1 xs) = Header 1 $ bottomUp capitalize xs
64capitalizeHeaders x = x
66capitalize :: Inline -> Inline
67capitalize (Str xs) = Str $ map toUpper xs
68capitalize x = x
70removeSect :: [Inline] -> [Block] -> [Block]
71removeSect ils (Header 1 x:xs) | normalize x == normalize ils =
72 dropWhile (not . isHeader1) xs
73removeSect ils (x:xs) = x : removeSect ils xs
74removeSect _ [] = []
76extractSect :: [Inline] -> [Block] -> [Block]
77extractSect ils (Header 1 z:xs) | normalize z == normalize ils =
78 bottomUp promoteHeader $ takeWhile (not . isHeader1) xs
79 where promoteHeader (Header n x) = Header (n-1) x
80 promoteHeader x = x
81extractSect ils (x:xs) = extractSect ils xs
82extractSect _ [] = []
84isHeader1 :: Block -> Bool
85isHeader1 (Header 1 _) = True
86isHeader1 _ = False