7 Commits
Author SHA1 Message Date
paul ab667742da Bump version to 0.4 2023-03-24 12:53:38 +01:00
paul 647b5c2ad1 Rename /bytes to /utf8 2023-03-17 02:22:13 +01:00
paul 57173d14dd Limit number of returned codepoints 2023-03-17 02:19:15 +01:00
paul fde68ee833 Bump version to 0.3 2023-03-16 03:21:16 +01:00
paul 610c819483 Add some instructions for the Nix setup 2023-03-16 03:15:55 +01:00
paul 3fd4177cd4 Move common html code to its own function 2023-03-12 20:53:43 +01:00
paul 9881b44fa2 Add HTML version of /codepoints 2023-03-12 20:50:33 +01:00
6 changed files with 85 additions and 43 deletions
+20
View File
@@ -1 +1,21 @@
# utoy
## Building the executable
```
$ $(nix-build utoy.nix)/bin/utoy
```
## Building the Docker image
```
$ docker load < $(nix-build nix/docker-image.nix)
```
## Development Shell
Includes Stack, `haskell-language-server`, `gen-hie` etc.
```
$ nix-shell
```
+45 -23
View File
@@ -61,17 +61,17 @@ app :: Application
app = serve (Proxy :: Proxy API) server
type API =
"bytes" :> Capture "bytes" Text :> Get '[PlainText, HTML] BytesModel
:<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText] CodepointsModel
"utf8" :> Capture "bytes" Text :> Get '[PlainText, HTML] Utf8Model
:<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel
:<|> "text" :> Capture "text" Text :> Get '[HTML] TextModel
server :: Server API
server =
bytesR :<|> codepointsR :<|> textR
utf8R :<|> codepointsR :<|> textR
where
bytesR bytesP = do
utf8R bytesP = do
bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400
pure $ BytesModel $ Decode.decodeUtf8 bytes
pure $ mkUtf8Model bytes
codepointsR codepointsP = do
codepoints' <- Parsers.parseCodepoints codepointsP `orThrow` const err400
@@ -82,11 +82,14 @@ server =
-- /bytes/<bytes>
newtype BytesModel = BytesModel
newtype Utf8Model = Utf8Model
{ codepoints :: [([Word8], Either String Char)]
}
instance MimeRender PlainText BytesModel where
mkUtf8Model :: [Word8] -> Utf8Model
mkUtf8Model = Utf8Model . Decode.decodeUtf8
instance MimeRender PlainText Utf8Model where
mimeRender _ model = renderText $
Table.render " " $ concat
[ [ [ Table.cl $ Text.pack $ unwords $ map showByteHex bytes
@@ -107,13 +110,8 @@ instance MimeRender PlainText BytesModel where
| (bytes, eiC) <- model.codepoints
]
instance MimeRender HTML BytesModel where
mimeRender _ model = renderHtml $ H.docTypeHtml $ do
H.head $ do
H.meta ! A.charset "utf-8"
H.title "utoy"
H.style $ H.toHtml $ Encoding.decodeUtf8 $(embedFile "static/utoy.css")
H.body $ do
instance MimeRender HTML Utf8Model where
mimeRender _ model = renderHtml $ documentWithBody $ do
H.table $ for_ model.codepoints $ \(bytes, eiC) -> do
H.tr $ do
H.td $ H.pre $ H.toHtml $ unlines $ map unwords [map showByteHex bytes, map showByteBin bytes]
@@ -121,8 +119,7 @@ instance MimeRender HTML BytesModel where
Left err ->
H.td ! A.colspan "4" $ H.toHtml $ "Decoding error: " ++ err
Right c -> do
H.td $ do
H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.code $ printfHtml "U+%04X" c
H.td $ H.code $ H.toHtml $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
@@ -134,7 +131,14 @@ newtype CodepointsModel = CodepointsModel
}
mkCodepointsModel :: [(Word, Word)] -> CodepointsModel
mkCodepointsModel = CodepointsModel . map go . concatMap (uncurry enumFromTo)
mkCodepointsModel =
CodepointsModel
-- Limit number of returned codepoints. Otherwise it's
-- too easy to provoke massive response bodies with requests like
-- /codepoints/0-99999999
. take 100000
. map go
. concatMap (uncurry enumFromTo)
where
go codepoint = (codepoint, toChar codepoint)
@@ -162,6 +166,20 @@ instance MimeRender PlainText CodepointsModel where
| (codepoint, eiC) <- model.codepoints
]
instance MimeRender HTML CodepointsModel where
mimeRender _ model = renderHtml $ documentWithBody $ do
H.table $ for_ model.codepoints $ \(codepoint, eiC) ->
H.tr $ do
H.td $ H.code $ H.toHtml $ Text.pack $ printf "0x%X" codepoint
case eiC of
Left err -> do
H.td ! A.colspan "4" $ H.code $ H.toHtml $ "Decoding error: " <> Text.pack err
Right c -> do
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.code $ printfHtml "U+%04X" c
H.td $ H.code $ H.toHtml $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
-- /text/<text>
newtype TextModel = TextModel
@@ -169,12 +187,7 @@ newtype TextModel = TextModel
}
instance MimeRender HTML TextModel where
mimeRender _ model = renderHtml $ H.docTypeHtml $ do
H.head $ do
H.meta ! A.charset "utf-8"
H.title "utoy"
H.style $ H.toHtml $ Encoding.decodeUtf8 $(embedFile "static/utoy.css")
H.body $ do
mimeRender _ model = renderHtml $ documentWithBody $ do
H.table $ for_ (Text.unpack model.text) $ \c -> do
H.tr $ do
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
@@ -203,6 +216,15 @@ orThrow (Right val) _ = pure val
printfHtml :: PrintfArg a => String -> a -> H.Html
printfHtml fmt = (H.toHtml :: String -> H.Html) . printf fmt
documentWithBody :: H.Html -> H.Html
documentWithBody body =
H.docTypeHtml $ do
H.head $ do
H.meta ! A.charset "utf-8"
H.title "utoy"
H.style $ H.toHtml $ Encoding.decodeUtf8 $(embedFile "static/utoy.css")
H.body body
-- HTML routes
data HTML
+1 -1
View File
@@ -4,6 +4,6 @@ let
in
pkgs.dockerTools.buildImage {
name = "git.pbrinkmeier.de/paul/utoy";
tag = "0.2";
tag = utoy.version;
config.Cmd = [ "${utoy}/bin/utoy" ];
}
+1 -1
View File
@@ -1,7 +1,7 @@
# Adapted from new-template.hsfiles
name: utoy
version: 0.2
version: 0.4
git: "https://git.pbrinkmeier.de/paul/utoy"
license: MIT
author: "Paul Brinkmeier"
+1 -1
View File
@@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack
name: utoy
version: 0.2
version: 0.4
author: Paul Brinkmeier
maintainer: hallo@pbrinkmeier.de
copyright: 2023 Paul Brinkmeier
+1 -1
View File
@@ -8,7 +8,7 @@ let
utoy =
{ mkDerivation }:
mkDerivation {
version = "0.2";
version = "0.4";
pname = "utoy";
license = pkgs.lib.licenses.mit;
src =