-- | Defines functions to transform Drasil-based documents into a printable version.
module Language.Drasil.Printing.Import.Document (
  makeDocument, makeProject
) where

import Control.Lens ((^.))
import Data.Bifunctor (bimap, second)
import Data.Map (fromList)

import Language.Drasil.Document
import Drasil.Code.CodeExpr.Development (expr)

import Language.Drasil.Printing.Import.Citation (layCite)
import Language.Drasil.Printing.Import.CodeExpr (codeExpr)
import Language.Drasil.Printing.Import.ModelExpr (modelExpr)
import Language.Drasil.Printing.Import.Sentence (spec)
import qualified Language.Drasil.Printing.AST as P
import qualified Language.Drasil.Printing.Citation as P
import qualified Language.Drasil.Printing.LayoutObj as T
import Language.Drasil.Printing.PrintingInformation (PrintingInformation)

-- * Main Function

-- | Translates from 'Document' to a printable representation of 'T.Document'.
makeDocument :: PrintingInformation -> Document -> T.Document
makeDocument :: PrintingInformation -> Document -> Document
makeDocument PrintingInformation
sm (Document Title
titleLb Title
authorName ShowTableOfContents
_ [Section]
sections) =
  Title -> Title -> [LayoutObj] -> Document
T.Document (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
titleLb) (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
authorName) (PrintingInformation -> [Section] -> [LayoutObj]
createLayout PrintingInformation
sm [Section]
sections)
makeDocument PrintingInformation
sm (Notebook Title
titleLb Title
authorName [Section]
sections) =
  Title -> Title -> [LayoutObj] -> Document
T.Document (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
titleLb) (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
authorName) (PrintingInformation -> [Section] -> [LayoutObj]
createLayout' PrintingInformation
sm [Section]
sections)

-- | Translates from 'Document' to a printable representation of 'T.Project'.
makeProject :: PrintingInformation -> Document -> T.Project
makeProject :: PrintingInformation -> Document -> Project
makeProject PrintingInformation
_ Notebook {} = String -> Project
forall a. HasCallStack => String -> a
error String
"Unsupported format: Notebook"
makeProject PrintingInformation
sm (Document Title
titleLb Title
authorName ShowTableOfContents
_ [Section]
sections) =
  Title -> Title -> RefMap -> [File] -> Project
T.Project (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
titleLb) (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
authorName) RefMap
refMap [File]
files
  where
    files :: [File]
files   = PrintingInformation -> [Section] -> [File]
createFiles PrintingInformation
sm [Section]
sections
    refMap :: RefMap
refMap = [(String, String)] -> RefMap
forall k a. Ord k => [(k, a)] -> Map k a
fromList ([(String, String)] -> RefMap) -> [(String, String)] -> RefMap
forall a b. (a -> b) -> a -> b
$ (File -> [(String, String)]) -> [File] -> [(String, String)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap File -> [(String, String)]
createRefMap' [File]
files

-- * Helpers

-- | Helper function for creating sections as Files.
createFiles :: PrintingInformation -> [Section] -> [T.File]
createFiles :: PrintingInformation -> [Section] -> [File]
createFiles PrintingInformation
sm [Section]
secs = ((Int, Section) -> File) -> [(Int, Section)] -> [File]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> (Int, Section) -> File
file PrintingInformation
sm) [(Int, Section)]
secs'
  where
    secs' :: [(Int, Section)]
secs' = (Section -> [(Int, Section)]) -> [Section] -> [(Int, Section)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Int -> Section -> [(Int, Section)]
extractSubS Int
0) [Section]
secs

