git.iko.soy/typeable/pandoc
on master help sign in
Added skeleton of basic docbook reader.
commitd339b29967878f64d5fe45d03d214476e9d88f7e authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 14 Apr 2012, 16:44 (15 years ago) parente37c452
4 files changed+40−1
pandoc.cabal+1 src/Text/Pandoc.hs+2 src/Text/Pandoc/Pretty.hs+1−1 src/Text/Pandoc/Readers/DocBook.hs+36
pandoc.cabal+1
@@ -246,6 +246,7 @@ Library
246246 Text.Pandoc.Readers.LaTeX,
247247 Text.Pandoc.Readers.Markdown,
248248 Text.Pandoc.Readers.RST,
249+ Text.Pandoc.Readers.DocBook,
249250 Text.Pandoc.Readers.TeXMath,
250251 Text.Pandoc.Readers.Textile,
251252 Text.Pandoc.Readers.Native,
src/Text/Pandoc.hs+2
@@ -119,6 +119,7 @@ import Text.Pandoc.Definition
119119import Text.Pandoc.Generic
120120import Text.Pandoc.Readers.Markdown
121121import Text.Pandoc.Readers.RST
122+import Text.Pandoc.Readers.DocBook
122123import Text.Pandoc.Readers.LaTeX
123124import Text.Pandoc.Readers.HTML
124125import Text.Pandoc.Readers.Textile
@@ -162,6 +163,7 @@ readers = [("native" , \_ -> readNative)
162163 ,("rst" , readRST)
163164 ,("rst+lhs" , \st ->
164165 readRST st{ stateLiterateHaskell = True})
166+ ,("docbook" , readDocBook)
165167 ,("textile" , readTextile) -- TODO : textile+lhs
166168 ,("html" , readHtml)
167169 ,("latex" , readLaTeX)
src/Text/Pandoc/Pretty.hs+1−1
@@ -1,4 +1,4 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1+{-# LANGUAGE GeneralizedNewtypeDeriving, DatatypeContexts #-}
22{-
33Copyright (C) 2010 John MacFarlane <jgm@berkeley.edu>
44
src/Text/Pandoc/Readers/DocBook.hsnew file+36
@@ -0,0 +1,36 @@
1+module Text.Pandoc.Readers.DocBook ( readDocBook ) where
2+import Text.Pandoc.Parsing (ParserState(..), defaultParserState)
3+import Text.Pandoc.Definition
4+import Text.Pandoc.Builder
5+import Text.XML.Light
6+import Data.Monoid
7+import Data.Char (isSpace)
8+
9+readDocBook :: ParserState -> String -> Pandoc
10+readDocBook st inp = Pandoc (Meta [] [] []) $ toList blocks
11+ where blocks = mconcat $ map (parseBlock st) $ parseXML inp
12+
13+parseBlock :: ParserState -> Content -> Blocks
14+parseBlock st (Text (CData _ s _)) = if all isSpace s
15+ then mempty
16+ else plain $ text s
17+parseBlock st (Elem e) =
18+ case qName (elName e) of
19+ "para" -> para $ trimInlines $ mconcat
20+ $ map (parseInline st) $ elContent e
21+ _ -> mconcat $ map (parseBlock st) $ elContent e
22+parseBlock st (CRef _) = mempty
23+
24+parseInline :: ParserState -> Content -> Inlines
25+parseInline st (Text (CData _ s _)) = text s
26+parseInline st (Elem e) =
27+ case qName (elName e) of
28+ "emphasis" -> case lookupAttrBy (\attr -> qName attr == "role")
29+ (elAttribs e) of
30+ Just "strong" -> strong innerInlines
31+ _ -> emph innerInlines
32+ _ -> innerInlines
33+ where innerInlines = trimInlines . mconcat . map (parseInline st)
34+ $ elContent e
35+parseInline st (CRef _) = mempty
36+