diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Text/Pandoc/Data.hsb | 3 | ||||
-rw-r--r-- | src/Text/Pandoc/ManPages.hs | 101 | ||||
-rw-r--r-- | src/Text/Pandoc/Shared.hs | 20 |
3 files changed, 118 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Data.hsb b/src/Text/Pandoc/Data.hsb index 28e7f5112..cd8836a0b 100644 --- a/src/Text/Pandoc/Data.hsb +++ b/src/Text/Pandoc/Data.hsb @@ -5,3 +5,6 @@ import qualified Data.ByteString as B dataFiles :: [(FilePath, B.ByteString)] dataFiles = %blobs "data" + +readmeFile :: B.ByteString +readmeFile = %blob "README" diff --git a/src/Text/Pandoc/ManPages.hs b/src/Text/Pandoc/ManPages.hs new file mode 100644 index 000000000..cc5f162f8 --- /dev/null +++ b/src/Text/Pandoc/ManPages.hs @@ -0,0 +1,101 @@ +{-# LANGUAGE CPP #-} +{- +Copyright (C) 2013-2015 John MacFarlane <jgm@berkeley.edu> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +-} + +{- | + Module : Text.Pandoc.ManPages + Copyright : Copyright (C) 2013-2015 John MacFarlane + License : GNU GPL, version 2 or above + + Maintainer : John MacFarlane <jgm@berkeley.edu> + Stability : alpha + Portability : portable + +Functions to build pandoc's man pages (pandoc.1 and pandoc_markdown.5) +from pandoc's README. +-} +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 (n-1) attr x + promoteHeader x = x +extractSect ils (_:xs) = extractSect ils xs +extractSect _ [] = [] + +isHeader1 :: Block -> Bool +isHeader1 (Header 1 _ _ ) = True +isHeader1 _ = False diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index 362db2fed..2090e1734 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -132,7 +132,7 @@ import qualified Data.Text as T (toUpper, pack, unpack) import Data.ByteString.Lazy (toChunks) #ifdef EMBED_DATA_FILES -import Text.Pandoc.Data (dataFiles) +import Text.Pandoc.Data (dataFiles, readmeFile) #else import Paths_pandoc (getDataFileName) #endif @@ -743,6 +743,12 @@ inDirectory path action = E.bracket (const $ setCurrentDirectory path >> action) readDefaultDataFile :: FilePath -> IO BS.ByteString +readDefaultDataFile "README" = +#ifdef EMBED_DATA_FILES + return readmeFile +#else + getDataFileName "README" >>= checkExistence >>= BS.readFile +#endif readDefaultDataFile fname = #ifdef EMBED_DATA_FILES case lookup (makeCanonical fname) dataFiles of @@ -755,13 +761,15 @@ readDefaultDataFile fname = go as x = x : as #else getDataFileName ("data" </> fname) >>= checkExistence >>= BS.readFile - where checkExistence fn = do - exists <- doesFileExist fn - if exists - then return fn - else err 97 ("Could not find data file " ++ fname) #endif +checkExistence :: FilePath -> IO FilePath +checkExistence fn = do + exists <- doesFileExist fn + if exists + then return fn + else err 97 ("Could not find data file " ++ fn) + -- | Read file from specified user data directory or, if not found there, from -- Cabal data directory. readDataFile :: Maybe FilePath -> FilePath -> IO BS.ByteString |