-- | Extract various kinds of UIDs from a Sentence. Used in conjunction with the
-- chunk database in order to render terms, symbols, and references properly.
module Language.Drasil.Sentence.Extract(sdep, shortdep, lnames, lnames') where

import Language.Drasil.UID (UID)
import Language.Drasil.Sentence(Sentence(..), SentenceStyle(..))
import Language.Drasil.ModelExpr.Extract (meNames)

import Data.Containers.ListUtils (nubOrd)

-- | Generic traverse of all positions that could lead to /symbolic/ 'UID's from 'Sentence's.
getUIDs :: Sentence -> [UID]
getUIDs :: Sentence -> [UID]
getUIDs (Ch SentenceStyle
ShortStyle TermCapitalization
_ UID
_) = []
getUIDs (Ch SentenceStyle
TermStyle TermCapitalization
_ UID
_)  = []
getUIDs (Ch SentenceStyle
PluralTerm TermCapitalization
_ UID
_) = []
getUIDs (SyCh UID
a)            = [UID
a]
getUIDs Sy {}               = []
getUIDs S {}                = []
getUIDs P {}                = []
getUIDs Ref {}              = []
getUIDs Sentence
Percent             = []
getUIDs ((:+:) Sentence
a Sentence
b)         = Sentence -> [UID]
getUIDs Sentence
a [UID] -> [UID] -> [UID]
forall a. [a] -> [a] -> [a]
++ Sentence -> [UID]
getUIDs Sentence
b
getUIDs (Quote Sentence
a)           = Sentence -> [UID]
getUIDs Sentence
a
getUIDs (E ModelExpr
a)               = ModelExpr -> [UID]
meNames ModelExpr
a
getUIDs Sentence
EmptyS              = []

-- | Generic traverse of all positions that could lead to /symbolic/ and /abbreviated/ 'UID's from 'Sentence's
-- but doesn't go into expressions.
getUIDshort :: Sentence -> [UID]
getUIDshort :: Sentence -> [UID]
getUIDshort (Ch SentenceStyle
ShortStyle TermCapitalization
_ UID
a) = [UID
a]
getUIDshort (Ch SentenceStyle
TermStyle TermCapitalization
_ UID
_)  = []
getUIDshort (Ch SentenceStyle
PluralTerm TermCapitalization
_ UID
_) = []
getUIDshort SyCh {}             = []
getUIDshort Sy {}               = []
getUIDshort S {}                = []
getUIDshort Sentence
Percent             = []
getUIDshort P {}                = []
getUIDshort Ref {}              = []
getUIDshort ((:+:) Sentence
a Sentence
b)         = Sentence -> [UID]
getUIDshort Sentence
a [UID] -> [UID] -> [UID]
forall a. [a] -> [a] -> [a]
++ Sentence -> [UID]
getUIDshort Sentence
b
getUIDshort (Quote Sentence
a)           = Sentence -> [UID]
getUIDshort Sentence
a
getUIDshort E {}                = []
getUIDshort Sentence
EmptyS              = []

-----------------------------------------------------------------------------
-- And now implement the exported traversals all in terms of the above
-- | This is to collect /symbolic/ 'UID's that are printed out as a 'Symbol'.
sdep :: Sentence -> [UID]
sdep :: Sentence -> [UID]
sdep = [UID] -> [UID]
forall a. Ord a => [a] -> [a]
nubOrd ([UID] -> [UID]) -> (Sentence -> [UID]) -> Sentence -> [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sentence -> [UID]
getUIDs

-- This is to collect symbolic 'UID's that are printed out as an /abbreviation/.
shortdep :: Sentence -> [UID]
shortdep :: Sentence -> [UID]
shortdep = [UID] -> [UID]
forall a. Ord a => [a] -> [a]
nubOrd ([UID] -> [UID]) -> (Sentence -> [UID]) -> Sentence -> [UID]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sentence -> [UID]
getUIDshort

-- | Generic traverse of all positions that could lead to /reference/ 'UID's from 'Sentence's.
lnames :: Sentence -> [UID]
lnames :: Sentence -> [UID]
lnames Ch {}       = []
lnames SyCh {}     = []
lnames Sy {}       = []
lnames S {}        = []
lnames Sentence
Percent     = []
lnames P {}        = []
lnames (Ref UID
a Sentence
_ RefInfo
_) = [UID
a]
lnames ((:+:) Sentence
a Sentence
b) = Sentence -> [UID]
lnames Sentence
a [UID] -> [UID] -> [UID]
forall a. [a] -> [a] -> [a]
++ Sentence -> [UID]
lnames Sentence
b
lnames Quote {}    = []
lnames E {}        = []
lnames Sentence
EmptyS      = []

-- | Get /reference/ 'UID's from 'Sentence's.
lnames'  :: [Sentence] -> [UID]
lnames' :: [Sentence] -> [UID]
lnames' = (Sentence -> [UID]) -> [Sentence] -> [UID]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Sentence -> [UID]
lnames