-- | Defines .json printers to generate jupyter notebooks. For more information on each of the helper functions, please view the [source files](https://jacquescarette.github.io/Drasil/docs/full/drasil-printers-0.1.10.0/src/Language.Drasil.JSON.Print.html).
module Language.Drasil.JSON.Print(genJupyter) where

import Prelude hiding (print, (<>))
import Text.PrettyPrint hiding (Str)
import Numeric (showEFloat)

import qualified Language.Drasil as L

import Language.Drasil.Format (DocType(Lesson))

import Language.Drasil.Printing.Import (makeDocument)
import Language.Drasil.Printing.AST (Spec, ItemType(Flat, Nested),  
  ListType(Ordered, Unordered, Definitions, Desc, Simple), Expr, 
  Ops(..), Expr(..), Spec(Quote, EmptyS, Ref, HARDNL, Sp, S, E, (:+:)),
  Fonts(Bold), OverSymb(Hat), Label, LinkType(Internal, Cite2, External))
import Language.Drasil.Printing.Citation (BibRef)
import Language.Drasil.Printing.LayoutObj (Document(Document), LayoutObj(..))
import Language.Drasil.Printing.Helpers (sqbrac, unders, hat)
import Language.Drasil.Printing.PrintingInformation (PrintingInformation)

import qualified Language.Drasil.TeX.Print as TeX (spec, pExpr)
import Language.Drasil.TeX.Monad (runPrint, MathContext(Math), D, toMath, PrintLaTeX(PL))
import Language.Drasil.HTML.Monad (unPH)
import Language.Drasil.HTML.Helpers (th, bold, reflinkInfo)
import Language.Drasil.HTML.Print(renderCite, OpenClose(Open, Close), fence,
  htmlBibFormatter)

import Language.Drasil.JSON.Helpers (makeMetadata, h, stripnewLine, nbformat, codeformat,
 tr, td, image, li, pa, ba, table, refwrap, refID, reflink, reflinkURI, mkDiv, 
 markdownB, markdownB', markdownE, markdownE', markdownCell, codeCell)

-- | Generate a python notebook document (using json).
-- build : build the SRS document in JSON format
-- build': build the general Jupyter Notbook document
genJupyter :: PrintingInformation -> DocType -> L.Document -> Doc
genJupyter :: PrintingInformation -> DocType -> Document -> Doc
genJupyter PrintingInformation
sm DocType
Lesson Document
doc = Document -> Doc
build  (PrintingInformation -> Document -> Document
makeDocument PrintingInformation
sm Document
doc)
genJupyter PrintingInformation
sm DocType
_      Document
doc = Document -> Doc
build' (PrintingInformation -> Document -> Document
makeDocument PrintingInformation
sm Document
doc)

-- | Build the JSON Document, called by genJSON
build :: Document -> Doc
build :: Document -> Doc
build (Document Title
t Title
a [LayoutObj]
c) = 
  Doc
markdownB Doc -> Doc -> Doc
$$
  Doc -> Doc
nbformat (String -> Doc
text String
"# " Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
t) Doc -> Doc -> Doc
$$
  Doc -> Doc
nbformat (String -> Doc
text String
"## " Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
a) Doc -> Doc -> Doc
$$
  Doc
markdownE Doc -> Doc -> Doc
$$
  [LayoutObj] -> Doc
print' [LayoutObj]
c Doc -> Doc -> Doc
$$
  Doc
markdownB' Doc -> Doc -> Doc
$$
  Doc
markdownE' Doc -> Doc -> Doc
$$
  Doc
makeMetadata Doc -> Doc -> Doc
$$
  String -> Doc
text String
"}" 

build' :: Document -> Doc
build' :: Document -> Doc
build' (Document Title
t Title
a [LayoutObj]
c) = 
  Doc
markdownB Doc -> Doc -> Doc
$$
  Doc -> Doc
nbformat (String -> Doc
text String
"# " Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
t) Doc -> Doc -> Doc
$$
  Doc -> Doc
nbformat (String -> Doc
text String
"## " Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
a) Doc -> Doc -> Doc
$$
  Doc
markdownE Doc -> Doc -> Doc
$$
  Doc
markdownB' Doc -> Doc -> Doc
$$ 
  [LayoutObj] -> Doc
print [LayoutObj]
c Doc -> Doc -> Doc
$$
  Doc
