Problem with nesting in Text.XHtml library using Haskell

I have the following code to create an empty html page with a series of divs with id and classes in Haskell using the Text.XHtml.Strict library:

module Main where

import Text.XHtml.Strict
import Text.Printf


page :: Html
page = pHeader +++ pTop +++ pBody +++ pFooter

pHeader :: Html
pHeader = header << thetitle << "Page title" 

pTop :: Html
pTop = (dC "header") << (dI "title") 

pFooter :: Html
pFooter = (dC "footer") << (dI "foottext")

pBody :: Html
pBody = body << (dC "main") << (dI "window") << (dI "content")

dC :: String -> Html
dC x = (thediv noHtml)! [theclass x]

dI :: String -> Html
dI x = (thediv noHtml) ! [identifier x]

main :: IO ()
main = do
    printf $ prettyHtml $ page

Functions dCand dIshould be empty with the class or identifier, respectively. In the interpreter, these functions work perfectly when concatenated, for example:

 printf $ prettyHtmlFragment $ dC "1" +++ dC "2"
<div class="1">
</div>
<div class="2">
</div>

But not when I try to nest them using <<instead +++, I get an error:

<interactive>:1:28:
    Couldn't match expected type `Html -> b'
           against inferred type `Html'

This is what I consider the cause of the problem in the main part of the code, but I do not know how to fix it. Any ideas?

+3
source share
2 answers

nohtml :

dC :: String -> Html -> Html
dC x = thediv ! [theclass x]

dI :: String -> Html -> Html
dI x = thediv ! [identifier x]

! Html, , Html. thediv - , Html <div>. !, , Html <div class="…"> ( id="…").

> :type thediv
thediv :: Html -> Html

> let dC c = thediv ! [theclass c]
> let dI i = thediv ! [identifier i]

> :type dC
dC :: String -> Html -> Html
> :type dI
dI :: String -> Html -> Html

> putStr $ prettyHtmlFragment $ body << dC "main" << dI "window" << dI "content" << "Hi"
<body>
   <div class="main">
      <div id="window">
         <div id="content">
            Hi
         </div>
      </div>
   </div>
</body>

, .

+2

dC cI , . , . :

dC classx child = (thediv child)! [theclass classx] 
dI x y = (thediv y) ! [identifier x] 

:

pBody x = body << (dC "main") << (dI "window") << (dI "content" x) 
+1

Source: https://habr.com/ru/post/1731528/


All Articles