git.iko.soy/typeable/pandoc
on master help sign in
Include Header.Attr.attributes as XML attributes on section

Add key-value pairs found in the attributes list of Header.Attr as XML attributes on the corresponding section element. Any key name not allowed as an XML attribute name is dropped, as are keys with invalid values where they are defined as enums in DocBook, and xml:id (for DocBook 5)/id (for DocBook 4) to not intervene with computed identifiers.

commit82e8c29cb0a89d7129f459bef6696254ec56e0c6 authorErik Rask <erik.rask@paligo.net> dateFri 19 Feb 2021, 13:05 (6 years ago) parent3861809
2 files changed+82−2
src/Text/Pandoc/Writers/Docbook.hs+45−2 test/Tests/Writers/Docbook.hs+37
src/Text/Pandoc/Writers/Docbook.hs+45−2
@@ -168,7 +168,7 @@ blockToDocbook :: PandocMonad m => WriterOptions -> Block -> DB m (Doc Text)
168168blockToDocbook _ Null = return empty
169169-- Add ids to paragraphs in divs with ids - this is needed for
170170-- pandoc-citeproc to get link anchors in bibliographies:
171−blockToDocbook opts (Div (id',"section":_,_) (Header lvl _ ils : xs)) = do
171+blockToDocbook opts (Div (id',"section":_,_) (Header lvl (_,_,attrs) ils : xs)) = do
172172 version <- ask
173173 -- Docbook doesn't allow sections with no content, so insert some if needed
174174 let bs = if null xs
@@ -188,7 +188,10 @@ blockToDocbook opts (Div (id',"section":_,_) (Header lvl _ ils : xs)) = do
188188 -- standalone documents will include them in the template.
189189 then [("xmlns", "http://docbook.org/ns/docbook"),("xmlns:xlink", "http://www.w3.org/1999/xlink")]
190190 else []
191− attribs = nsAttr <> idAttr
191+
192+ -- Populate miscAttr with Header.Attr.attributes, filtering out non-valid DocBook section attributes, id, and xml:id
193+ miscAttr = filter (isSectionAttr version) attrs
194+ attribs = nsAttr <> idAttr <> miscAttr
192195 title' <- inlinesToDocbook opts ils
193196 contents <- blocksToDocbook opts bs
194197 return $ inTags True tag attribs $ inTagsSimple "title" title' $$ contents
@@ -451,3 +454,43 @@ idAndRole (id',cls,_) = ident <> role
451454 where
452455 ident = [("id", id') | not (T.null id')]
453456 role = [("role", T.unwords cls) | not (null cls)]
457+
458+isSectionAttr :: DocBookVersion -> (Text, Text) -> Bool
459+isSectionAttr _ ("label",_) = True
460+isSectionAttr _ ("status",_) = True
461+isSectionAttr DocBook5 ("annotations",_) = True
462+isSectionAttr DocBook5 ("dir","ltr") = True
463+isSectionAttr DocBook5 ("dir","rtl") = True
464+isSectionAttr DocBook5 ("dir","lro") = True
465+isSectionAttr DocBook5 ("dir","rlo") = True
466+isSectionAttr _ ("remap",_) = True
467+isSectionAttr _ ("revisionflag","changed") = True
468+isSectionAttr _ ("revisionflag","added") = True
469+isSectionAttr _ ("revisionflag","deleted") = True
470+isSectionAttr _ ("revisionflag","off") = True
471+isSectionAttr _ ("role",_) = True
472+isSectionAttr DocBook5 ("version",_) = True
473+isSectionAttr DocBook5 ("xml:base",_) = True
474+isSectionAttr DocBook5 ("xml:lang",_) = True
475+isSectionAttr _ ("xreflabel",_) = True
476+isSectionAttr DocBook5 ("linkend",_) = True
477+isSectionAttr DocBook5 ("linkends",_) = True
478+isSectionAttr DocBook5 ("xlink:actuate",_) = True
479+isSectionAttr DocBook5 ("xlink:arcrole",_) = True
480+isSectionAttr DocBook5 ("xlink:from",_) = True
481+isSectionAttr DocBook5 ("xlink:href",_) = True
482+isSectionAttr DocBook5 ("xlink:label",_) = True
483+isSectionAttr DocBook5 ("xlink:role",_) = True
484+isSectionAttr DocBook5 ("xlink:show",_) = True
485+isSectionAttr DocBook5 ("xlink:title",_) = True
486+isSectionAttr DocBook5 ("xlink:to",_) = True
487+isSectionAttr DocBook5 ("xlink:type",_) = True
488+isSectionAttr DocBook4 ("arch",_) = True
489+isSectionAttr DocBook4 ("condition",_) = True
490+isSectionAttr DocBook4 ("conformance",_) = True
491+isSectionAttr DocBook4 ("lang",_) = True
492+isSectionAttr DocBook4 ("os",_) = True
493+isSectionAttr DocBook4 ("revision",_) = True
494+isSectionAttr DocBook4 ("security",_) = True
495+isSectionAttr DocBook4 ("vendor",_) = True
496+isSectionAttr _ (_,_) = False
test/Tests/Writers/Docbook.hs+37
@@ -11,9 +11,14 @@ import Text.Pandoc.Builder
1111docbook :: (ToPandoc a) => a -> String
1212docbook = docbookWithOpts def{ writerWrapText = WrapNone }
1313
14+docbook5 :: (ToPandoc a) => a -> String
15+docbook5 = docbook5WithOpts def{ writerWrapText = WrapNone }
16+
1417docbookWithOpts :: ToPandoc a => WriterOptions -> a -> String
1518docbookWithOpts opts = unpack . purely (writeDocbook4 opts) . toPandoc
1619
20+docbook5WithOpts :: ToPandoc a => WriterOptions -> a -> String
21+docbook5WithOpts opts = unpack . purely (writeDocbook5 opts) . toPandoc
1722{-
1823 "my test" =: X =?> Y
1924
@@ -366,4 +371,36 @@ tests = [ testGroup "line blocks"
366371 ]
367372 ]
368373 ]
374+ , testGroup "section attributes" $
375+ let
376+ headers = headerWith ("myid1",[],[("role","internal"),("xml:id","anotherid"),("dir","rtl")]) 1 "header1"
377+ <> headerWith ("myid2",[],[("invalidname","value"),("arch","linux"),("dir","invaliddir")]) 1 "header2"
378+ in
379+ [ test docbook5 "sections with attributes (db5)" $
380+ headers =?>
381+ unlines [ "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid1\" role=\"internal\" dir=\"rtl\">"
382+ , " <title>header1</title>"
383+ , " <para>"
384+ , " </para>"
385+ , "</section>"
386+ , "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid2\">"
387+ , " <title>header2</title>"
388+ , " <para>"
389+ , " </para>"
390+ , "</section>"
391+ ]
392+ , test docbook "sections with attributes (db4)" $
393+ headers =?>
394+ unlines [ "<sect1 id=\"myid1\" role=\"internal\">"
395+ , " <title>header1</title>"
396+ , " <para>"
397+ , " </para>"
398+ , "</sect1>"
399+ , "<sect1 id=\"myid2\" arch=\"linux\">"
400+ , " <title>header2</title>"
401+ , " <para>"
402+ , " </para>"
403+ , "</sect1>"
404+ ]
405+ ]
369406 ]