From 255897b66a0d6d3999ff1e923ebc925d46ff153c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 20 Apr 2012 11:01:54 -0700 Subject: DocBook reader: Refactored with new function attrValue. --- src/Text/Pandoc/Readers/DocBook.hs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/Text/Pandoc') diff --git a/src/Text/Pandoc/Readers/DocBook.hs b/src/Text/Pandoc/Readers/DocBook.hs index 30a1cd7ee..826fed8b8 100644 --- a/src/Text/Pandoc/Readers/DocBook.hs +++ b/src/Text/Pandoc/Readers/DocBook.hs @@ -3,7 +3,6 @@ import Text.Pandoc.Parsing (ParserState(..)) import Text.Pandoc.Definition import Text.Pandoc.Builder import Text.XML.Light -import Data.Maybe (fromMaybe) import Data.Monoid import Data.Char (isSpace) import Control.Monad.State @@ -510,6 +509,13 @@ readDocBook st inp = setTitle (dbDocTitle st') , dbDocDate = mempty } +-- convenience function to get an attribute value, defaulting to "" +attrValue :: String -> Element -> String +attrValue attr elt = + case lookupAttrBy (\x -> qName x == attr) (elAttribs elt) of + Just z -> z + Nothing -> "" + parseBlock :: Content -> DB Blocks parseBlock (Text (CData CDataRaw _ _)) = return mempty -- DOCTYPE parseBlock (Text (CData _ s _)) = if all isSpace s @@ -595,19 +601,14 @@ parseInline (Elem e) = "constant" -> return $ codeWith ("",["constant"],[]) $ strContent e "userinput" -> return $ codeWith ("",["userinput"],[]) $ strContent e "varargs" -> return $ str "(…)" - "ulink" -> link - (fromMaybe "" (lookupAttrBy (\attr -> qName attr == "url") - (elAttribs e))) "" <$> innerInlines + "ulink" -> link (attrValue "url" e) "" <$> innerInlines "link" -> case findAttr (QName "href" Nothing $ Just "xlink") e of Just href -> link href "" <$> innerInlines - _ -> link ('#':anchor) "" <$> innerInlines - where anchor = fromMaybe "" $ lookupAttrBy - (\attr -> qName attr == "linkend") - (elAttribs e) - "emphasis" -> case lookupAttrBy (\attr -> qName attr == "role") - (elAttribs e) of - Just "strong" -> strong <$> innerInlines - _ -> emph <$> innerInlines + _ -> link ('#' : attrValue "linkend" e) "" + <$> innerInlines + "emphasis" -> case attrValue "role" e of + "strong" -> strong <$> innerInlines + _ -> emph <$> innerInlines "footnote" -> (note . mconcat) <$> (mapM parseBlock $ elContent e) _ -> innerInlines where innerInlines = (trimInlines . mconcat) <$> -- cgit v1.2.3