cryptol/utils/CryHtml.hs

84 lines
2.2 KiB
Haskell
Raw Normal View History

2014-04-18 02:34:25 +04:00
#!/usr/bin/env runhaskell
2018-03-06 04:07:19 +03:00
{-# LANGUAGE OverloadedStrings #-}
2014-04-18 02:34:25 +04:00
-- |
-- Module : Main
-- Copyright : (c) 2013-2016 Galois, Inc.
2014-04-18 02:34:25 +04:00
-- License : BSD3
-- Maintainer : cryptol@galois.com
-- Stability : provisional
-- Portability : portable
import Cryptol.Parser.Lexer
import Cryptol.Utils.PP
2017-10-25 21:12:37 +03:00
import qualified Data.Text.IO as Text
import Text.Blaze.Html (Html, AttributeValue, toValue, toHtml, (!))
2018-03-06 04:07:19 +03:00
import qualified Text.Blaze.Html as H
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
import Text.Blaze.Html.Renderer.Pretty (renderHtml)
2014-04-18 02:34:25 +04:00
main :: IO ()
2017-10-25 21:12:37 +03:00
main =
do txt <- Text.getContents
2018-03-06 04:07:19 +03:00
putStrLn $ renderHtml
$ page
$ toHtml
$ map toBlaze
2017-10-25 21:12:37 +03:00
$ fst
$ primLexer defaultConfig txt
2014-04-18 02:34:25 +04:00
2018-03-06 04:07:19 +03:00
page :: Html -> Html
page inner = do
H.docTypeHtml ! A.xmlns "http://www.w3.org/1999/xhtml" $ do
H.head $ do
H.meta ! A.httpEquiv "Content-Type" ! A.content "text/html; charset=UTF-8"
H.title "Cryptol Source"
H.style $ H.preEscapedString sty
H.body inner
toBlaze :: Located Token -> Html
toBlaze tok = H.span ! (A.class_ $ cl $ tokenType $ thing tok)
! (A.title $ toValue $ show $ pp $ srcRange tok)
$ H.toHtml
$ tokenText
$ thing tok
cl :: TokenT -> AttributeValue
cl tok =
case tok of
Num {} -> "number"
Ident {} -> "identifier"
KW {} -> "keyword"
Op {} -> "operator"
Sym {} -> "symbol"
Virt {} -> "virtual"
White Space -> "white"
White _ -> "comment"
Err {} -> "error"
EOF -> "eof"
StrLit {} -> "text"
ChrLit {} -> "text"
2014-04-18 02:34:25 +04:00
sty :: String
sty = unlines
2018-03-06 04:07:19 +03:00
[ "body { font-family: monospace }"
2014-04-18 02:34:25 +04:00
, ".number { color: #cc00cc }"
, ".identifier { }"
, ".keyword { color: blue; }"
, ".operator { color: #cc00cc }"
, ".symbol { color: blue }"
, ".white { }"
, ".virtual { background-color: red }"
, ".comment { color: green }"
, ".error { color: red }"
, ".text { color: #cc00cc }"
]