diff --git a/src/Text/Pandoc/Readers/DocBook.hs b/src/Text/Pandoc/Readers/DocBook.hs index a73e46ce0393..904be0fa4a5c 100644 --- a/src/Text/Pandoc/Readers/DocBook.hs +++ b/src/Text/Pandoc/Readers/DocBook.hs @@ -46,7 +46,7 @@ import Text.Pandoc.Builder import Text.Pandoc.Class.PandocMonad (PandocMonad, report) import Text.Pandoc.Options import Text.Pandoc.Logging (LogMessage(..)) -import Text.Pandoc.Shared (safeRead, extractSpaces) +import Text.Pandoc.Shared (safeRead, extractSpaces, addPandocAttributes) import Text.Pandoc.Sources (ToSources(..), sourcesToText) import Text.Pandoc.Transforms (headerShift) import Text.TeXMath (readMathML, writeTeX) @@ -855,6 +855,10 @@ getBlocks :: PandocMonad m => Element -> DB m Blocks getBlocks e = mconcat <$> mapM parseBlock (elContent e) +getRoleAttr :: Element -> [(Text, Text)] -- extract role attribute and add it to the attribute list +getRoleAttr e = case attrValue "role" e of + "" -> [] + r -> [("role", r)] parseBlock :: PandocMonad m => Content -> DB m Blocks parseBlock (Text (CData CDataRaw _ _)) = return mempty -- DOCTYPE @@ -862,8 +866,8 @@ parseBlock (Text (CData _ s _)) = if T.all isSpace s then return mempty else return $ plain $ trimInlines $ text s parseBlock (CRef x) = return $ plain $ str $ T.toUpper x -parseBlock (Elem e) = - case qName (elName e) of +parseBlock (Elem e) = do + parsedBlock <- case qName (elName e) of "toc" -> skip -- skip TOC, since in pandoc it's autogenerated "index" -> skip -- skip index, since page numbers meaningless "para" -> parseMixed para (elContent e) @@ -975,6 +979,7 @@ parseBlock (Elem e) = "title" -> return mempty -- handled in parent element "subtitle" -> return mempty -- handled in parent element _ -> skip >> getBlocks e + return $ addPandocAttributes (getRoleAttr e) parsedBlock where skip = do let qn = qName $ elName e let name = if "pi-" `T.isPrefixOf` qn @@ -1112,7 +1117,12 @@ parseBlock (Elem e) = modify $ \st -> st{ dbSectionLevel = n } b <- getBlocks e modify $ \st -> st{ dbSectionLevel = n - 1 } - return $ headerWith (elId, classes, maybeToList titleabbrevElAsAttr++attrs) n' headerText <> b + let content = headerWith (elId, classes, maybeToList titleabbrevElAsAttr) + n' headerText <> b + return $ case attrValue "role" e of + "" -> content + _ -> divWith ("", ["section"], + ("level", T.pack $ show n') : attrs) content titleabbrevElAsAttr = case filterChild (named "titleabbrev") e `mplus` (filterChild (named "info") e >>= @@ -1135,9 +1145,8 @@ parseBlock (Elem e) = b <- p case mbt of Nothing -> return b - Just t -> return $ divWith (attrValue "id" e,[],[]) + Just t -> return $ divWith (attrValue "id" e, [], getRoleAttr e) (divWith ("", ["title"], []) (plain t) <> b) - -- Admonitions are parsed into a div. Following other Docbook tools that output HTML, -- we parse the optional title as a div with the @title@ class, and give the -- block itself a class corresponding to the admonition name. @@ -1226,8 +1235,8 @@ parseInline (Text (CData _ s _)) = do else return $ text s parseInline (CRef ref) = return $ text $ fromMaybe (T.toUpper ref) $ lookupEntity ref -parseInline (Elem e) = - case qName (elName e) of +parseInline (Elem e) = do + parsedInline <- case qName (elName e) of "anchor" -> do return $ spanWith (attrValue "id" e, [], []) mempty "phrase" -> do @@ -1349,6 +1358,9 @@ parseInline (Elem e) = -- to in handleInstructions, above. "pi-asciidoc-br" -> return linebreak _ -> skip >> innerInlines id + return $ case qName (elName e) of + "emphasis" -> parsedInline + _ -> addPandocAttributes (getRoleAttr e) parsedInline where skip = do let qn = qName $ elName e let name = if "pi-" `T.isPrefixOf` qn diff --git a/test/docbook-reader.docbook b/test/docbook-reader.docbook index 5c3b527875c2..bd9fb8812c0e 100644 --- a/test/docbook-reader.docbook +++ b/test/docbook-reader.docbook @@ -27,9 +27,9 @@ This is a set of tests for pandoc. Most of them are adapted from John Gruber’s markdown test suite. - + Headers - + Level 2 with an <ulink url="/url">embedded link</ulink> Level 3 with <emphasis>emphasis</emphasis> @@ -74,6 +74,9 @@ Here’s a regular paragraph. + + And here’s a regular paragraph with a role. + In Markdown 1.0.0 and earlier. Version 8. This line turns into a list item. Because a hard-wrapped line in the middle of a paragraph looked like @@ -93,6 +96,11 @@ This is a block quote. It is pretty short. +
+ + This is a block quote with a role. + +
Code in a block quote: @@ -233,6 +241,26 @@ These should not be escaped: \$ \\ \> \[ \{ + + with role: + + + + + First + + + + + Second + + + + + Third + + + and tight: @@ -702,6 +730,12 @@ These should not be escaped: \$ \\ \> \[ \{ So is this word. + + So is this word with a role. + + + So is this phrase with a role. + This is code: >, $, \, \$, @@ -1408,7 +1442,7 @@ or here: <http://example.com/> Table with attributes - +
Attribute table caption @@ -1444,7 +1478,7 @@ or here: <http://example.com/> Table with attributes, without caption - +
diff --git a/test/docbook-reader.native b/test/docbook-reader.native index 13f5c49fcb7c..f46d60f4e4b9 100644 --- a/test/docbook-reader.native +++ b/test/docbook-reader.native @@ -62,39 +62,55 @@ Pandoc , Space , Str "suite." ] - , Header 1 ( "headers" , [] , [] ) [ Str "Headers" ] - , Header - 2 - ( "level-2-with-an-embedded-link" , [] , [] ) - [ Str "Level" - , Space - , Str "2" - , Space - , Str "with" - , Space - , Str "an" - , Space - , Link - ( "" , [] , [] ) - [ Str "embedded" , Space , Str "link" ] - ( "/url" , "" ) - ] - , Header - 3 - ( "level-3-with-emphasis" , [] , [] ) - [ Str "Level" - , Space - , Str "3" - , Space - , Str "with" - , Space - , Emph [ Str "emphasis" ] + , Div + ( "" + , [ "section" ] + , [ ( "role" , "sect1role" ) , ( "level" , "1" ) ] + ) + [ Header 1 ( "headers" , [] , [] ) [ Str "Headers" ] + , Div + ( "" + , [ "section" ] + , [ ( "role" , "sect2role" ) , ( "level" , "2" ) ] + ) + [ Header + 2 + ( "level-2-with-an-embedded-link" , [] , [] ) + [ Str "Level" + , Space + , Str "2" + , Space + , Str "with" + , Space + , Str "an" + , Space + , Link + ( "" , [] , [] ) + [ Str "embedded" , Space , Str "link" ] + ( "/url" , "" ) + ] + , Header + 3 + ( "level-3-with-emphasis" , [] , [] ) + [ Str "Level" + , Space + , Str "3" + , Space + , Str "with" + , Space + , Emph [ Str "emphasis" ] + ] + , Header + 4 + ( "level-4" , [] , [] ) + [ Str "Level" , Space , Str "4" ] + , Header + 5 + ( "level-5" , [] , [] ) + [ Str "Level" , Space , Str "5" ] + , Para [ Str "Hi." ] + ] ] - , Header - 4 ( "level-4" , [] , [] ) [ Str "Level" , Space , Str "4" ] - , Header - 5 ( "level-5" , [] , [] ) [ Str "Level" , Space , Str "5" ] - , Para [ Str "Hi." ] , Header 1 ( "level-1" , [] , [] ) [ Str "Level" , Space , Str "1" ] , Header @@ -151,6 +167,29 @@ Pandoc , Space , Str "paragraph." ] + , Div + ( "" + , [] + , [ ( "wrapper" , "1" ) , ( "role" , "pararole" ) ] + ) + [ Para + [ Str "And" + , Space + , Str "here\8217s" + , Space + , Str "a" + , Space + , Str "regular" + , Space + , Str "paragraph" + , Space + , Str "with" + , Space + , Str "a" + , Space + , Str "role." + ] + ] , Para [ Str "In" , Space @@ -251,6 +290,31 @@ Pandoc , Str "short." ] ] + , Div + ( "" + , [] + , [ ( "wrapper" , "1" ) , ( "role" , "roleblockquote" ) ] + ) + [ BlockQuote + [ Para + [ Str "This" + , Space + , Str "is" + , Space + , Str "a" + , Space + , Str "block" + , Space + , Str "quote" + , Space + , Str "with" + , Space + , Str "a" + , Space + , Str "role." + ] + ] + ] , BlockQuote [ Para [ Str "Code" @@ -348,6 +412,19 @@ Pandoc , [ Para [ Str "Second" ] ] , [ Para [ Str "Third" ] ] ] + , Para [ Str "with" , Space , Str "role:" ] + , Div + ( "" + , [] + , [ ( "wrapper" , "1" ) , ( "role" , "listrole" ) ] + ) + [ OrderedList + ( 1 , Decimal , DefaultDelim ) + [ [ Para [ Str "First" ] ] + , [ Para [ Str "Second" ] ] + , [ Para [ Str "Third" ] ] + ] + ] , Para [ Str "and" , Space , Str "tight:" ] , OrderedList ( 1 , Decimal , DefaultDelim ) @@ -931,6 +1008,38 @@ Pandoc , Space , Str "word." ] + , Para + [ Str "So" + , Space + , Str "is" + , Space + , Emph [ Emph [ Str "this" ] ] + , Space + , Str "word" + , Space + , Str "with" + , Space + , Str "a" + , Space + , Str "role." + ] + , Para + [ Str "So" + , Space + , Str "is" + , Space + , Span + ( "" , [ "phraserole" ] , [ ( "role" , "phraserole" ) ] ) + [ Str "this" ] + , Space + , Str "phrase" + , Space + , Str "with" + , Space + , Str "a" + , Space + , Str "role." + ] , Para [ Str "This" , Space @@ -2570,7 +2679,9 @@ Pandoc , Table ( "mytableid1" , [ "mytableclass1" , "mytableclass2" ] - , [ ( "custom-style" , "mytabstyle1" ) ] + , [ ( "role" , "tablerole1" ) + , ( "custom-style" , "mytabstyle1" ) + ] ) (Caption Nothing @@ -2636,7 +2747,9 @@ Pandoc , Table ( "mytableid2" , [ "mytableclass3" , "mytableclass4" ] - , [ ( "custom-style" , "mytabstyle2" ) ] + , [ ( "role" , "tablerole2" ) + , ( "custom-style" , "mytabstyle2" ) + ] ) (Caption Nothing []) [ ( AlignDefault , ColWidthDefault )