Simplify/fix reading of --metadata values on command line.

Previously we used HsYAML's decodeStrict to recognize
boolean values (treating everything else as a string).
This caused problems relating to hvr/HsYAML#7.

We now just check for the recognized boolean values
`true|True|TRUE|false|False|FALSE`, and avoid using
HsYAML.

Closes #5177.
This commit is contained in:
John MacFarlane 2018-12-31 21:20:56 -08:00
parent c8b79b0a04
commit ffc2192caf
2 changed files with 20 additions and 7 deletions

View file

@ -55,7 +55,6 @@ import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TE import qualified Data.Text.Lazy.Encoding as TE
import qualified Data.Text.Encoding.Error as TE import qualified Data.Text.Encoding.Error as TE
import qualified Data.Text.Encoding.Error as TSE import qualified Data.Text.Encoding.Error as TSE
import qualified Data.YAML as YAML
import Network.URI (URI (..), parseURI) import Network.URI (URI (..), parseURI)
import System.Directory (getAppUserDataDirectory) import System.Directory (getAppUserDataDirectory)
import System.Exit (exitSuccess) import System.Exit (exitSuccess)
@ -332,12 +331,14 @@ removeMetaKeys :: [(String,String)] -> Pandoc -> Pandoc
removeMetaKeys kvs pdc = foldr (deleteMeta . fst) pdc kvs removeMetaKeys kvs pdc = foldr (deleteMeta . fst) pdc kvs
readMetaValue :: String -> MetaValue readMetaValue :: String -> MetaValue
readMetaValue s = case YAML.decodeStrict (UTF8.fromString s) of readMetaValue s
Right [YAML.Scalar (YAML.SStr t)] | s == "true" = MetaBool True
-> MetaString $ T.unpack t | s == "True" = MetaBool True
Right [YAML.Scalar (YAML.SBool b)] | s == "TRUE" = MetaBool True
-> MetaBool b | s == "false" = MetaBool False
_ -> MetaString s | s == "False" = MetaBool False
| s == "FALSE" = MetaBool False
| otherwise = MetaString s
-- Transformations of a Pandoc document post-parsing: -- Transformations of a Pandoc document post-parsing:

12
test/command/5177.md Normal file
View file

@ -0,0 +1,12 @@
This should not give a "Prelude.read: no parse" error:
```
% pandoc -M foo=1e -s -t markdown
hi
^D
---
foo: 1e
---
hi
```