Odt reader: Support Preformatted Text style

LibreOffice uses `Preformatted_20_Text` as the default style for
code listings etc. Let’s detect if a paragraph has the `style-name`
and convert it to `CodeBlock` if it is the case.

Unfortunately, pandoc itself generates a separate style class like P123
for each paragraph so we will not yet support code listings from documents
generated by pandoc itself.

Closes #4841.
This commit is contained in:
Jan Tojnar
2026-02-14 22:37:32 +01:00
committed by John MacFarlane
parent 8b994e01a5
commit b4f71f7ef0
4 changed files with 35 additions and 20 deletions
+30 -20
View File
@@ -652,26 +652,36 @@ read_span = matchingElement NsText "span"
--
read_paragraph :: ElementMatcher CombiningBlocks
read_paragraph = matchingElement NsText "p"
$ liftA CombiningBlocks
$ constructPara
$ liftA para
$ withNewStyle
$ matchChildContent [ read_span
, read_spaces
, read_line_break
, read_tab
, read_link
, read_note
, read_citation
, read_bookmark
, read_bookmark_start
, read_reference_start
, read_bookmark_ref
, read_reference_ref
, read_frame
, read_text_seq
] read_plain_text
read_paragraph = matchingElement NsText "p" $
liftA CombiningBlocks $ proc blocks -> do
fStyle <- readStyleByName -< blocks
case fStyle of
Right (styleName, _) | isPreformattedStyle styleName -> do
liftA (codeBlock . stringify) $ matchParagraphContent -< blocks
_ ->
constructPara $ liftA para $ withNewStyle matchParagraphContent -< blocks
where
isPreformattedStyle :: StyleName -> Bool
isPreformattedStyle "Preformatted_20_Text" = True
isPreformattedStyle _ = False
matchParagraphContent :: ODTReaderSafe a Inlines
matchParagraphContent = matchChildContent [ read_span
, read_spaces
, read_line_break
, read_tab
, read_link
, read_note
, read_citation
, read_bookmark
, read_bookmark_start
, read_reference_start
, read_bookmark_ref
, read_reference_ref
, read_frame
, read_text_seq
] read_plain_text
----------------------
+1
View File
@@ -176,6 +176,7 @@ namesOfTestsComparingToNative = [ "blockquote"
, "orderedListRoman"
, "orderedListSimple"
, "orderedListHeader"
, "preformattedText"
, "referenceToChapter"
, "referenceToListItem"
, "referenceToText"
+4
View File
@@ -0,0 +1,4 @@
[ CodeBlock
( "" , [] , [] )
"void main() {\n printf(\"Hello world\");\n}"
]
Binary file not shown.