{-# LANGUAGE CPP #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE DeriveDataTypeable #-}-- | Most everything exported here is exported also by "Text.Hamlet". The-- exceptions to that rule should not be necessary for normal usage.moduleText.Hamlet.RT(-- * Public APIHamletRT(..),HamletData(..),HamletMap,HamletException(..),parseHamletRT,renderHamletRT,renderHamletRT',SimpleDoc(..))whereimportText.Shakespeare.BaseimportData.Monoid(mconcat)importControl.Monad(liftM,forM)importControl.Exception(Exception)importData.Typeable(Typeable)importControl.FailureimportText.Hamlet.ParseimportData.List(intercalate)#if MIN_VERSION_blaze_html(0,5,0)importText.Blaze.Html(Html)importText.Blaze.Internal(preEscapedString,preEscapedText)#elseimportText.Blaze(preEscapedString,preEscapedText,Html)#endifimportData.Text(Text)typeHamletMapurl=[([String],HamletDataurl)]dataHamletDataurl=HDHtmlHtml|HDUrlurl|HDUrlParamsurl[(Text,Text)]|HDTemplateHamletRT|HDBoolBool|HDMaybe(Maybe(HamletMapurl))|HDList[HamletMapurl]-- FIXME switch to Text?dataSimpleDoc=SDRawString|SDVar[String]|SDUrlBool[String]|SDTemplate[String]|SDForall[String]String[SimpleDoc]|SDMaybe[String]String[SimpleDoc][SimpleDoc]|SDCond[([String],[SimpleDoc])][SimpleDoc]newtypeHamletRT=HamletRT[SimpleDoc]dataHamletException=HamletParseExceptionString|HamletUnsupportedDocExceptionDoc|HamletRenderExceptionStringderiving(Show,Typeable)instanceExceptionHamletExceptionparseHamletRT::FailureHamletExceptionm=>HamletSettings->String->mHamletRTparseHamletRTsets=caseparseDocsetsofErrors'->failure$HamletParseExceptions'Ok(_,x)->liftMHamletRT$mapMconvertxwhereconvertx@(DocForallderef(BindAs__)docs)=error"Runtime Hamlet does not currently support 'as' patterns"convertx@(DocForallderef(BindVar(Identident))docs)=doderef'<-flattenDeref'xderefdocs'<-mapMconvertdocsreturn$SDForallderef'identdocs'convertDocForall{}=error"Runtime Hamlet does not currently support tuple patterns"convertx@(DocMaybederef(BindAs__)jdocsndocs)=error"Runtime Hamlet does not currently support 'as' patterns"convertx@(DocMaybederef(BindVar(Identident))jdocsndocs)=doderef'<-flattenDeref'xderefjdocs'<-mapMconvertjdocsndocs'<-maybe(return[])(mapMconvert)ndocsreturn$SDMaybederef'identjdocs'ndocs'convertDocMaybe{}=error"Runtime Hamlet does not currently support tuple patterns"convert(DocContent(ContentRaws'))=return$SDRaws'convertx@(DocContent(ContentVarderef))=doy<-flattenDeref'xderefreturn$SDVaryconvertx@(DocContent(ContentUrlpderef))=doy<-flattenDeref'xderefreturn$SDUrlpyconvertx@(DocContent(ContentEmbedderef))=doy<-flattenDeref'xderefreturn$SDTemplateyconvert(DocContentContentMsg{})=error"Runtime hamlet does not currently support message interpolation"convert(DocContentContentAttrs{})=error"Runtime hamlet does not currently support attrs interpolation"convertx@(DocCondcondsels)=doconds'<-mapMgocondsels'<-maybe(return[])(mapMconvert)elsreturn$SDCondconds'els'wherego(deref,docs')=doderef'<-flattenDeref'xderefdocs''<-mapMconvertdocs'return(deref',docs'')convertDocWith{}=error"Runtime hamlet does not currently support $with"convertDocCase{}=error"Runtime hamlet does not currently support $case"renderHamletRT::FailureHamletExceptionm=>HamletRT->HamletMapurl->(url->[(Text,Text)]->Text)->mHtmlrenderHamletRT=renderHamletRT'FalserenderHamletRT'::FailureHamletExceptionm=>Bool->HamletRT->HamletMapurl->(url->[(Text,Text)]->Text)->mHtmlrenderHamletRT'tempAsHtml(HamletRTdocs)scope0renderUrl=liftMmconcat$mapM(goscope0)docswherego_(SDRaws)=return$preEscapedStringsgoscope(SDVarn)=dov<-lookup'nnscopecasevofHDHtmlh->returnh_->fa$showNamen++": expected HDHtml"goscope(SDUrlpn)=dov<-lookup'nnscopecase(p,v)of(False,HDUrlu)->return$preEscapedText$renderUrlu[](True,HDUrlParamsuq)->return$preEscapedText$renderUrluq(False,_)->fa$showNamen++": expected HDUrl"(True,_)->fa$showNamen++": expected HDUrlParams"goscope(SDTemplaten)=dov<-lookup'nnscopecase(tempAsHtml,v)of(False,HDTemplateh)->renderHamletRT'tempAsHtmlhscoperenderUrl(False,_)->fa$showNamen++": expected HDTemplate"(True,HDHtmlh)->returnh(True,_)->fa$showNamen++": expected HDHtml"goscope(SDForallnidentdocs')=dov<-lookup'nnscopecasevofHDListos->liftMmconcat$forMos$\o->doletscope'=map(\(x,y)->(ident:x,y))o++scoperenderHamletRT'tempAsHtml(HamletRTdocs')scope'renderUrl_->fa$showNamen++": expected HDList"goscope(SDMaybenidentjdocsndocs)=dov<-lookup'nnscope(scope',docs')<-casevofHDMaybeNothing->return(scope,ndocs)HDMaybe(Justo)->doletscope'=map(\(x,y)->(ident:x,y))o++scopereturn(scope',jdocs)_->fa$showNamen++": expected HDMaybe"renderHamletRT'tempAsHtml(HamletRTdocs')scope'renderUrlgoscope(SDCond[]docs')=renderHamletRT'tempAsHtml(HamletRTdocs')scoperenderUrlgoscope(SDCond((b,docs'):cs)els)=dov<-lookup'bbscopecasevofHDBoolTrue->renderHamletRT'tempAsHtml(HamletRTdocs')scoperenderUrlHDBoolFalse->goscope(SDCondcsels)_->fa$showNameb++": expected HDBool"lookup'::FailureHamletExceptionm=>[String]->[String]->HamletMapurl->m(HamletDataurl)lookup'origkm=caselookupkmofNothing->fa$showNameorig++": not found"Justx->returnxfa::FailureHamletExceptionm=>String->mafa=failure.HamletRenderExceptionshowName::[String]->StringshowName=intercalate".".reverseflattenDeref'::FailureHamletExceptionf=>Doc->Deref->f[String]flattenDeref'origderef=caseflattenDerefderefofNothing->failure$HamletUnsupportedDocExceptionorigJustx->returnx