{-# LANGUAGE CPP #-}
module Skylighting.Loader ( loadSyntaxFromFile
, loadSyntaxesFromDir
)
where
import Control.Monad (filterM, foldM)
import Control.Monad.Except (ExceptT(ExceptT), runExceptT)
import Control.Monad.IO.Class (liftIO)
import System.Directory (listDirectory, doesFileExist)
import System.FilePath ((</>), takeExtension)
import Skylighting.Types (SyntaxMap, Syntax)
import Skylighting.Parser (addSyntaxDefinition, parseSyntaxDefinition)
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup
#endif
syntaxFileExtension :: String
syntaxFileExtension :: String
syntaxFileExtension = String
".xml"
isSyntaxFile :: FilePath -> Bool
isSyntaxFile :: String -> Bool
isSyntaxFile = (String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
syntaxFileExtension) (String -> Bool) -> (String -> String) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
takeExtension
loadSyntaxFromFile :: FilePath -> IO (Either String Syntax)
loadSyntaxFromFile :: String -> IO (Either String Syntax)
loadSyntaxFromFile String
path = do
Either String Syntax
result <- String -> IO (Either String Syntax)
parseSyntaxDefinition String
path
case Either String Syntax
result of
Left String
e -> Either String Syntax -> IO (Either String Syntax)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String Syntax -> IO (Either String Syntax))
-> Either String Syntax -> IO (Either String Syntax)
forall a b. (a -> b) -> a -> b
$ String -> Either String Syntax
forall a b. a -> Either a b
Left (String -> Either String Syntax) -> String -> Either String Syntax
forall a b. (a -> b) -> a -> b
$ String
"Error parsing file " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> String
forall a. Show a => a -> String
show String
path String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
e
Right Syntax
s -> Either String Syntax -> IO (Either String Syntax)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either String Syntax -> IO (Either String Syntax))
-> Either String Syntax -> IO (Either String Syntax)
forall a b. (a -> b) -> a -> b
$ Syntax -> Either String Syntax
forall a b. b -> Either a b
Right Syntax
s
loadSyntaxesFromDir :: FilePath -> IO (Either String SyntaxMap)
loadSyntaxesFromDir :: String -> IO (Either String SyntaxMap)
loadSyntaxesFromDir String
path = ExceptT String IO SyntaxMap -> IO (Either String SyntaxMap)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT String IO SyntaxMap -> IO (Either String SyntaxMap))
-> ExceptT String IO SyntaxMap -> IO (Either String SyntaxMap)
forall a b. (a -> b) -> a -> b
$ do
[String]
files <- IO [String] -> ExceptT String IO [String]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [String] -> ExceptT String IO [String])
-> IO [String] -> ExceptT String IO [String]
forall a b. (a -> b) -> a -> b
$ String -> IO [String]
syntaxFiles String
path
let loadSyntax :: SyntaxMap -> String -> ExceptT String IO SyntaxMap
loadSyntax SyntaxMap
sMap String
file = do
Syntax
s <- IO (Either String Syntax) -> ExceptT String IO Syntax
forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (IO (Either String Syntax) -> ExceptT String IO Syntax)
-> IO (Either String Syntax) -> ExceptT String IO Syntax
forall a b. (a -> b) -> a -> b
$ String -> IO (Either String Syntax)
loadSyntaxFromFile String
file
SyntaxMap -> ExceptT String IO SyntaxMap
forall (m :: * -> *) a. Monad m => a -> m a
return (SyntaxMap -> ExceptT String IO SyntaxMap)
-> SyntaxMap -> ExceptT String IO SyntaxMap
forall a b. (a -> b) -> a -> b
$ Syntax -> SyntaxMap -> SyntaxMap
addSyntaxDefinition Syntax
s SyntaxMap
sMap
(SyntaxMap -> String -> ExceptT String IO SyntaxMap)
-> SyntaxMap -> [String] -> ExceptT String IO SyntaxMap
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM SyntaxMap -> String -> ExceptT String IO SyntaxMap
loadSyntax SyntaxMap
forall a. Monoid a => a
mempty [String]
files
syntaxFiles :: FilePath -> IO [FilePath]
syntaxFiles :: String -> IO [String]
syntaxFiles String
dir = do
[String]
entries <- String -> IO [String]
listDirectory String
dir
let absEntries :: [String]
absEntries = (String
dir String -> String -> String
</>) (String -> String) -> [String] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter String -> Bool
isSyntaxFile [String]
entries
(String -> IO Bool) -> [String] -> IO [String]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM String -> IO Bool
doesFileExist [String]
absEntries