From 652f9a88f4e112804dcb5035783235a08aff3aa8 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 19 Sep 2013 20:21:35 -0700 Subject: Handle Boolean values in `--metadata`. Note that anything not parseable as a YAML boolean or string is treated as a literal string. Note that you can still get a string value with "yes" or any of the strings interpretable as booleans: -M boolvalue=yes -M stringvalue='"yes"' --- pandoc.hs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'pandoc.hs') diff --git a/pandoc.hs b/pandoc.hs index bf588805b..f3c2af38f 100644 --- a/pandoc.hs +++ b/pandoc.hs @@ -63,6 +63,9 @@ import Data.Aeson (eitherDecode', encode) import qualified Data.Map as M import System.IO.Error(ioeGetErrorType) import GHC.IO.Exception (IOErrorType(ResourceVanished)) +import Data.Yaml (decode) +import qualified Data.Yaml as Yaml +import qualified Data.Text as T copyrightMessage :: String copyrightMessage = "\nCopyright (C) 2006-2013 John MacFarlane\n" ++ @@ -333,7 +336,7 @@ options = (ReqArg (\arg opt -> do let (key,val) = case break (`elem` ":=") arg of - (k,_:v) -> (k, MetaString v) + (k,_:v) -> (k, readMetaValue v) (k,_) -> (k, MetaBool True) return opt{ optMetadata = addMetadata key val $ optMetadata opt }) @@ -664,7 +667,7 @@ options = , Option "" ["bibliography"] (ReqArg (\arg opt -> return opt{ optMetadata = addMetadata - "bibliography" (MetaString arg) + "bibliography" (readMetaValue arg) $ optMetadata opt }) "FILE") @@ -674,7 +677,7 @@ options = (ReqArg (\arg opt -> return opt{ optMetadata = addMetadata "csl" - (MetaString arg) + (readMetaValue arg) $ optMetadata opt }) "FILE") "" @@ -684,7 +687,7 @@ options = (\arg opt -> return opt{ optMetadata = addMetadata "citation-abbreviations" - (MetaString arg) + (readMetaValue arg) $ optMetadata opt }) "FILE") "" @@ -793,6 +796,12 @@ addMetadata k v m = case M.lookup k m of (MetaList (xs ++ [v])) m Just x -> M.insert k (MetaList [v, x]) m +readMetaValue :: String -> MetaValue +readMetaValue s = case decode (UTF8.fromString s) of + Just (Yaml.String t) -> MetaString $ T.unpack t + Just (Yaml.Bool b) -> MetaBool b + _ -> MetaString s + -- Returns usage message usageMessage :: String -> [OptDescr (Opt -> IO Opt)] -> String usageMessage programName = usageInfo -- cgit v1.2.3