Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The utility reads a set of lines of text from stdin, modifies the
- -- lines and writes them to stdout. If a line contains several
- -- portions of text separated by horizontal tabulations, the utility
- -- replaces the horizontal tabulations with combinations of a
- -- non-breaking space, a rightwards arrow, and a space, and prepends
- -- the line with an index number, a period, and a space. If a line
- -- starts with an index number, a period, and a space and contains
- -- combinations of a non-breaking space, a rightwards arrow, and a
- -- space, the utility removes the index number, the period and the
- -- space, and replaces the combinations of a non-breaking space, a
- -- rightwards arrow, and a space with horizontal tabulations.
- -- A line like this
- -- "foo bar baz"
- -- will become
- -- "1. foo → bar → baz"
- -- and a line like this
- -- "42. ham → spam → eggs"
- -- will become
- -- "ham spam eggs"
- import Data.List (group)
- import System.Exit (die)
- main :: IO ()
- main = do
- input <- getContents
- if null input
- then die "No data."
- else mapM_ (putStrLn . dispatcher) $ zip [1 .. ] $ lines input
- dispatcher :: (Int, String) -> String
- dispatcher line@(_, string)
- | isNumerated string = (removeNumeration . arrowsAndSpacesToTabs) string
- | otherwise = (tabsToArrowsAndSpaces . numberLines) line
- isNumerated :: String -> Bool
- isNumerated = (2 <) . length . takeWhile isSpacePeriodOrDigit
- isSpacePeriodOrDigit :: Char -> Bool
- isSpacePeriodOrDigit = (`elem` " .0123456789")
- removeNumeration :: String -> String
- removeNumeration = dropWhile isSpacePeriodOrDigit
- arrowsAndSpacesToTabs :: String -> String
- arrowsAndSpacesToTabs [] = []
- arrowsAndSpacesToTabs [a] = [a]
- arrowsAndSpacesToTabs [a, b] = [a, b]
- arrowsAndSpacesToTabs (a:b:c:cs)
- | a == '\160' && b == '\8594' && c == ' ' = '\t' : arrowsAndSpacesToTabs cs
- | otherwise = a : arrowsAndSpacesToTabs (b : c : cs)
- tabsToArrowsAndSpaces :: String -> String
- tabsToArrowsAndSpaces [] = []
- tabsToArrowsAndSpaces [x] = [x]
- tabsToArrowsAndSpaces lst = concatMap go (group lst)
- where
- go [] = []
- go str@(x:_)
- | x == '\t' = "\160\8594 "
- | otherwise = str
- numberLines :: (Int, String) -> String
- numberLines (number, string) = show number ++ ". " ++ string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement