module Language.Drasil.HTML.Helpers where
import Prelude hiding ((<>))
import Text.PrettyPrint (Doc, text, empty, ($$), (<>), (<+>), vcat, hcat, nest,
cat, hcat)
import Data.List (intersperse)
import Language.Drasil hiding (Expr)
import Language.Drasil.Printing.AST (Expr)
html, headTag, body, title, paragraph, code, tr, th, td, figure,
figcaption, li, pa, ba :: Doc -> Doc
html :: Doc -> Doc
html = String -> [String] -> Doc -> Doc
wrap String
"html" []
headTag :: Doc -> Doc
headTag = String -> [String] -> Doc -> Doc
wrap String
"head" []
body :: Doc -> Doc
body = String -> [String] -> Doc -> Doc
wrap String
"body" []
title :: Doc -> Doc
title = String -> [String] -> Doc -> Doc
wrap String
"title" []
paragraph :: Doc -> Doc
paragraph = String -> [String] -> Doc -> Doc
wrap String
"p" [String
"paragraph"]
code :: Doc -> Doc
code = String -> [String] -> Doc -> Doc
wrap String
"code" [String
"code"]
tr :: Doc -> Doc
tr = String -> [String] -> Doc -> Doc
wrap String
"tr" []
th :: Doc -> Doc
th = String -> [String] -> Doc -> Doc
wrap String
"th" []
td :: Doc -> Doc
td = String -> [String] -> Doc -> Doc
wrap String
"td" []
figure :: Doc -> Doc
figure = String -> [String] -> Doc -> Doc
wrap String
"figure" []
figcaption :: Doc -> Doc
figcaption = String -> [String] -> Doc -> Doc
wrap String
"figcaption" []
li :: Doc -> Doc
li = String -> [String] -> Doc -> Doc
wrap String
"li" []
pa :: Doc -> Doc
pa = String -> [String] -> Doc -> Doc
wrap String
"p" []
ba :: Doc -> Doc
ba = String -> [String] -> Doc -> Doc
wrap String
"b" []
ol, ul, table :: [String] -> Doc -> Doc
ol :: [String] -> Doc -> Doc
ol = String -> [String] -> Doc -> Doc
wrap String
"ol"
ul :: [String] -> Doc -> Doc
ul = String -> [String] -> Doc -> Doc
wrap String
"ul"
table :: [String] -> Doc -> Doc
table = String -> [String] -> Doc -> Doc
wrap String
"table"
img :: [(String, Doc)] -> Doc
img :: [(String, Doc)] -> Doc
img = String -> [(String, Doc)] -> Doc
wrapInside String
"img"
h :: Int -> Doc -> Doc
h :: Int -> Doc -> Doc
h Int
n | Int
n forall a. Ord a => a -> a -> Bool
< Int
1 = forall a. HasCallStack => String -> a
error String
"Illegal header (too small)"
| Int
n forall a. Ord a => a -> a -> Bool
> Int
7 = forall a. HasCallStack => String -> a
error String
"Illegal header (too large)"
| Bool
otherwise = String -> [String] -> Doc -> Doc
wrap (String
"h" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n) []
data Variation = Class | Id
wrap :: String -> [String] -> Doc -> Doc
wrap :: String -> [String] -> Doc -> Doc
wrap String
a = Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen Variation
Class String
a Doc
empty
wrap' :: String -> [String] -> Doc -> Doc
wrap' :: String -> [String] -> Doc -> Doc
wrap' String
a = ([Doc] -> Doc)
-> Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen' [Doc] -> Doc
hcat Variation
Class String
a Doc
empty
wrapGen' :: ([Doc] -> Doc) -> Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen' :: ([Doc] -> Doc)
-> Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen' [Doc] -> Doc
sepf Variation
_ String
s Doc
_ [] = \Doc
x ->
let tb :: String -> Doc
tb String
c = String -> Doc
text forall a b. (a -> b) -> a -> b
$ String
"<" forall a. [a] -> [a] -> [a]
++ String
c forall a. [a] -> [a] -> [a]
++ String
">"
in [Doc] -> Doc
sepf [String -> Doc
tb String
s, Doc -> Doc
indent Doc
x, String -> Doc
tb forall a b. (a -> b) -> a -> b
$ Char
'/'forall a. a -> [a] -> [a]
:String
s]
wrapGen' [Doc] -> Doc
sepf Variation
Class String
s Doc
_ [String]
ts = \Doc
x ->
let tb :: String -> Doc
tb String
c = String -> Doc
text forall a b. (a -> b) -> a -> b
$ String
"<" forall a. [a] -> [a] -> [a]
++ String
c forall a. [a] -> [a] -> [a]
++ String
" class=\"" forall a. [a] -> [a] -> [a]
++ forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 forall a. [a] -> [a] -> [a]
(++) (forall a. a -> [a] -> [a]
intersperse String
" " [String]
ts) forall a. [a] -> [a] -> [a]
++ String
"\">"
in let te :: String -> Doc
te String
c = String -> Doc
text forall a b. (a -> b) -> a -> b
$ String
"</" forall a. [a] -> [a] -> [a]
++ String
c forall a. [a] -> [a] -> [a]
++ String
">"
in [Doc] -> Doc
sepf [String -> Doc
tb String
s, Doc -> Doc
indent Doc
x, String -> Doc
te String
s]
wrapGen' [Doc] -> Doc
sepf Variation
Id String
s Doc
ti [String]
_ = \Doc
x ->
let tb :: String -> Doc
tb String
c = String -> Doc
text (String
"<" forall a. [a] -> [a] -> [a]
++ String
c forall a. [a] -> [a] -> [a]
++ String
" id=\"") Doc -> Doc -> Doc
<> Doc
ti Doc -> Doc -> Doc
<> String -> Doc
text String
"\">"
te :: String -> Doc
te String
c = String -> Doc
text forall a b. (a -> b) -> a -> b
$ String
"</" forall a. [a] -> [a] -> [a]
++ String
c forall a. [a] -> [a] -> [a]
++ String
">"
in [Doc] -> Doc
sepf [String -> Doc
tb String
s, Doc -> Doc
indent Doc
x, String -> Doc
te String
s]
wrapGen :: Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen :: Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen = ([Doc] -> Doc)
-> Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen' [Doc] -> Doc
cat
wrapInside :: String -> [(String, Doc)] -> Doc
wrapInside :: String -> [(String, Doc)] -> Doc
wrapInside String
t [(String, Doc)]
p = String -> Doc
text (String
"<" forall a. [a] -> [a] -> [a]
++ String
t forall a. [a] -> [a] -> [a]
++ String
" ") Doc -> Doc -> Doc
<> forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 Doc -> Doc -> Doc
(<>) (forall a b. (a -> b) -> [a] -> [b]
map (String, Doc) -> Doc
foldStr [(String, Doc)]
p) Doc -> Doc -> Doc
<> String -> Doc
text String
">"
where foldStr :: (String, Doc) -> Doc
foldStr (String
attr, Doc
val) = String -> Doc
text (String
attr forall a. [a] -> [a] -> [a]
++ String
"=\"") Doc -> Doc -> Doc
<> Doc
val Doc -> Doc -> Doc
<> String -> Doc
text String
"\" "
caption :: Doc -> Doc
caption :: Doc -> Doc
caption = String -> [String] -> Doc -> Doc
wrap String
"p" [String
"caption"]
refwrap :: Doc -> Doc -> Doc
refwrap :: Doc -> Doc -> Doc
refwrap = forall a b c. (a -> b -> c) -> b -> a -> c
flip (Variation -> String -> Doc -> [String] -> Doc -> Doc
wrapGen Variation
Id String
"div") [String
""]
reflink :: String -> Doc -> Doc
reflink :: String -> Doc -> Doc
reflink String
rf Doc
txt = String -> Doc
text (String
"<a href=#" forall a. [a] -> [a] -> [a]
++ String
rf forall a. [a] -> [a] -> [a]
++ String
">") Doc -> Doc -> Doc
<> Doc
txt Doc -> Doc -> Doc
<> String -> Doc
text String
"</a>"
reflinkInfo :: String -> Doc -> Doc -> Doc
reflinkInfo :: String -> Doc -> Doc -> Doc
reflinkInfo String
rf Doc
txt Doc
info = String -> Doc
text (String
"<a href=#" forall a. [a] -> [a] -> [a]
++ String
rf forall a. [a] -> [a] -> [a]
++ String
">") Doc -> Doc -> Doc
<> Doc
txt Doc -> Doc -> Doc
<> String -> Doc
text String
"</a>" Doc -> Doc -> Doc
<+> Doc
info
reflinkURI :: String -> Doc -> Doc
reflinkURI :: String -> Doc -> Doc
reflinkURI String
rf Doc
txt = String -> Doc
text (String
"<a href=\"" forall a. [a] -> [a] -> [a]
++ String
rf forall a. [a] -> [a] -> [a]
++ String
"\">") Doc -> Doc -> Doc
<> Doc
txt Doc -> Doc -> Doc
<> String -> Doc
text String
"</a>"
image :: Doc -> Doc -> MaxWidthPercent -> Doc
image :: Doc -> Doc -> MaxWidthPercent -> Doc
image Doc
f Doc
c MaxWidthPercent
100 =
Doc -> Doc
figure forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat [
[(String, Doc)] -> Doc
img [(String
"src", Doc
f), (String
"alt", Doc
c)],
Doc -> Doc
figcaption Doc
c]
image Doc
f Doc
c MaxWidthPercent
wp =
Doc -> Doc
figure forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat [
[(String, Doc)] -> Doc
img [(String
"src", Doc
f), (String
"alt", Doc
c), (String
"width", String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show MaxWidthPercent
wp forall a. [a] -> [a] -> [a]
++ String
"%")],
Doc -> Doc
figcaption Doc
c]
em, sup, sub, bold :: Doc -> Doc
em :: Doc -> Doc
em = String -> [String] -> Doc -> Doc
wrap' String
"em" []
sup :: Doc -> Doc
sup = String -> [String] -> Doc -> Doc
wrap' String
"sup" []
sub :: Doc -> Doc
sub = String -> [String] -> Doc -> Doc
wrap' String
"sub" []
bold :: Doc -> Doc
bold = String -> [String] -> Doc -> Doc
wrap' String
"b" []
articleTitle, author :: Doc -> Doc
articleTitle :: Doc -> Doc
articleTitle Doc
t = [String] -> Doc -> Doc
divTag [String
"title"] (Int -> Doc -> Doc
h Int
1 Doc
t)
author :: Doc -> Doc
author Doc
a = [String] -> Doc -> Doc
divTag [String
"author"] (Int -> Doc -> Doc
h Int
2 Doc
a)
divTag :: [String] -> Doc -> Doc
divTag :: [String] -> Doc -> Doc
divTag = String -> [String] -> Doc -> Doc
wrap String
"div"
spanTag :: [String] -> Doc -> Doc
spanTag :: [String] -> Doc -> Doc
spanTag = String -> [String] -> Doc -> Doc
wrap String
"span"
indent :: Doc -> Doc
indent :: Doc -> Doc
indent = Int -> Doc -> Doc
nest Int
2
makeCases :: [(Expr,Expr)] -> (Expr -> Doc) -> Doc
makeCases :: [(Expr, Expr)] -> (Expr -> Doc) -> Doc
makeCases [] Expr -> Doc
_ = Doc
empty
makeCases ((Expr, Expr)
p:[(Expr, Expr)]
ps) Expr -> Doc
pExpr = [String] -> Doc -> Doc
spanTag [] (Expr -> Doc
pExpr (forall a b. (a, b) -> a
fst (Expr, Expr)
p) Doc -> Doc -> Doc
<> String -> Doc
text String
" , " Doc -> Doc -> Doc
<>
[String] -> Doc -> Doc
spanTag [String
"case"] (Expr -> Doc
pExpr (forall a b. (a, b) -> b
snd (Expr, Expr)
p))) Doc -> Doc -> Doc
$$
[(Expr, Expr)] -> (Expr -> Doc) -> Doc
makeCases [(Expr, Expr)]
ps Expr -> Doc
pExpr