Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 57 additions & 20 deletions src/Text/Pandoc/Writers/ICML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +47,7 @@ data WriterState = WriterState{
, links :: Hyperlink
, listDepth :: Int
, maxListDepth :: Int
, firstPara :: Bool
}

type WS m = StateT WriterState m
Expand All @@ -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
Expand All @@ -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
Expand All @@ -107,6 +114,8 @@ subListParName :: Text
footnoteName :: Text
citeName :: Text
paragraphName = "Paragraph"
firstParagraphName = "FirstParagraph"
bibliographyName = "Bibliography"
figureName = "Figure"
imgCaptionName = "Caption"
codeBlockName = "CodeBlock"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions test/command/5541-localLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ if you can read this text, [and it's linked]{#spanner} - all good!
</TabList>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/Header1" Name="Header1" LeftIndent="0" PointSize="36">
<ParagraphStyle Self="ParagraphStyle/FirstParagraph" Name="FirstParagraph" LeftIndent="0">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/Header2" Name="Header2" LeftIndent="0" PointSize="30">
<ParagraphStyle Self="ParagraphStyle/Header1" Name="Header1" LeftIndent="0" PointSize="36">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/Paragraph" Name="Paragraph" LeftIndent="0">
<ParagraphStyle Self="ParagraphStyle/Header2" Name="Header2" LeftIndent="0" PointSize="30">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
</Properties>
Expand All @@ -77,7 +77,7 @@ if you can read this text, [and it's linked]{#spanner} - all good!
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>this is some text</Content>
</CharacterStyleRange>
Expand All @@ -90,7 +90,7 @@ if you can read this text, [and it's linked]{#spanner} - all good!
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>some more text that </Content>
</CharacterStyleRange>
Expand Down Expand Up @@ -119,7 +119,7 @@ if you can read this text, [and it's linked]{#spanner} - all good!
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>if you can read this text, </Content>
</CharacterStyleRange>
Expand Down
10 changes: 5 additions & 5 deletions test/command/5541-urlLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ some more text that [links to](https://www.pandoc.org) Pandoc.
</TabList>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/Header1" Name="Header1" LeftIndent="0" PointSize="36">
<ParagraphStyle Self="ParagraphStyle/FirstParagraph" Name="FirstParagraph" LeftIndent="0">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/Header2" Name="Header2" LeftIndent="0" PointSize="30">
<ParagraphStyle Self="ParagraphStyle/Header1" Name="Header1" LeftIndent="0" PointSize="36">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/Paragraph" Name="Paragraph" LeftIndent="0">
<ParagraphStyle Self="ParagraphStyle/Header2" Name="Header2" LeftIndent="0" PointSize="30">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
</Properties>
Expand All @@ -73,7 +73,7 @@ some more text that [links to](https://www.pandoc.org) Pandoc.
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>this is some text</Content>
</CharacterStyleRange>
Expand All @@ -86,7 +86,7 @@ some more text that [links to](https://www.pandoc.org) Pandoc.
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>some more text that </Content>
</CharacterStyleRange>
Expand Down
9 changes: 7 additions & 2 deletions test/command/6675.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ and some text that [links to](#header-1) the first header
</TabList>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/FirstParagraph" Name="FirstParagraph" LeftIndent="0">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
</Properties>
</ParagraphStyle>
<ParagraphStyle Self="ParagraphStyle/Header1" Name="Header1" LeftIndent="0" PointSize="36">
<Properties>
<BasedOn type="object">$ID/NormalParagraphStyle</BasedOn>
Expand Down Expand Up @@ -75,7 +80,7 @@ and some text that [links to](#header-1) the first header
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>this is some text</Content>
</CharacterStyleRange>
Expand All @@ -88,7 +93,7 @@ and some text that [links to](#header-1) the first header
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>some more text that </Content>
</CharacterStyleRange>
Expand Down
8 changes: 4 additions & 4 deletions test/command/svg.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
<Properties>
Expand Down Expand Up @@ -36,7 +36,7 @@
% pandoc -f latex -t icml
\includegraphics{command/SVG_logo.svg}
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 37.5 -37.5">
<Properties>
Expand Down Expand Up @@ -69,7 +69,7 @@
% pandoc -f latex -t icml
\includegraphics{command/SVG_logo-without-xml-declaration.svg}
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 37.5 -37.5">
<Properties>
Expand Down Expand Up @@ -103,7 +103,7 @@
% pandoc -f latex -t icml
\includegraphics{command/inkscape-cube.svg}
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 54.75 -65.25">
<Properties>
Expand Down
14 changes: 7 additions & 7 deletions test/tables.icml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>Simple table with caption:</Content>
</CharacterStyleRange>
Expand Down Expand Up @@ -128,7 +128,7 @@
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>Simple table without caption:</Content>
</CharacterStyleRange>
Expand Down Expand Up @@ -255,7 +255,7 @@
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/TableCaption">
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>Simple table indented two spaces:</Content>
</CharacterStyleRange>
Expand Down Expand Up @@ -385,7 +385,7 @@
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>Multiline table with caption:</Content>
</CharacterStyleRange>
Expand Down Expand Up @@ -487,7 +487,7 @@
</CharacterStyleRange>
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>Multiline table without caption:</Content>
</CharacterStyleRange>
Expand Down Expand Up @@ -586,7 +586,7 @@
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/TableCaption">
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>Table without column headers:</Content>
</CharacterStyleRange>
Expand Down Expand Up @@ -685,7 +685,7 @@
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/TableCaption">
</ParagraphStyleRange>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/FirstParagraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
<Content>Multiline table without column headers:</Content>
</CharacterStyleRange>
Expand Down
Loading