diff options
author | dr@jones.dk <dr@jones.dk> | 2009-12-14 12:57:35 +0100 |
---|---|---|
committer | dr@jones.dk <dr@jones.dk> | 2009-12-14 12:57:35 +0100 |
commit | 789d0772d8b5d9c066fb8624bd51576cbde5e30b (patch) | |
tree | 7141187124ecc41b13861c81c7b642076cb88078 /src/markdown2pdf.hs | |
parent | 88b315ccee666385e1a4c52e2eb5fb0b0ffe8d60 (diff) |
Imported Upstream version 1.3
Diffstat (limited to 'src/markdown2pdf.hs')
-rw-r--r-- | src/markdown2pdf.hs | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/src/markdown2pdf.hs b/src/markdown2pdf.hs index 6a04999fa..7bcbf4a82 100644 --- a/src/markdown2pdf.hs +++ b/src/markdown2pdf.hs @@ -1,18 +1,20 @@ module Main where -import Data.List (isInfixOf, intercalate, (\\)) +import Data.List (isInfixOf, intercalate, isPrefixOf) import Data.Maybe (isNothing) -import Control.Monad (when, unless, guard) +import Control.Monad (unless, guard) import Control.Exception (tryJust, bracket) -import System.IO (stderr, hPutStrLn) +import System.IO (stderr) import System.IO.Error (isDoesNotExistError) +import System.Environment ( getArgs, getProgName ) +import Prelude hiding ( putStr, putStrLn, writeFile, readFile, getContents ) +import System.IO.UTF8 import System.Exit (ExitCode (..), exitWith) import System.FilePath import System.Directory import System.Process (readProcessWithExitCode) -import System.Environment (getArgs, getProgName) run :: FilePath -> [String] -> IO (Either String String) @@ -62,12 +64,12 @@ runLatexRaw file = do runLatex :: FilePath -> IO (Either String FilePath) runLatex file = step 3 where - step 0 = return $ Left "Limit of attempts reached" step n = do result <- runLatexRaw file case result of Left (Left err) -> return $ Left err - Left (Right _ ) -> step (n-1 :: Int) + Left (Right _) | n > 1 -> step (n-1 :: Int) + Left (Right msg) -> return $ Left msg Right pdfFile -> return $ Right pdfFile checkLatex :: String -> (Bool, Bool, Bool, String) @@ -127,13 +129,8 @@ saveStdin file = do saveOutput :: FilePath -> FilePath -> IO () saveOutput input output = do - outputExist <- doesFileExist output - when outputExist $ do - let output' = output ++ "~" - renameFile output output' - putStrLn $! "Created backup file " ++ output' copyFile input output - putStrLn $! "Created " ++ output + hPutStrLn stderr $! "Created " ++ output main :: IO () main = bracket @@ -155,21 +152,26 @@ main = bracket unless (null miss) $ exit $! "Could not find " ++ intercalate ", " miss args <- getArgs -- check for invalid arguments and print help message if needed - let goodopts = ["-f","-r","--from","--read","--strict","-N", - "-p","--preserve-tabs","--tab-stop","-R","--parse-raw", + let goodopts = ["-f","-r","-N", "-p","-R","-H","-B","-A", "-C","-o"] + let goodoptslong = ["--from","--read","--strict", + "--preserve-tabs","--tab-stop","--parse-raw", "--toc","--table-of-contents", - "--number-sections","-H","--include-in-header", - "-B","--include-before-body","-A","--include-after-body", - "-C","--custom-header","-o","--output"] - let goodoptsLong = filter (\op -> length op > 2) goodopts + "--number-sections","--include-in-header", + "--include-before-body","--include-after-body", + "--custom-header","--output"] let isOpt ('-':_) = True isOpt _ = False - unless (null (filter isOpt args \\ goodopts)) $ do + let opts = filter isOpt args + -- note that a long option can come in this form: --opt=val + let isGoodopt x = x `elem` (goodopts ++ goodoptslong) || + any (\o -> (o ++ "=") `isPrefixOf` x) goodoptslong + unless (all isGoodopt opts) $ do (code, out, _err) <- readProcessWithExitCode "pandoc" ["--help"] "" putStrLn "markdown2pdf [OPTIONS] [FILES]\nOptions:" putStr $ unlines $ - filter (\l -> any (`isInfixOf` l) goodoptsLong) $ lines out + filter (\l -> any (`isInfixOf` l) goodoptslong) $ lines out exitWith code + -- parse arguments -- if no input given, use 'stdin' pandocArgs <- parsePandocArgs args |