Skip to content

Commit

Permalink
T.P.Shared, makeSections: put some attributes on section element only.
Browse files Browse the repository at this point in the history
Certain `role` and `epub:type` attributes should only be on the section
(and indeed, many `role`s give a validation error if left on the heading
element).
  • Loading branch information
jgm committed Feb 16, 2025
1 parent 2d25708 commit abf0b51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/Text/Pandoc/Shared.hs
Original file line number Diff line number Diff line change
Expand Up @@ -547,9 +547,14 @@ makeSectionsWithOffsets numoffsets numbering mbBaseLevel bs =
("number", T.intercalate "." (map tshow newnum)) : kvs
_ -> kvs
let divattr = (ident, "section":classes, kvs')
let attr = ("",classes,kvs')
let isHeadingAttr ("epub:type",_) = False
isHeadingAttr ("role",v) =
v `elem` ["tab", "presentation", "none", "treeitem",
"menuitem", "button", "heading"]
isHeadingAttr _ = True
let hattr = ("",classes, filter isHeadingAttr kvs')
return $
Div divattr (Header level' attr title' : sectionContents') : rest'
Div divattr (Header level' hattr title' : sectionContents') : rest'
go (Div divattr@(dident,dclasses,_) (Header level hattr title':ys) : xs)
| all (\case
Header level' _ _ -> level' > level
Expand Down
5 changes: 3 additions & 2 deletions src/Text/Pandoc/Writers/DocBook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ imageToDocBook _ attr src = selfClosingTag "imagedata" $
blockToDocBook :: PandocMonad m => WriterOptions -> Block -> DB m (Doc Text)
-- Add ids to paragraphs in divs with ids - this is needed for
-- pandoc-citeproc to get link anchors in bibliographies:
blockToDocBook opts (Div (id',"section":_,_) (Header lvl (_,classes,attrs) ils : xs)) = do
blockToDocBook opts (Div (id',"section":_classes,divattrs)
(Header lvl (_,hclasses,_) ils : xs)) = do
version <- ask
-- DocBook doesn't allow sections with no content, so insert some if needed
let bs = if null xs
Expand All @@ -198,7 +199,7 @@ blockToDocBook opts (Div (id',"section":_,_) (Header lvl (_,classes,attrs) ils :

-- Populate miscAttr with Header.Attr.attributes, filtering out non-valid DocBook section attributes, id, and xml:id
-- Also enrich the role attribute with certain class tokens
miscAttr = enrichRole (filter (isSectionAttr version) attrs) classes
miscAttr = enrichRole (filter (isSectionAttr version) divattrs) hclasses
attribs = nsAttr <> idAttr <> miscAttr
title' <- inlinesToDocBook opts ils
contents <- blocksToDocBook opts bs
Expand Down

0 comments on commit abf0b51

Please sign in to comment.