mirror of
https://github.com/jgm/pandoc.git
synced 2026-08-28 17:20:47 +08:00
More in-depth refactoring and cleanup (#6123)
* Avoid duplicating the dash case
* Pull common functions out of case branches
* Make sure list lengths are only calculated once
* Use unless
* Simplify parseURIReference' and avoid an unnecessary call to length
* Use <$> instead of reimplementing it
* Use swap instead of reimplementing it
* Remove eta-expansion that's been unnecessary since 90f5dd8
* Use tailDef instead of reimplementing it
* Use second instead of fmap, per @tarleb
This commit is contained in:
+10
-11
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 '-'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user