{-# Language TemplateHaskell #-}
module Language.Drasil.Document.Sections (
section, fig, figNoCap, figWithWidth, figNoCapWithWidth, Section(..),
SecCons(..) , llcc, llccFig, llccTab, llccEqn, llccFig', llccTab', llccEqn',
ulcc, Document(..), mkParagraph, mkFig, mkRawLC, ShowTableOfContents(..),
checkToC, makeTabRef, makeFigRef, makeSecRef, makeEqnRef, makeURI,
makeTabRef', makeFigRef', makeSecRef', makeEqnRef', makeURI'
) where
import Control.Lens ((^.), makeLenses, view)
import Drasil.Database (UID, HasUID(..), (+++.), mkUid, nsUid, HasChunkRefs(..))
import Utils.Drasil (repUnd)
import qualified Data.Set as Set
import Language.Drasil.ShortName (HasShortName(..), ShortName, shortname')
import Language.Drasil.Document.Core (UnlabelledContent(UnlblC),
LabelledContent(LblC), HasCaption(..), RawContent(Figure, Paragraph),
Contents(..), Lbl, Filepath, Author, Title, MaxWidthPercent)
import Language.Drasil.Label.Type (getAdd, prepend, LblType(..),
Referable(..), HasRefAddress(..) )
import Language.Drasil.Document.Reference (Reference(Reference))
import Language.Drasil.Sentence (Sentence(..))
data SecCons = Sub Section
| Con Contents
data Section = Section
{ Section -> Title
tle :: Title
, Section -> [SecCons]
cons :: [SecCons]
, Section -> Reference
_lab :: Reference
}
makeLenses ''Section
instance HasUID Section where uid :: Getter Section UID
uid = (Reference -> f Reference) -> Section -> f Section
Lens' Section Reference
lab ((Reference -> f Reference) -> Section -> f Section)
-> ((UID -> f UID) -> Reference -> f Reference)
-> (UID -> f UID)
-> Section
-> f Section
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (UID -> f UID) -> Reference -> f Reference
forall c. HasUID c => Getter c UID
Getter Reference UID
uid
instance HasChunkRefs Section where
chunkRefs :: Section -> Set UID
chunkRefs Section
sec = [Set UID] -> Set UID
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions
[ Title -> Set UID
forall a. HasChunkRefs a => a -> Set UID
chunkRefs (Section -> Title
tle Section
sec)
, Reference -> Set UID
forall a. HasChunkRefs a => a -> Set UID
chunkRefs (Section
sec Section -> Getting Reference Section Reference -> Reference
forall s a. s -> Getting a s a -> a
^. Getting Reference Section Reference
Lens' Section Reference
lab)
]
{-# INLINABLE chunkRefs #-}
instance Eq Section where Section
a == :: Section -> Section -> Bool
== Section
b = Section
a Section -> Getting UID Section UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID Section UID
forall c. HasUID c => Getter c UID
Getter Section UID
uid UID -> UID -> Bool
forall a. Eq a => a -> a -> Bool
== Section
b Section -> Getting UID Section UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID Section UID
forall c. HasUID c => Getter c UID
Getter Section UID
uid
instance HasShortName Section where shortname :: Section -> ShortName
shortname = Reference -> ShortName
forall s. HasShortName s => s -> ShortName
shortname (Reference -> ShortName)
-> (Section -> Reference) -> Section -> ShortName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting Reference Section Reference -> Section -> Reference
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Reference Section Reference
Lens' Section Reference
lab
instance Referable Section where
refAdd :: Section -> String
refAdd = LblType -> String
getAdd (LblType -> String) -> (Section -> LblType) -> Section -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Reference -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd (Reference -> LblType)
-> (Section -> Reference) -> Section -> LblType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting Reference Section Reference -> Section -> Reference
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Reference Section Reference
Lens' Section Reference
lab
renderRef :: Section -> LblType
renderRef (Section Title
_ [SecCons]
_ Reference
lb) = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Sec") (LblType -> String
getAdd (LblType -> String) -> LblType -> String
forall a b. (a -> b) -> a -> b
$ Reference -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd Reference
lb)
instance HasRefAddress Section where getRefAdd :: Section -> LblType
getRefAdd (Section Title
_ [SecCons]
_ Reference
lb) = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Sec") (LblType -> String
getAdd (LblType -> String) -> LblType -> String
forall a b. (a -> b) -> a -> b
$ Reference -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd Reference
lb)
data Document = Document Title Author ShowTableOfContents [Section]
| Notebook Title Author [Section]
data ShowTableOfContents = ToC | NoToC
checkToC :: Document -> Document
checkToC :: Document -> Document
checkToC (Document Title
t Title
a ShowTableOfContents
toC [Section]
sc) =
case ShowTableOfContents
toC of
ShowTableOfContents
ToC -> Title -> Title -> ShowTableOfContents -> [Section] -> Document
Document Title
t Title
a ShowTableOfContents
toC ([Section] -> Document) -> [Section] -> Document
forall a b. (a -> b) -> a -> b
$ Int -> [Section] -> [Section]
forall a. Int -> [a] -> [a]
drop Int
1 [Section]
sc
ShowTableOfContents
_ -> Title -> Title -> ShowTableOfContents -> [Section] -> Document
Document Title
t Title
a ShowTableOfContents
toC [Section]
sc
checkToC (Notebook Title
t Title
a [Section]
sc) = Title -> Title -> [Section] -> Document
Notebook Title
t Title
a [Section]
sc
llcc :: UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc :: UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc UID
u LblType
lbl ShortName
sn = UID -> Reference -> RawContent -> LabelledContent
LblC UID
u (UID -> LblType -> ShortName -> Reference
Reference UID
u LblType
lbl ShortName
sn)
llccFig :: String -> RawContent -> LabelledContent
llccFig :: String -> RawContent -> LabelledContent
llccFig String
rs = UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (String -> UID
docUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Fig") (String
"Figure:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd String
rs)) (Title -> ShortName
shortname' (String -> Title
S String
rs))
llccTab :: String -> RawContent -> LabelledContent
llccTab :: String -> RawContent -> LabelledContent
llccTab String
rs = UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (String -> UID
docUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Tab") (String
"Table:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd String
rs)) (Title -> ShortName
shortname' (String -> Title
S String
rs))
llccEqn :: String -> RawContent -> LabelledContent
llccEqn :: String -> RawContent -> LabelledContent
llccEqn String
rs = UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (String -> UID
docUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Eqn") (String
"Equation:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd String
rs)) (Title -> ShortName
shortname' (String -> Title
S String
rs))
llccFig' :: UID -> RawContent -> LabelledContent
llccFig' :: UID -> RawContent -> LabelledContent
llccFig' UID
rs = UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (UID -> UID
docNs UID
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Fig") (String
"Figure:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd (UID -> String
forall a. Show a => a -> String
show UID
rs))) (Title -> ShortName
shortname' (String -> Title
S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ UID -> String
forall a. Show a => a -> String
show UID
rs))
llccTab' :: UID -> RawContent -> LabelledContent
llccTab' :: UID -> RawContent -> LabelledContent
llccTab' UID
rs = UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (UID -> UID
docNs UID
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Tab") (String
"Table:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd (UID -> String
forall a. Show a => a -> String
show UID
rs))) (Title -> ShortName
shortname' (String -> Title
S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ UID -> String
forall a. Show a => a -> String
show UID
rs))
llccEqn' :: UID -> RawContent -> LabelledContent
llccEqn' :: UID -> RawContent -> LabelledContent
llccEqn' UID
rs = UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (UID -> UID
docNs UID
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Eqn") (String
"Equation:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd (UID -> String
forall a. Show a => a -> String
show UID
rs))) (Title -> ShortName
shortname' (String -> Title
S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ UID -> String
forall a. Show a => a -> String
show UID
rs))
ulcc :: RawContent -> UnlabelledContent
ulcc :: RawContent -> UnlabelledContent
ulcc = RawContent -> UnlabelledContent
UnlblC
mkParagraph :: Sentence -> Contents
mkParagraph :: Title -> Contents
mkParagraph Title
x = UnlabelledContent -> Contents
UlC (UnlabelledContent -> Contents) -> UnlabelledContent -> Contents
forall a b. (a -> b) -> a -> b
$ RawContent -> UnlabelledContent
ulcc (RawContent -> UnlabelledContent)
-> RawContent -> UnlabelledContent
forall a b. (a -> b) -> a -> b
$ Title -> RawContent
Paragraph Title
x
mkFig :: Reference -> RawContent -> Contents
mkFig :: Reference -> RawContent -> Contents
mkFig Reference
r RawContent
rc = LabelledContent -> Contents
LlC (LabelledContent -> Contents) -> LabelledContent -> Contents
forall a b. (a -> b) -> a -> b
$ UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (Reference
r Reference -> Getting UID Reference UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID Reference UID
forall c. HasUID c => Getter c UID
Getter Reference UID
uid) (Reference -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd Reference
r) (Reference -> ShortName
forall s. HasShortName s => s -> ShortName
shortname Reference
r) RawContent
rc
mkRawLC :: RawContent -> Reference -> LabelledContent
mkRawLC :: RawContent -> Reference -> LabelledContent
mkRawLC RawContent
rc Reference
r = UID -> LblType -> ShortName -> RawContent -> LabelledContent
llcc (Reference
r Reference -> Getting UID Reference UID -> UID
forall s a. s -> Getting a s a -> a
^. Getting UID Reference UID
forall c. HasUID c => Getter c UID
Getter Reference UID
uid) (Reference -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd Reference
r) (Reference -> ShortName
forall s. HasShortName s => s -> ShortName
shortname Reference
r) RawContent
rc
section :: Sentence -> [Contents] -> [Section] -> Reference -> Section
section :: Title -> [Contents] -> [Section] -> Reference -> Section
section Title
title [Contents]
intro [Section]
secs = Title -> [SecCons] -> Reference -> Section
Section Title
title ((Contents -> SecCons) -> [Contents] -> [SecCons]
forall a b. (a -> b) -> [a] -> [b]
map Contents -> SecCons
Con [Contents]
intro [SecCons] -> [SecCons] -> [SecCons]
forall a. [a] -> [a] -> [a]
++ (Section -> SecCons) -> [Section] -> [SecCons]
forall a b. (a -> b) -> [a] -> [b]
map Section -> SecCons
Sub [Section]
secs)
fig :: Lbl -> Filepath -> RawContent
fig :: Title -> String -> RawContent
fig Title
l String
f = Title -> String -> MaxWidthPercent -> HasCaption -> RawContent
Figure Title
l String
f MaxWidthPercent
100 HasCaption
WithCaption
figNoCap :: Lbl -> Filepath -> RawContent
figNoCap :: Title -> String -> RawContent
figNoCap Title
l String
f = Title -> String -> MaxWidthPercent -> HasCaption -> RawContent
Figure Title
l String
f MaxWidthPercent
100 HasCaption
NoCaption
figWithWidth :: Lbl -> Filepath -> MaxWidthPercent -> RawContent
figWithWidth :: Title -> String -> MaxWidthPercent -> RawContent
figWithWidth Title
l String
f MaxWidthPercent
wp = Title -> String -> MaxWidthPercent -> HasCaption -> RawContent
Figure Title
l String
f MaxWidthPercent
wp HasCaption
WithCaption
figNoCapWithWidth :: Lbl -> Filepath -> MaxWidthPercent -> RawContent
figNoCapWithWidth :: Title -> String -> MaxWidthPercent -> RawContent
figNoCapWithWidth Title
l String
f MaxWidthPercent
wp = Title -> String -> MaxWidthPercent -> HasCaption -> RawContent
Figure Title
l String
f MaxWidthPercent
wp HasCaption
NoCaption
docNs :: UID -> UID
docNs :: UID -> UID
docNs = String -> UID -> UID
nsUid String
"doc"
docUid :: String -> UID
docUid :: String -> UID
docUid = UID -> UID
docNs (UID -> UID) -> (String -> UID) -> String -> UID
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> UID
mkUid
makeTabRef :: String -> Reference
makeTabRef :: String -> Reference
makeTabRef String
rs = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
docUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Tab") (String
"Table:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd String
rs)) (Title -> ShortName
shortname' (String -> Title
S String
rs))
makeFigRef :: String -> Reference
makeFigRef :: String -> Reference
makeFigRef String
rs = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
docUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Fig") (String
"Figure:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd String
rs)) (Title -> ShortName
shortname' (String -> Title
S String
rs))
makeSecRef :: String -> Sentence -> Reference
makeSecRef :: String -> Title -> Reference
makeSecRef String
r Title
s = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
mkUid (String -> UID) -> String -> UID
forall a b. (a -> b) -> a -> b
$ String
r String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"Label") (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Sec") (String
"Sec:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd String
r))
(Title -> ShortName
shortname' Title
s)
makeEqnRef :: String -> Reference
makeEqnRef :: String -> Reference
makeEqnRef String
rs = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
docUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Eqn") (String
"Equation:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd String
rs)) (Title -> ShortName
shortname' (String -> Title
S String
rs))
makeURI :: String -> String -> ShortName -> Reference
makeURI :: String -> String -> ShortName -> Reference
makeURI String
u String
r = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
mkUid String
u) (String -> LblType
URI String
r)
makeTabRef' :: UID -> Reference
makeTabRef' :: UID -> Reference
makeTabRef' UID
rs = UID -> LblType -> ShortName -> Reference
Reference (UID -> UID
docNs UID
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Tab") (String
"Table:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd (UID -> String
forall a. Show a => a -> String
show UID
rs))) (Title -> ShortName
shortname' (String -> Title
S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ UID -> String
forall a. Show a => a -> String
show UID
rs))
makeFigRef' :: UID -> Reference
makeFigRef' :: UID -> Reference
makeFigRef' UID
rs = UID -> LblType -> ShortName -> Reference
Reference (UID -> UID
docNs UID
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Fig") (String
"Figure:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd (UID -> String
forall a. Show a => a -> String
show UID
rs))) (Title -> ShortName
shortname' (String -> Title
S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ UID -> String
forall a. Show a => a -> String
show UID
rs))
makeSecRef' :: UID -> Sentence -> Reference
makeSecRef' :: UID -> Title -> Reference
makeSecRef' UID
r Title
s = UID -> LblType -> ShortName -> Reference
Reference (UID
r UID -> String -> UID
+++. String
"Label") (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Sec") (String
"Sec:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd (UID -> String
forall a. Show a => a -> String
show UID
r)))
(Title -> ShortName
shortname' Title
s)
makeEqnRef' :: UID -> Reference
makeEqnRef' :: UID -> Reference
makeEqnRef' UID
rs = UID -> LblType -> ShortName -> Reference
Reference (UID -> UID
docNs UID
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend String
"Eqn") (String
"Equation:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
repUnd (UID -> String
forall a. Show a => a -> String
show UID
rs))) (Title -> ShortName
shortname' (String -> Title
S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ UID -> String
forall a. Show a => a -> String
show UID
rs))
makeURI' :: UID -> String -> ShortName -> Reference
makeURI' :: UID -> String -> ShortName -> Reference
makeURI' UID
u String
r = UID -> LblType -> ShortName -> Reference
Reference UID
u (String -> LblType
URI String
r)