markdownE' Doc -> Doc -> Doc
$$
  Doc
makeMetadata Doc -> Doc -> Doc
$$
  String -> Doc
text String
"}" 

-- | Helper for rendering a D from Latex print
printMath :: D -> Doc
printMath :: D -> Doc
printMath = (D -> MathContext -> Doc
forall a. PrintLaTeX a -> MathContext -> a
`runPrint` MathContext
Math)

-- | Helper for rendering LayoutObjects into JSON
-- printLO is used for generating SRS
printLO :: LayoutObj -> Doc
printLO :: LayoutObj -> Doc
printLO (Header Int
n Title
contents Title
l)            = Doc -> Doc
nbformat Doc
empty Doc -> Doc -> Doc
$$ Doc -> Doc
nbformat (Int -> Doc
h (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
contents) Doc -> Doc -> Doc
$$ Doc -> Doc
refID (Title -> Doc
pSpec Title
l)
printLO (Cell [LayoutObj]
layoutObs)                 = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat ((LayoutObj -> Doc) -> [LayoutObj] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> Doc
printLO [LayoutObj]
layoutObs)
printLO (HDiv Tags
_ [LayoutObj]
layoutObs Title
_)             = [Doc] -> Doc
vcat ((LayoutObj -> Doc) -> [LayoutObj] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> Doc
printLO [LayoutObj]
layoutObs)
printLO (Paragraph Title
contents)             = Doc -> Doc
nbformat Doc
empty Doc -> Doc -> Doc
$$ Doc -> Doc
nbformat (String -> Doc
stripnewLine (Doc -> String
forall a. Show a => a -> String
show(Title -> Doc
pSpec Title
contents)))
printLO (EqnBlock Title
contents)              = Doc -> Doc
nbformat Doc
mathEqn
  where
    toMathHelper :: PrintLaTeX a -> PrintLaTeX a
toMathHelper (PL MathContext -> a
g) = (MathContext -> a) -> PrintLaTeX a
forall a. (MathContext -> a) -> PrintLaTeX a
PL (\MathContext
_ -> MathContext -> a
g MathContext
Math)
    mjDelimDisp :: a -> Doc
mjDelimDisp a
d  = String -> Doc
text String
"$$" Doc -> Doc -> Doc
<> String -> Doc
stripnewLine (a -> String
forall a. Show a => a -> String
show a
d) Doc -> Doc -> Doc
<> String -> Doc
text String
"$$" 
    mathEqn :: Doc
mathEqn = Doc -> Doc
forall {a}. Show a => a -> Doc
mjDelimDisp (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ D -> Doc
printMath (D -> Doc) -> D -> Doc
forall a b. (a -> b) -> a -> b
$ D -> D
forall {a}. PrintLaTeX a -> PrintLaTeX a
toMathHelper (D -> D) -> D -> D
forall a b. (a -> b) -> a -> b
$ Title -> D
TeX.spec Title
contents
printLO (Table Tags
_ [[Title]]
rows Title
r Bool
_ Title
_)            = Doc -> Doc
nbformat Doc
empty Doc -> Doc -> Doc
$$ [[Title]] -> Doc -> Doc
makeTable [[Title]]
rows (Title -> Doc
pSpec Title
r)
printLO (Definition DType
dt [(String, [LayoutObj])]
ssPs Title
l)          = Doc -> Doc
nbformat (String -> Doc
text String
"<br>") Doc -> Doc -> Doc
$$ DType -> [(String, [LayoutObj])] -> Doc -> Doc
makeDefn DType
dt [(String, [LayoutObj])]
ssPs (Title -> Doc
pSpec Title
l)
printLO (List ListType
t)                        = Doc -> Doc
nbformat Doc
empty Doc -> Doc -> Doc
$$ ListType -> Bool -> Doc
makeList ListType
t Bool
False
printLO (Figure Title
r Maybe Title
c String
f MaxWidthPercent
wp)               = Doc -> Maybe Doc -> Doc -> MaxWidthPercent -> Doc
makeFigure (Title -> Doc
pSpec Title
r) ((Title -> Doc) -> Maybe Title -> Maybe Doc
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Title -> Doc
pSpec Maybe Title
c) (String -> Doc
text String
f) MaxWidthPercent
wp
printLO (Bib BibRef
bib)                       = BibRef -> Doc
makeBib BibRef
bib
printLO Graph{}                         = Doc
empty 
printLO CodeBlock {}                    = Doc
empty

-- printLO' is used for generating general notebook (lesson plans)
printLO' :: LayoutObj -> Doc
printLO' :: LayoutObj -> Doc
printLO' (HDiv [String
"equation"] [LayoutObj]
layoutObs Title
_)  = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat ((LayoutObj -> Doc) -> [LayoutObj] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> Doc
printLO' [LayoutObj]
layoutObs)
printLO' (Header Int
n Title
contents Title
l)            = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
nbformat (Int -> Doc
h (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
contents) Doc -> Doc -> Doc
$$ Doc -> Doc
refID (Title -> Doc
pSpec Title
l)
printLO' (Cell [LayoutObj]
layoutObs)                 = [Doc] -> Doc
vcat ((LayoutObj -> Doc) -> [LayoutObj] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> Doc
printLO' [LayoutObj]
layoutObs)
printLO' HDiv {}                          = Doc
empty
printLO' (Paragraph Title
contents)             = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
nbformat (String -> Doc
stripnewLine (Doc -> String
forall a. Show a => a -> String
show(Title -> Doc
pSpec Title
contents)))
printLO' (EqnBlock Title
contents)              = Doc -> Doc
nbformat Doc
mathEqn
  where
    toMathHelper :: PrintLaTeX a -> PrintLaTeX a
toMathHelper (PL MathContext -> a
g) = (MathContext -> a) -> PrintLaTeX a
forall a. (MathContext -> a) -> PrintLaTeX a
PL (\MathContext
_ -> MathContext -> a
g MathContext
Math)
    mjDelimDisp :: a -> Doc
mjDelimDisp a
d  = String -> Doc
text String
"$$" Doc -> Doc -> Doc
<> String -> Doc
stripnewLine (a -> String
forall a. Show a => a -> String
show a
d) Doc -> Doc -> Doc
<> String -> Doc
text String
"$$" 
    mathEqn :: Doc
mathEqn = Doc -> Doc
forall {a}. Show a => a -> Doc
mjDelimDisp (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ D -> Doc
printMath (D -> Doc) -> D -> Doc
forall a b. (a -> b) -> a -> b
$ D -> D
forall {a}. PrintLaTeX a -> PrintLaTeX a
toMathHelper (D -> D) -> D -> D
forall a b. (a -> b) -> a -> b
$ Title -> D
TeX.spec Title
contents
printLO' (Table Tags
_ [[Title]]
rows Title
r Bool
_ Title
_)            = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ [[Title]] -> Doc -> Doc
makeTable [[Title]]
rows (Title -> Doc
pSpec Title
r)
printLO' Definition {}                   = Doc
empty
printLO' (List ListType
t)                        = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ ListType -> Bool -> Doc
makeList ListType
t Bool
False
printLO' (Figure Title
r Maybe Title
c String
f MaxWidthPercent
wp)               = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Maybe Doc -> Doc -> MaxWidthPercent -> Doc
makeFigure (Title -> Doc
pSpec Title
r) ((Title -> Doc) -> Maybe Title -> Maybe Doc
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Title -> Doc
pSpec Maybe Title
c) (String -> Doc
text String
f) MaxWidthPercent
wp
printLO' (Bib BibRef
bib)                       = Doc -> Doc
markdownCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ BibRef -> Doc
makeBib BibRef
bib
printLO' Graph{}                         = Doc
empty 
printLO' (CodeBlock Title
contents)            = Doc -> Doc
codeCell (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
codeformat (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Title -> Doc
cSpec Title
contents


-- | Called by build, uses 'printLO' to render the layout
-- objects in Doc format.
print :: [LayoutObj] -> Doc
print :: [LayoutObj] -> Doc
print = (LayoutObj -> Doc -> Doc) -> Doc -> [LayoutObj] -> Doc
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Doc -> Doc -> Doc
($$) (Doc -> Doc -> Doc)
-> (LayoutObj -> Doc) -> LayoutObj -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutObj -> Doc
printLO) Doc
empty

-- | Called by build', uses 'printLO'' to render the layout
-- objects in Doc format.
print' :: [LayoutObj] -> Doc
print' :: [LayoutObj] -> Doc
print' = (LayoutObj -> Doc -> Doc) -> Doc -> [LayoutObj] -> Doc
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Doc -> Doc -> Doc
($$) (Doc -> Doc -> Doc)
-> (LayoutObj -> Doc) -> LayoutObj -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutObj -> Doc
printLO') Doc
empty

pSpec :: Spec -> Doc
pSpec :: Title -> Doc
pSpec (E Expr
e)  = String -> Doc
text String
"$" Doc -> Doc -> Doc
<> Expr -> Doc
pExpr Expr
e Doc -> Doc -> Doc
<> String -> Doc
text String
"$" -- symbols used
pSpec (Title
a :+: Title
b) = Title -> Doc
pSpec Title
a Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
b
pSpec (S String
s)     = (String -> Doc) -> (String -> Doc) -> Either String String -> Doc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Doc
forall a. HasCallStack => String -> a
error (String -> Doc
text (String -> Doc) -> (String -> String) -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> String) -> String -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> String
escapeChars) (Either String String -> Doc) -> Either String String -> Doc
forall a b. (a -> b) -> a -> b
$ String -> String -> Either String String
L.checkValidStr String
s String
invalid
  where
    invalid :: String
invalid = [Char
'<', Char
'>']
    escapeChars :: Char -> String
escapeChars Char
'&' = String
"\\&"
    escapeChars Char
c = [Char
c]
pSpec (Sp Special
s)    = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ PrintHTML -> String
unPH (PrintHTML -> String) -> PrintHTML -> String
forall a b. (a -> b) -> a -> b
$ Special -> PrintHTML
forall r. RenderSpecial r => Special -> r
L.special Special
s
pSpec Title
HARDNL    = Doc
empty
pSpec (Ref LinkType
Internal String
r Title
a)      = String -> Doc -> Doc
reflink     String
r (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Title -> Doc
pSpec Title
a
pSpec (Ref (Cite2 Title
n)   String
r Title
a)    = String -> Doc -> Doc -> Doc
reflinkInfo String
r (Title -> Doc
pSpec Title
a) (Title -> Doc
pSpec Title
n)
pSpec (Ref LinkType
External String
r Title
a)      = String -> Doc -> Doc
reflinkURI  String
r (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Title -> Doc
pSpec Title
a
pSpec Title
EmptyS    = String -> Doc
text String
"" -- Expected in the output
pSpec (Quote Title
q) = Doc -> Doc
doubleQuotes (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Title -> Doc
pSpec Title
q

cSpec :: Spec -> Doc
cSpec :: Title -> Doc
cSpec (E Expr
e)  = Expr -> Doc
pExpr Expr
e 
cSpec Title
_      = Doc
empty


-- | Renders expressions in JSON (called by multiple functions)
pExpr :: Expr -> Doc
pExpr :: Expr -> Doc
pExpr (Dbl Double
d)        = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Maybe Int -> Double -> String -> String
forall a. RealFloat a => Maybe Int -> a -> String -> String
showEFloat Maybe Int
forall a. Maybe a
Nothing Double
d String
""
pExpr (Int Integer
i)        = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Integer -> String
forall a. Show a => a -> String
show Integer
i
pExpr (Str String
s)        = Doc -> Doc
doubleQuotes (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ String -> Doc
text String
s
pExpr (Div Expr
n Expr
d)      = String -> Doc -> Doc -> Doc
mkDiv String
"frac" (Expr -> Doc
pExpr Expr
n) (Expr -> Doc
pExpr Expr
d)
pExpr (Row [Expr]
l)        = [Doc] -> Doc
hcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Expr -> Doc) -> [Expr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Expr -> Doc
pExpr [Expr]
l
pExpr (Set [Expr]
l)        = [Doc] -> Doc
hcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (Expr -> Doc) -> [Expr] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Expr -> Doc
pExpr [Expr]
l
pExpr (Ident String
s)      = String -> Doc
text String
s
pExpr (Label String
s)      = String -> Doc
text String
s
pExpr (Spec Special
s)       = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ PrintHTML -> String
unPH (PrintHTML -> String) -> PrintHTML -> String
forall a b. (a -> b) -> a -> b
$ Special -> PrintHTML
forall r. RenderSpecial r => Special -> r
L.special Special
s
pExpr (Sub Expr
e)        = Doc
unders Doc -> Doc -> Doc
<> Expr -> Doc
pExpr Expr
e
pExpr (Sup Expr
e)        = Doc
hat Doc -> Doc -> Doc
<> Expr -> Doc
pExpr Expr
e
pExpr (Over OverSymb
Hat Expr
s)   = Expr -> Doc
pExpr Expr
s Doc -> Doc -> Doc
<> String -> Doc
text String
"&#770;"
pExpr (MO Ops
o)         = String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ Ops -> String
pOps Ops
o
pExpr (Fenced Fence
l Fence
r Expr
e) = String -> Doc
text (OpenClose -> Fence -> String
fence OpenClose
Open Fence
l) Doc -> Doc -> Doc
<> Expr -> Doc
pExpr Expr
e Doc -> Doc -> Doc
<> String -> Doc
text (OpenClose -> Fence -> String
fence OpenClose
Close Fence
r)
pExpr (Font Fonts
Bold Expr
e)  = Expr -> Doc
pExpr Expr
e
--pExpr (Font Bold e)  = bold $ pExpr e -- used before
--pExpr (Font Emph e)  = text "<em>" <> pExpr e <> text "</em>" -- HTML used
--pExpr (Spc Thin)     = text "&#8239;" -- HTML used
-- Uses TeX for Mathjax for all other exprs 
pExpr Expr
e              = D -> Doc
printMath (D -> Doc) -> D -> Doc
forall a b. (a -> b) -> a -> b
$ D -> D
toMath (D -> D) -> D -> D
forall a b. (a -> b) -> a -> b
$ Expr -> D
TeX.pExpr Expr
e



-- TODO: edit all operations in markdown format
pOps :: Ops -> String
pOps :: Ops -> String
pOps Ops
IsIn       = String
"&thinsp;&isin;&thinsp;"
pOps Ops
Integer    = String
"&#8484;"
pOps Ops
Rational   = String
"&#8474;"
pOps Ops
Real       = String
"&#8477;"
pOps Ops
Natural    = String
"&#8469;"
pOps Ops
Boolean    = String
"&#120121;"
pOps Ops
Comma      = String
","
pOps Ops
Prime      = String
"&prime;"
pOps Ops
Log        = String
"log"
pOps Ops
Ln         = String
"ln"
pOps Ops
Sin        = String
"sin"
pOps Ops
Cos        = String
"cos"
pOps Ops
Tan        = String
"tan"
pOps Ops
Sec        = String
"sec"
pOps Ops
Csc        = String
"csc"
pOps Ops
Cot        = String
"cot"
pOps Ops
Arcsin     = String
"arcsin"
pOps Ops
Arccos     = String
"arccos"
pOps Ops
Arctan     = String
"arctan"
pOps Ops
Not        = String
"&not;"
pOps Ops
Dim        = String
"dim"
pOps Ops
Exp        = String
"e"
pOps Ops
Neg        = String
"-"
pOps Ops
Cross      = String
"&#10799;"
pOps Ops
VAdd       = String
" + "
pOps Ops
VSub       = String
" - "
pOps Ops
Dot        = String
"&sdot;"
pOps Ops
Scale      = String
"" -- same as Mul
pOps Ops
Eq         = String
" = " -- with spaces?
pOps Ops
NEq        = String
"&ne;"
pOps Ops
Lt         = String
"&thinsp;&lt;&thinsp;" --thin spaces make these more readable
pOps Ops
Gt         = String
"&thinsp;&gt;&thinsp;"
pOps Ops
LEq        = String
"&thinsp;&le;&thinsp;"
pOps Ops
GEq        = String
"&thinsp;&ge;&thinsp;"
pOps Ops
Impl       = String
" &rArr; "
pOps Ops
Iff        = String
" &hArr; "
pOps Ops
Subt       = String
" - "
pOps Ops
And        = String
" &and; "
pOps Ops
Or         = String
" &or; "
pOps Ops
Add        = String
" + "
pOps Ops
Mul        = String
""
pOps Ops
Summ       = String
"&sum"
pOps Ops
Inte       = String
"&int;"
pOps Ops
Prod       = String
"&prod;"
pOps Ops
Point      = String
"."
pOps Ops
Perc       = String
"%"
pOps Ops
LArrow     = String
" &larr; "
pOps Ops
RArrow     = String
" &rarr; "
pOps Ops
ForAll     = String
" ForAll "
pOps Ops
Partial    = String
"&part;"
pOps Ops
SAdd       = String
" + "
pOps Ops
SRemove    = String
" - "
pOps Ops
SContains  = String
" in "
pOps Ops
SUnion     = String
" and "

-- | Renders Markdown table, called by 'printLO'
makeTable :: [[Spec]] -> Doc -> Doc
makeTable :: [[Title]] -> Doc -> Doc
makeTable [] Doc
_      = String -> Doc
forall a. HasCallStack => String -> a
error String
"No table to print"
makeTable ([Title]
l:[[Title]]
lls) Doc
r = Doc -> Doc
refID Doc
r Doc -> Doc -> Doc
$$ Doc -> Doc
nbformat Doc
empty Doc -> Doc -> Doc
$$ ([Title] -> Doc
makeHeaderCols [Title]
l Doc -> Doc -> Doc
$$ [[Title]] -> Doc
makeRows [[Title]]
lls) Doc -> Doc -> Doc
$$ Doc -> Doc
nbformat Doc
empty

-- | Helper for creating table rows
makeRows :: [[Spec]] -> Doc
makeRows :: [[Title]] -> Doc
makeRows = ([Title] -> Doc -> Doc) -> Doc -> [[Title]] -> Doc
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Doc -> Doc -> Doc
($$) (Doc -> Doc -> Doc) -> ([Title] -> Doc) -> [Title] -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Title] -> Doc
makeColumns) Doc
empty

-- | makeHeaderCols: Helper for creating table header row (each of the column header cells)
-- | makeColumns: Helper for creating table columns
makeHeaderCols, makeColumns :: [Spec] -> Doc
makeHeaderCols :: [Title] -> Doc
makeHeaderCols [Title]
l = Doc -> Doc
nbformat (String -> Doc
text String
header) Doc -> Doc -> Doc
$$ Doc -> Doc
nbformat (String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ String
genMDtable String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"|")
  where header :: String
header = Doc -> String
forall a. Show a => a -> String
show(String -> Doc
text String
"|" Doc -> Doc -> Doc
<> [Doc] -> Doc
hcat(Doc -> [Doc] -> [Doc]
punctuate (String -> Doc
text String
"|") ((Title -> Doc) -> [Title] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Title -> Doc
pSpec [Title]
l)) Doc -> Doc -> Doc
<> String -> Doc
text String
"|")        
        c :: Int
c = Char -> String -> Int
count Char
'|' String
header
        genMDtable :: String
genMDtable = Tags -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Int -> String -> Tags
forall a. Int -> a -> [a]
replicate (Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) String
"|:--- ")

makeColumns :: [Title] -> Doc
makeColumns [Title]
ls = Doc -> Doc
nbformat (String -> Doc
text String
"|" Doc -> Doc -> Doc
<> [Doc] -> Doc
hcat(Doc -> [Doc] -> [Doc]
punctuate (String -> Doc
text String
"|") ((Title -> Doc) -> [Title] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map Title -> Doc
pSpec [Title]
ls)) Doc -> Doc -> Doc
<> String -> Doc
text String
"|")

count :: Char -> String -> Int
count :: Char -> String -> Int
count Char
_ [] = Int
0
count Char
c (Char
x:String
xs) 
  | Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
x = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> String -> Int
count Char
c String
xs
  | Bool
otherwise = Char -> String -> Int
count Char
c String
xs

-- | Renders definition tables (Data, General, Theory, etc.)
makeDefn :: L.DType -> [(String,[LayoutObj])] -> Doc -> Doc
makeDefn :: DType -> [(String, [LayoutObj])] -> Doc -> Doc
makeDefn DType
_ [] Doc
_  = String -> Doc
forall a. HasCallStack => String -> a
error String
"L.Empty definition"
makeDefn DType
dt [(String, [LayoutObj])]
ps Doc
l = Doc -> Doc
refID Doc
l Doc -> Doc -> Doc
$$ Tags -> Doc -> Doc
table [DType -> String
dtag DType
dt]
  (Doc -> Doc
tr (Doc -> Doc
nbformat (Doc -> Doc
th (String -> Doc
text String
"Refname")) Doc -> Doc -> Doc
$$ Doc -> Doc
td (Doc -> Doc
nbformat(Doc -> Doc
bold Doc
l))) Doc -> Doc -> Doc
$$ [(String, [LayoutObj])] -> Doc
makeDRows [(String, [LayoutObj])]
ps)
  where dtag :: DType -> String
dtag DType
L.General  = String
"gdefn"
        dtag DType
L.Instance = String
"idefn"
        dtag DType
L.Theory   = String
"tdefn"
        dtag DType
L.Data     = String
"ddefn"

-- | Helper for making the definition table rows
makeDRows :: [(String,[LayoutObj])] -> Doc
makeDRows :: [(String, [LayoutObj])] -> Doc
makeDRows []         = String -> Doc
forall a. HasCallStack => String -> a
error String
"No fields to create defn table"
makeDRows [(String
f,[LayoutObj]
d)]    = Doc -> Doc
tr (Doc -> Doc
nbformat (Doc -> Doc
th (String -> Doc
text String
f)) Doc -> Doc -> Doc
$$ Doc -> Doc
td ([Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (LayoutObj -> Doc) -> [LayoutObj] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> Doc
printLO [LayoutObj]
d))
makeDRows ((String
f,[LayoutObj]
d):[(String, [LayoutObj])]
ps) = Doc -> Doc
tr (Doc -> Doc
nbformat (Doc -> Doc
th (String -> Doc
text String
f)) Doc -> Doc -> Doc
$$ Doc -> Doc
td ([Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ (LayoutObj -> Doc) -> [LayoutObj] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map LayoutObj -> Doc
printLO [LayoutObj]
d)) Doc -> Doc -> Doc
$$ [(String, [LayoutObj])] -> Doc
makeDRows [(String, [LayoutObj])]
ps


-- | Renders lists
makeList :: ListType -> Bool -> Doc -- FIXME: ref id's should be folded into the li
makeList :: ListType -> Bool -> Doc
makeList (Simple [(Title, ItemType, Maybe Title)]
items) Bool
_      = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ 
  ((Title, ItemType, Maybe Title) -> Doc)
-> [(Title, ItemType, Maybe Title)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\(Title
b,ItemType
e,Maybe Title
l) -> Maybe Title -> Doc -> Doc
mlref Maybe Title
l (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
nbformat (Title -> Doc
pSpec Title
b Doc -> Doc -> Doc
<> String -> Doc
text String
": " Doc -> Doc -> Doc
<> ItemType -> Doc
sItem ItemType
e) Doc -> Doc -> Doc
$$ Doc -> Doc
nbformat Doc
empty) [(Title, ItemType, Maybe Title)]
items
makeList (Desc [(Title, ItemType, Maybe Title)]
items) Bool
bl       = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ 
  ((Title, ItemType, Maybe Title) -> Doc)
-> [(Title, ItemType, Maybe Title)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\(Title
b,ItemType
e,Maybe Title
l) -> Doc -> Doc
pa (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Maybe Title -> Doc -> Doc
mlref Maybe Title
l (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
ba (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Title -> Doc
pSpec Title
b Doc -> Doc -> Doc
<> String -> Doc
text String
": " Doc -> Doc -> Doc
<> ItemType -> Bool -> Doc
pItem ItemType
e Bool
bl) [(Title, ItemType, Maybe Title)]
items
makeList (Ordered [(ItemType, Maybe Title)]
items) Bool
bl    = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ((ItemType, Maybe Title) -> Doc)
-> [(ItemType, Maybe Title)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\(ItemType
i,Maybe Title
l) -> Maybe Title -> Doc -> Doc
mlref Maybe Title
l (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ ItemType -> Bool -> Doc
pItem ItemType
i Bool
bl) [(ItemType, Maybe Title)]
items
makeList (Unordered [(ItemType, Maybe Title)]
items) Bool
bl  = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ((ItemType, Maybe Title) -> Doc)
-> [(ItemType, Maybe Title)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\(ItemType
i,Maybe Title
l) -> Maybe Title -> Doc -> Doc
mlref Maybe Title
l (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ ItemType -> Bool -> Doc
pItem ItemType
i Bool
bl) [(ItemType, Maybe Title)]
items
--makeList (Definitions items) _ = ul ["hide-list-style-no-indent"] $ vcat $ 
  --map (\(b,e,l) -> li $ mlref l $ quote(pSpec b <> text " is the" <+> sItem e)) items
makeList (Definitions [(Title, ItemType, Maybe Title)]
items) Bool
_ = [Doc] -> Doc
vcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ((Title, ItemType, Maybe Title) -> Doc)
-> [(Title, ItemType, Maybe Title)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (\(Title
b,ItemType
e,Maybe Title
l) -> Doc -> Doc
nbformat (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
li (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Maybe Title -> Doc -> Doc
mlref Maybe Title
l (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Title -> Doc
pSpec Title
b Doc -> Doc -> Doc
<> String -> Doc
text String
" is the" Doc -> Doc -> Doc
<+> ItemType -> Doc
sItem ItemType
e) [(Title, ItemType, Maybe Title)]
items

-- | Helper for setting up references
mlref :: Maybe Label -> Doc -> Doc
mlref :: Maybe Title -> Doc -> Doc
mlref = (Doc -> Doc) -> (Title -> Doc -> Doc) -> Maybe Title -> Doc -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc -> Doc
forall a. a -> a
id ((Title -> Doc -> Doc) -> Maybe Title -> Doc -> Doc)
-> (Title -> Doc -> Doc) -> Maybe Title -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc -> Doc
refwrap (Doc -> Doc -> Doc) -> (Title -> Doc) -> Title -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Title -> Doc
pSpec

-- | Helper for rendering list items
pItem :: ItemType ->  Bool -> Doc
pItem :: ItemType -> Bool -> Doc
pItem (Flat Title
s)     Bool
b = Doc -> Doc
nbformat (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ (if Bool
b then String -> Doc
text String
" - " else String -> Doc
text String
"- ") Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
s
pItem (Nested Title
s ListType
l) Bool
_ = [Doc] -> Doc
vcat [Doc -> Doc
nbformat (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ String -> Doc
text String
"- " Doc -> Doc -> Doc
<> Title -> Doc
pSpec Title
s, ListType -> Bool -> Doc
makeList ListType
l Bool
True]
  --where listIndent = strBreak "\"" (show $ makeList l)
--indent <> text "\"- " <> pSpec s <> text "\\n\","

sItem :: ItemType -> Doc
sItem :: ItemType -> Doc
sItem (Flat Title
s)     = Title -> Doc
pSpec Title
s
sItem (Nested Title
s ListType
l) = [Doc] -> Doc
vcat [Title -> Doc
pSpec Title
s, ListType -> Bool -> Doc
makeList ListType
l Bool
False]

-- | Renders figures in HTML
makeFigure :: Doc -> Maybe Doc -> Doc -> L.MaxWidthPercent -> Doc
makeFigure :: Doc -> Maybe Doc -> Doc -> MaxWidthPercent -> Doc
makeFigure Doc
r Maybe Doc
c Doc
f MaxWidthPercent
wp = Doc -> Doc
refID Doc
r Doc -> Doc -> Doc
$$ Doc -> Maybe Doc -> MaxWidthPercent -> Doc
image Doc
f Maybe Doc
c MaxWidthPercent
wp

-- | Renders assumptions, requirements, likely changes
makeRefList :: Doc -> Doc -> Doc -> Doc
makeRefList :: Doc -> Doc -> Doc -> Doc
makeRefList Doc
a Doc
l Doc
i = Doc -> Doc
refID Doc
l Doc -> Doc -> Doc
$$ Doc -> Doc
nbformat (Doc
i Doc -> Doc -> Doc
<> String -> Doc
text String
": " Doc -> Doc -> Doc
<> Doc
a)

makeBib :: BibRef -> Doc
makeBib :: BibRef -> Doc
makeBib = [Doc] -> Doc
vcat ([Doc] -> Doc) -> (BibRef -> [Doc]) -> BibRef -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  (Doc -> (Doc, Doc) -> Doc) -> [Doc] -> [(Doc, Doc)] -> [Doc]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (((Doc, (Doc, Doc)) -> Doc) -> Doc -> (Doc, Doc) -> Doc
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (\(Doc
x,(Doc
y,Doc
z)) -> Doc -> Doc -> Doc -> Doc
makeRefList Doc
z Doc
y Doc
x))
  [String -> Doc
text (String -> Doc) -> String -> Doc
forall a b. (a -> b) -> a -> b
$ String -> String
sqbrac (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
x | Int
x <- [Int
1..] :: [Int]] ([(Doc, Doc)] -> [Doc])
-> (BibRef -> [(Doc, Doc)]) -> BibRef -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Citation -> (Doc, Doc)) -> BibRef -> [(Doc, Doc)]
forall a b. (a -> b) -> [a] -> [b]
map (BibFormatter -> Citation -> (Doc, Doc)
renderCite BibFormatter
htmlBibFormatter)