From efbc2050315b60c8a753dee6255465f1083019ab Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 30 Jun 2020 12:12:55 -0700 Subject: 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. --- src/Text/Pandoc/Readers/Ipynb.hs | 3 ++- src/Text/Pandoc/Writers/Ipynb.hs | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/Text/Pandoc') diff --git a/src/Text/Pandoc/Readers/Ipynb.hs b/src/Text/Pandoc/Readers/Ipynb.hs index a245bdad3..056fa431c 100644 --- a/src/Text/Pandoc/Readers/Ipynb.hs +++ b/src/Text/Pandoc/Readers/Ipynb.hs @@ -79,7 +79,8 @@ cellToBlocks opts lang c = do case cellType c of Ipynb.Markdown -> do Pandoc _ bs <- walk fixImage <$> readMarkdown opts source - return $ B.divWith ("",["cell","markdown"],kvs) + let kvs' = ("source", source) : kvs + return $ B.divWith ("",["cell","markdown"],kvs') $ B.fromList bs Ipynb.Heading lev -> do Pandoc _ bs <- readMarkdown opts diff --git a/src/Text/Pandoc/Writers/Ipynb.hs b/src/Text/Pandoc/Writers/Ipynb.hs index d01d5a7e5..86e2abbdf 100644 --- a/src/Text/Pandoc/Writers/Ipynb.hs +++ b/src/Text/Pandoc/Writers/Ipynb.hs @@ -102,10 +102,13 @@ extractCells _ [] = return [] extractCells opts (Div (_id,classes,kvs) xs : bs) | "cell" `elem` classes , "markdown" `elem` classes = do - let meta = pairsToJSONMeta kvs + let meta = pairsToJSONMeta [(k,v) | (k,v) <- kvs, k /= "source"] (newdoc, attachments) <- runStateT (walkM addAttachment (Pandoc nullMeta xs)) mempty - source <- writeMarkdown opts{ writerTemplate = Nothing } newdoc + source <- case lookup "source" kvs of + Just s -> return s + Nothing -> writeMarkdown opts{ writerTemplate = Nothing } + newdoc (Ipynb.Cell{ cellType = Markdown , cellSource = Source $ breakLines $ T.stripEnd source -- cgit v1.2.3