|
|
|
@ -4,20 +4,23 @@ import Control.Applicative ((<$>)) |
|
|
|
import Control.Monad.Trans.Class |
|
|
|
import Control.Monad.Trans.Class |
|
|
|
import Control.Monad.Trans.Resource as R |
|
|
|
import Control.Monad.Trans.Resource as R |
|
|
|
|
|
|
|
|
|
|
|
import Data.Text as T |
|
|
|
import qualified Data.ByteString as BS |
|
|
|
import Text.XML.Unresolved |
|
|
|
import qualified Data.Text as Text |
|
|
|
|
|
|
|
import qualified Data.Text.Encoding as Text |
|
|
|
import Data.XML.Types |
|
|
|
import Data.XML.Types |
|
|
|
|
|
|
|
import qualified Text.XML.Stream.Render as TXSR |
|
|
|
|
|
|
|
import Text.XML.Unresolved as TXU |
|
|
|
|
|
|
|
|
|
|
|
import Data.Conduit as C |
|
|
|
import Data.Conduit as C |
|
|
|
import Data.Conduit.List as CL |
|
|
|
import Data.Conduit.List as CL |
|
|
|
|
|
|
|
|
|
|
|
import Text.XML.Stream.Parse |
|
|
|
import System.IO.Unsafe(unsafePerformIO) |
|
|
|
|
|
|
|
|
|
|
|
compressNodes :: [Node] -> [Node] |
|
|
|
compressNodes :: [Node] -> [Node] |
|
|
|
compressNodes [] = [] |
|
|
|
compressNodes [] = [] |
|
|
|
compressNodes [x] = [x] |
|
|
|
compressNodes [x] = [x] |
|
|
|
compressNodes (NodeContent (ContentText x) : NodeContent (ContentText y) : z) = |
|
|
|
compressNodes (NodeContent (ContentText x) : NodeContent (ContentText y) : z) = |
|
|
|
compressNodes $ NodeContent (ContentText $ x `T.append` y) : z |
|
|
|
compressNodes $ NodeContent (ContentText $ x `Text.append` y) : z |
|
|
|
compressNodes (x:xs) = x : compressNodes xs |
|
|
|
compressNodes (x:xs) = x : compressNodes xs |
|
|
|
|
|
|
|
|
|
|
|
elementFromEvents :: R.MonadThrow m => C.Sink Event m Element |
|
|
|
elementFromEvents :: R.MonadThrow m => C.Sink Event m Element |
|
|
|
@ -27,7 +30,7 @@ elementFromEvents = do |
|
|
|
Just (EventBeginElement n as) -> goE n as |
|
|
|
Just (EventBeginElement n as) -> goE n as |
|
|
|
_ -> lift $ R.monadThrow $ InvalidEventStream $ "not an element: " ++ show x |
|
|
|
_ -> lift $ R.monadThrow $ InvalidEventStream $ "not an element: " ++ show x |
|
|
|
where |
|
|
|
where |
|
|
|
many f = |
|
|
|
many' f = |
|
|
|
go id |
|
|
|
go id |
|
|
|
where |
|
|
|
where |
|
|
|
go front = do |
|
|
|
go front = do |
|
|
|
@ -38,7 +41,7 @@ elementFromEvents = do |
|
|
|
dropReturn x = CL.drop 1 >> return x |
|
|
|
dropReturn x = CL.drop 1 >> return x |
|
|
|
goE n as = do |
|
|
|
goE n as = do |
|
|
|
CL.drop 1 |
|
|
|
CL.drop 1 |
|
|
|
ns <- many goN |
|
|
|
ns <- many' goN |
|
|
|
y <- CL.head |
|
|
|
y <- CL.head |
|
|
|
if y == Just (EventEndElement n) |
|
|
|
if y == Just (EventEndElement n) |
|
|
|
then return $ Element n as $ compressNodes ns |
|
|
|
then return $ Element n as $ compressNodes ns |
|
|
|
@ -57,15 +60,10 @@ elementFromEvents = do |
|
|
|
openElementToEvents :: Element -> [Event] |
|
|
|
openElementToEvents :: Element -> [Event] |
|
|
|
openElementToEvents (Element name as ns) = EventBeginElement name as : goN ns [] |
|
|
|
openElementToEvents (Element name as ns) = EventBeginElement name as : goN ns [] |
|
|
|
where |
|
|
|
where |
|
|
|
goM [] = id |
|
|
|
goE (Element name' as' ns') = |
|
|
|
goM [x] = (goM' x :) |
|
|
|
(EventBeginElement name' as' :) |
|
|
|
goM (x:xs) = (goM' x :) . goM xs |
|
|
|
. goN ns' |
|
|
|
goM' (MiscInstruction i) = EventInstruction i |
|
|
|
. (EventEndElement name' :) |
|
|
|
goM' (MiscComment t) = EventComment t |
|
|
|
|
|
|
|
goE (Element name as ns) = |
|
|
|
|
|
|
|
(EventBeginElement name as :) |
|
|
|
|
|
|
|
. goN ns |
|
|
|
|
|
|
|
. (EventEndElement name :) |
|
|
|
|
|
|
|
goN [] = id |
|
|
|
goN [] = id |
|
|
|
goN [x] = goN' x |
|
|
|
goN [x] = goN' x |
|
|
|
goN (x:xs) = goN' x . goN xs |
|
|
|
goN (x:xs) = goN' x . goN xs |
|
|
|
@ -76,3 +74,15 @@ openElementToEvents (Element name as ns) = EventBeginElement name as : goN ns [] |
|
|
|
|
|
|
|
|
|
|
|
elementToEvents :: Element -> [Event] |
|
|
|
elementToEvents :: Element -> [Event] |
|
|
|
elementToEvents e@(Element name _ _) = openElementToEvents e ++ [EventEndElement name] |
|
|
|
elementToEvents e@(Element name _ _) = openElementToEvents e ++ [EventEndElement name] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderOpenElement :: Element -> BS.ByteString |
|
|
|
|
|
|
|
renderOpenElement e = Text.encodeUtf8 . Text.concat . unsafePerformIO |
|
|
|
|
|
|
|
$ CL.sourceList (openElementToEvents e) $$ TXSR.renderText def =$ CL.consume |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderElement :: Element -> BS.ByteString |
|
|
|
|
|
|
|
renderElement e = Text.encodeUtf8 . Text.concat . unsafePerformIO |
|
|
|
|
|
|
|
$ CL.sourceList (elementToEvents e) $$ TXSR.renderText def =$ CL.consume |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ppElement :: Element -> String |
|
|
|
|
|
|
|
ppElement = Text.unpack . Text.decodeUtf8 . renderElement |