Created
September 6, 2019 11:51
-
-
Save scravy/506551a140ca4a3e6519939d3d6ef670 to your computer and use it in GitHub Desktop.
A Haskell Script to nicify all kinds of structured output
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
| import Data.List (replicate) | |
| main = getContents >>= (\x -> putStr (proc 0 x)) | |
| space = ' ' | |
| newline = '\n' | |
| isOpening = \c -> case c of | |
| '[' -> True | |
| '{' -> True | |
| '(' -> True | |
| _ -> False | |
| isClosing = \c -> case c of | |
| ']' -> True | |
| '}' -> True | |
| ')' -> True | |
| _ -> False | |
| isSeparator = \c -> case c of | |
| ';' -> True | |
| ',' -> True | |
| _ -> False | |
| isStringOpening = \c -> case c of | |
| '"' -> True | |
| _ -> False | |
| indentation = flip replicate space . (2 *) | |
| proc :: Int -> String -> String | |
| proc indent = \s -> case s of | |
| x : xs | |
| | isOpening x -> | |
| let indent' = succ indent in | |
| newline : indentation indent ++ x : newline : indentation indent' ++ proc indent' xs | |
| | isClosing x -> | |
| let indent' = pred indent in | |
| newline : indentation indent' ++ x : proc indent' xs | |
| | isSeparator x -> | |
| x : newline : indentation indent ++ proc indent (dropWhile (== space) xs) | |
| | isStringOpening x -> | |
| let (str, rest) = break (== x) xs in | |
| x : str ++ take 1 rest ++ proc indent (drop 1 rest) | |
| | otherwise -> | |
| x : proc indent xs | |
| _ -> "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment