git.iko.soy/typeable/pandoc
on master help sign in
OpenDocument Writer: Implement Div and Span ident support (#6755)
75c881eNils Carlson
src/Text/Pandoc/Writers/OpenDocument.hs+33−10
@@ -188,15 +188,23 @@ formulaStyle mt = inTags False "style:style"
188 ,("style:horizontal-rel", "paragraph-content")
188 ,("style:horizontal-rel", "paragraph-content")
189 ,("style:wrap", "none")]
189 ,("style:wrap", "none")]
190
190
191+inBookmarkTags :: Text -> Doc Text -> Doc Text
192+inBookmarkTags ident d =
193+ selfClosingTag "text:bookmark-start" [ ("text:name", ident) ]
194+ <> d <>
195+ selfClosingTag "text:bookmark-end" [ ("text:name", ident) ]
196+
197+selfClosingBookmark :: Text -> Doc Text
198+selfClosingBookmark ident =
199+ selfClosingTag "text:bookmark" [("text:name", ident)]
200+
191inHeaderTags :: PandocMonad m => Int -> Text -> Doc Text -> OD m (Doc Text)
201inHeaderTags :: PandocMonad m => Int -> Text -> Doc Text -> OD m (Doc Text)
192inHeaderTags i ident d =
202inHeaderTags i ident d =
193 return $ inTags False "text:h" [ ("text:style-name", "Heading_20_" <> tshow i)
203 return $ inTags False "text:h" [ ("text:style-name", "Heading_20_" <> tshow i)
194 , ("text:outline-level", tshow i)]
204 , ("text:outline-level", tshow i)]
195 $ if T.null ident
205 $ if T.null ident
196 then d
206 then d
197− else selfClosingTag "text:bookmark-start" [ ("text:name", ident) ]
207+ else inBookmarkTags ident d
198− <> d <>
199− selfClosingTag "text:bookmark-end" [ ("text:name", ident) ]
200
208
201inQuotes :: QuoteType -> Doc Text -> Doc Text
209inQuotes :: QuoteType -> Doc Text -> Doc Text
202inQuotes SingleQuote s = char '\8216' <> s <> char '\8217'
210inQuotes SingleQuote s = char '\8216' <> s <> char '\8217'
@@ -360,12 +368,7 @@ blockToOpenDocument o bs
360 then return empty
368 then return empty
361 else inParagraphTags =<< inlinesToOpenDocument o b
369 else inParagraphTags =<< inlinesToOpenDocument o b
362 | LineBlock b <- bs = blockToOpenDocument o $ linesToPara b
370 | LineBlock b <- bs = blockToOpenDocument o $ linesToPara b
363− | Div attr xs <- bs = do
371+ | Div attr xs <- bs = mkDiv attr xs
364− let (_,_,kvs) = attr
365− withLangFromAttr attr $
366− case lookup "custom-style" kvs of
367− Just sty -> withParagraphStyle o sty xs
368− _ -> blocksToOpenDocument o xs
369 | Header i (ident,_,_) b
372 | Header i (ident,_,_) b
370 <- bs = setFirstPara >> (inHeaderTags i ident
373 <- bs = setFirstPara >> (inHeaderTags i ident
371 =<< inlinesToOpenDocument o b)
374 =<< inlinesToOpenDocument o b)
@@ -390,6 +393,16 @@ blockToOpenDocument o bs
390 setInDefinitionList False
393 setInDefinitionList False
391 return r
394 return r
392 preformatted s = flush . vcat <$> mapM (inPreformattedTags . escapeStringForXML) (T.lines s)
395 preformatted s = flush . vcat <$> mapM (inPreformattedTags . escapeStringForXML) (T.lines s)
396+ mkDiv attr s = do
397+ let (ident,_,kvs) = attr
398+ i = withLangFromAttr attr $
399+ case lookup "custom-style" kvs of
400+ Just sty -> withParagraphStyle o sty s
401+ _ -> blocksToOpenDocument o s
402+ mkBookmarkedDiv = inTags False "text:section" [("text:name", ident)]
403+ if T.null ident
404+ then i
405+ else fmap mkBookmarkedDiv i
393 mkBlockQuote b = do increaseIndent
406 mkBlockQuote b = do increaseIndent
394 i <- paraStyle
407 i <- paraStyle
395 [("style:parent-style-name","Quotations")]
408 [("style:parent-style-name","Quotations")]
@@ -567,7 +580,7 @@ inlineToOpenDocument o ils
567 | writerWrapText o == WrapPreserve
580 | writerWrapText o == WrapPreserve
568 -> return $ preformatted "\n"
581 -> return $ preformatted "\n"
569 | otherwise -> return space
582 | otherwise -> return space
570− Span attr xs -> withLangFromAttr attr (inlinesToOpenDocument o xs)
583+ Span attr xs -> mkSpan attr xs
571 LineBreak -> return $ selfClosingTag "text:line-break" []
584 LineBreak -> return $ selfClosingTag "text:line-break" []
572 Str s -> return $ handleSpaces $ escapeStringForXML s
585 Str s -> return $ handleSpaces $ escapeStringForXML s
573 Emph l -> withTextStyle Italic $ inlinesToOpenDocument o l
586 Emph l -> withTextStyle Italic $ inlinesToOpenDocument o l
@@ -625,6 +638,16 @@ inlineToOpenDocument o ils
625 , ("xlink:type" , "simple")
638 , ("xlink:type" , "simple")
626 , ("xlink:show" , "embed" )
639 , ("xlink:show" , "embed" )
627 , ("xlink:actuate", "onLoad")]
640 , ("xlink:actuate", "onLoad")]
641+ mkSpan attr xs = do
642+ let (ident,_,_) = attr
643+ i = withLangFromAttr attr (inlinesToOpenDocument o xs)
644+ mkBookmarkedSpan b =
645+ if isEmpty b
646+ then selfClosingBookmark ident
647+ else inBookmarkTags ident b
648+ if T.null ident
649+ then i
650+ else fmap mkBookmarkedSpan i
628 mkNote l = do
651 mkNote l = do
629 n <- length <$> gets stNotes
652 n <- length <$> gets stNotes
630 let footNote t = inTags False "text:note"
653 let footNote t = inTags False "text:note"
test/command/6755.mdnew file+11
@@ -0,0 +1,11 @@
1+```
2+%pandoc -f native -t opendocument
3+[Div ("divId",[],[])
4+ [Para [Str "Here",Space,Str "is",Space,Str "a",Space,Str "div."]],
5+Para [Span ("spanId",[],[]) [Str "And",Space,Str "here",Space,Str "is",Space,Str "a",Space,Str "span."]]]
6+^D
7+<text:section text:name="divId"><text:p text:style-name="Text_20_body">Here
8+is a div.</text:p></text:section>
9+<text:p text:style-name="Text_20_body"><text:bookmark-start text:name="spanId" />And
10+here is a span.<text:bookmark-end text:name="spanId" /></text:p>
11+```