Use HsYAML-0.2.0.0

Most of this is due to @vijayphoenix (#5704), but it
needed some revisions to integrate with current
master, and to use the released HsYAML.

Closes #5704.
This commit is contained in:
John MacFarlane 2019-09-22 10:15:42 -07:00
parent fc443712d3
commit b64410ff9c
5 changed files with 24 additions and 19 deletions

1
.gitignore vendored
View file

@ -24,3 +24,4 @@ data/reference.docx
data/reference.odt data/reference.odt
.stack-work .stack-work
cabal.project.local cabal.project.local
/dist-newstyle/

View file

@ -421,7 +421,7 @@ library
http-types >= 0.8 && < 0.13, http-types >= 0.8 && < 0.13,
case-insensitive >= 1.2 && < 1.3, case-insensitive >= 1.2 && < 1.3,
unicode-transforms >= 0.3 && < 0.4, unicode-transforms >= 0.3 && < 0.4,
HsYAML >= 0.1.1.1 && < 0.2, HsYAML >= 0.2 && < 0.3,
doclayout >= 0.1 && < 0.2, doclayout >= 0.1 && < 0.2,
ipynb >= 0.1 && < 0.2, ipynb >= 0.1 && < 0.2,
attoparsec >= 0.12 && < 0.14 attoparsec >= 0.12 && < 0.14

View file

@ -28,6 +28,7 @@ import qualified Data.Set as Set
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.YAML as YAML import qualified Data.YAML as YAML
import qualified Data.YAML.Event as YE
import System.FilePath (addExtension, takeExtension) import System.FilePath (addExtension, takeExtension)
import Text.HTML.TagSoup import Text.HTML.TagSoup
import Text.Pandoc.Builder (Blocks, Inlines) import Text.Pandoc.Builder (Blocks, Inlines)
@ -244,22 +245,22 @@ yamlBsToMeta :: PandocMonad m => BS.ByteString -> MarkdownParser m (F Meta)
yamlBsToMeta bstr = do yamlBsToMeta bstr = do
pos <- getPosition pos <- getPosition
case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of case YAML.decodeNode' YAML.failsafeSchemaResolver False False bstr of
Right ((YAML.Doc (YAML.Mapping _ o)):_) -> (fmap Meta) <$> yamlMap o Right ((YAML.Doc (YAML.Mapping _ _ o)):_) -> (fmap Meta) <$> yamlMap o
Right [] -> return . return $ mempty Right [] -> return . return $ mempty
Right [YAML.Doc (YAML.Scalar YAML.SNull)] -> return . return $ mempty Right [YAML.Doc (YAML.Scalar _ YAML.SNull)] -> return . return $ mempty
Right _ -> do Right _ -> do
logMessage $ logMessage $
CouldNotParseYamlMetadata "not an object" CouldNotParseYamlMetadata "not an object"
pos pos
return . return $ mempty return . return $ mempty
Left err' -> do Left (_pos, err') -> do
logMessage $ CouldNotParseYamlMetadata logMessage $ CouldNotParseYamlMetadata
err' pos err' pos
return . return $ mempty return . return $ mempty
nodeToKey :: Monad m => YAML.Node -> m Text nodeToKey :: Monad m => YAML.Node YE.Pos -> m Text
nodeToKey (YAML.Scalar (YAML.SStr t)) = return t nodeToKey (YAML.Scalar _ (YAML.SStr t)) = return t
nodeToKey (YAML.Scalar (YAML.SUnknown _ t)) = return t nodeToKey (YAML.Scalar _ (YAML.SUnknown _ t)) = return t
nodeToKey _ = fail "Non-string key in YAML mapping" nodeToKey _ = fail "Non-string key in YAML mapping"
toMetaValue :: PandocMonad m toMetaValue :: PandocMonad m
@ -291,8 +292,8 @@ checkBoolean t =
else Nothing else Nothing
yamlToMetaValue :: PandocMonad m yamlToMetaValue :: PandocMonad m
=> YAML.Node -> MarkdownParser m (F MetaValue) => YAML.Node YE.Pos-> MarkdownParser m (F MetaValue)
yamlToMetaValue (YAML.Scalar x) = yamlToMetaValue (YAML.Scalar _ x) =
case x of case x of
YAML.SStr t -> toMetaValue t YAML.SStr t -> toMetaValue t
YAML.SBool b -> return $ return $ MetaBool b YAML.SBool b -> return $ return $ MetaBool b
@ -303,16 +304,16 @@ yamlToMetaValue (YAML.Scalar x) =
Just b -> return $ return $ MetaBool b Just b -> return $ return $ MetaBool b
Nothing -> toMetaValue t Nothing -> toMetaValue t
YAML.SNull -> return $ return $ MetaString "" YAML.SNull -> return $ return $ MetaString ""
yamlToMetaValue (YAML.Sequence _ xs) = do yamlToMetaValue (YAML.Sequence _ _ xs) = do
xs' <- mapM yamlToMetaValue xs xs' <- mapM yamlToMetaValue xs
return $ do return $ do
xs'' <- sequence xs' xs'' <- sequence xs'
return $ B.toMetaValue xs'' return $ B.toMetaValue xs''
yamlToMetaValue (YAML.Mapping _ o) = fmap B.toMetaValue <$> yamlMap o yamlToMetaValue (YAML.Mapping _ _ o) = fmap B.toMetaValue <$> yamlMap o
yamlToMetaValue _ = return $ return $ MetaString "" yamlToMetaValue _ = return $ return $ MetaString ""
yamlMap :: PandocMonad m yamlMap :: PandocMonad m
=> M.Map YAML.Node YAML.Node => M.Map (YAML.Node YE.Pos) (YAML.Node YE.Pos)
-> MarkdownParser m (F (M.Map String MetaValue)) -> MarkdownParser m (F (M.Map String MetaValue))
yamlMap o = do yamlMap o = do
kvs <- forM (M.toList o) $ \(key, v) -> do kvs <- forM (M.toList o) $ \(key, v) -> do

