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

import Language.Drasil
import Data.Drasil.Concepts.Documentation (abbreviation, fullForm, abbAcc)

import Control.Lens ((^.))
import Data.List (sortBy)
import Data.Function (on)
import Drasil.Sections.ReferenceMaterial (emptySectSentPlu)

-- | Helper function that gets the acronym out of an 'Idea'.
select :: (Idea s) => [s] -> [(String, s)]
select :: forall s. Idea s => [s] -> [(String, s)]
select [] = []
select (s
x:[s]
xs) = case s -> Maybe String
forall c. Idea c => c -> Maybe String
getA s
x of
  Maybe String
Nothing -> [s] -> [(String, s)]
forall s. Idea s => [s] -> [(String, s)]
select [s]
xs
  Just String
y  -> (String
y, s
x) (String, s) -> [(String, s)] -> [(String, s)]
forall a. a -> [a] -> [a]
: [s] -> [(String, s)]
forall s. Idea s => [s] -> [(String, s)]
select [s]
xs

-- | The actual table creation function.
tableAbbAccGen :: (Idea s) => [s] -> LabelledContent
tableAbbAccGen :: forall s. Idea s => [s] -> LabelledContent
tableAbbAccGen [] = Reference -> RawContent -> LabelledContent
llcc Reference
tableAbbAccRef (RawContent -> LabelledContent) -> RawContent -> LabelledContent
forall a b. (a -> b) -> a -> b
$ 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]
tableAbbAccGen [s]
ls = let chunks :: [(String, s)]
chunks = ((String, s) -> (String, s) -> Ordering)
-> [(String, s)] -> [(String, s)]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (String -> String -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (String -> String -> Ordering)
-> ((String, s) -> String)
-> (String, s)
-> (String, s)
-> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (String, s) -> String
forall a b. (a, b) -> a
fst) ([(String, s)] -> [(String, s)]) -> [(String, s)] -> [(String, s)]
forall a b. (a -> b) -> a -> b
$ [s] -> [(String, s)]
forall s. Idea s => [s] -> [(String, s)]
select [s]
ls in
  Reference -> RawContent -> LabelledContent
llcc Reference
tableAbbAccRef (RawContent -> LabelledContent) -> RawContent -> LabelledContent
forall a b. (a -> b) -> a -> b
$ [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]) ([(String, s) -> Sentence] -> [(String, s)] -> [[Sentence]]
forall a b. [a -> b] -> [a] -> [[b]]
mkTable
  [\(String
a,s
_) -> String -> Sentence
S String
a,
   \(String
_,s
b) -> s -> Sentence
forall n. NamedIdea n => n -> Sentence
titleize s
b]
  [(String, s)]
chunks)
  (IdeaDict -> Sentence
forall n. NamedIdea n => n -> Sentence
titleize' IdeaDict
abbAcc) Bool
True

-- | 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