git.iko.soy/typeable/pandoc
on master help sign in
DocBook reader: metadata handling improvements.

Now we properly parse title and subtitle elements that are direct children of book and article (as well as children of bookinfo, articleinfo, or info). We also now use the "subtitle" metadata field for subtitles, rather than tacking the subtitle on to the title.

commit78dca68a0a484f3230535e91a98a30cb8090aee8 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 30 Jul 2018, 14:38 (8 years ago) parentfb94c0f
2 files changed+29−39
src/Text/Pandoc/Readers/DocBook.hs+28−38 test/docbook-xref.native+1−1
src/Text/Pandoc/Readers/DocBook.hs+28−38
@@ -537,7 +537,6 @@ type DB m = StateT DBState m
537537data DBState = DBState{ dbSectionLevel :: Int
538538 , dbQuoteType :: QuoteType
539539 , dbMeta :: Meta
540− , dbAcceptsMeta :: Bool
541540 , dbBook :: Bool
542541 , dbFigureTitle :: Inlines
543542 , dbContent :: [Content]
@@ -547,7 +546,6 @@ instance Default DBState where
547546 def = DBState{ dbSectionLevel = 0
548547 , dbQuoteType = DoubleQuote
549548 , dbMeta = mempty
550− , dbAcceptsMeta = False
551549 , dbBook = False
552550 , dbFigureTitle = mempty
553551 , dbContent = [] }
@@ -609,18 +607,26 @@ named s e = qName (elName e) == s
609607
610608--
611609
612−acceptingMetadata :: PandocMonad m => DB m a -> DB m a
613−acceptingMetadata p = do
614− modify (\s -> s { dbAcceptsMeta = True } )
615− res <- p
616− modify (\s -> s { dbAcceptsMeta = False })
617− return res
618−
619−checkInMeta :: (PandocMonad m, Monoid a) => DB m () -> DB m a
620−checkInMeta p = do
621− accepts <- dbAcceptsMeta <$> get
622− when accepts p
623− return mempty
610+addMetadataFromElement :: PandocMonad m => Element -> DB m Blocks
611+addMetadataFromElement e = do
612+ case filterChild (named "title") e of
613+ Nothing -> return ()
614+ Just z -> do
615+ getInlines z >>= addMeta "title"
616+ addMetaField "subtitle" z
617+ case filterChild (named "authorgroup") e of
618+ Nothing -> return ()
619+ Just z -> addMetaField "author" z
620+ addMetaField "subtitle" e
621+ addMetaField "author" e
622+ addMetaField "date" e
623+ addMetaField "release" e
624+ return mempty
625+ where addMetaField fieldname elt =
626+ case filterChildren (named fieldname) elt of
627+ [] -> return ()
628+ [z] -> getInlines z >>= addMeta fieldname
629+ zs -> mapM getInlines zs >>= addMeta fieldname
624630
625631addMeta :: PandocMonad m => ToMetaValue a => String -> a -> DB m ()
626632addMeta field val = modify (setMeta field val)
@@ -718,11 +724,6 @@ parseBlock (Elem e) =
718724 "attribution" -> return mempty
719725 "titleabbrev" -> return mempty
720726 "authorinitials" -> return mempty
721− "title" -> checkInMeta getTitle
722− "author" -> checkInMeta getAuthor
723− "authorgroup" -> checkInMeta getAuthorGroup
724− "releaseinfo" -> checkInMeta (getInlines e >>= addMeta "release")
725− "date" -> checkInMeta getDate
726727 "bibliography" -> sect 0
727728 "bibliodiv" -> sect 1
728729 "biblioentry" -> parseMixed para (elContent e)
@@ -788,8 +789,8 @@ parseBlock (Elem e) =
788789 "figure" -> getFigure e
789790 "mediaobject" -> para <$> getMediaobject e
790791 "caption" -> return mempty
791− "info" -> metaBlock
792− "articleinfo" -> metaBlock
792+ "info" -> addMetadataFromElement e
793+ "articleinfo" -> addMetadataFromElement e
793794 "sectioninfo" -> return mempty -- keywords & other metadata
794795 "refsectioninfo" -> return mempty -- keywords & other metadata
795796 "refsect1info" -> return mempty -- keywords & other metadata
@@ -803,10 +804,11 @@ parseBlock (Elem e) =
803804 "chapterinfo" -> return mempty -- keywords & other metadata
804805 "glossaryinfo" -> return mempty -- keywords & other metadata
805806 "appendixinfo" -> return mempty -- keywords & other metadata
806− "bookinfo" -> metaBlock
807+ "bookinfo" -> addMetadataFromElement e
807808 "article" -> modify (\st -> st{ dbBook = False }) >>
808− getBlocks e
809− "book" -> modify (\st -> st{ dbBook = True }) >> getBlocks e
809+ addMetadataFromElement e >> getBlocks e
810+ "book" -> modify (\st -> st{ dbBook = True }) >>
811+ addMetadataFromElement e >> getBlocks e
810812 "table" -> parseTable
811813 "informaltable" -> parseTable
812814 "informalexample" -> divWith ("", ["informalexample"], []) <$>
@@ -816,6 +818,8 @@ parseBlock (Elem e) =
816818 "screen" -> codeBlockWithLang
817819 "programlisting" -> codeBlockWithLang
818820 "?xml" -> return mempty
821+ "title" -> return mempty -- handled in parent element
822+ "subtitle" -> return mempty -- handled in parent element
819823 _ -> getBlocks e
820824 where parseMixed container conts = do
821825 let (ils,rest) = break isBlockElement conts
@@ -857,19 +861,6 @@ parseBlock (Elem e) =
857861 terms' <- mapM getInlines terms
858862 items' <- mapM getBlocks items
859863 return (mconcat $ intersperse (str "; ") terms', items')
860− getTitle = do
861− tit <- getInlines e
862− subtit <- case filterChild (named "subtitle") e of
863− Just s -> (text ": " <>) <$>
864− getInlines s
865− Nothing -> return mempty
866− addMeta "title" (tit <> subtit)
867−
868− getAuthor = (:[]) <$> getInlines e >>= addMeta "author"
869− getAuthorGroup = do
870− let terms = filterChildren (named "author") e
871− mapM getInlines terms >>= addMeta "author"
872− getDate = getInlines e >>= addMeta "date"
873864 parseTable = do
874865 let isCaption x = named "title" x || named "caption" x
875866 caption <- case filterChild isCaption e of
@@ -935,7 +926,6 @@ parseBlock (Elem e) =
935926 modify $ \st -> st{ dbSectionLevel = n - 1 }
936927 return $ headerWith (ident,[],[]) n' headerText <> b
937928 lineItems = mapM getInlines $ filterChildren (named "line") e
938− metaBlock = acceptingMetadata (getBlocks e) >> return mempty
939929
940930getInlines :: PandocMonad m => Element -> DB m Inlines
941931getInlines e' = (trimInlines . mconcat) <$>
test/docbook-xref.native+1−1
@@ -1,4 +1,4 @@
1−Pandoc (Meta {unMeta = fromList []})
1+Pandoc (Meta {unMeta = fromList [("title",MetaInlines [Str "An",Space,Str "Example",Space,Str "Book"])]})
22[Header 1 ("ch01",[],[]) [Str "XRef",Space,Str "Samples"]
33,Para [Str "This",Space,Str "paragraph",Space,Str "demonstrates",Space,Str "several",Space,Str "features",Space,Str "of",SoftBreak,Str "XRef."]
44,BulletList