From 664defb9d6733452f465275562708c00228d64f9 Mon Sep 17 00:00:00 2001 From: defanor Date: Thu, 22 Feb 2018 00:54:45 +0300 Subject: RDF: avoid an unnecessary query --- Text/Pandoc/Readers/RDF.hs | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/Text/Pandoc/Readers/RDF.hs b/Text/Pandoc/Readers/RDF.hs index 081b71e..bf58695 100644 --- a/Text/Pandoc/Readers/RDF.hs +++ b/Text/Pandoc/Readers/RDF.hs @@ -46,10 +46,7 @@ import Text.Pandoc.Error (PandocError(..)) import Control.Exception import System.Directory -import Foreign -import Redland.MidLevel -import Redland.LowLevel -import Redland.Util +import Redland detectAlternateVersion :: T.Text -> IO (Maybe URI) @@ -89,32 +86,31 @@ readRDF bu rf t = do parseDoc = Right . Pandoc mempty . pure . LineBlock <$> do withWSMU "memory" [] "temporary" "" (uriToString id bu "") $ \world _ model uri -> do - tryParseStringIntoModel world ["turtle", "rdfxml", "n-triples"] - model uri (T.unpack t) - withQuery world model "sparql" "SELECT ?s ?p ?o WHERE { ?s ?p ?o }" - (Just uri) $ \results -> do - cacheDir <- getXdgDirectory XdgCache "pancake" - createDirectoryIfMissing True cacheDir - withWSMU "hashes" [("hash-type", "bdb"), ("dir", cacheDir)] - "rdf-cache" "" (uriToString id bu "") $ \world' _ model' uri' -> - mapM (printTriple (world', model', uri')) results - -- todo: should probably use statements instead, need to complete - -- that part of the bindings. + guessingParseStringIntoModel world model uri (T.unpack t) + withStatements world model (Triple Nothing Nothing Nothing) $ + \triples -> do + cacheDir <- getXdgDirectory XdgCache "pancake" + createDirectoryIfMissing True cacheDir + withWSMU "hashes" [("hash-type", "bdb"), ("dir", cacheDir)] + "rdf-cache" "" (uriToString id bu "") $ \world' _ model' uri' -> + mapM (printTriple (world', model', uri')) triples printTriple :: ( ForeignPtr RedlandWorld , ForeignPtr RedlandModel , ForeignPtr RedlandURI) - -> [(String, Node)] + -> Triple -> IO [Inline] - printTriple wmu l = - concat . intersperse [Space] <$> mapM (printNode wmu . snd) l + printTriple wmu triple = + concat . intersperse [Space] <$> + mapM (printNode wmu) [subject triple, predicate triple, object triple] printNode :: ( ForeignPtr RedlandWorld , ForeignPtr RedlandModel , ForeignPtr RedlandURI) - -> Node + -> Maybe Node -> IO [Inline] - printNode _ (BlankNode s) = pure [Str s] - printNode _ (LiteralNode s) = pure [Str s] - printNode (w, m, u) (ResourceNode s) = + printNode _ Nothing = pure [Str "-"] + printNode _ (Just (BlankNode s)) = pure [Str s] + printNode _ (Just (LiteralNode s)) = pure [Str s] + printNode (w, m, u) (Just (ResourceNode s)) = let su = showURI s q = "SELECT ?label WHERE { <" ++ s ++ "> ?label }" -- cgit v1.2.3