diff options
author | John MacFarlane <jgm@berkeley.edu> | 2019-03-27 17:11:14 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2019-03-27 17:11:14 -0700 |
commit | 6157f7ec4f549f5db3406f86cb37d6047d2483fc (patch) | |
tree | a661fc13291abdcac6b6336243857fa0b0ff2ef5 /src | |
parent | 2138ef823921ae58a71c6ca367ccfac4e9367ff9 (diff) | |
download | pandoc-6157f7ec4f549f5db3406f86cb37d6047d2483fc.tar.gz |
Writers.Shared.metaValueToJSON: use Number Values for integers.
Pandoc's MetaValue doesn't have a distinguished number type,
so numbers are put in MetaStrings. If the MetaString consists
entirely of digits, we convert it to a Number.
We should probably consider adding a MetaNumber constructor
to MetaValue, for better round-tripping with JSON etc.
This change aids round-tripping in ipynb metadata fields,
like `toc_depth`.
Diffstat (limited to 'src')
-rw-r--r-- | src/Text/Pandoc/Writers/Shared.hs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs index 01fbbc2be..7027a9576 100644 --- a/src/Text/Pandoc/Writers/Shared.hs +++ b/src/Text/Pandoc/Writers/Shared.hs @@ -36,9 +36,10 @@ module Text.Pandoc.Writers.Shared ( where import Prelude import Control.Monad (zipWithM) +import qualified Data.Aeson as Aeson import Data.Aeson (FromJSON (..), Result (..), ToJSON (..), Value (Object), encode, fromJSON) -import Data.Char (chr, ord, isSpace) +import Data.Char (chr, ord, isSpace, isDigit) import qualified Data.HashMap.Strict as H import Data.List (groupBy, intersperse, transpose) import qualified Data.Map as M @@ -49,7 +50,8 @@ import qualified Text.Pandoc.Builder as Builder import Text.Pandoc.Definition import Text.Pandoc.Options import Text.Pandoc.Pretty -import Text.Pandoc.Shared (stringify, hierarchicalize, Element(..), deNote) +import Text.Pandoc.Shared (stringify, hierarchicalize, Element(..), deNote, + safeRead) import Text.Pandoc.Walk (walk) import Text.Pandoc.UTF8 (toStringLazy) import Text.Pandoc.XML (escapeStringForXML) @@ -105,8 +107,11 @@ metaValueToJSON blockWriter inlineWriter (MetaMap metamap) = toJSON <$> metaValueToJSON blockWriter inlineWriter (MetaList xs) = toJSON <$> Traversable.mapM (metaValueToJSON blockWriter inlineWriter) xs metaValueToJSON _ _ (MetaBool b) = return $ toJSON b -metaValueToJSON _ inlineWriter (MetaString s) = toJSON <$> - inlineWriter (Builder.toList (Builder.text s)) +metaValueToJSON _ inlineWriter (MetaString s) = + if all isDigit s + then return $ + maybe (Aeson.String . T.pack $ s) Aeson.Number $ safeRead s + else toJSON <$> inlineWriter (Builder.toList (Builder.text s)) metaValueToJSON blockWriter _ (MetaBlocks bs) = toJSON <$> blockWriter bs metaValueToJSON _ inlineWriter (MetaInlines is) = toJSON <$> inlineWriter is |