From 6950ae14b1a1482765c1d2b055ee660c948f05cd Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Thu, 16 Jan 2020 22:02:05 +1300 Subject: [PATCH] Draft parser for FreeDesktop.Org config files. This'll be used for dispatching URLs to be opened in external apps. --- src/Network/URI/XDG/Ini.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/Network/URI/XDG/Ini.hs diff --git a/src/Network/URI/XDG/Ini.hs b/src/Network/URI/XDG/Ini.hs new file mode 100644 index 0000000..aa96682 --- /dev/null +++ b/src/Network/URI/XDG/Ini.hs @@ -0,0 +1,30 @@ +module Network.URI.XDG.Ini(parseIni) where + +import Data.Char (isSpace) +import Data.List (dropWhile, dropWhileEnd) + +type INI = [(String, [(String, String)])] + +parseIni :: String -> INI +parseIni source = parseIni' $ filter (not . isComment) $ map strip $ lines source + +strip cs = dropWhile isSpace $ dropWhileEnd isSpace cs +strip2 (a, b) = (strip a, strip b) + +isComment ('#':_) = True +isComment "" = True +isComment _ = False + +parseIni' (('[':cs):lines) | ']':header <- reverse cs = + let (keys, rest) = parseKeys lines in (reverse header, keys) : parseIni' rest +parseIni' _ = [] + +parseKeys :: [String] -> ([(String, String)], [String]) +parseKeys lines@(('[':_):_) = ([], lines) +parseKeys (line:lines) = + let (keys, rest) = parseKeys lines in (strip2 (parseKey line) : keys, rest) +parseKeys [] = ([], []) + +parseKey ('=':as) = ([], as) +parseKey (a:as) = let (x, y) = parseKey as in (a:x, y) +parseKey [] = ([], []) -- 2.30.2