View file

@ -76,7 +76,7 @@ instance FromJSON Term where
parseJSON invalid = Aeson.typeMismatch "Term" invalid parseJSON invalid = Aeson.typeMismatch "Term" invalid
instance YAML.FromYAML Term where instance YAML.FromYAML Term where
parseYAML (YAML.Scalar (YAML.SStr t)) = parseYAML (YAML.Scalar _ (YAML.SStr t)) =
case safeRead (T.unpack t) of case safeRead (T.unpack t) of
Just t' -> pure t' Just t' -> pure t'
Nothing -> fail $ "Invalid Term name " ++ Nothing -> fail $ "Invalid Term name " ++
@ -99,12 +99,12 @@ instance FromJSON Translations where
instance YAML.FromYAML Translations where instance YAML.FromYAML Translations where
parseYAML = YAML.withMap "Translations" $ parseYAML = YAML.withMap "Translations" $
\tr -> Translations .M.fromList <$> mapM addItem (M.toList tr) \tr -> Translations .M.fromList <$> mapM addItem (M.toList tr)
where addItem (n@(YAML.Scalar (YAML.SStr k)), v) = where addItem (n@(YAML.Scalar _ (YAML.SStr k)), v) =
case safeRead (T.unpack k) of case safeRead (T.unpack k) of
Nothing -> YAML.typeMismatch "Term" n Nothing -> YAML.typeMismatch "Term" n
Just t -> Just t ->
case v of case v of
(YAML.Scalar (YAML.SStr s)) -> (YAML.Scalar _ (YAML.SStr s)) ->
return (t, T.unpack (T.strip s)) return (t, T.unpack (T.strip s))
n' -> YAML.typeMismatch "String" n' n' -> YAML.typeMismatch "String" n'
addItem (n, _) = YAML.typeMismatch "String" n addItem (n, _) = YAML.typeMismatch "String" n
@ -115,6 +115,8 @@ lookupTerm t (Translations tm) = M.lookup t tm
readTranslations :: String -> Either String Translations readTranslations :: String -> Either String Translations
readTranslations s = readTranslations s =
case YAML.decodeStrict $ UTF8.fromString s of case YAML.decodeStrict $ UTF8.fromString s of
Left err' -> Left err' Left (pos,err') -> Left $ err' ++
Right (t:_) -> Right t " (line " ++ show (YAML.posLine pos) ++ " column " ++
Right [] -> Left "empty YAML document" show (YAML.posColumn pos) ++ ")"
Right (t:_) -> Right t
Right [] -> Left "empty YAML document"

View file

@ -25,7 +25,8 @@ extra-deps:
- skylighting-core-0.8.2 - skylighting-core-0.8.2
- skylighting-0.8.2 - skylighting-0.8.2
- doclayout-0.1 - doclayout-0.1
#- doctemplates-0.6 - HsYAML-0.2.0.0
#- doctemplates-0.6
ghc-options: ghc-options:
"$locals": -fhide-source-paths -Wno-missing-home-modules "$locals": -fhide-source-paths -Wno-missing-home-modules
resolver: lts-13.17 resolver: lts-13.17