Last active
July 16, 2018 11:27
-
-
Save cppxor2arr/1280e2d2c96a2b344055941930d9c18b to your computer and use it in GitHub Desktop.
squigly text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Squiglies (squiglies) where | |
import Data.Map.Strict (Map, fromList) | |
import Data.Text (Text) | |
squiglies :: Map Char Text | |
squiglies = fromList | |
[ | |
('a',"àáâãäåæāăą") | |
,('b',"b") | |
,('c',"çćč") | |
,('d',"ďđ") | |
,('e',"èéêëēėęěĕə") | |
,('f',"f") | |
,('g',"ģğ") | |
,('h',"h") | |
,('i',"ìíîïīįı") | |
,('j',"j") | |
,('k',"k") | |
,('l',"ĺļľł") | |
,('m',"m") | |
,('n',"ñńņň") | |
,('o',"òóôõöøőœ") | |
,('p',"p") | |
,('q',"q") | |
,('r',"ŕř") | |
,('s',"śšş") | |
,('t',"ťțţ") | |
,('u',"ùúûüūůűų") | |
,('v',"v") | |
,('w',"w") | |
,('x',"x") | |
,('y',"ý") | |
,('z',"źżž") | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import System.Environment (getArgs) | |
import System.IO (isEOF) | |
import Control.Monad (unless) | |
import Squiglies (squiglies) | |
import qualified Data.Text as T (Text, length, null, index) | |
import qualified Data.Map.Strict as M (lookup) | |
import Data.Char (toLower, isAlpha, isLower, toUpper) | |
import System.Random (randomRIO) | |
main :: IO () | |
main = do | |
args <- getArgs | |
if null args then pipeMode else argMode args | |
pipeMode :: IO () | |
pipeMode = do | |
eof <- isEOF | |
unless eof $ getLine >>= printSquigly >> pipeMode | |
argMode :: [String] -> IO () | |
argMode args = do | |
let str = unwords args | |
printSquigly str | |
printSquigly :: String -> IO () | |
printSquigly str = do | |
squiglyStr <- toSquigly str | |
unless (null squiglyStr) $ putStrLn squiglyStr | |
toSquigly :: String -> IO String | |
toSquigly = mapM replace | |
where | |
replace c = do | |
let value = M.lookup (toLower c) squiglies | |
maybe (pure c) noCaseRandomPick value | |
where | |
noCaseRandomPick = (convertBack <$>) . randomPick '\NUL' | |
convertBack | |
| not (isAlpha c) || isLower c = id | |
| otherwise = toUpper | |
randomPick :: Char -> T.Text -> IO Char | |
randomPick def text | |
| T.null text = pure def | |
| otherwise = do | |
rindex <- randomRIO (0,T.length text - 1) | |
pure $ T.index text rindex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment