diff options
author | John MacFarlane <jgm@berkeley.edu> | 2019-03-09 14:17:21 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2019-03-09 14:27:34 -0800 |
commit | 873f342f1144111f79f9415abd3dc00d44a7a3c6 (patch) | |
tree | 86619a113dd88ec25cae98afe730ee5af746f99d /src/Text | |
parent | bf4a164a6f832bbb8435c4021a6b019f8e1c29f7 (diff) | |
download | pandoc-873f342f1144111f79f9415abd3dc00d44a7a3c6.tar.gz |
Ipynb reader/writer: better handling of cell metadata.
We now handle even complex cell metadata in the Div's attributes.
Simple metadata fields are rendered as a plain string, and complex ones
as JSON.
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/Readers/Ipynb.hs | 17 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/Ipynb.hs | 13 |
2 files changed, 16 insertions, 14 deletions
diff --git a/src/Text/Pandoc/Readers/Ipynb.hs b/src/Text/Pandoc/Readers/Ipynb.hs index 13550d9a1..156e6bb8d 100644 --- a/src/Text/Pandoc/Readers/Ipynb.hs +++ b/src/Text/Pandoc/Readers/Ipynb.hs @@ -18,6 +18,7 @@ Ipynb (Jupyter notebook JSON format) reader for pandoc. module Text.Pandoc.Readers.Ipynb ( readIpynb ) where import Prelude +import Data.Char (isDigit) import Data.List (isPrefixOf) import Data.Maybe (fromMaybe) import Data.Digest.Pure.SHA (sha1, showDigest) @@ -39,6 +40,7 @@ import qualified Data.ByteString.Lazy as BL import Data.Aeson as Aeson import Control.Monad.Except (throwError) import Text.Pandoc.Readers.Markdown (readMarkdown) +import qualified Text.Pandoc.UTF8 as UTF8 readIpynb :: PandocMonad m => ReaderOptions -> Text -> m Pandoc readIpynb opts t = do @@ -217,10 +219,11 @@ jsonMetaToMeta = M.mapKeys T.unpack . M.map valueToMetaValue valueToMetaValue Aeson.Null = MetaString "" jsonMetaToPairs :: JSONMeta -> [(String, String)] -jsonMetaToPairs = M.toList . M.mapMaybe - (\case - MetaString s -> Just s - MetaBool True -> Just "true" - MetaBool False -> Just "false" - -- for now we skip complex cell metadata: - _ -> Nothing) . jsonMetaToMeta +jsonMetaToPairs = M.toList . M.mapKeys T.unpack . M.map + (\case + String t + | not (T.all isDigit t) + , t /= "true" + , t /= "false" + -> T.unpack t + x -> UTF8.toStringLazy $ Aeson.encode x) diff --git a/src/Text/Pandoc/Writers/Ipynb.hs b/src/Text/Pandoc/Writers/Ipynb.hs index 661f585f4..0babf7112 100644 --- a/src/Text/Pandoc/Writers/Ipynb.hs +++ b/src/Text/Pandoc/Writers/Ipynb.hs @@ -213,10 +213,9 @@ extractData bs = do pairsToJSONMeta :: [(String, String)] -> JSONMeta pairsToJSONMeta kvs = - M.fromList [(T.pack k, case v of - "true" -> Bool True - "false" -> Bool False - _ -> case safeRead v of - Just n -> Number n - _ -> String (T.pack v)) - | (k,v) <- kvs , k /= "execution_count" ] + M.fromList [(T.pack k, case Aeson.decode (UTF8.fromStringLazy v) of + Just val -> val + Nothing -> String (T.pack v)) + | (k,v) <- kvs + , k /= "execution_count" + ] |