Skip to content

Commit 4746d0c

Browse files
authored
Store "unnumbered" class in DocBook role attribute (#8481)
Markdown allows marking a heading as unnumbered, which is stored as a class token internally. This change will recognize this particular class token and append it to the role attribute, or create a role attribute with it if needed. This does not imply any processing in DocBook but is intended to let customized stylesheets identify these sections and act accordingly. Closes #1402
1 parent 87c4743 commit 4746d0c

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/Text/Pandoc/Writers/DocBook.hs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Conversion of 'Pandoc' documents to DocBook XML.
1414
module Text.Pandoc.Writers.DocBook ( writeDocBook4, writeDocBook5 ) where
1515
import Control.Monad.Reader
1616
import Data.Generics (everywhere, mkT)
17-
import Data.Maybe (isNothing)
17+
import Data.Maybe (isNothing, maybeToList)
1818
import Data.Monoid (Any (..))
1919
import Data.Text (Text)
2020
import qualified Data.Text as T
@@ -169,7 +169,7 @@ blockToDocBook :: PandocMonad m => WriterOptions -> Block -> DB m (Doc Text)
169169
blockToDocBook _ Null = return empty
170170
-- Add ids to paragraphs in divs with ids - this is needed for
171171
-- pandoc-citeproc to get link anchors in bibliographies:
172-
blockToDocBook opts (Div (id',"section":_,_) (Header lvl (_,_,attrs) ils : xs)) = do
172+
blockToDocBook opts (Div (id',"section":_,_) (Header lvl (_,classes,attrs) ils : xs)) = do
173173
version <- ask
174174
-- DocBook doesn't allow sections with no content, so insert some if needed
175175
let bs = if null xs
@@ -191,7 +191,8 @@ blockToDocBook opts (Div (id',"section":_,_) (Header lvl (_,_,attrs) ils : xs))
191191
else []
192192

193193
-- Populate miscAttr with Header.Attr.attributes, filtering out non-valid DocBook section attributes, id, and xml:id
194-
miscAttr = filter (isSectionAttr version) attrs
194+
-- Also enrich the role attribute with certain class tokens
195+
miscAttr = enrichRole (filter (isSectionAttr version) attrs) classes
195196
attribs = nsAttr <> idAttr <> miscAttr
196197
title' <- inlinesToDocBook opts ils
197198
contents <- blocksToDocBook opts bs
@@ -464,6 +465,14 @@ idAndRole (id',cls,_) = ident <> role
464465
ident = [("id", id') | not (T.null id')]
465466
role = [("role", T.unwords cls) | not (null cls)]
466467

468+
-- Used in blockToDocBook for Header (section) to create or extend
469+
-- the role attribute with candidate class tokens
470+
enrichRole :: [(Text, Text)] -> [Text] -> [(Text, Text)]
471+
enrichRole mattrs cls = [("role",rolevals) | rolevals /= ""]<>(filter (\x -> (fst x) /= "role") mattrs)
472+
where
473+
rolevals = T.unwords((filter (`elem` cand) cls)<>(maybeToList(lookup "role" mattrs)))
474+
cand = ["unnumbered"]
475+
467476
isSectionAttr :: DocBookVersion -> (Text, Text) -> Bool
468477
isSectionAttr _ ("label",_) = True
469478
isSectionAttr _ ("status",_) = True

test/Tests/Writers/DocBook.hs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,34 +387,45 @@ tests = [ testGroup "inline elements"
387387
]
388388
, testGroup "section attributes" $
389389
let
390-
headers = headerWith ("myid1",[],[("role","internal"),("xml:id","anotherid"),("dir","rtl")]) 1 "header1"
391-
<> headerWith ("myid2",[],[("invalidname","value"),("arch","linux"),("dir","invaliddir")]) 1 "header2"
390+
headers = headerWith ("myid1",["unnumbered","ignored"],[("role","internal"),("xml:id","anotherid"),("dir","rtl")]) 1 "header1"
391+
<> headerWith ("myid2",["unnumbered"],[("invalidname","value"),("arch","linux"),("dir","invaliddir")]) 1 "header2"
392+
<> headerWith ("myid3",["ignored"],[]) 1 "header3"
392393
in
393394
[ test docbook5 "sections with attributes (db5)" $
394395
headers =?>
395-
unlines [ "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid1\" role=\"internal\" dir=\"rtl\">"
396+
unlines [ "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid1\" role=\"unnumbered internal\" dir=\"rtl\">"
396397
, " <title>header1</title>"
397398
, " <para>"
398399
, " </para>"
399400
, "</section>"
400-
, "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid2\">"
401+
, "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid2\" role=\"unnumbered\">"
401402
, " <title>header2</title>"
402403
, " <para>"
403404
, " </para>"
404405
, "</section>"
406+
, "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid3\">"
407+
, " <title>header3</title>"
408+
, " <para>"
409+
, " </para>"
410+
, "</section>"
405411
]
406412
, test docbook "sections with attributes (db4)" $
407413
headers =?>
408-
unlines [ "<sect1 id=\"myid1\" role=\"internal\">"
414+
unlines [ "<sect1 id=\"myid1\" role=\"unnumbered internal\">"
409415
, " <title>header1</title>"
410416
, " <para>"
411417
, " </para>"
412418
, "</sect1>"
413-
, "<sect1 id=\"myid2\" arch=\"linux\">"
419+
, "<sect1 id=\"myid2\" role=\"unnumbered\" arch=\"linux\">"
414420
, " <title>header2</title>"
415421
, " <para>"
416422
, " </para>"
417423
, "</sect1>"
424+
, "<sect1 id=\"myid3\">"
425+
, " <title>header3</title>"
426+
, " <para>"
427+
, " </para>"
428+
, "</sect1>"
418429
]
419430
]
420431
]

0 commit comments

Comments
 (0)