\begin{code} module Html where import Edu.Berkeley.Sbp.Haskell.SBP import FromTree import Doc import List(isSuffixOf,isPrefixOf) -- FIXME: use pretty-printing when asked to for better display class ToHtml a where toHtml :: a -> String instance ToHtml a => ToHtml [a] where toHtml x = concatMap toHtml x style = "\n\n" instance ToHtml Doc where toHtml (Doc h secs) = "\n"++ "\n\n"++ "\n"++ "\n"++ style++ --FIXME: title tag "\n"++ "\n"++ -- tell jsmath we will escape stuff manually -- FIXME: only put this in if math appears on the page "\n"++ "\n"++ "\n"++ "\n"++ "
\n"++ (toHtml secs) ++ "

\n"++ ""++ "\n"++ "
\n"++ "" instance ToHtml Section where toHtml (Section level header paragraphs) = "\n\n"++ (toHtml header)++ "\n\n"++ (toHtml paragraphs) stag t body = "\n<"++t++">\n"++body++"\n\n" tag t body = "<"++t++">"++body++"" instance ToHtml Paragraph where toHtml (Blockquote t) = "\n\n" ++"\n" ++"
\n" ++(toHtml t) ++"
\n" toHtml HR = stag "hr" [] toHtml (OL t) = stag "ol" $ concatMap (\x -> stag "li" $ concatMap toHtml x) t toHtml (UL t) = stag "ul" $ concatMap (\x -> stag "li" $ concatMap toHtml x) t toHtml (P t) = stag "p" $ toHtml t link ref body = ""++icon++body++"" where icon = if ".pdf" `isSuffixOf` ref then " " else if "mailto:" `isPrefixOf` ref then " " else "" img = "style='vertical-align: text-bottom;' border=0 " -- margin-bottom: -2px; padding-bottom: 2px; border-bottom: 1px blue solid; instance ToHtml Text where toHtml WS = " " toHtml (Chars s) = toHtml s -- directional quotes: see http://www.dwheeler.com/essays/quotes-in-html.html toHtml (Quotes x) = "“"++(toHtml x)++"”" toHtml (Verbatim x) = pre x toHtml (Link t ref) = link (show ref) (toHtml t) toHtml (Command "url" y) = ""++(link (toHtml y) (toHtml y))++"" toHtml (Command "WiX" y) = "WIX" toHtml (Command "TeX" y) = "TEX" -- u'1/2' : u'\u00BD', -- u'1/4' : u'\u00BC', -- u'3/4' : u'\u00BE', -- u'1/3' : u'\u2153', -- u'2/3' : u'\u2154', -- u'1/5' : u'\u2155', -- u'2/5' : u'\u2156', -- u'3/5' : u'\u2157', -- u'4/5' : u'\u2158', -- u'1/6' : u'\u2159', -- u'5/6' : u'\u215A', -- u'1/8' : u'\u215B', -- u'3/8' : u'\u215C', -- u'5/8' : u'\u215D', -- u'7/8' : u'\u215E', toHtml (Styled Underline x) = tag "u" $ toHtml x toHtml (Styled TT x) = tag "tt" $ toHtml x toHtml (Styled Italic x) = tag "i" $ toHtml x toHtml (Styled Strikethrough x) = tag "strike" $ toHtml x toHtml (Styled Superscript x) = tag "sup" $ toHtml x toHtml (Styled Subscript x) = tag "sub" $ toHtml x toHtml (Styled Bold x) = tag "b" $ toHtml x toHtml (Styled Highlight x) = ""++(toHtml x)++"" toHtml (Keyword x) = tag "tt" $ toHtml x toHtml (SubPar x) = stag "p" $ concatMap toHtml x toHtml (Command "red" y) = ""++(toHtml y)++"" toHtml (Command "orange" y) = ""++(toHtml y)++"" toHtml (Command "green" y) = ""++(toHtml y)++"" toHtml (Command "sc" y) = ""++(toHtml y)++"" toHtml (Command "image" y) = "" toHtml (Command "image3" y) = "" toHtml (Command "image4" y) = "
" toHtml (Command "warn" y) = "\n
\n\n" ++"\n" ++"
\n" ++(toHtml y) ++"
\n" toHtml (Command "announce" y) = "\n
\n\n" ++"\n" ++"
\n" ++(toHtml y) ++"
\n" toHtml (Command "br" _) = "\n
\n" toHtml (Command "cent" _) = "½" toHtml (Command "euro" _) = "€" toHtml (Command "ordinal" x) = (toHtml x)++""++"th"++"" -- FIXME: use "unicode vulgar fractions" here toHtml (Command "fraction" [n,d]) = ""++(toHtml n)++""++"/"++""++(toHtml d)++"" toHtml (Command "rfc" x) = "RFC"++(toHtml x)++"" -- FIXME: add div as well (for display-mode math) toHtml (Math m) = "" ++ (toHtml m) ++ "" toHtml (Footnote x) = error $ "footnotes not supported" toHtml (GlyphText Euro) = "€" toHtml (GlyphText CircleR) = "¢" toHtml (GlyphText CircleC) = "®" toHtml (GlyphText TradeMark) = "™" toHtml (GlyphText ServiceMark) = "™" toHtml (GlyphText Emdash) = "—" toHtml (GlyphText Ellipsis) = "…" toHtml (GlyphText Cent) = "½" toHtml (GlyphText Daggar) = "†" toHtml (GlyphText DoubleDaggar) = "‡" toHtml (GlyphText Clover) = "⌘" toHtml (GlyphText Flat) = "⋖" toHtml (GlyphText Natural) = "⋗" toHtml (GlyphText Sharp) = "⋘" toHtml (GlyphText CheckMark) = "✓" toHtml (GlyphText XMark) = "✗" toHtml (GlyphText LeftArrow) = "&#;" -- FIXME toHtml (GlyphText DoubleLeftArrow) = "&#;" -- FIXME toHtml (GlyphText DoubleRightArrow) = "&#;" -- FIXME toHtml (GlyphText DoubleLeftRightArrow) = "&#;" -- FIXME toHtml (GlyphText LeftRightArrow) = "&#;" -- FIXME toHtml (GlyphText Degree) = "&#;" -- FIXME toHtml (Command ('k':'e':'y':'s':'t':'r':'o':'k':'e':':':k) _) = "&#x"++(case k of "command" -> "2318" "shift" -> "21E7" "option" -> "2325" "control" -> "2303" "capslock" -> "21EA" "apple" -> "F8FF" )++";" toHtml (Command x y) = error $ "unsupported command "++(show x) instance ToHtml String where toHtml s = concatMap htmlEscapeChar s where htmlEscapeChar '<' = "<" htmlEscapeChar '>' = ">" htmlEscapeChar '&' = "&" htmlEscapeChar '\'' = "'" htmlEscapeChar '\"' = """ htmlEscapeChar c = [c] pre x = "\n
"++ (pre' x) ++ "\n
\n" where pre' (' ':b) = " "++(pre' b) pre' ('\n':b) = "
\n"++(pre' b) pre' (a:b) = a:(pre' b) pre' [] = [] \end{code}