-- | Standard code to make a table of abbreviations and acronyms.
module Drasil.Sections.TableOfAbbAndAcronyms
  (tableAbbAccGen, tableAbbAccRef) where

-- Generic Haskell
import Control.Lens ((^.))
import Data.Function (on)
import Data.List (sortBy)

-- Generic Drasil
import Language.Drasil
import Language.Drasil.Development (toSent)
import Drasil.Database (HasUID(..))
import Drasil.Database.SearchTools (TermAbbr (shortForm), longForm)
import Utils.Drasil (mkTable)

-- Vocabulary
import Drasil.Metadata.Documentation (abbreviation, fullForm, abbAcc)

-- other docLang
import Drasil.Sections.ReferenceMaterial (emptySectSentPlu)

-- | Create a table of abbreviations from the given 'TermAbbr's. If the list is
-- empty, it will return a paragraph saying there are no abbreviations or
-- acronyms. It is assumed that the provided 'TermAbbr's are unique and all have
-- a short form.
tableAbbAccGen :: [TermAbbr] -> LabelledContent
tableAbbAccGen :: [TermAbbr] -> LabelledContent
tableAbbAccGen [] = RawContent -> Reference -> LabelledContent
mkRawLC (Sentence -> RawContent
Paragraph (Sentence -> RawContent) -> Sentence -> RawContent
forall a b. (a -> b) -> a -> b
$ [IdeaDict] -> Sentence
forall n. NamedIdea n => [n] -> Sentence
emptySectSentPlu [IdeaDict
abbAcc]) Reference
tableAbbAccRef
tableAbbAccGen [TermAbbr]
ls = let chunks :: [TermAbbr]
chunks = (TermAbbr -> TermAbbr -> Ordering) -> [TermAbbr] -> [TermAbbr]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (Maybe String -> Maybe String -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Maybe String -> Maybe String -> Ordering)
-> (TermAbbr -> Maybe String) -> TermAbbr -> TermAbbr -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` TermAbbr -> Maybe String
shortForm) [TermAbbr]
ls in
  RawContent -> Reference -> LabelledContent
mkRawLC ([Sentence] -> [[Sentence]] -> Sentence -> Bool -> RawContent
Table
  ((IdeaDict -> Sentence) -> [IdeaDict] -> [Sentence]
forall a b. (a -> b) -> [a] -> [b]
map IdeaDict -> Sentence
forall n. NamedIdea n => n -> Sentence
titleize [IdeaDict
abbreviation, IdeaDict
fullForm]) ([TermAbbr -> Sentence] -> [TermAbbr] -> [[Sentence]]
forall a b. [a -> b] -> [a] -> [[b]]
mkTable
    [Sentence -> (String -> Sentence) -> Maybe String -> Sentence
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Sentence
EmptyS String -> Sentence
S (Maybe String -> Sentence)
-> (TermAbbr -> Maybe String) -> TermAbbr -> Sentence
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermAbbr -> Maybe String
shortForm,
     NPStruct -> Sentence
toSent (NPStruct -> Sentence)
-> (TermAbbr -> NPStruct) -> TermAbbr -> Sentence
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NP -> NPStruct
forall n. NounPhrase n => n -> NPStruct
titleizeNP (NP -> NPStruct) -> (TermAbbr -> NP) -> TermAbbr -> NPStruct
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TermAbbr -> NP
longForm]
  [TermAbbr]
chunks)
  (IdeaDict -> Sentence
forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
abbAcc) Bool
True) Reference
tableAbbAccRef

-- | Table of abbreviations and acronyms reference.
tableAbbAccRef :: Reference
tableAbbAccRef :: Reference
tableAbbAccRef = UID -> Reference
makeTabRef' (UID -> Reference) -> UID -> Reference
forall a b. (a -> b) -> a -> b
$ IdeaDict
abbAcc IdeaDict -> Getting UID IdeaDict UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID IdeaDict UID
forall c. HasUID c => Getter c UID
Getter IdeaDict UID
uid