-- | Helper function for creating a RefMap for a Document.
createRefMap' :: T.File -> [(String, T.Filename)]
createRefMap' :: File -> [(String, String)]
createRefMap' (T.File Title
_ String
l Int
_ [LayoutObj]
c) = (LayoutObj -> [(String, String)])
-> [LayoutObj] -> [(String, String)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (String -> LayoutObj -> [(String, String)]
createRefMap String
l) [LayoutObj]
c

-- | Helper function for creating a RefMap for a LayoutObj
createRefMap :: T.Filename -> T.LayoutObj -> [(String, T.Filename)]
createRefMap :: String -> LayoutObj -> [(String, String)]
createRefMap String
fn (T.Header Int
_ Title
_ Title
l)     = String -> Title -> [(String, String)]
createRef String
fn Title
l
createRefMap String
fn (T.HDiv   Tags
_ [LayoutObj]
_ Title
l)     = String -> Title -> [(String, String)]
createRef String
fn Title
l
createRefMap String
fn (T.Table  Tags
_ [[Title]]
_ Title
l Bool
_ Title
_) = String -> Title -> [(String, String)]
createRef String
fn Title
l
createRefMap String
fn (T.Definition [(String, [LayoutObj])]
_ Title
l)   = String -> Title -> [(String, String)]
createRef String
fn Title
l
createRefMap String
fn (T.List ListType
t)           = ListType -> [(String, String)]
pass ListType
t
  where
    pass :: ListType -> [(String, String)]
pass (P.Ordered [(ItemType, Maybe Title)]
ls)     = [(ItemType, Maybe Title)] -> [(String, String)]
process  [(ItemType, Maybe Title)]
ls
    pass (P.Unordered [(ItemType, Maybe Title)]
ls)   = [(ItemType, Maybe Title)] -> [(String, String)]
process  [(ItemType, Maybe Title)]
ls
    pass (P.Simple [(Title, ItemType, Maybe Title)]
ls)      = [(Title, ItemType, Maybe Title)] -> [(String, String)]
process' [(Title, ItemType, Maybe Title)]
ls
    pass (P.Desc [(Title, ItemType, Maybe Title)]
ls)        = [(Title, ItemType, Maybe Title)] -> [(String, String)]
process' [(Title, ItemType, Maybe Title)]
ls
    pass (P.Definitions [(Title, ItemType, Maybe Title)]
ls) = [(Title, ItemType, Maybe Title)] -> [(String, String)]
process' [(Title, ItemType, Maybe Title)]
ls
    process :: [(ItemType, Maybe Title)] -> [(String, String)]
process  = ((ItemType, Maybe Title) -> [(String, String)])
-> [(ItemType, Maybe Title)] -> [(String, String)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(ItemType
_, Maybe Title
l)    -> [(String, String)]
-> (Title -> [(String, String)])
-> Maybe Title
-> [(String, String)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (String -> Title -> [(String, String)]
createRef String
fn) Maybe Title
l)
    process' :: [(Title, ItemType, Maybe Title)] -> [(String, String)]
process' = ((Title, ItemType, Maybe Title) -> [(String, String)])
-> [(Title, ItemType, Maybe Title)] -> [(String, String)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\(Title
_, ItemType
_, Maybe Title
l) -> [(String, String)]
-> (Title -> [(String, String)])
-> Maybe Title
-> [(String, String)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (String -> Title -> [(String, String)]
createRef String
fn) Maybe Title
l)
createRefMap String
fn (T.Figure Title
l Maybe Title
_ String
_ MaxWidthPercent
_)   = String -> Title -> [(String, String)]
createRef String
fn Title
l
createRefMap String
fn (T.Bib BibRef
ls)           = (Citation -> (String, String)) -> BibRef -> [(String, String)]
forall a b. (a -> b) -> [a] -> [b]
map Citation -> (String, String)
bibRefs BibRef
ls
  where
    bibRefs :: Citation -> (String, String)
bibRefs (P.Cite String
l CitationKind
_ [CiteField]
_) = (String
l, String
fn)
createRefMap String
_ LayoutObj
_                     = []

-- | Helper function for mapping a Label to a Filename
createRef :: T.Filename -> P.Label -> [(String, T.Filename)]
createRef :: String -> Title -> [(String, String)]
createRef String
fn (P.S String
l) = [(String
l, String
fn)]
createRef String
_   Title
_      = []

-- | Helper function for creating sections as layout objects.
createLayout :: PrintingInformation -> [Section] -> [T.LayoutObj]
createLayout :: PrintingInformation -> [Section] -> [LayoutObj]
createLayout PrintingInformation
sm = (Section -> LayoutObj) -> [Section] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Int -> Section -> LayoutObj
sec PrintingInformation
sm Int
0)

createLayout' :: PrintingInformation -> [Section] -> [T.LayoutObj]
createLayout' :: PrintingInformation -> [Section] -> [LayoutObj]
createLayout' PrintingInformation
sm = (Section -> [LayoutObj]) -> [Section] -> [LayoutObj]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (PrintingInformation -> Int -> Section -> [LayoutObj]
cel PrintingInformation
sm Int
0)

-- | Helper for extracting subsections into their own sections.
extractSubS :: Int -> Section -> [(T.Depth, Section)]
extractSubS :: Int -> Section -> [(Int, Section)]
extractSubS Int
d x :: Section
x@(Section Title
tl [SecCons]
c Reference
r)
  | Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = [(Int
d, Section
x)]
  | Bool
otherwise = (Int
d, Title -> [SecCons] -> Reference -> Section
Section Title
tl ((SecCons -> Bool) -> [SecCons] -> [SecCons]
forall a. (a -> Bool) -> [a] -> [a]
filter SecCons -> Bool
isCon [SecCons]
c) Reference
r) (Int, Section) -> [(Int, Section)] -> [(Int, Section)]
forall a. a -> [a] -> [a]
:
      (SecCons -> [(Int, Section)]) -> [SecCons] -> [(Int, Section)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Int -> SecCons -> [(Int, Section)]
sepSub (Int
d Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) [SecCons]
c
  where
    isCon :: SecCons -> Bool
isCon (Con Contents
_)        = Bool
True
    isCon  SecCons
_             = Bool
False
    sepSub :: Int -> SecCons -> [(Int, Section)]
sepSub Int
_   (Con Contents
_)   = []
    sepSub Int
dep (Sub Section
s) = Int -> Section -> [(Int, Section)]
extractSubS Int
dep Section
s

-- | Helper for converting a Section to a File
file :: PrintingInformation -> (T.Depth, Section) -> T.File
file :: PrintingInformation -> (Int, Section) -> File
file PrintingInformation
sm (Int
d, x :: Section
x@(Section Title
titleLb [SecCons]
contents Reference
_)) =
  Title -> String -> Int -> [LayoutObj] -> File
T.File (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
titleLb) String
fn Int
d [LayoutObj]
los
  where
    refr :: String
refr = Section -> String
forall s. Referable s => s -> String
refAdd Section
x
    fn :: String
fn = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
':') String
refr
    los :: [LayoutObj]
los = Int -> Title -> Title -> LayoutObj
T.Header Int
d (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
titleLb) (String -> Title
P.S String
refr) LayoutObj -> [LayoutObj] -> [LayoutObj]
forall a. a -> [a] -> [a]
:
      (SecCons -> LayoutObj) -> [SecCons] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Int -> SecCons -> LayoutObj
layout PrintingInformation
sm Int
d) [SecCons]
contents

-- | Helper function for creating sections at the appropriate depth.
sec :: PrintingInformation -> Int -> Section -> T.LayoutObj
sec :: PrintingInformation -> Int -> Section -> LayoutObj
sec PrintingInformation
sm Int
depth x :: Section
x@(Section Title
titleLb [SecCons]
contents Reference
_) = --FIXME: should ShortName be used somewhere?
  let refr :: Title
refr = String -> Title
P.S (Section -> String
forall s. Referable s => s -> String
refAdd Section
x) in
  Tags -> [LayoutObj] -> Title -> LayoutObj
T.HDiv [Tags -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Int -> String -> Tags
forall a. Int -> a -> [a]
replicate Int
depth String
"sub") String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"section"]
  (Int -> Title -> Title -> LayoutObj
T.Header Int
depth (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
titleLb) Title
refr LayoutObj -> [LayoutObj] -> [LayoutObj]
forall a. a -> [a] -> [a]
:
   (SecCons -> LayoutObj) -> [SecCons] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Int -> SecCons -> LayoutObj
layout PrintingInformation
sm Int
depth) [SecCons]
contents) Title
refr

