10 Commits
Author SHA1 Message Date
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
paul 36f9bb390d Bump version 2023-03-10 18:51:13 +01:00
paul f5fd1089b1 Add /text endpoint 2023-03-10 18:46:34 +01:00
paul 5bdf69ef91 Regenerate hie.yaml 2023-03-10 18:33:05 +01:00
paul da2540c3fa Add docker image derivation 2023-03-09 02:51:12 +01:00
paul d5cd8905bb Reduce files copied into build 2023-03-09 02:22:34 +01:00
paul 66ad565cba Add nix derivation 2023-03-09 01:39:45 +01:00
9 changed files with 135 additions and 28 deletions
+1
View File
@@ -1,3 +1,4 @@
.stack-work/ .stack-work/
.vscode/ .vscode/
*.swp *.swp
result
+20
View File
@@ -1 +1,21 @@
# utoy # 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
```
+57 -21
View File
@@ -62,11 +62,12 @@ app = serve (Proxy :: Proxy API) server
type API = type API =
"bytes" :> Capture "bytes" Text :> Get '[PlainText, HTML] BytesModel "bytes" :> Capture "bytes" Text :> Get '[PlainText, HTML] BytesModel
:<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText] CodepointsModel :<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel
:<|> "text" :> Capture "text" Text :> Get '[HTML] TextModel
server :: Server API server :: Server API
server = server =
bytesR :<|> codepointsR bytesR :<|> codepointsR :<|> textR
where where
bytesR bytesP = do bytesR bytesP = do
bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400 bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400
@@ -76,6 +77,9 @@ server =
codepoints' <- Parsers.parseCodepoints codepointsP `orThrow` const err400 codepoints' <- Parsers.parseCodepoints codepointsP `orThrow` const err400
pure $ mkCodepointsModel codepoints' pure $ mkCodepointsModel codepoints'
textR textP = do
pure $ TextModel textP
-- /bytes/<bytes> -- /bytes/<bytes>
newtype BytesModel = BytesModel newtype BytesModel = BytesModel
@@ -104,24 +108,18 @@ instance MimeRender PlainText BytesModel where
] ]
instance MimeRender HTML BytesModel where instance MimeRender HTML BytesModel where
mimeRender _ model = renderHtml $ H.docTypeHtml $ do mimeRender _ model = renderHtml $ documentWithBody $ do
H.head $ do H.table $ for_ model.codepoints $ \(bytes, eiC) -> do
H.meta ! A.charset "utf-8" H.tr $ do
H.title "utoy" H.td $ H.pre $ H.toHtml $ unlines $ map unwords [map showByteHex bytes, map showByteBin bytes]
H.style $ H.toHtml $ Encoding.decodeUtf8 $(embedFile "utoy.css") case eiC of
H.body $ do Left err ->
H.table $ for_ model.codepoints $ \(bytes, eiC) -> do H.td ! A.colspan "4" $ H.toHtml $ "Decoding error: " ++ err
H.tr $ do Right c -> do
H.td $ H.pre $ H.toHtml $ unlines $ map unwords [map showByteHex bytes, map showByteBin bytes] H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
case eiC of H.td $ H.code $ printfHtml "U+%04X" c
Left err -> H.td $ H.code $ H.toHtml $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
H.td ! A.colspan "4" $ H.toHtml $ "Decoding error: " ++ err H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
Right c -> do
H.td $ do
H.input ! A.value (H.toValue [c]) ! A.style "text-align: center; width: 2em; font-size: 1em;"
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.toHtml $ fromMaybe "" $ blockName c
-- /codepoints/<codepoints> -- /codepoints/<codepoints>
@@ -158,6 +156,35 @@ instance MimeRender PlainText CodepointsModel where
| (codepoint, eiC) <- model.codepoints | (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
{ text :: Text
}
instance MimeRender HTML TextModel where
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])
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
-- Utilities -- Utilities
renderText :: Text -> BL.ByteString renderText :: Text -> BL.ByteString
@@ -167,7 +194,7 @@ showByteHex :: Word8 -> String
showByteHex = printf " %02X" showByteHex = printf " %02X"
showByteBin :: Word8 -> String showByteBin :: Word8 -> String
showByteBin = printf "%8b" showByteBin = printf "%08b"
blockName :: Char -> Maybe String blockName :: Char -> Maybe String
blockName c = UnicodeBlocks.blockName . UnicodeBlocks.blockDefinition <$> UnicodeBlocks.block c blockName c = UnicodeBlocks.blockName . UnicodeBlocks.blockDefinition <$> UnicodeBlocks.block c
@@ -179,6 +206,15 @@ orThrow (Right val) _ = pure val
printfHtml :: PrintfArg a => String -> a -> H.Html printfHtml :: PrintfArg a => String -> a -> H.Html
printfHtml fmt = (H.toHtml :: String -> H.Html) . printf fmt 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 -- HTML routes
data HTML data HTML
+1 -1
View File
@@ -4,7 +4,7 @@ cradle:
component: "utoy:lib" component: "utoy:lib"
- path: "./app/Main.hs" - path: "./app/Main.hs"
component: "utoy:exe:utoy-exe" component: "utoy:exe:utoy"
- path: "./test" - path: "./test"
component: "utoy:test:utoy-test" component: "utoy:test:utoy-test"
+9
View File
@@ -0,0 +1,9 @@
let
pkgs = import ./pkgs.nix {};
utoy = import ../utoy.nix;
in
pkgs.dockerTools.buildImage {
name = "git.pbrinkmeier.de/paul/utoy";
tag = utoy.version;
config.Cmd = [ "${utoy}/bin/utoy" ];
}
+3 -3
View File
@@ -1,7 +1,7 @@
# Adapted from new-template.hsfiles # Adapted from new-template.hsfiles
name: utoy name: utoy
version: 0.1.0.0 version: 0.3
git: "https://git.pbrinkmeier.de/paul/utoy" git: "https://git.pbrinkmeier.de/paul/utoy"
license: MIT license: MIT
author: "Paul Brinkmeier" author: "Paul Brinkmeier"
@@ -10,7 +10,7 @@ copyright: "2023 Paul Brinkmeier"
extra-source-files: extra-source-files:
- README.md - README.md
- utoy.css - static/utoy.css
dependencies: dependencies:
- base >= 4.7 && < 5 - base >= 4.7 && < 5
@@ -32,7 +32,7 @@ library:
source-dirs: src source-dirs: src
executables: executables:
utoy-exe: utoy:
main: Main.hs main: Main.hs
source-dirs: app source-dirs: app
ghc-options: ghc-options:
+6
View File
@@ -19,3 +19,9 @@ pre, code {
pre { pre {
margin: 0; font-size: 0.5em; margin: 0; font-size: 0.5em;
} }
.charbox {
text-align: center;
width: 2em;
font-size: 1em;
}
+3 -3
View File
@@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
name: utoy name: utoy
version: 0.1.0.0 version: 0.3
author: Paul Brinkmeier author: Paul Brinkmeier
maintainer: hallo@pbrinkmeier.de maintainer: hallo@pbrinkmeier.de
copyright: 2023 Paul Brinkmeier copyright: 2023 Paul Brinkmeier
@@ -14,7 +14,7 @@ license-file: LICENSE
build-type: Simple build-type: Simple
extra-source-files: extra-source-files:
README.md README.md
utoy.css static/utoy.css
source-repository head source-repository head
type: git type: git
@@ -36,7 +36,7 @@ library
, text , text
default-language: Haskell2010 default-language: Haskell2010
executable utoy-exe executable utoy
main-is: Main.hs main-is: Main.hs
hs-source-dirs: hs-source-dirs:
app app
+35
View File
@@ -0,0 +1,35 @@
let
pkgs = import ./nix/pkgs.nix {};
settings = import ./nix/settings.nix;
haskellDeps = import ./nix/haskell-deps.nix;
haskellPackages = pkgs.haskell.packages."${settings.ghc}";
utoy =
{ mkDerivation }:
mkDerivation {
version = "0.3";
pname = "utoy";
license = pkgs.lib.licenses.mit;
src =
let
buildFiles = [
./LICENSE
./utoy.cabal
./Setup.hs
./app
./src
./static
./test
];
in
pkgs.lib.sources.cleanSourceWith {
src = ./.;
filter = path: _type: pkgs.lib.any (prefix: pkgs.lib.hasPrefix (toString prefix) path) buildFiles;
};
libraryHaskellDepends = haskellDeps haskellPackages;
};
in
pkgs.haskell.lib.justStaticExecutables
(haskellPackages.callPackage utoy {})