{-# LANGUAGE PatternGuards #-}{-|
This module converts a list of 'Tag' back into a string.
-}moduleText.HTML.TagSoup.Render(renderTags,renderTagsOptions,escapeHTML,RenderOptions(..),renderOptions)whereimportData.CharimportqualifiedData.IntMapasIntMapimportText.HTML.TagSoup.EntityimportText.HTML.TagSoup.TypeimportText.StringLike-- | These options control how 'renderTags' works.---- The strange quirk of only minimizing @\<br\>@ tags is due to Internet Explorer treating-- @\<br\>\<\/br\>@ as @\<br\>\<br\>@.dataRenderOptionsstr=RenderOptions{optEscape::str->str-- ^ Escape a piece of text (default = escape the four characters @&\"\<\>@),optMinimize::str->Bool-- ^ Minimise \<b\>\<\/b\> -> \<b/\> (default = minimise only @\<br\>@ tags)}-- | Replace the four characters @&\"\<\>@ with their HTML entities.escapeHTML::StringLikestr=>str->strescapeHTML=fromString.concatMapesc1.toStringwhereesc=IntMap.fromList[(b,"&"++a++";")|(a,b)<-htmlEntities]esc1x=IntMap.findWithDefault[x](ordx)esc-- | The default render options value, described in 'RenderOptions'.renderOptions::StringLikestr=>RenderOptionsstrrenderOptions=RenderOptionsescapeHTML(\x->toStringx=="br")-- | Show a list of tags, as they might have been parsed, using the default settings given in-- 'RenderOptions'.---- > renderTags [TagOpen "hello" [],TagText "my&",TagClose "world"] == "<hello>my&amp;</world>"renderTags::StringLikestr=>[Tagstr]->strrenderTags=renderTagsOptionsrenderOptions-- | Show a list of tags using settings supplied by the 'RenderOptions' parameter,-- eg. to avoid escaping any characters one could do:---- > renderTagsOptions renderOptions{optEscape = id} [TagText "my&"] == "my&"renderTagsOptions::StringLikestr=>RenderOptionsstr->[Tagstr]->strrenderTagsOptionsopts=strConcat.tagswheres=fromStringssx=[sx]tags(TagOpennameatts:TagClosename2:xs)|name==name2&&optMinimizeoptsname=opennameatts(s" /")++tagsxstags(TagOpennameatts:xs)|Just('?',_)<-unconsname=opennameatts(s" ?")++tagsxstags(x:xs)=tagx++tagsxstags[]=[]tag(TagOpennameatts)=opennameatts(s"")tag(TagClosename)=[s"</",name,s">"]tag(TagTexttext)=[txttext]tag(TagCommenttext)=ss"<!--"++comtext++ss"-->"tag_=ss""txt=optEscapeoptsopennameattsshut=[s"<",name]++concatMapattatts++[shut,s">"]att(x,y)|xnull&&ynull=[s" \"\""]|ynull=[s" ",x]|xnull=[s" \"",txty,s"\""]|otherwise=[s" ",x,s"=\"",txty,s"\""]where(xnull,ynull)=(strNullx,strNully)comxs|Just('-',xs)<-unconsxs,Just('-',xs)<-unconsxs,Just('>',xs)<-unconsxs=s"-- >":comxscomxs=caseunconsxsofNothing->[]Just(x,xs)->fromCharx:comxs