cel :: PrintingInformation -> Int -> Section -> [T.LayoutObj]
cel :: PrintingInformation -> Int -> Section -> [LayoutObj]
cel PrintingInformation
sm Int
depth x :: Section
x@(Section Title
titleLb [SecCons]
contents Reference
_) =
  let refr :: Title
refr = String -> Title
P.S (Section -> String
forall s. Referable s => s -> String
refAdd Section
x) in
  [LayoutObj] -> LayoutObj
T.Cell [Int -> Title -> Title -> LayoutObj
T.Header Int
depth (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
titleLb) Title
refr] LayoutObj -> [LayoutObj] -> [LayoutObj]
forall a. a -> [a] -> [a]
:
   (SecCons -> LayoutObj) -> [SecCons] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map ([LayoutObj] -> LayoutObj
T.Cell ([LayoutObj] -> LayoutObj)
-> (SecCons -> [LayoutObj]) -> SecCons -> LayoutObj
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrintingInformation -> Int -> SecCons -> [LayoutObj]
layout' PrintingInformation
sm Int
depth) [SecCons]
contents

-- | Helper for translating sections into a printable representation of layout objects ('T.LayoutObj').
layout :: PrintingInformation -> Int -> SecCons -> T.LayoutObj
layout :: PrintingInformation -> Int -> SecCons -> LayoutObj
layout PrintingInformation
sm Int
currDepth (Sub Section
s) = PrintingInformation -> Int -> Section -> LayoutObj
sec PrintingInformation
sm (Int
currDepthInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Section
s
layout PrintingInformation
sm Int
_         (Con Contents
c) = PrintingInformation -> Contents -> LayoutObj
lay PrintingInformation
sm Contents
c

-- | Helper for translating sections into a printable representation of layout
-- objects ('T.LayoutObj').
layout' :: PrintingInformation -> Int -> SecCons -> [T.LayoutObj]
layout' :: PrintingInformation -> Int -> SecCons -> [LayoutObj]
layout' PrintingInformation
sm Int
currDepth (Sub Section
s) = PrintingInformation -> Int -> Section -> [LayoutObj]
cel PrintingInformation
sm (Int
currDepthInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Section
s
layout' PrintingInformation
sm Int
_         (Con Contents
c) = [PrintingInformation -> Contents -> LayoutObj
lay PrintingInformation
sm Contents
c]

-- | Helper that translates 'Contents' to a printable representation of 'T.LayoutObj'.
-- Called internally by 'layout'.
lay :: PrintingInformation -> Contents -> T.LayoutObj
lay :: PrintingInformation -> Contents -> LayoutObj
lay PrintingInformation
sm (LlC LabelledContent
x) = PrintingInformation -> LabelledContent -> LayoutObj
layLabelled PrintingInformation
sm LabelledContent
x
lay PrintingInformation
sm (UlC UnlabelledContent
x) = PrintingInformation -> RawContent -> LayoutObj
layUnlabelled PrintingInformation
sm (UnlabelledContent
x UnlabelledContent
-> Getting RawContent UnlabelledContent RawContent -> RawContent
forall s a. s -> Getting a s a -> a
^. Getting RawContent UnlabelledContent RawContent
forall c. HasContents c => Lens' c RawContent
Lens' UnlabelledContent RawContent
accessContents)

-- | Helper that translates 'LabelledContent's to a printable representation of 'T.LayoutObj'.
layLabelled :: PrintingInformation -> LabelledContent -> T.LayoutObj
layLabelled :: PrintingInformation -> LabelledContent -> LayoutObj
layLabelled PrintingInformation
sm x :: LabelledContent
x@(LblC UID
_ Reference
_ (Table [Title]
hdr [[Title]]
lls Title
t Bool
b)) = Tags -> [[Title]] -> Title -> Bool -> Title -> LayoutObj
T.Table [String
"table"]
  ((Title -> Title) -> [Title] -> [Title]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Title -> Title
spec PrintingInformation
sm) [Title]
hdr [Title] -> [[Title]] -> [[Title]]
forall a. a -> [a] -> [a]
: ([Title] -> [Title]) -> [[Title]] -> [[Title]]
forall a b. (a -> b) -> [a] -> [b]
map ((Title -> Title) -> [Title] -> [Title]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Title -> Title
spec PrintingInformation
sm)) [[Title]]
lls)
  (String -> Title
P.S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ LblType -> String
getAdd (LblType -> String) -> LblType -> String
forall a b. (a -> b) -> a -> b
$ LabelledContent -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd LabelledContent
x)
  Bool
