{-# Language TemplateHaskell #-}
module Language.Drasil.Document where
import Language.Drasil.ShortName (HasShortName(..), ShortName, shortname')
import Language.Drasil.Document.Core (UnlabelledContent(UnlblC),
LabelledContent(LblC), RawContent(Figure, Paragraph),
Contents(..), Lbl, Filepath, Author, Title, MaxWidthPercent )
import Language.Drasil.Label.Type (getAdd, prepend, LblType(..),
Referable(..), HasRefAddress(..) )
import Language.Drasil.Misc (repUnd)
import Language.Drasil.Reference (Reference(Reference))
import Language.Drasil.Sentence (Sentence(..))
import Language.Drasil.UID (UID, HasUID(..), (+++.), mkUid)
import Control.Lens ((^.), makeLenses, view)
data SecCons = Sub Section
| Con Contents
data Partition = Sections
| Part
| Chapter
data Section = Section
{ Section -> Title
tle :: Title
, Section -> [SecCons]
cons :: [SecCons]
, Section -> Reference
_lab :: Reference
}
makeLenses ''Section
instance HasUID Section where uid :: (UID -> f UID) -> Section -> f Section
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 => Lens' c UID
uid
instance Eq Section where a :: Section
a == :: Section -> Section -> Bool
== b :: 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 => Lens' c 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 => Lens' c 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 _ _ lb :: Reference
lb) = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "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 _ _ lb :: Reference
lb) = IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "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 t :: Title
t a :: Title
a toC :: ShowTableOfContents
toC sc :: [Section]
sc) =
case ShowTableOfContents
toC of
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 1 [Section]
sc
_ -> Title -> Title -> ShowTableOfContents -> [Section] -> Document
Document Title
t Title
a ShowTableOfContents
toC [Section]
sc
checkToC (Notebook t :: Title
t a :: Title
a sc :: [Section]
sc) = Title -> Title -> [Section] -> Document
Notebook Title
t Title
a [Section]
sc
llcc :: Reference -> RawContent -> LabelledContent
llcc :: Reference -> RawContent -> LabelledContent
llcc = Reference -> RawContent -> LabelledContent
LblC
ulcc :: RawContent -> UnlabelledContent
ulcc :: RawContent -> UnlabelledContent
ulcc = RawContent -> UnlabelledContent
UnlblC
mkParagraph :: Sentence -> Contents
mkParagraph :: Title -> Contents
mkParagraph x :: 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 x :: Reference
x y :: RawContent
y = LabelledContent -> Contents
LlC (LabelledContent -> Contents) -> LabelledContent -> Contents
forall a b. (a -> b) -> a -> b
$ Reference -> RawContent -> LabelledContent
llcc Reference
x RawContent
y
mkRawLC :: RawContent -> Reference -> LabelledContent
mkRawLC :: RawContent -> Reference -> LabelledContent
mkRawLC x :: RawContent
x lb :: Reference
lb = Reference -> RawContent -> LabelledContent
llcc Reference
lb RawContent
x
section :: Sentence -> [Contents] -> [Section] -> Reference -> Section
section :: Title -> [Contents] -> [Section] -> Reference -> Section
section title :: Title
title intro :: [Contents]
intro secs :: [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)
extractSection :: Document -> [Section]
(Document _ _ _ sec :: [Section]
sec) = (Section -> [Section]) -> [Section] -> [Section]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Section -> [Section]
getSec [Section]
sec
extractSection (Notebook _ _ sec :: [Section]
sec) = (Section -> [Section]) -> [Section] -> [Section]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Section -> [Section]
getSec [Section]
sec
getSec :: Section -> [Section]
getSec :: Section -> [Section]
getSec t :: Section
t@(Section _ sc :: [SecCons]
sc _) = Section
t Section -> [Section] -> [Section]
forall a. a -> [a] -> [a]
: (SecCons -> [Section]) -> [SecCons] -> [Section]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap SecCons -> [Section]
getSecCons [SecCons]
sc
getSecCons :: SecCons -> [Section]
getSecCons :: SecCons -> [Section]
getSecCons (Sub sec :: Section
sec) = Section -> [Section]
getSec Section
sec
getSecCons (Con _) = []
fig :: Lbl -> Filepath -> RawContent
fig :: Title -> String -> RawContent
fig l :: Title
l f :: String
f = Title -> String -> MaxWidthPercent -> RawContent
Figure Title
l String
f 100
figWithWidth :: Lbl -> Filepath -> MaxWidthPercent -> RawContent
figWithWidth :: Title -> String -> MaxWidthPercent -> RawContent
figWithWidth = Title -> String -> MaxWidthPercent -> RawContent
Figure
makeTabRef :: String -> Reference
makeTabRef :: String -> Reference
makeTabRef rs :: String
rs = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
mkUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Tab") ("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 rs :: String
rs = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
mkUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Fig") ("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 r :: String
r s :: 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]
++ "Label") (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Sec") ("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 rs :: String
rs = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
mkUid String
rs) (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Eqn") ("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 u :: String
u r :: String
r = UID -> LblType -> ShortName -> Reference
Reference (String -> UID
mkUid String
u) (String -> LblType
URI String
r)
makeTabRef' :: UID -> Reference
makeTabRef' :: UID -> Reference
makeTabRef' rs :: UID
rs = UID -> LblType -> ShortName -> Reference
Reference UID
rs (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Tab") ("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' rs :: UID
rs = UID -> LblType -> ShortName -> Reference
Reference UID
rs (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Fig") ("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' r :: UID
r s :: Title
s = UID -> LblType -> ShortName -> Reference
Reference (UID
r UID -> String -> UID
+++. "Label") (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Sec") ("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' rs :: UID
rs = UID -> LblType -> ShortName -> Reference
Reference UID
rs (IRefProg -> String -> LblType
RP (String -> IRefProg
prepend "Eqn") ("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' u :: UID
u r :: String
r = UID -> LblType -> ShortName -> Reference
Reference UID
u (String -> LblType
URI String
r)