module Skylighting.Core (
lookupSyntax
, syntaxByName
, syntaxByShortName
, syntaxesByExtension
, syntaxesByFilename
, module Skylighting.Types
, module Skylighting.Tokenizer
, module Skylighting.Parser
, module Skylighting.Regex
, module Skylighting.Styles
, module Skylighting.Loader
, module Skylighting.Format.ANSI
, module Skylighting.Format.HTML
, module Skylighting.Format.LaTeX
) where
import Control.Monad
import Data.List (tails)
import qualified Data.Map as Map
import Data.Maybe (listToMaybe)
import Data.Text (Text)
import qualified Data.Text as Text
import Skylighting.Format.ANSI
import Skylighting.Format.HTML
import Skylighting.Format.LaTeX
import Skylighting.Loader
import Skylighting.Parser
import Skylighting.Regex
import Skylighting.Styles
import Skylighting.Tokenizer
import Skylighting.Types
syntaxesByExtension :: SyntaxMap -> String -> [Syntax]
syntaxesByExtension syntaxmap ('.':ext) =
syntaxesByFilename syntaxmap ("*." ++ ext)
syntaxesByExtension syntaxmap ext =
syntaxesByFilename syntaxmap ("*." ++ ext)
syntaxesByFilename :: SyntaxMap -> String -> [Syntax]
syntaxesByFilename syntaxmap fn = [s | s <- Map.elems syntaxmap
, matchGlobs fn (sExtensions s)]
syntaxByName :: SyntaxMap -> Text -> Maybe Syntax
syntaxByName syntaxmap name =
Map.lookup (Text.toLower name) (Map.mapKeys Text.toLower syntaxmap)
syntaxByShortName :: SyntaxMap -> Text -> Maybe Syntax
syntaxByShortName syntaxmap name = listToMaybe
[s | s <- Map.elems syntaxmap
, Text.toLower (sShortname s) == Text.toLower name ]
lookupSyntax :: Text -> SyntaxMap -> Maybe Syntax
lookupSyntax lang syntaxmap
| lang == Text.pack "csharp" = lookupSyntax (Text.pack "cs") syntaxmap
| otherwise =
syntaxByName syntaxmap lang `mplus`
syntaxByShortName syntaxmap lang `mplus`
listToMaybe (syntaxesByExtension syntaxmap (Text.unpack lang))
matchGlobs :: String -> [String] -> Bool
matchGlobs fn globs = any (flip matchGlob fn) globs
matchGlob :: String -> String -> Bool
matchGlob ('*':xs) fn = any (matchGlob xs) (tails fn)
matchGlob (x:xs) (y:ys) = x == y && matchGlob xs ys
matchGlob "" "" = True
matchGlob _ _ = False