Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store "unnumbered" class in DocBook role attribute #8481

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Text/Pandoc/Writers/DocBook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Conversion of 'Pandoc' documents to DocBook XML.
module Text.Pandoc.Writers.DocBook ( writeDocBook4, writeDocBook5 ) where
import Control.Monad.Reader
import Data.Generics (everywhere, mkT)
import Data.Maybe (isNothing)
import Data.Maybe (isNothing, maybeToList)
import Data.Monoid (Any (..))
import Data.Text (Text)
import qualified Data.Text as T
Expand Down Expand Up @@ -169,7 +169,7 @@ blockToDocBook :: PandocMonad m => WriterOptions -> Block -> DB m (Doc Text)
blockToDocBook _ Null = return empty
-- 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 (_,_,attrs) ils : xs)) = do
blockToDocBook opts (Div (id',"section":_,_) (Header lvl (_,classes,attrs) 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 @@ -191,7 +191,8 @@ blockToDocBook opts (Div (id',"section":_,_) (Header lvl (_,_,attrs) ils : xs))
else []

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

-- Used in blockToDocBook for Header (section) to create or extend
-- the role attribute with candidate class tokens
enrichRole :: [(Text, Text)] -> [Text] -> [(Text, Text)]
enrichRole mattrs cls = [("role",rolevals) | rolevals /= ""]<>(filter (\x -> (fst x) /= "role") mattrs)
where
rolevals = T.unwords((filter (`elem` cand) cls)<>(maybeToList(lookup "role" mattrs)))
cand = ["unnumbered"]
Copy link
Collaborator

@tarleb tarleb Jan 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! This is good to merge from my POV.

Your comments on readability made me wonder, and I ended up writing an (untested!!) version trying to strike a balance between readability and and conciseness. Not sure if I succeeded, but here we go:

enrichRole :: [(Text, Text)] -> [Text] -> [(Text, Text)]
enrichRole mattrs cls = [("role", T.unword roles) | not (null roles)] <> nonRole
  where
    (roleAttr, nonRole) = partition (\(key, _v) -> key == "role") mattrs
    roles = nub $ ["unnumbered" | "unnumbered" `elem` cls] <> map snd roleAttr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tarleb Thank you for the suggestion! I'm fine either way, although having cand separate was something I hoped to keep, as I can envision there are more class tokens that would make sense to add as role tokens in DocBook, and adding to that list is (perhaps) less intimidating to future contributors. Let me know if you would prefer it changed, and if not, if there's more to do before merge (rebase on current master?)

Copy link
Collaborator

@tarleb tarleb Jan 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, keeping the list of "role classes" separate seems sensible.

I just noticed that some lines break the 80 chars limit, we generally want to keep lines below that length. Once that's fixed we can squash-merge everything into a single commit; no need for a rebase.

EDIT: We're less strict about this limit in the tests, those are fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tarleb Sorry, didn't get to the 80char fix before it merged. Will keep it in mind in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem at all. Thanks again, looking forward to the next one!


isSectionAttr :: DocBookVersion -> (Text, Text) -> Bool
isSectionAttr _ ("label",_) = True
isSectionAttr _ ("status",_) = True
Expand Down
23 changes: 17 additions & 6 deletions test/Tests/Writers/DocBook.hs
Original file line number Diff line number Diff line change
Expand Up @@ -387,34 +387,45 @@ tests = [ testGroup "inline elements"
]
, testGroup "section attributes" $
let
headers = headerWith ("myid1",[],[("role","internal"),("xml:id","anotherid"),("dir","rtl")]) 1 "header1"
<> headerWith ("myid2",[],[("invalidname","value"),("arch","linux"),("dir","invaliddir")]) 1 "header2"
headers = headerWith ("myid1",["unnumbered","ignored"],[("role","internal"),("xml:id","anotherid"),("dir","rtl")]) 1 "header1"
<> headerWith ("myid2",["unnumbered"],[("invalidname","value"),("arch","linux"),("dir","invaliddir")]) 1 "header2"
<> headerWith ("myid3",["ignored"],[]) 1 "header3"
in
[ test docbook5 "sections with attributes (db5)" $
headers =?>
unlines [ "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid1\" role=\"internal\" dir=\"rtl\">"
unlines [ "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid1\" role=\"unnumbered internal\" dir=\"rtl\">"
, " <title>header1</title>"
, " <para>"
, " </para>"
, "</section>"
, "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid2\">"
, "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid2\" role=\"unnumbered\">"
, " <title>header2</title>"
, " <para>"
, " </para>"
, "</section>"
, "<section xmlns=\"http://docbook.org/ns/docbook\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:id=\"myid3\">"
, " <title>header3</title>"
, " <para>"
, " </para>"
, "</section>"
]
, test docbook "sections with attributes (db4)" $
headers =?>
unlines [ "<sect1 id=\"myid1\" role=\"internal\">"
unlines [ "<sect1 id=\"myid1\" role=\"unnumbered internal\">"
, " <title>header1</title>"
, " <para>"
, " </para>"
, "</sect1>"
, "<sect1 id=\"myid2\" arch=\"linux\">"
, "<sect1 id=\"myid2\" role=\"unnumbered\" arch=\"linux\">"
, " <title>header2</title>"
, " <para>"
, " </para>"
, "</sect1>"
, "<sect1 id=\"myid3\">"
, " <title>header3</title>"
, " <para>"
, " </para>"
, "</sect1>"
]
]
]