@@ -87,7 +87,7 @@ import Text.Pandoc.Readers.Docx.Reducible
8787import Text.Pandoc.Readers.Docx.TexChar
8888import Text.Pandoc.Shared
8989import Data.Maybe (mapMaybe, fromMaybe)
90−import Data.List (delete, isPrefixOf, (\\), intercalate)
90+import Data.List (delete, isPrefixOf, (\\), intercalate, intersect)
9191import qualified Data.ByteString as BS
9292import qualified Data.ByteString.Lazy as B
9393import Data.ByteString.Base64 (encode)
@@ -101,7 +101,8 @@ readDocx :: ReaderOptions
101101 -> Pandoc
102102readDocx opts bytes =
103103 case archiveToDocx (toArchive bytes) of
104− Right docx -> Pandoc nullMeta (docxToBlocks opts docx)
104+ Right docx -> Pandoc meta blks where
105+ (meta, blks) = (docxToMetaAndBlocks opts docx)
105106 Left _ -> error $ "couldn't parse docx file"
106107
107108data DState = DState { docxAnchorMap :: M.Map String String
@@ -134,6 +135,65 @@ spansToKeep = []
134135divsToKeep :: [String]
135136divsToKeep = ["list-item", "Definition", "DefinitionTerm"]
136137
138+metaStyles :: M.Map String String
139+metaStyles = M.fromList [ ("Title", "title")
140+ , ("Subtitle", "subtitle")
141+ , ("Author", "author")
142+ , ("Date", "date")
143+ , ("Abstract", "abstract")]
144+
145+sepBodyParts :: [BodyPart] -> ([BodyPart], [BodyPart])
146+sepBodyParts = span (\bp -> (isMetaPar bp || isEmptyPar bp))
147+
148+isMetaPar :: BodyPart -> Bool
149+isMetaPar (Paragraph pPr _) =
150+ not $ null $ intersect (pStyle pPr) (M.keys metaStyles)
151+isMetaPar _ = False
152+
153+isEmptyPar :: BodyPart -> Bool
154+isEmptyPar (Paragraph _ parParts) =
155+ all isEmptyParPart parParts
156+ where
157+ isEmptyParPart (PlainRun (Run _ runElems)) = all isEmptyElem runElems
158+ isEmptyParPart _ = False
159+ isEmptyElem (TextRun s) = trim s == ""
160+ isEmptyElem _ = True
161+isEmptyPar _ = False
162+
163+bodyPartsToMeta' :: [BodyPart] -> DocxContext (M.Map String MetaValue)
164+bodyPartsToMeta' [] = return M.empty
165+bodyPartsToMeta' (bp : bps)
166+ | (Paragraph pPr parParts) <- bp
167+ , (c : _)<- intersect (pStyle pPr) (M.keys metaStyles)
168+ , (Just metaField) <- M.lookup c metaStyles = do
169+ inlines <- parPartsToInlines parParts
170+ remaining <- bodyPartsToMeta' bps
171+ let
172+ f (MetaInlines ils) (MetaInlines ils') = MetaBlocks [Para ils, Para ils']
173+ f (MetaInlines ils) (MetaBlocks blks) = MetaBlocks ((Para ils) : blks)
174+ f m (MetaList mv) = MetaList (m : mv)
175+ f m n = MetaList [m, n]
176+ return $ M.insertWith f metaField (MetaInlines inlines) remaining
177+bodyPartsToMeta' (_ : bps) = bodyPartsToMeta' bps
178+
179+bodyPartsToMeta :: [BodyPart] -> DocxContext Meta
180+bodyPartsToMeta bps = do
181+ mp <- bodyPartsToMeta' bps
182+ let mp' =
183+ case M.lookup "author" mp of
184+ Just mv -> M.insert "author" (fixAuthors mv) mp
185+ Nothing -> mp
186+ return $ Meta mp'
187+
188+fixAuthors :: MetaValue -> MetaValue
189+fixAuthors (MetaBlocks blks) =
190+ MetaList $ map g $ filter f blks
191+ where f (Para _) = True
192+ f _ = False
193+ g (Para ils) = MetaInlines ils
194+ g _ = MetaInlines []
195+fixAuthors mv = mv
196+
137197runStyleToContainers :: RunStyle -> [Container Inline]
138198runStyleToContainers rPr =
139199 let spanClassToContainers :: String -> [Container Inline]
@@ -615,24 +675,26 @@ rewriteLink l@(Link ils ('#':target, title)) = do
615675 Nothing -> l
616676rewriteLink il = return il
617677
618−
619−bodyToBlocks :: Body -> DocxContext [Block]
620−bodyToBlocks (Body bps) = do
621− blks <- concatMapM bodyPartToBlocks bps >>=
678+bodyToMetaAndBlocks :: Body -> DocxContext (Meta, [Block])
679+bodyToMetaAndBlocks (Body bps) = do
680+ let (metabps, blkbps) = sepBodyParts bps
681+ meta <- bodyPartsToMeta metabps
682+ blks <- concatMapM bodyPartToBlocks blkbps >>=
622683 walkM rewriteLink
623684 return $
624− blocksToDefinitions $
625− blocksToBullets $ blks
685+ (meta,
686+ blocksToDefinitions $
687+ blocksToBullets $ blks)
626688
627−docxToBlocks :: ReaderOptions -> Docx -> [Block]
628−docxToBlocks opts d@(Docx (Document _ body)) =
689+docxToMetaAndBlocks :: ReaderOptions -> Docx -> (Meta, [Block])
690+docxToMetaAndBlocks opts d@(Docx (Document _ body)) =
629691 let dState = DState { docxAnchorMap = M.empty
630692 , docxInHeaderBlock = False
631693 , docxInTexSubscript = False}
632694 dEnv = DEnv { docxOptions = opts
633695 , docxDocument = d}
634696 in
635− evalDocxContext (bodyToBlocks body) dEnv dState
697+ evalDocxContext (bodyToMetaAndBlocks body) dEnv dState
636698
637699ilToCode :: Inline -> String
638700ilToCode (Str s) = s