From 75c881e2d97df015857a18ec46df0ecc17347778 Mon Sep 17 00:00:00 2001 From: Nils Carlson Date: Mon, 23 Nov 2020 06:23:30 +0000 Subject: OpenDocument Writer: Implement Div and Span ident support (#6755) Spans and Divs containing an ident in the Attr will become bookmarks or sections with idents in OpenDocument format. --- src/Text/Pandoc/Writers/OpenDocument.hs | 43 +++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'src/Text/Pandoc') diff --git a/src/Text/Pandoc/Writers/OpenDocument.hs b/src/Text/Pandoc/Writers/OpenDocument.hs index 789dbe9b0..071a5542f 100644 --- a/src/Text/Pandoc/Writers/OpenDocument.hs +++ b/src/Text/Pandoc/Writers/OpenDocument.hs @@ -188,15 +188,23 @@ formulaStyle mt = inTags False "style:style" ,("style:horizontal-rel", "paragraph-content") ,("style:wrap", "none")] +inBookmarkTags :: Text -> Doc Text -> Doc Text +inBookmarkTags ident d = + selfClosingTag "text:bookmark-start" [ ("text:name", ident) ] + <> d <> + selfClosingTag "text:bookmark-end" [ ("text:name", ident) ] + +selfClosingBookmark :: Text -> Doc Text +selfClosingBookmark ident = + selfClosingTag "text:bookmark" [("text:name", ident)] + inHeaderTags :: PandocMonad m => Int -> Text -> Doc Text -> OD m (Doc Text) inHeaderTags i ident d = return $ inTags False "text:h" [ ("text:style-name", "Heading_20_" <> tshow i) , ("text:outline-level", tshow i)] $ if T.null ident then d - else selfClosingTag "text:bookmark-start" [ ("text:name", ident) ] - <> d <> - selfClosingTag "text:bookmark-end" [ ("text:name", ident) ] + else inBookmarkTags ident d inQuotes :: QuoteType -> Doc Text -> Doc Text inQuotes SingleQuote s = char '\8216' <> s <> char '\8217' @@ -360,12 +368,7 @@ blockToOpenDocument o bs then return empty else inParagraphTags =<< inlinesToOpenDocument o b | LineBlock b <- bs = blockToOpenDocument o $ linesToPara b - | Div attr xs <- bs = do - let (_,_,kvs) = attr - withLangFromAttr attr $ - case lookup "custom-style" kvs of - Just sty -> withParagraphStyle o sty xs - _ -> blocksToOpenDocument o xs + | Div attr xs <- bs = mkDiv attr xs | Header i (ident,_,_) b <- bs = setFirstPara >> (inHeaderTags i ident =<< inlinesToOpenDocument o b) @@ -390,6 +393,16 @@ blockToOpenDocument o bs setInDefinitionList False return r preformatted s = flush . vcat <$> mapM (inPreformattedTags . escapeStringForXML) (T.lines s) + mkDiv attr s = do + let (ident,_,kvs) = attr + i = withLangFromAttr attr $ + case lookup "custom-style" kvs of + Just sty -> withParagraphStyle o sty s + _ -> blocksToOpenDocument o s + mkBookmarkedDiv = inTags False "text:section" [("text:name", ident)] + if T.null ident + then i + else fmap mkBookmarkedDiv i mkBlockQuote b = do increaseIndent i <- paraStyle [("style:parent-style-name","Quotations")] @@ -567,7 +580,7 @@ inlineToOpenDocument o ils | writerWrapText o == WrapPreserve -> return $ preformatted "\n" | otherwise -> return space - Span attr xs -> withLangFromAttr attr (inlinesToOpenDocument o xs) + Span attr xs -> mkSpan attr xs LineBreak -> return $ selfClosingTag "text:line-break" [] Str s -> return $ handleSpaces $ escapeStringForXML s Emph l -> withTextStyle Italic $ inlinesToOpenDocument o l @@ -625,6 +638,16 @@ inlineToOpenDocument o ils , ("xlink:type" , "simple") , ("xlink:show" , "embed" ) , ("xlink:actuate", "onLoad")] + mkSpan attr xs = do + let (ident,_,_) = attr + i = withLangFromAttr attr (inlinesToOpenDocument o xs) + mkBookmarkedSpan b = + if isEmpty b + then selfClosingBookmark ident + else inBookmarkTags ident b + if T.null ident + then i + else fmap mkBookmarkedSpan i mkNote l = do n <- length <$> gets stNotes let footNote t = inTags False "text:note" -- cgit v1.2.3