git.iko.soy/typeable/pandoc
on master help sign in
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.

commit873f342f1144111f79f9415abd3dc00d44a7a3c6 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 9 Mar 2019, 14:17 (8 years ago) parentbf4a164
5 files changed+24−17
src/Text/Pandoc/Readers/Ipynb.hs+10−7 src/Text/Pandoc/Writers/Ipynb.hs+6−7 test/ipynb/simple.in.native+1−1 test/ipynb/simple.ipynb+6−1 test/ipynb/simple.out.native+1−1
src/Text/Pandoc/Readers/Ipynb.hs+10−7
@@ -18,6 +18,7 @@ Ipynb (Jupyter notebook JSON format) reader for pandoc.
1818module Text.Pandoc.Readers.Ipynb ( readIpynb )
1919where
2020import Prelude
21+import Data.Char (isDigit)
2122import Data.List (isPrefixOf)
2223import Data.Maybe (fromMaybe)
2324import Data.Digest.Pure.SHA (sha1, showDigest)
@@ -39,6 +40,7 @@ import qualified Data.ByteString.Lazy as BL
3940import Data.Aeson as Aeson
4041import Control.Monad.Except (throwError)
4142import Text.Pandoc.Readers.Markdown (readMarkdown)
43+import qualified Text.Pandoc.UTF8 as UTF8
4244
4345readIpynb :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
4446readIpynb opts t = do
@@ -217,10 +219,11 @@ jsonMetaToMeta = M.mapKeys T.unpack . M.map valueToMetaValue
217219 valueToMetaValue Aeson.Null = MetaString ""
218220
219221jsonMetaToPairs :: JSONMeta -> [(String, String)]
220−jsonMetaToPairs = M.toList . M.mapMaybe
221− (\case
222− MetaString s -> Just s
223− MetaBool True -> Just "true"
224− MetaBool False -> Just "false"
225− -- for now we skip complex cell metadata:
226− _ -> Nothing) . jsonMetaToMeta
222+jsonMetaToPairs = M.toList . M.mapKeys T.unpack . M.map
223+ (\case
224+ String t
225+ | not (T.all isDigit t)
226+ , t /= "true"
227+ , t /= "false"
228+ -> T.unpack t
229+ x -> UTF8.toStringLazy $ Aeson.encode x)
src/Text/Pandoc/Writers/Ipynb.hs+6−7
@@ -213,10 +213,9 @@ extractData bs = do
213213
214214pairsToJSONMeta :: [(String, String)] -> JSONMeta
215215pairsToJSONMeta kvs =
216− M.fromList [(T.pack k, case v of
217− "true" -> Bool True
218− "false" -> Bool False
219− _ -> case safeRead v of
220− Just n -> Number n
221− _ -> String (T.pack v))
222− | (k,v) <- kvs , k /= "execution_count" ]
216+ M.fromList [(T.pack k, case Aeson.decode (UTF8.fromStringLazy v) of
217+ Just val -> val
218+ Nothing -> String (T.pack v))
219+ | (k,v) <- kvs
220+ , k /= "execution_count"
221+ ]
test/ipynb/simple.in.native+1−1
@@ -8,6 +8,6 @@ Pandoc (Meta {unMeta = fromList [("jupyter",MetaMap (fromList [("nbformat",MetaI
88 [Header 2 ("pyout",[],[]) [Str "Pyout"]]
99,Div ("",["cell","code"],[])
1010 [CodeBlock ("",["python"],[]) "from IPython.display import HTML\nHTML(\"\"\"\n<script>\nconsole.log(\"hello\");\n</script>\n<b>HTML</b>\n\"\"\")"]
11−,Div ("",["cell","markdown"],[])
11+,Div ("",["cell","markdown"],[("tags","[\"foo\",\"bar\"]")])
1212 [Header 2 ("image",[],[]) [Str "Image"]
1313 ,Para [Str "This",Space,Str "image",Space,Image ("",[],[]) [Str "the",Space,Str "moon"] ("lalune.jpg",""),Space,Str "will",Space,Str "be",Space,Str "included",Space,Str "as",Space,Str "a",Space,Str "cell",SoftBreak,Str "attachment."]]]
test/ipynb/simple.ipynb+6−1
@@ -45,7 +45,12 @@
4545 },
4646 {
4747 "cell_type": "markdown",
48− "metadata": {},
48+ "metadata": {
49+ "tags": [
50+ "foo",
51+ "bar"
52+ ]
53+ },
4954 "source": [
5055 "Image\n",
5156 "-----\n",
test/ipynb/simple.out.native+1−1
@@ -8,6 +8,6 @@ Pandoc (Meta {unMeta = fromList [("jupyter",MetaMap (fromList [("nbformat",MetaS
88 [Header 2 ("pyout",[],[]) [Str "Pyout"]]
99,Div ("",["cell","code"],[])
1010 [CodeBlock ("",["python"],[]) "from IPython.display import HTML\nHTML(\"\"\"\n<script>\nconsole.log(\"hello\");\n</script>\n<b>HTML</b>\n\"\"\")"]
11−,Div ("",["cell","markdown"],[])
11+,Div ("",["cell","markdown"],[("tags","[\"foo\",\"bar\"]")])
1212 [Header 2 ("image",[],[]) [Str "Image"]
1313 ,Para [Str "This",Space,Str "image",Space,Image ("",[],[]) [Str "the",Space,Str "moon"] ("lalune.jpg",""),Space,Str "will",Space,Str "be",Space,Str "included",Space,Str "as",Space,Str "a",Space,Str "cell",SoftBreak,Str "attachment."]]]