From f3a9bdafef45ef0990d302207410f014d75d9860 Mon Sep 17 00:00:00 2001
From: mb21 <mb21@users.noreply.github.com>
Date: Wed, 16 Dec 2015 10:18:41 +0100
Subject: [PATCH] ICML writer: added figure handling, closes #2590

---
 src/Text/Pandoc/Writers/ICML.hs | 15 ++++++++++++---
 tests/writer.icml               | 17 ++++++++++++++++-
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/Text/Pandoc/Writers/ICML.hs b/src/Text/Pandoc/Writers/ICML.hs
index db837571e..3b0d072f6 100644
--- a/src/Text/Pandoc/Writers/ICML.hs
+++ b/src/Text/Pandoc/Writers/ICML.hs
@@ -71,6 +71,8 @@ linkName        = "Link"
 
 -- block element names (appear in InDesign's paragraph styles pane)
 paragraphName     :: String
+figureName        :: String
+imgCaptionName    :: String
 codeBlockName     :: String
 blockQuoteName    :: String
 orderedListName   :: String
@@ -94,6 +96,8 @@ subListParName    :: String
 footnoteName      :: String
 citeName          :: String
 paragraphName     = "Paragraph"
+figureName        = "Figure"
+imgCaptionName    = "Caption"
 codeBlockName     = "CodeBlock"
 blockQuoteName    = "Blockquote"
 orderedListName   = "NumList"
@@ -285,6 +289,11 @@ blocksToICML opts style lst = vcat `fmap` mapM (blockToICML opts style) lst
 -- | Convert a Pandoc block element to ICML.
 blockToICML :: WriterOptions -> Style -> Block -> WS Doc
 blockToICML opts style (Plain lst) = parStyle opts style lst
+-- title beginning with fig: indicates that the image is a figure
+blockToICML opts style (Para img@[Image _ txt (_,'f':'i':'g':':':_)]) = do
+  figure  <- parStyle opts (figureName:style) img
+  caption <- parStyle opts (imgCaptionName:style) txt
+  return $ figure $$ caption
 blockToICML opts style (Para lst) = parStyle opts (paragraphName:style) lst
 blockToICML opts style (CodeBlock _ str) = parStyle opts (codeBlockName:style) $ [Str str]
 blockToICML _ _ (RawBlock f str)
@@ -434,7 +443,7 @@ inlineToICML opts style (Link _ lst (url, title)) = do
                 cont  = inTags True "HyperlinkTextSource"
                          [("Self","htss-"++show ident), ("Name",title), ("Hidden","false")] content
             in  (cont, newst)
-inlineToICML opts style (Image attr alt target) = imageICML opts style attr alt target
+inlineToICML opts style (Image attr _ target) = imageICML opts style attr target
 inlineToICML opts style (Note lst) = footnoteToICML opts style lst
 inlineToICML opts style (Span _ lst) = inlinesToICML opts style lst
 
@@ -513,8 +522,8 @@ styleToStrAttr style =
   in  (stlStr, attrs)
 
 -- | Assemble an ICML Image.
-imageICML :: WriterOptions -> Style -> Attr -> [Inline] -> Target -> WS Doc
-imageICML opts style attr _ (src, _) = do
+imageICML :: WriterOptions -> Style -> Attr -> Target -> WS Doc
+imageICML opts style attr (src, _) = do
   res  <- liftIO $ fetchItem (writerSourceURL opts) src
   imgS <- case res of
             Left (_) -> do
diff --git a/tests/writer.icml b/tests/writer.icml
index 57f9d2f8a..d3e37a06d 100644
--- a/tests/writer.icml
+++ b/tests/writer.icml
@@ -215,6 +215,11 @@
           </TabList>
         </Properties>
       </ParagraphStyle>
+      <ParagraphStyle Self="ParagraphStyle/Caption" Name="Caption" LeftIndent="0">
+        <Properties>
+          <BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
+        </Properties>
+      </ParagraphStyle>
       <ParagraphStyle Self="ParagraphStyle/CodeBlock" Name="CodeBlock" LeftIndent="0">
         <Properties>
           <BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
@@ -257,6 +262,11 @@
           <BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
         </Properties>
       </ParagraphStyle>
+      <ParagraphStyle Self="ParagraphStyle/Figure" Name="Figure" LeftIndent="0">
+        <Properties>
+          <BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
+        </Properties>
+      </ParagraphStyle>
       <ParagraphStyle Self="ParagraphStyle/Footnote &gt; CodeBlock" Name="Footnote &gt; CodeBlock" LeftIndent="0">
         <Properties>
           <BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
@@ -2542,7 +2552,7 @@ These should not be escaped:  \$ \\ \&gt; \[ \{</Content>
     <Content> by Georges Melies (1902):</Content>
   </CharacterStyleRange><Br />
 </ParagraphStyleRange>
-<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
+<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Figure">
   <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
     <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 75.00000 -75.00000">
       <Properties>
@@ -2568,6 +2578,11 @@ These should not be escaped:  \$ \\ \&gt; \[ \{</Content>
     </Rectangle>
   </CharacterStyleRange><Br />
 </ParagraphStyleRange>
+<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Caption">
+  <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
+    <Content>lalune</Content>
+  </CharacterStyleRange><Br />
+</ParagraphStyleRange>
 <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
   <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
     <Content>Here is a movie </Content>