module Text.Pandoc.ManPages (
manPandoc1,
manPandocMarkdown5
) where
import Text.Pandoc
import Text.Pandoc.Error (handleError)
import Data.Char (toUpper)
import System.FilePath
import Text.Pandoc.Shared (normalize, readDataFileUTF8)
manPandoc1 :: IO String
manPandoc1 = do
readme <- readDataFileUTF8 Nothing "README"
let (Pandoc meta blocks) = normalize $ handleError
$ readMarkdown def readme
let manBlocks = removeSect [Str "Wrappers"]
$ removeSect [Str "Pandoc's",Space,Str "markdown"] blocks
makeManPage "pandoc.1" meta manBlocks
manPandocMarkdown5 :: IO String
manPandocMarkdown5 = do
readme <- readDataFileUTF8 Nothing "README"
let (Pandoc meta blocks) = normalize $ handleError
$ readMarkdown def readme
let syntaxBlocks = extractSect [Str "Pandoc's",Space,Str "markdown"] blocks
makeManPage "pandoc_markdown.5" meta syntaxBlocks
makeManPage :: String -> Meta -> [Block] -> IO String
makeManPage page meta blocks = do
let templ = page <.> "template"
manTemplate <- readDataFileUTF8 Nothing templ
return $ writeManPage manTemplate (Pandoc meta blocks)
writeManPage :: String -> Pandoc -> String
writeManPage templ doc =
writeMan def{ writerStandalone = True
, writerTemplate = templ
, writerVariables = [("version", pandocVersion)] } $
bottomUp (concatMap removeLinks) $
bottomUp capitalizeHeaders doc
removeLinks :: Inline -> [Inline]
removeLinks (Link l _) = l
removeLinks x = [x]
capitalizeHeaders :: Block -> Block
capitalizeHeaders (Header 1 attr xs) = Header 1 attr $ bottomUp capitalize xs
capitalizeHeaders x = x
capitalize :: Inline -> Inline
capitalize (Str xs) = Str $ map toUpper xs
capitalize x = x
removeSect :: [Inline] -> [Block] -> [Block]
removeSect ils (Header 1 _ x:xs) | x == ils =
dropWhile (not . isHeader1) xs
removeSect ils (x:xs) = x : removeSect ils xs
removeSect _ [] = []
extractSect :: [Inline] -> [Block] -> [Block]
extractSect ils (Header 1 _ z:xs) | z == ils =
bottomUp promoteHeader $ takeWhile (not . isHeader1) xs
where promoteHeader (Header n attr x) = Header (n1) attr x
promoteHeader x = x
extractSect ils (_:xs) = extractSect ils xs
extractSect _ [] = []
isHeader1 :: Block -> Bool
isHeader1 (Header 1 _ _ ) = True
isHeader1 _ = False