Skip to content

Commit

Permalink
Document IRIs using rdfs:comment
Browse files Browse the repository at this point in the history
This patch reads all `rdfs:comment`s for a given subject, concatenates
them together, separated by newlines, and uses this value as the
Haddock documentation for that subject.

Not all vocabularies in this library are fully documented using
`rdfs:comment`, though, so there remain some generated subjects that
lack documentation.  This is for two reasons.  First, for those nodes
that lack any documentation at all, this patch elects to remain
faithful to the underlying vocabulary and keep these nodes
undocumented.  Second, some vocabularies, like SKOS, use their own
predicate for documentation; this patch chooses to only read the
standard `rdfs:comment`.  Additional documentation vocabularies can be
added later if required.
  • Loading branch information
pniedzielski committed Mar 20, 2024
1 parent e99f5c7 commit 09a4034
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Data/RDF/Vocabulary/Generator/VocabularyGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ module Data.RDF.Vocabulary.Generator.VocabularyGenerator
)
where

import Control.Monad (join)
import Data.Char (isLower)
import Data.List (nub)
import qualified Data.Map as M
import Data.Maybe (maybeToList)
import Data.RDF
( AdjHashMap,
Node (UNode),
LValue (..),
Node (..),
PrefixMappings (PrefixMappings),
RDF,
Rdf,
TurtleParser (TurtleParser),
objectOf,
parseFile,
prefixMappings,
query,
subjectOf,
triplesOf,
)
Expand Down Expand Up @@ -64,7 +68,11 @@ vocabulary graph =
subject <- nub $ subjectOf <$> triplesOf graph
iri <- maybeToList $ toIRI subject
name <- maybeToList $ iriToName iri
return (name, declareIRI name iri Nothing)
let comment = combineComments .
sequenceA .
fmap (nodeToComment . objectOf) $
query graph (Just subject) (Just rdfsCommentNode) Nothing
return (name, declareIRI name iri comment)
(PrefixMappings prefixMappings') = prefixMappings graph
namespaceDecls = do
(prefix, iri) <- M.toList prefixMappings'
Expand All @@ -87,6 +95,23 @@ unodeFun = VarE $ mkName "Data.RDF.Types.unode"
mkPrefixedNSFun :: Exp
mkPrefixedNSFun = VarE $ mkName "Data.RDF.Namespace.mkPrefixedNS"

nodeToComment :: Node -> Maybe Text
nodeToComment (UNode uri) = Just $ "See \\<<" <> uri <> ">\\>."
nodeToComment (BNode _) = Nothing
nodeToComment (BNodeGen _) = Nothing
nodeToComment (LNode (PlainL l)) = Just l
nodeToComment (LNode (PlainLL l _)) = Just l
nodeToComment (LNode (TypedL l _)) = Just l

combineComments :: Maybe [Text] -> Maybe Text
combineComments = join . fmap combineComments'
where
combineComments' [] = Nothing
combineComments' comments = Just . T.intercalate "\n" $ comments

rdfsCommentNode :: Node
rdfsCommentNode = UNode "http://www.w3.org/2000/01/rdf-schema#comment"

declareIRI :: Name -> Text -> Maybe Text -> Q Dec
declareIRI name iri comment =
let iriLiteral = LitE . StringL $ T.unpack iri
Expand Down

0 comments on commit 09a4034

Please sign in to comment.