Skip to content

Commit 110a190

Browse files
committed
Chapter 6 - HTML content
1 parent d0d76aa commit 110a190

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

src/HsBlog/Convert.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ convertStructure :: Markup.Structure -> Html.Structure
1212
convertStructure structure =
1313
case structure of
1414
Markup.Heading n txt ->
15-
Html.h_ n txt
15+
Html.h_ n $ Html.txt_ txt
1616

1717
Markup.Paragraph p ->
18-
Html.p_ p
18+
Html.p_ $ Html.txt_ p
1919

2020
Markup.UnorderedList list ->
21-
Html.ul_ $ map Html.p_ list
21+
Html.ul_ $ map (Html.p_ . Html.txt_) list
2222

2323
Markup.OrderedList list ->
24-
Html.ol_ $ map Html.p_ list
24+
Html.ol_ $ map (Html.p_ . Html.txt_) list
2525

2626
Markup.CodeBlock list ->
2727
Html.code_ (unlines list)

src/HsBlog/Html.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ module HsBlog.Html
1010
, ul_
1111
, ol_
1212
, code_
13+
, Content
14+
, txt_
15+
, img_
16+
, link_
17+
, b_
18+
, i_
1319
, render
1420
)
1521
where

src/HsBlog/Html/Internal.hs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ newtype Html
1212
newtype Structure
1313
= Structure String
1414

15+
newtype Content
16+
= Content String
17+
1518
type Title
1619
= String
1720

@@ -26,11 +29,13 @@ html_ title content =
2629
)
2730
)
2831

29-
p_ :: String -> Structure
30-
p_ = Structure . el "p" . escape
32+
-- * Structure
33+
34+
p_ :: Content -> Structure
35+
p_ = Structure . el "p" . getContentString
3136

32-
h_ :: Natural -> String -> Structure
33-
h_ n = Structure . el ("h" <> show n) . escape
37+
h_ :: Natural -> Content -> Structure
38+
h_ n = Structure . el ("h" <> show n) . getContentString
3439

3540
ul_ :: [Structure] -> Structure
3641
ul_ =
@@ -50,6 +55,38 @@ instance Semigroup Structure where
5055
instance Monoid Structure where
5156
mempty = Structure ""
5257

58+
-- * Content
59+
60+
txt_ :: String -> Content
61+
txt_ = Content . escape
62+
63+
link_ :: FilePath -> Content -> Content
64+
link_ path content =
65+
Content $
66+
elAttr
67+
"a"
68+
("href=\"" <> escape path <> "\"")
69+
(getContentString content)
70+
71+
img_ :: FilePath -> Content
72+
img_ path =
73+
Content $ "<img src=\"" <> escape path <> "\">"
74+
75+
b_ :: Content -> Content
76+
b_ content =
77+
Content $ el "b" (getContentString content)
78+
79+
i_ :: Content -> Content
80+
i_ content =
81+
Content $ el "i" (getContentString content)
82+
83+
instance Semigroup Content where
84+
(<>) c1 c2 =
85+
Content (getContentString c1 <> getContentString c2)
86+
87+
instance Monoid Content where
88+
mempty = Content ""
89+
5390
-- * Render
5491

5592
render :: Html -> String
@@ -63,11 +100,20 @@ el :: String -> String -> String
63100
el tag content =
64101
"<" <> tag <> ">" <> content <> "</" <> tag <> ">"
65102

103+
elAttr :: String -> String -> String -> String
104+
elAttr tag attrs content =
105+
"<" <> tag <> " " <> attrs <> ">" <> content <> "</" <> tag <> ">"
106+
66107
getStructureString :: Structure -> String
67-
getStructureString content =
68-
case content of
108+
getStructureString structure =
109+
case structure of
69110
Structure str -> str
70111

112+
getContentString :: Content -> String
113+
getContentString content =
114+
case content of
115+
Content str -> str
116+
71117
escape :: String -> String
72118
escape =
73119
let

0 commit comments

Comments
 (0)