From acab2087bb7ad5ebeaec79e451e95c97da505642 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 29 Mar 2019 21:08:00 -0700
Subject: [PATCH] ipynb writer - consolidate adjacent raw blocks.

Sometimes pandoc creates two HTML blocks, e.g. one for
the open tag and one for a close tag.  If these aren't
consolidated, only one will show up in output cell.
---
 src/Text/Pandoc/Writers/Ipynb.hs | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/Text/Pandoc/Writers/Ipynb.hs b/src/Text/Pandoc/Writers/Ipynb.hs
index f31cc38fd..2a95f669e 100644
--- a/src/Text/Pandoc/Writers/Ipynb.hs
+++ b/src/Text/Pandoc/Writers/Ipynb.hs
@@ -133,7 +133,7 @@ extractCells opts (Div (_id,classes,kvs) xs : bs)
         , cellAttachments = Nothing } :) <$> extractCells opts bs
   | "cell" `elem` classes
   , "raw" `elem` classes =
-      case xs of
+      case consolidateAdjacentRawBlocks xs of
         [RawBlock (Format f) raw] -> do
           let format' =
                 case f of
@@ -195,7 +195,7 @@ blockToOutput _ = return Nothing
 
 extractData :: PandocMonad m => [Block] -> m (MimeBundle, JSONMeta)
 extractData bs = do
-  (mmap, meta) <- foldM go mempty bs
+  (mmap, meta) <- foldM go mempty $ consolidateAdjacentRawBlocks bs
   return (MimeBundle mmap, meta)
   where
     go (mmap, meta) b@(Para [Image (_,_,kvs) _ (src,_)]) = do
@@ -227,3 +227,11 @@ pairsToJSONMeta kvs =
              | (k,v) <- kvs
              , k /= "execution_count"
              ]
+
+consolidateAdjacentRawBlocks :: [Block] -> [Block]
+consolidateAdjacentRawBlocks [] = []
+consolidateAdjacentRawBlocks (RawBlock f1 x : RawBlock f2 y : xs)
+  | f1 == f2
+  = consolidateAdjacentRawBlocks (RawBlock f1 (x <> "\n" <> y) : xs)
+consolidateAdjacentRawBlocks (x : xs) =
+  x : consolidateAdjacentRawBlocks xs