diff --git a/src/Text/Pandoc/Writers/ICML.hs b/src/Text/Pandoc/Writers/ICML.hs index 1d766ab13072..d5a52ec90a76 100644 --- a/src/Text/Pandoc/Writers/ICML.hs +++ b/src/Text/Pandoc/Writers/ICML.hs @@ -17,9 +17,9 @@ into InDesign with File -> Place. -} module Text.Pandoc.Writers.ICML (writeICML) where import Control.Monad.Except (catchError) -import Control.Monad (liftM2) +import Control.Monad (liftM2, when) import Control.Monad.State.Strict - ( MonadTrans(lift), StateT(runStateT), MonadState(state, get, put) ) + ( MonadTrans(lift), StateT(runStateT), MonadState(state, get, put), gets, modify ) import Data.List (intersperse) import Data.Maybe (fromMaybe, maybeToList) import qualified Data.Set as Set @@ -47,6 +47,7 @@ data WriterState = WriterState{ , links :: Hyperlink , listDepth :: Int , maxListDepth :: Int + , firstPara :: Bool } type WS m = StateT WriterState m @@ -58,8 +59,12 @@ defaultWriterState = WriterState{ , links = [] , listDepth = 1 , maxListDepth = 0 + , firstPara = False } +setFirstPara :: PandocMonad m => WS m () +setFirstPara = modify $ \s -> s { firstPara = True } + -- inline names (appear in InDesign's character styles pane) emphName :: Text underlineName :: Text @@ -82,6 +87,8 @@ linkName = "Link" -- block element names (appear in InDesign's paragraph styles pane) paragraphName :: Text +firstParagraphName :: Text +bibliographyName :: Text figureName :: Text imgCaptionName :: Text codeBlockName :: Text @@ -107,6 +114,8 @@ subListParName :: Text footnoteName :: Text citeName :: Text paragraphName = "Paragraph" +firstParagraphName = "FirstParagraph" +bibliographyName = "Bibliography" figureName = "Figure" imgCaptionName = "Caption" codeBlockName = "CodeBlock" @@ -145,7 +154,8 @@ writeICML opts doc = do (renderBlockMeta blocksToICML) (renderInlineMeta inlinesToICML) meta - (main, st) <- runStateT (blocksToICML opts [] blocks) defaultWriterState + (main, st) <- runStateT (setFirstPara >> blocksToICML opts [] blocks) + defaultWriterState let context = defField "body" main $ defField "charStyles" (charStylesToDoc st) $ defField "parStyles" (parStylesToDoc st) @@ -316,26 +326,48 @@ blocksToICML opts style lst = do -- | Convert a Pandoc block element to ICML. blockToICML :: PandocMonad m => WriterOptions -> Style -> Block -> WS m (Doc Text) blockToICML opts style (Plain lst) = parStyle opts style "" lst -blockToICML opts style (Para lst) = parStyle opts (paragraphName:style) "" lst +blockToICML opts style (Para lst) = do + isfirst <- gets firstPara + modify $ \s -> s{ firstPara = False } + parStyle opts ((if isfirst + then firstParagraphName + else paragraphName):style) "" lst blockToICML opts style (LineBlock lns) = blockToICML opts style $ linesToPara lns -blockToICML opts style (CodeBlock _ str) = parStyle opts (codeBlockName:style) "" [Str str] +blockToICML opts style (CodeBlock _ str) = do + setFirstPara + parStyle opts (codeBlockName:style) "" [Str str] blockToICML _ _ b@(RawBlock f str) | f == Format "icml" = return $ literal str | otherwise = do report $ BlockNotRendered b return empty -blockToICML opts style (BlockQuote blocks) = blocksToICML opts (blockQuoteName:style) blocks -blockToICML opts style (OrderedList attribs lst) = listItemsToICML opts orderedListName style (Just attribs) lst -blockToICML opts style (BulletList lst) = listItemsToICML opts bulletListName style Nothing lst -blockToICML opts style (DefinitionList lst) = intersperseBrs `fmap` mapM (definitionListItemToICML opts style) lst -blockToICML opts style (Header lvl (ident, cls, _) lst) = +blockToICML opts style (BlockQuote blocks) = do + result <- blocksToICML opts (blockQuoteName:style) blocks + setFirstPara + return result +blockToICML opts style (OrderedList attribs lst) = do + result <- listItemsToICML opts orderedListName style (Just attribs) lst + setFirstPara + return result +blockToICML opts style (BulletList lst) = do + result <- listItemsToICML opts bulletListName style Nothing lst + setFirstPara + return result +blockToICML opts style (DefinitionList lst) = do + result <- intersperseBrs `fmap` mapM (definitionListItemToICML opts style) lst + setFirstPara + return result +blockToICML opts style (Header lvl (ident, cls, _) lst) = do let stl = (headerName <> tshow lvl <> unnumbered):style unnumbered = if "unnumbered" `elem` cls then " (unnumbered)" else "" - in parStyle opts stl ident lst -blockToICML _ _ HorizontalRule = return empty -- we could insert a page break instead + setFirstPara + parStyle opts stl ident lst +blockToICML _ _ HorizontalRule = do + setFirstPara + return empty -- we could insert a page break instead blockToICML opts style (Table attr blkCapt specs thead tbody tfoot) = let (caption, aligns, widths, headers, rows) = toLegacyTable blkCapt specs thead tbody tfoot @@ -382,20 +414,24 @@ blockToICML opts style (Table attr blkCapt specs thead tbody tfoot) = , ("BodyRowCount", tshow nrRows) , ("ColumnCount", tshow nrCols) ] (colDescs $$ cells) - liftM2 ($$) tableDoc $ parStyle opts (tableCaptionName:style) "" caption -blockToICML opts style (Div (_ident, _, kvs) lst) = + result <- liftM2 ($$) tableDoc $ parStyle opts (tableCaptionName:style) "" caption + setFirstPara + return result +blockToICML opts style (Div (_ident, cls, kvs) lst) = do let dynamicStyle = maybeToList $ lookup dynamicStyleKey kvs - in blocksToICML opts (dynamicStyle <> style) lst -blockToICML opts style (Figure attr capt@(Caption _ longcapt) body) = - case body of + let bibStyle = [bibliographyName | "csl-entry" `elem` cls] + blocksToICML opts (bibStyle <> dynamicStyle <> style) lst +blockToICML opts style (Figure attr capt@(Caption _ longcapt) body) = do + result <- case body of [Plain [img@(Image {})]] -> do figure <- parStyle opts (figureName:style) "" [img] caption <- parStyle opts (imgCaptionName:style) "" $ blocksToInlines longcapt return $ intersperseBrs [figure, caption] - _ -> -- fallback to rendering the figure as a Div + _ -> do -- fallback to rendering the figure as a Div blockToICML opts style $ figureDiv attr capt body - + setFirstPara + return result -- | Convert a list of lists of blocks to ICML list items. listItemsToICML :: PandocMonad m => WriterOptions -> Text -> Style -> Maybe ListAttributes -> [[Block]] -> WS m (Doc Text) @@ -474,7 +510,8 @@ inlineToICML opts style ident SoftBreak = WrapNone -> charStyle style ident space WrapPreserve -> charStyle style ident cr inlineToICML _ style ident LineBreak = charStyle style ident $ literal lineSeparator -inlineToICML opts style ident (Math mt str) = +inlineToICML opts style ident (Math mt str) = do + when (mt == DisplayMath) setFirstPara lift (texMathToInlines mt str) >>= (fmap mconcat . mapM (inlineToICML opts style ident)) inlineToICML _ _ _ il@(RawInline f str) diff --git a/test/command/5541-localLink.md b/test/command/5541-localLink.md index 077005160e0f..9579dc00ee00 100644 --- a/test/command/5541-localLink.md +++ b/test/command/5541-localLink.md @@ -40,17 +40,17 @@ if you can read this text, [and it's linked]{#spanner} - all good! - + $ID/NormalParagraphStyle - + $ID/NormalParagraphStyle - + $ID/NormalParagraphStyle @@ -77,7 +77,7 @@ if you can read this text, [and it's linked]{#spanner} - all good!
- + this is some text @@ -90,7 +90,7 @@ if you can read this text, [and it's linked]{#spanner} - all good!
- + some more text that @@ -119,7 +119,7 @@ if you can read this text, [and it's linked]{#spanner} - all good!
- + if you can read this text, diff --git a/test/command/5541-urlLink.md b/test/command/5541-urlLink.md index 453653ffca35..00fa750f59e6 100644 --- a/test/command/5541-urlLink.md +++ b/test/command/5541-urlLink.md @@ -36,17 +36,17 @@ some more text that [links to](https://www.pandoc.org) Pandoc.
- + $ID/NormalParagraphStyle - + $ID/NormalParagraphStyle - + $ID/NormalParagraphStyle @@ -73,7 +73,7 @@ some more text that [links to](https://www.pandoc.org) Pandoc.
- + this is some text @@ -86,7 +86,7 @@ some more text that [links to](https://www.pandoc.org) Pandoc.
- + some more text that diff --git a/test/command/6675.md b/test/command/6675.md index d2984571feed..6d4d2207bb84 100644 --- a/test/command/6675.md +++ b/test/command/6675.md @@ -38,6 +38,11 @@ and some text that [links to](#header-1) the first header
+ + + $ID/NormalParagraphStyle + + $ID/NormalParagraphStyle @@ -75,7 +80,7 @@ and some text that [links to](#header-1) the first header
- + this is some text @@ -88,7 +93,7 @@ and some text that [links to](#header-1) the first header
- + some more text that diff --git a/test/command/svg.md b/test/command/svg.md index 74a2a1eed1cc..b4ef3c5aa143 100644 --- a/test/command/svg.md +++ b/test/command/svg.md @@ -3,7 +3,7 @@ \includegraphics{command/corrupt.svg} ^D 2> [WARNING] Could not determine image size for command/corrupt.svg: could not determine image type - + @@ -36,7 +36,7 @@ % pandoc -f latex -t icml \includegraphics{command/SVG_logo.svg} ^D - + @@ -69,7 +69,7 @@ % pandoc -f latex -t icml \includegraphics{command/SVG_logo-without-xml-declaration.svg} ^D - + @@ -103,7 +103,7 @@ % pandoc -f latex -t icml \includegraphics{command/inkscape-cube.svg} ^D - + diff --git a/test/tables.icml b/test/tables.icml index 10945ef46168..5d51100d3fbf 100644 --- a/test/tables.icml +++ b/test/tables.icml @@ -1,4 +1,4 @@ - + Simple table with caption: @@ -128,7 +128,7 @@
- + Simple table without caption: @@ -255,7 +255,7 @@
- + Simple table indented two spaces: @@ -385,7 +385,7 @@

- + Multiline table with caption: @@ -487,7 +487,7 @@

- + Multiline table without caption: @@ -586,7 +586,7 @@
- + Table without column headers: @@ -685,7 +685,7 @@
- + Multiline table without column headers: diff --git a/test/writer.icml b/test/writer.icml index 0601ba494208..d6955b1319bd 100644 --- a/test/writer.icml +++ b/test/writer.icml @@ -85,6 +85,11 @@ $ID/NormalParagraphStyle
+ + + $ID/NormalParagraphStyle + + $ID/NormalParagraphStyle @@ -96,6 +101,11 @@ Courier New + + + $ID/NormalParagraphStyle + + $ID/NormalParagraphStyle @@ -202,6 +212,19 @@ + + + $ID/NormalParagraphStyle + + + LeftAlign + . + + 10 + + + + $ID/NormalParagraphStyle @@ -231,7 +254,7 @@ $ID/NormalParagraphStyle - + $ID/NormalParagraphStyle @@ -267,12 +290,22 @@ $ID/NormalParagraphStyle + + + $ID/NormalParagraphStyle + + $ID/NormalParagraphStyle Courier New + + + $ID/NormalParagraphStyle + + $ID/NormalParagraphStyle @@ -334,6 +367,11 @@ + + + $ID/NormalParagraphStyle + + $ID/NormalParagraphStyle @@ -400,7 +438,7 @@ $ID/NormalParagraphStyle - + $ID/NormalParagraphStyle @@ -436,7 +474,7 @@ - + This is a set of tests for pandoc. Most of them are adapted from John Gruber’s markdown test suite. @@ -512,7 +550,7 @@
- + with no blank line @@ -525,7 +563,7 @@
- + with no blank line @@ -538,7 +576,7 @@
- + Here’s a regular paragraph. @@ -575,7 +613,7 @@
- + E-mail style: @@ -587,7 +625,7 @@
- + Code in a block quote: @@ -601,7 +639,7 @@
- + A list: @@ -619,7 +657,7 @@
- + Nested block quotes: @@ -631,13 +669,13 @@
- + nested
- + This should not be a block quote: 2 > 1. @@ -656,7 +694,7 @@
- + Code: @@ -674,7 +712,7 @@ this code block is indented by one tab
- + And: @@ -702,7 +740,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Asterisks tight: @@ -726,7 +764,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Asterisks loose: @@ -750,7 +788,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Pluses tight: @@ -774,7 +812,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Pluses loose: @@ -798,7 +836,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Minuses tight: @@ -822,7 +860,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Minuses loose: @@ -853,7 +891,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Tight: @@ -877,7 +915,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + and: @@ -901,7 +939,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Loose using tabs: @@ -925,7 +963,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + and using spaces: @@ -949,7 +987,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Multiple paragraphs: @@ -1004,7 +1042,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Here’s another: @@ -1046,7 +1084,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Same thing but with paragraphs: @@ -1082,7 +1120,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Third @@ -1095,7 +1133,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + this is a list item indented with tabs @@ -1126,7 +1164,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + begins with 2 @@ -1168,7 +1206,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Nesting: @@ -1198,7 +1236,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Autonumbering: @@ -1222,7 +1260,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Should not be a list item: @@ -1247,7 +1285,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Tight using spaces: @@ -1289,7 +1327,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Tight using tabs: @@ -1331,7 +1369,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Loose: @@ -1373,7 +1411,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Multiple blocks with italics: @@ -1415,13 +1453,13 @@ These should not be escaped: \$ \\ \> \[ \{
- + orange block quote
- + Multiple definitions, tight: @@ -1463,7 +1501,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Multiple definitions, loose: @@ -1505,7 +1543,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Blank line after term, indented marker, alternate markers: @@ -1560,7 +1598,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Simple block on one line: @@ -1640,7 +1678,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + As should this: @@ -1652,7 +1690,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Now, nested: @@ -1688,7 +1726,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Just plain comment, with trailing spaces on the line: @@ -1706,7 +1744,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Hr’s: @@ -1719,7 +1757,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + This is @@ -1914,7 +1952,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + “Hello,” @@ -2156,7 +2194,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + These shouldn’t be math: @@ -2216,7 +2254,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Here’s a LaTeX table: @@ -2229,7 +2267,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Here is some unicode: @@ -2265,7 +2303,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + AT&T has an ampersand in their name. @@ -2405,7 +2443,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Just a @@ -2502,7 +2540,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Foo @@ -2595,7 +2633,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Foo @@ -2630,7 +2668,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Here’s a @@ -2693,7 +2731,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + With an ampersand: @@ -2724,7 +2762,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + An e-mail address: @@ -2746,7 +2784,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Auto-links should not occur here: @@ -2768,7 +2806,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + From @@ -2814,7 +2852,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Here is a movie @@ -2855,7 +2893,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + Here is a footnote reference, @@ -2901,7 +2939,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + If you want, you can indent every line, but you can also be lazy and just indent the first line of each block. @@ -2984,7 +3022,7 @@ These should not be escaped: \$ \\ \> \[ \{ - + In list. @@ -2993,7 +3031,7 @@ These should not be escaped: \$ \\ \> \[ \{
- + This paragraph should not be part of the note, as it is not indented.