b (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
t)
layLabelled PrintingInformation
sm x :: LabelledContent
x@(LblC UID
_ Reference
_ (Figure Title
c String
f MaxWidthPercent
wp HasCaption
hc))  = Title -> Maybe Title -> String -> MaxWidthPercent -> LayoutObj
T.Figure
  (String -> Title
P.S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ LblType -> String
getAdd (LblType -> String) -> LblType -> String
forall a b. (a -> b) -> a -> b
$ LabelledContent -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd LabelledContent
x)
  (if HasCaption
hc HasCaption -> HasCaption -> Bool
forall a. Eq a => a -> a -> Bool
== HasCaption
WithCaption then Title -> Maybe Title
forall a. a -> Maybe a
Just (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
c) else Maybe Title
forall a. Maybe a
Nothing)
  String
f MaxWidthPercent
wp
layLabelled PrintingInformation
sm x :: LabelledContent
x@(LblC UID
_ Reference
_ (Graph [(Title, Title)]
ps Maybe MaxWidthPercent
w Maybe MaxWidthPercent
h Title
t))    = [(Title, Title)]
-> Maybe MaxWidthPercent
-> Maybe MaxWidthPercent
-> Title
-> Title
-> LayoutObj
T.Graph
  (((Title, Title) -> (Title, Title))
-> [(Title, Title)] -> [(Title, Title)]
forall a b. (a -> b) -> [a] -> [b]
map ((Title -> Title)
-> (Title -> Title) -> (Title, Title) -> (Title, Title)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (PrintingInformation -> Title -> Title
spec PrintingInformation
sm) (PrintingInformation -> Title -> Title
spec PrintingInformation
sm)) [(Title, Title)]
ps) Maybe MaxWidthPercent
w Maybe MaxWidthPercent
h (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
t)
  (String -> Title
P.S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ LblType -> String
getAdd (LblType -> String) -> LblType -> String
forall a b. (a -> b) -> a -> b
$ LabelledContent -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd LabelledContent
x)
layLabelled PrintingInformation
sm x :: LabelledContent
x@(LblC UID
_ Reference
_ (Defini [(String, [Contents])]
pairs)) =
  [(String, [LayoutObj])] -> Title -> LayoutObj
T.Definition ([(String, [Contents])] -> [(String, [LayoutObj])]
layPairs [(String, [Contents])]
pairs) (String -> Title
P.S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ LblType -> String
getAdd (LblType -> String) -> LblType -> String
forall a b. (a -> b) -> a -> b
$ LabelledContent -> LblType
forall b. HasRefAddress b => b -> LblType
getRefAdd LabelledContent
x)
  where layPairs :: [(String, [Contents])] -> [(String, [LayoutObj])]
layPairs = ((String, [Contents]) -> (String, [LayoutObj]))
-> [(String, [Contents])] -> [(String, [LayoutObj])]
forall a b. (a -> b) -> [a] -> [b]
map (([Contents] -> [LayoutObj])
-> (String, [Contents]) -> (String, [LayoutObj])
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ((Contents -> LayoutObj) -> [Contents] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Contents -> LayoutObj
lay PrintingInformation
sm)))
layLabelled PrintingInformation
sm x :: LabelledContent
x@(LblC UID
_ Reference
_ (DerivBlock Title
h [RawContent]
d))    = Tags -> [LayoutObj] -> Title -> LayoutObj
T.HDiv [String
"subsubsubsection"]
  (Int -> Title -> Title -> LayoutObj
T.Header Int
3 (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
h) Title
refr LayoutObj -> [LayoutObj] -> [LayoutObj]
forall a. a -> [a] -> [a]
: (RawContent -> LayoutObj) -> [RawContent] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> RawContent -> LayoutObj
layUnlabelled PrintingInformation
sm) [RawContent]
d) Title
refr
  where refr :: Title
refr = String -> Title
P.S (String -> Title) -> String -> Title
forall a b. (a -> b) -> a -> b
$ LabelledContent -> String
forall s. Referable s => s -> String
refAdd LabelledContent
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"Deriv"
layLabelled PrintingInformation
sm (LblC UID
_ Reference
_ RawContent
rc)                    = PrintingInformation -> RawContent -> LayoutObj
layUnlabelled PrintingInformation
sm RawContent
rc

-- | Helper that translates 'RawContent's to a printable representation of 'T.LayoutObj'.
-- Called internally by 'lay'.
layUnlabelled :: PrintingInformation -> RawContent -> T.LayoutObj
layUnlabelled :: PrintingInformation -> RawContent -> LayoutObj
layUnlabelled PrintingInformation
sm (Table [Title]
hdr [[Title]]
lls Title
t Bool
b) = Tags -> [[Title]] -> Title -> Bool -> Title -> LayoutObj
T.Table [String
"table"]
  ((Title -> Title) -> [Title] -> [Title]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Title -> Title
spec PrintingInformation
sm) [Title]
hdr [Title] -> [[Title]] -> [[Title]]
forall a. a -> [a] -> [a]
: ([Title] -> [Title]) -> [[Title]] -> [[Title]]
forall a b. (a -> b) -> [a] -> [b]
map ((Title -> Title) -> [Title] -> [Title]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> Title -> Title
spec PrintingInformation
sm)) [[Title]]
lls) (String -> Title
P.S String
"nolabel0") Bool
b (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
t)
layUnlabelled PrintingInformation
sm (Paragraph Title
c)    = Title -> LayoutObj
T.Paragraph (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
c)
layUnlabelled PrintingInformation
sm (EqnBlock ModelExpr
c)     = Title -> LayoutObj
T.EqnBlock (Expr -> Title
P.E (ModelExpr -> PrintingInformation -> Expr
modelExpr ModelExpr
c PrintingInformation
sm))
layUnlabelled PrintingInformation
sm (DerivBlock Title
h [RawContent]
d) = Tags -> [LayoutObj] -> Title -> LayoutObj
T.HDiv [String
"subsubsubsection"]
  (Int -> Title -> Title -> LayoutObj
T.Header Int
3 (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
h) Title
refr LayoutObj -> [LayoutObj] -> [LayoutObj]
forall a. a -> [a] -> [a]
: (RawContent -> LayoutObj) -> [RawContent] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map (PrintingInformation -> RawContent -> LayoutObj
layUnlabelled PrintingInformation
sm) [RawContent]
d) Title
refr
  where refr :: Title
