diff options
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/App.hs | 21 | ||||
-rw-r--r-- | src/Text/Pandoc/App/CommandLineOptions.hs | 15 | ||||
-rw-r--r-- | src/Text/Pandoc/BCP47.hs | 8 | ||||
-rw-r--r-- | src/Text/Pandoc/CSV.hs | 12 | ||||
-rw-r--r-- | src/Text/Pandoc/Class.hs | 19 | ||||
-rw-r--r-- | src/Text/Pandoc/ImageSize.hs | 4 | ||||
-rw-r--r-- | src/Text/Pandoc/MIME.hs | 3 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers.hs | 2 |
8 files changed, 40 insertions, 44 deletions
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index 0565874ad..67315ad09 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -88,9 +88,8 @@ convertWithOpts opts = do let filters' = filters ++ [ JSONFilter "pandoc-citeproc" | needsCiteproc ] let sources = case optInputFiles opts of - Nothing -> ["-"] - Just xs | optIgnoreArgs opts -> ["-"] - | otherwise -> xs + Just xs | not (optIgnoreArgs opts) -> xs + _ -> ["-"] datadir <- case optDataDir opts of Nothing -> do @@ -192,10 +191,10 @@ convertWithOpts opts = do "use '-o -' to force output to stdout." - abbrevs <- Set.fromList . filter (not . T.null) . T.lines <$> + abbrevs <- Set.fromList . filter (not . T.null) . T.lines . UTF8.toText <$> case optAbbreviations opts of - Nothing -> UTF8.toText <$> readDataFile "abbreviations" - Just f -> UTF8.toText <$> readFileStrict f + Nothing -> readDataFile "abbreviations" + Just f -> readFileStrict f metadata <- if format == "jats" && isNothing (lookupMeta "csl" (optMetadata opts)) && @@ -341,13 +340,13 @@ readSource src = case parseURI src of then BS.getContents else BS.readFile fp E.catch (return $! UTF8.toText bs) - (\e -> case e of - TSE.DecodeError _ (Just w) -> do + (\e -> E.throwIO $ case e of + TSE.DecodeError _ (Just w) -> case BS.elemIndex w bs of - Just offset -> E.throwIO $ + Just offset -> PandocUTF8DecodingError (T.pack fp) offset w - _ -> E.throwIO $ PandocUTF8DecodingError (T.pack fp) 0 w - _ -> E.throwIO $ PandocAppError (tshow e)) + _ -> PandocUTF8DecodingError (T.pack fp) 0 w + _ -> PandocAppError (tshow e)) readURI :: FilePath -> PandocIO Text readURI src = UTF8.toText . fst <$> openURL (T.pack src) diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs index be6bc66f1..e407d8854 100644 --- a/src/Text/Pandoc/App/CommandLineOptions.hs +++ b/src/Text/Pandoc/App/CommandLineOptions.hs @@ -28,6 +28,7 @@ import Control.Monad.Trans import Control.Monad.Except (throwError) import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder, defConfig, Indent(..), NumberFormat(..)) +import Data.Bifunctor (second) import Data.Char (toLower) import Data.List (intercalate, sort) #ifdef _WINDOWS @@ -36,6 +37,7 @@ import Data.List (isPrefixOf) #endif #endif import Data.Maybe (fromMaybe, isJust) +import Safe (tailDef) import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme) import System.Console.GetOpt import System.Environment (getArgs, getProgName) @@ -981,10 +983,7 @@ writersNames = sort ("pdf" : map (T.unpack . fst) (writers :: [(Text, Writer PandocIO)])) splitField :: String -> (String, String) -splitField s = - case break (`elemText` ":=") s of - (k,_:v) -> (k,v) - (k,[]) -> (k,"true") +splitField = second (tailDef "true") . break (`elemText` ":=") -- | Apply defaults from --defaults file. applyDefaults :: Opt -> FilePath -> IO Opt @@ -994,10 +993,10 @@ applyDefaults opt file = runIOorExplode $ do else file setVerbosity $ optVerbosity opt dataDirs <- liftIO defaultUserDataDirs - let fps = case optDataDir opt of - Nothing -> (fp : map (</> ("defaults" </> fp)) - dataDirs) - Just dd -> [fp, dd </> "defaults" </> fp] + let fps = fp : case optDataDir opt of + Nothing -> map (</> ("defaults" </> fp)) + dataDirs + Just dd -> [dd </> "defaults" </> fp] fp' <- fromMaybe fp <$> findFile fps inp <- readFileLazy fp' case Y.decode1 inp of diff --git a/src/Text/Pandoc/BCP47.hs b/src/Text/Pandoc/BCP47.hs index e25134976..c9f144aa4 100644 --- a/src/Text/Pandoc/BCP47.hs +++ b/src/Text/Pandoc/BCP47.hs @@ -93,14 +93,16 @@ parseBCP47 lang = ds <- P.option "" (P.count 1 P.digit) cs <- P.many1 asciiLetter let var = ds ++ cs + lv = length var guard $ if null ds - then length var >= 5 && length var <= 8 - else length var == 4 + then lv >= 5 && lv <= 8 + else lv == 4 return $ T.toLower $ T.pack var pExtension = P.try $ do P.char '-' cs <- P.many1 $ P.satisfy (\c -> isAscii c && isAlphaNum c) - guard $ length cs >= 2 && length cs <= 8 + let lcs = length cs + guard $ lcs >= 2 && lcs <= 8 return $ T.toLower $ T.pack cs pPrivateUse = P.try $ do P.char '-' diff --git a/src/Text/Pandoc/CSV.hs b/src/Text/Pandoc/CSV.hs index 99324ee2e..633d34681 100644 --- a/src/Text/Pandoc/CSV.hs +++ b/src/Text/Pandoc/CSV.hs @@ -18,7 +18,7 @@ module Text.Pandoc.CSV ( ) where import Prelude -import Control.Monad (void) +import Control.Monad (unless, void) import Data.Text (Text) import qualified Data.Text as T import Text.Parsec @@ -63,10 +63,10 @@ pCSVQuotedCell opts = do return $ T.pack res escaped :: CSVOptions -> Parser Char -escaped opts = +escaped opts = try $ case csvEscape opts of - Nothing -> try $ char (csvQuote opts) >> char (csvQuote opts) - Just c -> try $ char c >> noneOf "\r\n" + Nothing -> char (csvQuote opts) >> char (csvQuote opts) + Just c -> char c >> noneOf "\r\n" pCSVUnquotedCell :: CSVOptions -> Parser Text pCSVUnquotedCell opts = T.pack <$> @@ -76,9 +76,7 @@ pCSVUnquotedCell opts = T.pack <$> pCSVDelim :: CSVOptions -> Parser () pCSVDelim opts = do char (csvDelim opts) - if csvKeepSpace opts - then return () - else skipMany (oneOf " \t") + unless (csvKeepSpace opts) $ skipMany (oneOf " \t") endline :: Parser () endline = do diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs index 098d95a09..5abb5fdd8 100644 --- a/src/Text/Pandoc/Class.hs +++ b/src/Text/Pandoc/Class.hs @@ -524,12 +524,11 @@ alertIndent (l:ls) = do -- single-letter schemes. Reason: these are usually windows absolute -- paths. parseURIReference' :: T.Text -> Maybe URI -parseURIReference' s = - case parseURIReference (T.unpack s) of - Just u - | length (uriScheme u) > 2 -> Just u - | null (uriScheme u) -> Just u -- protocol-relative - _ -> Nothing +parseURIReference' s = do + u <- parseURIReference (T.unpack s) + case uriScheme u of + [_] -> Nothing + _ -> Just u -- | Set the user data directory in common state. setUserDataDir :: PandocMonad m @@ -589,10 +588,10 @@ downloadOrRead s = do uriFragment = "" } dropFragmentAndQuery = T.takeWhile (\c -> c /= '?' && c /= '#') fp = unEscapeString $ T.unpack $ dropFragmentAndQuery s - mime = case takeExtension fp of - ".gz" -> getMimeType $ dropExtension fp - ".svgz" -> getMimeType $ dropExtension fp ++ ".svg" - x -> getMimeType x + mime = getMimeType $ case takeExtension fp of + ".gz" -> dropExtension fp + ".svgz" -> dropExtension fp ++ ".svg" + x -> x ensureEscaped = T.pack . escapeURIString isAllowedInURI . T.unpack . T.map convertSlash convertSlash '\\' = '/' convertSlash x = x diff --git a/src/Text/Pandoc/ImageSize.hs b/src/Text/Pandoc/ImageSize.hs index d9ded22be..d11f59fdc 100644 --- a/src/Text/Pandoc/ImageSize.hs +++ b/src/Text/Pandoc/ImageSize.hs @@ -212,9 +212,7 @@ showInPixel opts dim = T.pack $ show $ inPixel opts dim numUnit :: T.Text -> Maybe (Double, T.Text) numUnit s = let (nums, unit) = T.span (\c -> isDigit c || ('.'==c)) s - in case safeRead nums of - Just n -> Just (n, unit) - Nothing -> Nothing + in (\n -> (n, unit)) <$> safeRead nums -- | Scale a dimension by a factor. scaleDimension :: Double -> Dimension -> Dimension diff --git a/src/Text/Pandoc/MIME.hs b/src/Text/Pandoc/MIME.hs index aabc6dd1d..8032e1c3f 100644 --- a/src/Text/Pandoc/MIME.hs +++ b/src/Text/Pandoc/MIME.hs @@ -18,6 +18,7 @@ import Data.List (isPrefixOf, isSuffixOf) import qualified Data.Map as M import qualified Data.Text as T import Data.Maybe (fromMaybe, listToMaybe) +import Data.Tuple (swap) import System.FilePath type MimeType = T.Text @@ -50,7 +51,7 @@ mediaCategory :: FilePath -> Maybe T.Text mediaCategory fp = getMimeType fp >>= listToMaybe . T.splitOn "/" reverseMimeTypes :: M.Map MimeType T.Text -reverseMimeTypes = M.fromList $ map (\(k,v) -> (v,k)) mimeTypesList +reverseMimeTypes = M.fromList $ map swap mimeTypesList mimeTypes :: M.Map T.Text MimeType mimeTypes = M.fromList mimeTypesList diff --git a/src/Text/Pandoc/Writers.hs b/src/Text/Pandoc/Writers.hs index 724c22a50..753972855 100644 --- a/src/Text/Pandoc/Writers.hs +++ b/src/Text/Pandoc/Writers.hs @@ -125,7 +125,7 @@ data Writer m = TextWriter (WriterOptions -> Pandoc -> m Text) writers :: PandocMonad m => [ (Text, Writer m) ] writers = [ ("native" , TextWriter writeNative) - ,("json" , TextWriter $ \o d -> writeJSON o d) + ,("json" , TextWriter writeJSON) ,("docx" , ByteStringWriter writeDocx) ,("odt" , ByteStringWriter writeODT) ,("pptx" , ByteStringWriter writePowerpoint) |