git.iko.soy/typeable/pandoc
on master help sign in
Ipnyb: allow lossless round-tripping of markdown cell content.

The reader now parses the contents of the markdown cell to a Pandoc structure, but *also* stores the raw markdown in a `source` attribute on the cell Div. When we convert back to markdown, this attribute is stripped off and the original source is used. When we convert to other formats, the attribute is usually ignored (though it will come through in HTML as a `data-source` attribute, not unhelpfully). I'll note some potential drawbacks of this approach: - It makes it impossible to use pandoc to clean up or change the contents of markdown cells, e.g. going from `+smart` to `-smart`. - There may be formats where the addition of the `source` attribute is problematic. I can't think of any, though. Closes #5408.

commit02dfcdafb9ee237276dd29a556346b86e78587fd authorJohn MacFarlane <jgm@berkeley.edu> dateTue 30 Jun 2020, 12:12 (6 years ago) parentd3db10b
3 files changed+10−6
src/Text/Pandoc/Readers/Ipynb.hs+2−1 src/Text/Pandoc/Writers/Ipynb.hs+5−2 test/ipynb/simple.out.native+3−3
src/Text/Pandoc/Readers/Ipynb.hs+2−1
@@ -79,7 +79,8 @@ cellToBlocks opts lang c = do
7979 case cellType c of
8080 Ipynb.Markdown -> do
8181 Pandoc _ bs <- walk fixImage <$> readMarkdown opts source
82− return $ B.divWith ("",["cell","markdown"],kvs)
82+ let kvs' = ("source", source) : kvs
83+ return $ B.divWith ("",["cell","markdown"],kvs')
8384 $ B.fromList bs
8485 Ipynb.Heading lev -> do
8586 Pandoc _ bs <- readMarkdown opts
src/Text/Pandoc/Writers/Ipynb.hs+5−2
@@ -102,10 +102,13 @@ extractCells _ [] = return []
102102extractCells opts (Div (_id,classes,kvs) xs : bs)
103103 | "cell" `elem` classes
104104 , "markdown" `elem` classes = do
105− let meta = pairsToJSONMeta kvs
105+ let meta = pairsToJSONMeta [(k,v) | (k,v) <- kvs, k /= "source"]
106106 (newdoc, attachments) <-
107107 runStateT (walkM addAttachment (Pandoc nullMeta xs)) mempty
108− source <- writeMarkdown opts{ writerTemplate = Nothing } newdoc
108+ source <- case lookup "source" kvs of
109+ Just s -> return s
110+ Nothing -> writeMarkdown opts{ writerTemplate = Nothing }
111+ newdoc
109112 (Ipynb.Cell{
110113 cellType = Markdown
111114 , cellSource = Source $ breakLines $ T.stripEnd source
test/ipynb/simple.out.native+3−3
@@ -1,15 +1,15 @@
11Pandoc (Meta {unMeta = fromList [("jupyter",MetaMap (fromList [("nbformat",MetaString "4"),("nbformat_minor",MetaString "5")]))]})
2−[Div ("",["cell","markdown"],[])
2+[Div ("",["cell","markdown"],[("source","Lorem ipsum\n===========\n\n**Lorem ipsum** dolor sit amet, consectetur adipiscing elit. Nunc luctus\nbibendum felis dictum sodales.")])
33 [Header 1 ("lorem-ipsum",[],[]) [Str "Lorem",Space,Str "ipsum"]
44 ,Para [Strong [Str "Lorem",Space,Str "ipsum"],Space,Str "dolor",Space,Str "sit",Space,Str "amet,",Space,Str "consectetur",Space,Str "adipiscing",Space,Str "elit.",Space,Str "Nunc",Space,Str "luctus",SoftBreak,Str "bibendum",Space,Str "felis",Space,Str "dictum",Space,Str "sodales."]]
55,Div ("",["cell","code"],[])
66 [CodeBlock ("",["python"],[]) "print(\"hello\")"]
7−,Div ("",["cell","markdown"],[])
7+,Div ("",["cell","markdown"],[("source","Pyout\n-----")])
88 [Header 2 ("pyout",[],[]) [Str "Pyout"]]
99,Div ("",["cell","code"],[("execution_count","2")])
1010 [CodeBlock ("",["python"],[]) "from IPython.display import HTML\nHTML(\"\"\"\n<script>\nconsole.log(\"hello\");\n</script>\n<b>HTML</b>\n\"\"\")"
1111 ,Div ("",["output","execute_result"],[("execution_count","2")])
1212 [RawBlock (Format "html") "<script>\nconsole.log(\"hello\");\n</script>\n<b>HTML</b>\nhello"]]
13−,Div ("",["cell","markdown"],[("tags","[\"foo\",\"bar\"]")])
13+,Div ("",["cell","markdown"],[("source","Image\n-----\n\nThis image ![the moon](attachment:lalune.jpg) will be included as a cell\nattachment."),("tags","[\"foo\",\"bar\"]")])
1414 [Header 2 ("image",[],[]) [Str "Image"]
1515 ,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."]]]