refr = String -> Title
P.S String
"nolabel1"
layUnlabelled PrintingInformation
sm (Enumeration ListType
cs) = ListType -> LayoutObj
T.List (ListType -> LayoutObj) -> ListType -> LayoutObj
forall a b. (a -> b) -> a -> b
$ PrintingInformation -> ListType -> ListType
makeL PrintingInformation
sm ListType
cs
layUnlabelled PrintingInformation
sm (Figure Title
c String
f MaxWidthPercent
wp HasCaption
hc)  = Title -> Maybe Title -> String -> MaxWidthPercent -> LayoutObj
T.Figure (String -> Title
P.S String
"nolabel2")
  (if HasCaption
hc HasCaption -> HasCaption -> Bool
forall a. Eq a => a -> a -> Bool
== HasCaption
WithCaption then Title -> Maybe Title
forall a. a -> Maybe a
Just (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
c) else Maybe Title
forall a. Maybe a
Nothing) String
f MaxWidthPercent
wp
layUnlabelled PrintingInformation
sm (Graph [(Title, Title)]
ps Maybe MaxWidthPercent
w Maybe MaxWidthPercent
h Title
t) = [(Title, Title)]
-> Maybe MaxWidthPercent
-> Maybe MaxWidthPercent
-> Title
-> Title
-> LayoutObj
T.Graph (((Title, Title) -> (Title, Title))
-> [(Title, Title)] -> [(Title, Title)]
forall a b. (a -> b) -> [a] -> [b]
map ((Title -> Title)
-> (Title -> Title) -> (Title, Title) -> (Title, Title)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (PrintingInformation -> Title -> Title
spec PrintingInformation
sm) (PrintingInformation -> Title -> Title
spec PrintingInformation
sm)) [(Title, Title)]
ps)
                               Maybe MaxWidthPercent
w Maybe MaxWidthPercent
h (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
t) (String -> Title
P.S String
"nolabel6")
layUnlabelled PrintingInformation
sm (Defini [(String, [Contents])]
pairs)  = [(String, [LayoutObj])] -> Title -> LayoutObj
T.Definition ([(String, [Contents])] -> [(String, [LayoutObj])]
layPairs [(String, [Contents])]
pairs) (String -> Title
P.S String
"nolabel7")
  where layPairs :: [(String, [Contents])] -> [(String, [LayoutObj])]
layPairs = ((String, [Contents]) -> (String, [LayoutObj]))
-> [(String, [Contents])] -> [(String, [LayoutObj])]
forall a b. (a -> b) -> [a] -> [b]
map (([Contents] -> [LayoutObj])
-> (String, [Contents]) -> (String, [LayoutObj])
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ((Contents -> LayoutObj) -> [Contents] -> [LayoutObj]
forall a b. (a -> b) -> [a] -> [b]
map Contents -> LayoutObj
temp))
        temp :: Contents -> LayoutObj
temp  Contents
y   = PrintingInformation -> RawContent -> LayoutObj
layUnlabelled PrintingInformation
sm (Contents
y Contents -> Getting RawContent Contents RawContent -> RawContent
forall s a. s -> Getting a s a -> a
^. Getting RawContent Contents RawContent
forall c. HasContents c => Lens' c RawContent
Lens' Contents RawContent
accessContents)
layUnlabelled  PrintingInformation
_ (Bib BibRef
bib)              = BibRef -> LayoutObj
T.Bib (BibRef -> LayoutObj) -> BibRef -> LayoutObj
forall a b. (a -> b) -> a -> b
$ (Citation -> Citation) -> BibRef -> BibRef
forall a b. (a -> b) -> [a] -> [b]
map Citation -> Citation
layCite BibRef
bib
layUnlabelled PrintingInformation
sm (CodeBlock Expr
c)     = Title -> LayoutObj
T.CodeBlock (Expr -> Title
P.E (PrintingInformation -> CodeExpr -> Expr
codeExpr PrintingInformation
sm (Expr -> CodeExpr
expr Expr
c)))

