2

I have quite big markdown text hardcoded inside my handler function currently. To render it I use whamlet quasiquoter and Text.Markdown.markdown from markdown package:

let md :: L.Text
    md = "#Some markdown stuff"
authLayout $
    [whamlet|
        <div .StaticContent>
            #{markdown def md}
        |]

There are some hardcoded links in the text but I'd rather use variable interpolation.
I want to put this big text into external file, read its contents (this is a markdown with placeholders), apply interpolation (final markdown to be converted into HTML) and finally output the result. How can I do this?

Geradlus_RU
  • 1,466
  • 2
  • 20
  • 37

2 Answers2

2

Well, finally I've found really simple solution.

Any quasi quoter could be easily switched to file input rather than inline text with quoteFile function from Language.Haskell.TH.Quote module1. Let's describe in Foundation module following function:

import           Language.Haskell.TH.Quote (QuasiQuoter, quoteFile)
import           Text.Shakespeare.Text     (st)

stFile :: QuasiQuoter
stFile = quoteFile st

Now we can use this new quasi quoter in handler modules:

-- file: Handler.SomeHandler.hs
import           Foundation       (stFile)
import           Text.Markdown    (markdown)
import qualified Data.Text.Lazy   as L

getSomeHandlerR :: Handler Html
getSomeHandlerR =
  do let userName = "Guest" :: Text
         -- | `interpolated` is a strict `Text`
         interpolated = [stFile|text-input.md|]
         md           = markdown def (L.fromStrict interpolated)
     defaultLayout [whamlet|<div .static-content>#{md}|]

-- file: text-input.md
# Welcome!
Hello, **#{userName}**!

This will produce following content:

<div class="static-content">
    <h1>Welcome!</h1>
    <p>Hello, <b>Guest</b>!</p>

That's all!

Geradlus_RU
  • 1,466
  • 2
  • 20
  • 37
0

There is function called whamletFile (http://hackage.haskell.org/package/yesod-core-1.4.9.1/docs/Yesod-Core-Widget.html#v:whamletFile) that does exactly what you want, I think. See some more info at https://www.safaribooksonline.com/library/view/developing-web-applications/9781449336868/ch05s06.html.

JP Moresmau
  • 7,388
  • 17
  • 31
  • Yes, I use it (and `widgetFile`) a lot and I believe it gives a `widget` as a result. Though it will help with interpolation, I guess it expects a hamlet as input and will not give a possibility to apply `markdown` function to result. Also it seems that widget is a rendered HTML but `markdown` takes a lazy Text. – Geradlus_RU May 18 '15 at 22:04