git.iko.soy/typeable/pandoc
on master help sign in
Infer caption from the text following the img

Frame can contain other frames with the text boxes. This is something that has not been considered before and meant that the whole construction of images was broken in those cases. Also the captions were fixed/ignored.

commita02f276ff125eb9bede33524371cf8c7b660bb40 authorHubert Plociniczak <hubert.plociniczak@gmail.com> dateMon 17 Oct 2016, 16:35 (10 years ago) parentc924611
5 files changed+54−22
src/Text/Pandoc/Readers/Odt/ContentReader.hs+47−20 tests/Tests/Readers/Odt.hs+4−2 tests/odt/native/image.native+1 tests/odt/native/imageIndex.native+1 tests/odt/native/imageWithCaption.native+1
src/Text/Pandoc/Readers/Odt/ContentReader.hs+47−20
@@ -543,6 +543,10 @@ read_plain_text = fst ^&&& read_plain_text' >>% recover
543543 extractText (XML.Text cData) = succeedWith (XML.cdData cData)
544544 extractText _ = failEmpty
545545
546+read_text_seq :: InlineMatcher
547+read_text_seq = matchingElement NsText "sequence"
548+ $ matchChildContent [] read_plain_text
549+
546550
547551-- specifically. I honor that, although the current implementation of '(<>)'
548552-- for 'Inlines' in "Text.Pandoc.Builder" will collaps them agein.
@@ -591,7 +595,8 @@ read_paragraph = matchingElement NsText "p"
591595 , read_reference_start
592596 , read_bookmark_ref
593597 , read_reference_ref
594− , read_frame
598+ , read_maybe_nested_img_frame
599+ , read_text_seq
595600 ] read_plain_text
596601
597602
@@ -616,7 +621,7 @@ read_header = matchingElement NsText "h"
616621 , read_reference_start
617622 , read_bookmark_ref
618623 , read_reference_ref
619− , read_frame
624+ , read_maybe_nested_img_frame
620625 ] read_plain_text
621626 ) -< blocks
622627 anchor <- getHeaderAnchor -< children
@@ -726,18 +731,26 @@ read_table_cell = matchingElement NsTable "table-cell"
726731----------------------
727732
728733--
729−read_frame :: InlineMatcher
730−read_frame = matchingElement NsDraw "frame"
731− $ proc blocks -> do
732− w <- ( findAttr' NsSVG "width" ) -< ()
733− h <- ( findAttr' NsSVG "height" ) -< ()
734− attr <- arr (uncurry image_attributes) -< (w, h)
735− titleNodes <- ( matchChildContent' [ read_frame_title ] ) -< blocks
736− title <- arr inlineListToIdentifier -< (toList titleNodes)
737− src <- matchChildContent' [ read_image_src ] -< blocks
738− resource <- lookupResource -< src
739− _ <- updateMediaWithResource -< resource
740− arr (uncurry4 imageWith ) -< (attr, src, title, mempty)
734+read_maybe_nested_img_frame :: InlineMatcher
735+read_maybe_nested_img_frame = matchingElement NsDraw "frame"
736+ $ proc blocks -> do
737+ img <- (findChild' NsDraw "image") -< ()
738+ case img of
739+ Just _ -> read_frame -< blocks
740+ Nothing -> matchChildContent' [ read_frame_text_box ] -< blocks
741+
742+read_frame :: OdtReaderSafe Inlines Inlines
743+read_frame =
744+ proc blocks -> do
745+ w <- ( findAttr' NsSVG "width" ) -< ()
746+ h <- ( findAttr' NsSVG "height" ) -< ()
747+ titleNodes <- ( matchChildContent' [ read_frame_title ] ) -< blocks
748+ src <- matchChildContent' [ read_image_src ] -< blocks
749+ resource <- lookupResource -< src
750+ _ <- updateMediaWithResource -< resource
751+ alt <- (matchChildContent [] read_plain_text) -< blocks
752+ arr (uncurry4 imageWith ) -<
753+ (image_attributes w h, src, inlineListToIdentifier (toList titleNodes), alt)
741754
742755image_attributes :: Maybe String -> Maybe String -> Attr
743756image_attributes x y =
@@ -749,17 +762,31 @@ image_attributes x y =
749762
750763read_image_src :: (Namespace, ElementName, OdtReader Anchor Anchor)
751764read_image_src = matchingElement NsDraw "image"
752− $ proc _ -> do
753− imgSrc <- findAttr NsXLink "href" -< ()
754− case imgSrc of
755− Right src -> returnV src -<< ()
756− Left _ -> returnV "" -< ()
765+ $ proc _ -> do
766+ imgSrc <- findAttr NsXLink "href" -< ()
767+ case imgSrc of
768+ Right src -> returnV src -<< ()
769+ Left _ -> returnV "" -< ()
757770
758771read_frame_title :: InlineMatcher
759772read_frame_title = matchingElement NsSVG "title"
760773 $ (matchChildContent [] read_plain_text)
761774
762−
775+read_frame_text_box :: InlineMatcher
776+read_frame_text_box = matchingElement NsDraw "text-box"
777+ $ proc blocks -> do
778+ paragraphs <- (matchChildContent' [ read_paragraph ]) -< blocks
779+ case toList paragraphs of
780+ (p : []) -> -- require only a single paragraph
781+ arr read_img_with_caption -< p
782+ _ ->
783+ arr fromList -< []
784+
785+read_img_with_caption :: Block -> Inlines
786+read_img_with_caption (Para ((Image attr _ target) : txt)) =
787+ singleton (Image attr txt target) -- override caption with the text that follows
788+read_img_with_caption _ =
789+ fromList []
763790
764791----------------------
765792-- Internal links
tests/Tests/Readers/Odt.hs+4−2
@@ -141,7 +141,6 @@ namesOfTestsComparingToMarkdown = [ "bold"
141141 , "footnote"
142142 , "headers"
143143-- , "horizontalRule"
144−-- , "image"
145144 , "italic"
146145-- , "listBlocks"
147146 , "paragraph"
@@ -152,6 +151,9 @@ namesOfTestsComparingToMarkdown = [ "bold"
152151
153152namesOfTestsComparingToNative :: [ String ]
154153namesOfTestsComparingToNative = [ "blockquote"
154+ , "image"
155+ , "imageIndex"
156+ , "imageWithCaption"
155157 , "orderedListMixed"
156158 , "orderedListRoman"
157159 , "orderedListSimple"
@@ -162,4 +164,4 @@ namesOfTestsComparingToNative = [ "blockquote"
162164-- , "table"
163165 , "unicode"
164166 , "unorderedList"
165− ]
167+ ]
tests/odt/native/image.nativenew file+1
@@ -0,0 +1 @@
1+[Para [Image ("",[],[("width","5.292cm"),("height","5.292cm")]) [] ("Pictures/10000000000000FA000000FAD6A15225.jpg","")]]
tests/odt/native/imageIndex.nativenew file+1
@@ -0,0 +1 @@
1+[Para [Image ("",[],[("width","5.292cm"),("height","5.292cm")]) [Str "Abbildung",Space,Str "1:",Space,Str "Image",Space,Str "caption"] ("Pictures/10000000000000FA000000FAD6A15225.jpg","")]]
tests/odt/native/imageWithCaption.nativenew file+1
@@ -0,0 +1 @@
1+[Para [Image ("",[],[("width","5.292cm"),("height","5.292cm")]) [Str "Abbildung",Space,Str "1:",Space,Str "Image",Space,Str "caption"] ("Pictures/10000000000000FA000000FAD6A15225.jpg","")]]