-- | Translates lists to be printable.
makeL :: PrintingInformation -> ListType -> P.ListType
makeL :: PrintingInformation -> ListType -> ListType
makeL PrintingInformation
sm (Bullet [(ItemType, Maybe String)]
bs)      = [(ItemType, Maybe Title)] -> ListType
P.Unordered   ([(ItemType, Maybe Title)] -> ListType)
-> [(ItemType, Maybe Title)] -> ListType
forall a b. (a -> b) -> a -> b
$ ((ItemType, Maybe String) -> (ItemType, Maybe Title))
-> [(ItemType, Maybe String)] -> [(ItemType, Maybe Title)]
forall a b. (a -> b) -> [a] -> [b]
map ((ItemType -> ItemType)
-> (Maybe String -> Maybe Title)
-> (ItemType, Maybe String)
-> (ItemType, Maybe Title)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (PrintingInformation -> ItemType -> ItemType
item PrintingInformation
sm) ((String -> Title) -> Maybe String -> Maybe Title
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Title
P.S)) [(ItemType, Maybe String)]
bs
makeL PrintingInformation
sm (Numeric [(ItemType, Maybe String)]
ns)     = [(ItemType, Maybe Title)] -> ListType
P.Ordered     ([(ItemType, Maybe Title)] -> ListType)
-> [(ItemType, Maybe Title)] -> ListType
forall a b. (a -> b) -> a -> b
$ ((ItemType, Maybe String) -> (ItemType, Maybe Title))
-> [(ItemType, Maybe String)] -> [(ItemType, Maybe Title)]
forall a b. (a -> b) -> [a] -> [b]
map ((ItemType -> ItemType)
-> (Maybe String -> Maybe Title)
-> (ItemType, Maybe String)
-> (ItemType, Maybe Title)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (PrintingInformation -> ItemType -> ItemType
item PrintingInformation
sm) ((String -> Title) -> Maybe String -> Maybe Title
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Title
P.S)) [(ItemType, Maybe String)]
ns
makeL PrintingInformation
sm (Simple [ListTuple]
ps)      = [(Title, ItemType, Maybe Title)] -> ListType
P.Simple      ([(Title, ItemType, Maybe Title)] -> ListType)
-> [(Title, ItemType, Maybe Title)] -> ListType
forall a b. (a -> b) -> a -> b
$ (ListTuple -> (Title, ItemType, Maybe Title))
-> [ListTuple] -> [(Title, ItemType, Maybe Title)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Title
x,ItemType
y,Maybe String
z) -> (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
x, PrintingInformation -> ItemType -> ItemType
item PrintingInformation
sm ItemType
y, (String -> Title) -> Maybe String -> Maybe Title
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Title
P.S Maybe String
z)) [ListTuple]
ps
makeL PrintingInformation
sm (Desc [ListTuple]
ps)        = [(Title, ItemType, Maybe Title)] -> ListType
P.Desc        ([(Title, ItemType, Maybe Title)] -> ListType)
-> [(Title, ItemType, Maybe Title)] -> ListType
forall a b. (a -> b) -> a -> b
$ (ListTuple -> (Title, ItemType, Maybe Title))
-> [ListTuple] -> [(Title, ItemType, Maybe Title)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Title
x,ItemType
y,Maybe String
z) -> (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
x, PrintingInformation -> ItemType -> ItemType
item PrintingInformation
sm ItemType
y, (String -> Title) -> Maybe String -> Maybe Title
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Title
P.S Maybe String
z)) [ListTuple]
ps
makeL PrintingInformation
sm (Definitions [ListTuple]
ps) = [(Title, ItemType, Maybe Title)] -> ListType
P.Definitions ([(Title, ItemType, Maybe Title)] -> ListType)
-> [(Title, ItemType, Maybe Title)] -> ListType
forall a b. (a -> b) -> a -> b
$ (ListTuple -> (Title, ItemType, Maybe Title))
-> [ListTuple] -> [(Title, ItemType, Maybe Title)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Title
x,ItemType
y,Maybe String
z) -> (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
x, PrintingInformation -> ItemType -> ItemType
item PrintingInformation
sm ItemType
y, (String -> Title) -> Maybe String -> Maybe Title
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Title
P.S Maybe String
z)) [ListTuple]
ps

-- | Helper for translating list items to be printable.
item :: PrintingInformation -> ItemType -> P.ItemType
item :: PrintingInformation -> ItemType -> ItemType
item PrintingInformation
sm (Flat Title
i)     = Title -> ItemType
P.Flat (Title -> ItemType) -> Title -> ItemType
forall a b. (a -> b) -> a -> b
$ PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
i
item PrintingInformation
sm (Nested Title
t ListType
s) = Title -> ListType -> ItemType
P.Nested (PrintingInformation -> Title -> Title
spec PrintingInformation
sm Title
t) (PrintingInformation -> ListType -> ListType
makeL PrintingInformation
sm ListType
s)