Pptx reader: read bold, italic, sub/superscript and hyperlinks.

Text runs were read as plain text, so their formatting and hyperlinks
were lost.

Runs and fields now keep their bold and italic (`b`, `i`) and their
baseline shift (`baseline`, as superscript or subscript). Adjacent runs
with the same format are joined first, so two bold runs make one Strong,
and spaces at the edges of a formatted run are kept outside it.

A run with a hyperlink (`a:hlinkClick`) becomes a Link to the target of
its relationship; adjacent runs with the same relationship make one
link. Links that act within the presentation, such as a jump to another
slide, carry an `action` and are read as plain text.

Formatting in slide titles, table cells and SmartArt, which are read as
plain text, is still ignored, as is underline.

The test fixture was written with python-pptx 1.0.2.

Contributed by @nachogarcia.
This commit is contained in:
John MacFarlane committed 2026-11-09 22:00:23 -08:00
1 parent 01e9573268
commit 1744ea89a9
6 files changed
+195 -36

No files matched your search

+98 -13
View File
@@ -29,7 +29,7 @@ module Text.Pandoc.Readers.Pptx.Shapes
import Codec.Archive.Zip (Archive, findEntryByPath, fromEntry)
import qualified Data.ByteString.Lazy as B
import Data.Char (isSpace)
import Data.List (find, groupBy, intersperse)
import Data.List (find, groupBy)
import Data.Maybe (fromMaybe, listToMaybe, mapMaybe)
import qualified Data.Set as S
import qualified Data.Text as T
@@ -43,6 +43,7 @@ import Text.Pandoc.Readers.OOXML.Shared
import Text.Pandoc.Readers.OOXML.Charts
import Text.Pandoc.Readers.Pptx.SmartArt
import Text.Pandoc.Shared (collapseFilePath)
import Text.Pandoc.Walk (walk)
import Text.Pandoc.XML.Light
import Text.TeXMath (writeTeX)
import Text.TeXMath.Readers.OMML (readOMML)
@@ -214,8 +215,8 @@ parseTableRow ns trElem =
-- | Convert shape to Pandoc blocks
shapeToBlocks :: PandocMonad m => Archive -> [(Text, Text)] -> PptxShape -> m [Block]
shapeToBlocks _archive _rels (PptxTextBox paras) =
return $ paragraphsToBlocks paras
shapeToBlocks _archive rels (PptxTextBox paras) =
return $ resolveLinks rels $ paragraphsToBlocks paras
shapeToBlocks archive rels (PptxPicture relId title alt) = do
-- Resolve relationship to get media path
case lookup relId rels of
@@ -432,19 +433,103 @@ hasWingdingsSymbol ns pElem =
Nothing -> False
in any checkRun runs
-- | Inlines of a paragraph: its text, with its line breaks, and its
-- equations (@a14:m@) as math
-- | Inlines of a paragraph: the text of its runs and fields with their
-- bold, italic, superscript and subscript, its line breaks, its
-- hyperlinks, and its equations (@a14:m@) as math. A hyperlink's target
-- is a relationship id at this point, which 'resolveLinks' replaces.
paragraphInlines :: Element -> [Inline]
paragraphInlines pElem =
B.toList $ mconcat $ map toInlines $ groupBy bothText $ elChildren pElem
B.toList $ foldMap linked $ groupBy (\a b -> fst a == fst b) $
concatMap piece (elChildren pElem)
where
isMath el = qName (elName el) == "m"
bothText a b = not (isMath a || isMath b)
toInlines els@(el:_)
| isMath el = foldMap mathInlines (elChildren el)
| otherwise = mconcat $ intersperse B.linebreak $ map B.text $
drawingMLParagraphLines pElem{ elContent = map Elem els }
toInlines [] = mempty
piece el = case qName (elName el) of
"r" -> [(runLink el, Left (runFormat el, runText el))]
"fld" -> [(runLink el, Left (runFormat el, runText el))]
"br" -> [(Nothing, Right B.linebreak)]
"m" -> [(Nothing, Right (foldMap mathInlines (elChildren el)))]
_ -> []
linked grp =
let inls = foldMap formatted $ groupBy sameFormat (map snd grp)
in case grp of
(Just relId, _) : _ ->
B.linkWith ("", [], [(linkRelAttr, relId)]) "" "" inls
_ -> inls
sameFormat (Left (f, _)) (Left (g, _)) = f == g
sameFormat _ _ = False
-- Runs with the same format are joined before it is applied, so
-- that adjacent bold runs make one strong span.
formatted pieces@(Left (fmt, _) : _) =
applyFormat fmt (T.concat [t | Left (_, t) <- pieces])
formatted pieces = mconcat [ils | Right ils <- pieces]
-- | The format of a run: bold, italic, and its baseline shift (positive
-- for superscript, negative for subscript)
type RunFormat = (Bool, Bool, Int)
runFormat :: Element -> RunFormat
runFormat run =
case find ((== "rPr") . qName . elName) (elChildren run) of
Nothing -> (False, False, 0)
Just rPr ->
( flag "b" rPr, flag "i" rPr
, fromMaybe 0 $
findAttr (unqual "baseline") rPr >>= readMaybe . T.unpack )
where
flag name el = findAttr (unqual name) el `elem` [Just "1", Just "true"]
runText :: Element -> Text
runText = T.concat . map strContent .
filter ((== "t") . qName . elName) . elChildren
-- | Text with a run format. Spaces at the edges stay outside the
-- formatting, as Markdown emphasis can't start or end with a space.
applyFormat :: RunFormat -> Text -> B.Inlines
applyFormat (bold, italic, baseline) txt
| not (bold || italic || baseline /= 0) || T.null core = B.text txt
| otherwise = edge leading <> wrap (B.text core) <> edge trailing
where
isBlank = (`elem` (" \t\r\n" :: String))
core = T.dropAround isBlank txt
leading = T.takeWhile isBlank txt
trailing = T.takeWhileEnd isBlank txt
edge t = if T.null t then mempty else B.space
wrap = (if bold then B.strong else id) .
(if italic then B.emph else id) .
(if baseline > 0 then B.superscript
else if baseline < 0 then B.subscript else id)
-- | The relationship id of a run's hyperlink (@a:hlinkClick@). Links
-- that act within the presentation, such as a jump to another slide,
-- carry an @action@ and are not kept.
runLink :: Element -> Maybe Text
runLink run = do
rPr <- find ((== "rPr") . qName . elName) (elChildren run)
link <- find ((== "hlinkClick") . qName . elName) (elChildren rPr)
case findAttr (unqual "action") link of
Just _ -> Nothing
Nothing -> fmap attrVal $ find isRelId (elAttribs link)
where
isRelId a = qName (attrKey a) == "id" && qURI (attrKey a) == Just relNs
relNs =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships"
-- | The attribute that marks a hyperlink whose target is still a
-- relationship id
linkRelAttr :: Text
linkRelAttr = "pptx-relationship"
-- | Replace the relationship id of each hyperlink read by
-- 'paragraphInlines' with the relationship's target. A link whose
-- relationship is missing is replaced by its text.
resolveLinks :: [(Text, Text)] -> [Block] -> [Block]
resolveLinks rels = walk resolve
where
resolve (Link (_, _, [(key, relId)]) inls _)
| key == linkRelAttr =
case lookup relId rels of
Just target -> Link nullAttr inls (target, "")
Nothing -> Span nullAttr inls
resolve x = x
-- | An equation (@m:oMathPara@ or @m:oMath@) as math. If texmath can't
-- read it, its text is kept.
+4
View File
@@ -44,6 +44,10 @@ tests = [ testGroup "basic"
"charts"
"pptx-reader/charts.pptx"
"pptx-reader/charts.native"
, testCompare
"run formatting and hyperlinks"
"pptx-reader/formatting.pptx"
"pptx-reader/formatting.native"
, testCompare
"group shapes"
"pptx-reader/groups.pptx"
+26 -22
View File
@@ -79,16 +79,18 @@ Pandoc
, Space
, Str "diagram"
]
, Para [ Str "SKILLS" ]
, Para [ Strong [ Str "SKILLS" ] ]
, Para []
, Para
[ Str "Specialized"
, Space
, Str "Workers"
, Space
, Str "/"
, Space
, Str "Workflows:"
[ Strong
[ Str "Specialized"
, Space
, Str "Workers"
, Space
, Str "/"
, Space
, Str "Workflows:"
]
]
, Para []
, Para
@@ -100,18 +102,20 @@ Pandoc
, Space
]
, Para [ Str "other" , Space , Str "RPA" ]
, Para [ Str "BRAINS" ]
, Para [ Strong [ Str "BRAINS" ] ]
, Para []
, Para
[ Str "Universal"
, Space
, Str "Workers"
, Space
, Str "/"
, Space
, Str "AI"
, Space
, Str "Agents:"
[ Strong
[ Str "Universal"
, Space
, Str "Workers"
, Space
, Str "/"
, Space
, Str "AI"
, Space
, Str "Agents:"
]
]
, Para []
, Para [ Str "openai," , Space , Str "anthropic," ]
@@ -124,10 +128,10 @@ Pandoc
, Space
]
, Para [ Str "\8220AI" , Space , Str "natives\8221" ]
, Para [ Str "KNOWLEDGE" , Space ]
, Para [ Strong [ Str "KNOWLEDGE" ] , Space ]
, Para []
, Para [ Str "Data" , Space , Str "/" , Space ]
, Para [ Str "RAG" , Space , Str "Pipelines" ]
, Para [ Strong [ Str "Data" , Space , Str "/" ] , Space ]
, Para [ Strong [ Str "RAG" , Space , Str "Pipelines" ] ]
, Para []
, Para
[ Str "Vector"
@@ -145,7 +149,7 @@ Pandoc
, Str "\8230"
]
, Para [ Str "glean" ]
, Para [ Str "EW" ]
, Para [ Strong [ Str "EW" ] ]
, Header 2 ( "slide-3" , [] , [] ) [ Str "Table" ]
, Table
( "" , [] , [] )
+65
View File
@@ -0,0 +1,65 @@
Pandoc
Meta { unMeta = fromList [] }
[ Header 2 ( "slide-1" , [] , [] ) [ Str "Formatting" ]
, Para
[ Str "A"
, Space
, Strong [ Str "bold" , Space , Str "word" ]
, Space
, Str "and"
, Space
, Str "an"
, Space
, Emph [ Str "italic" ]
, Space
, Str "one,"
, Space
, Str "not"
, Space
, Str "bold"
, Space
, Str "and"
, Space
, Str "mid"
, Strong [ Str "word" ]
]
, Para
[ Str "Area"
, Space
, Str "in"
, Space
, Str "m"
, Superscript [ Str "2" ]
, Str ","
, Space
, Str "water"
, Space
, Str "is"
, Space
, Str "H"
, Subscript [ Str "2" ]
, Str "O."
]
, Para
[ Str "See"
, Space
, Link
( "" , [] , [] )
[ Str "the"
, Space
, Str "pandoc"
, Space
, Strong [ Str "site" ]
]
( "https://pandoc.org" , "" )
, Space
, Str "or"
, Space
, Link
( "" , [] , [] )
[ Str "the" , Space , Str "report" ]
( "Report%20draft.pdf" , "" )
, Str "."
]
, Para [ Str "Next" , Space , Str "slide" ]
]
Binary file not shown.
+2 -1
View File
@@ -4,7 +4,8 @@ Pandoc
2
( "slide-1" , [] , [] )
[ Str "Everworker" , Space , Str "diagram" ]
, Para [ Str "Hello" , Space , Str "world" ]
, Para
[ Str "Hel" , Strong [ Str "lo" ] , Space , Str "world" ]
, Para [ Str "openai," , Space , Str "anthropic" ]
, Para
[ Str "line"