83
81
} deriving (Read, Show)
84
-- | Converts a list of files (ending in .xml) and directories containing .xml files
85
-- into a list of .xml files.
86
argFiles :: [String] -> IO [String]
87
argFiles [] = error "Specify paths of xml files and/or directories containing xml syntax files."
89
let isXmlFile x = isSuffixOf ".xml" x
90
let (files, dirs) = partition isXmlFile args
91
dirContents <- mapM (\dir -> getDirectoryContents dir >>= return . map (combine dir) . filter isXmlFile) dirs
92
return $ nub (files ++ concat dirContents)
86
95
libraryPath = joinPath ["Text", "Highlighting", "Kate"]
87
96
destDir = joinPath [libraryPath, "Syntax"]
92
srcDir <- if null argv
93
then error "Specify path of directory containing xml syntax files."
94
else return $ argv !! 0
95
let isXmlFile x = isSuffixOf ".xml" x
96
files <- getDirectoryContents srcDir >>= (return . map (combine srcDir) . filter isXmlFile)
100
files <- getArgs >>= argFiles
97
101
destDirExists <- doesDirectoryExist destDir
100
104
else createDirectory destDir
101
mapM processOneFile files >> return ()
105
mapM_ processOneFile files
102
106
let syntaxFile = combine libraryPath (addExtension "Syntax" "hs")
103
107
putStrLn $ "Writing " ++ syntaxFile
104
let names = sort $ map nameFromPath files
108
-- Get all syntax files, not only the newly generated ones.
109
names <- getDirectoryContents destDir >>= return . sort . map dropExtension . filter (isSuffixOf ".hs")
105
110
let imports = unlines $ map (\name -> "import qualified Text.Highlighting.Kate.Syntax." ++ name ++ " as " ++ name) names
106
111
let cases = unlines $ map (\name -> " " ++ show (map toLower name) ++ " -> " ++ name ++ ".highlight") names
107
112
let languageExtensions = concat $ intersperse ", " $ map (\name -> "(" ++ show name ++ ", " ++ name ++ ".syntaxExtensions)") names
134
139
\ -> String -- ^ Source code to highlight\n\
135
140
\ -> Either String [SourceLine] -- ^ Either error message or result\n\
136
141
\highlightAs lang =\n\
137
\ case (map toLower lang) of\n" ++
142
\ let lang' = map toLower lang\n\
143
\ lang'' = if lang' `elem` map (map toLower) languages\n\
145
\ else case languagesByExtension lang' of\n\
146
\ [l] -> map toLower l -- go by extension if unambiguous\n\
148
\ in case lang'' of\n" ++
139
" _ -> (\\_ -> Left (\"Unknown language ++ \" ++ lang))\n"
150
" _ -> (\\_ -> Left (\"Unknown language: \" ++ lang))\n"
141
152
processOneFile :: FilePath -> IO ()
142
153
processOneFile src = do
238
249
endLineParser, withAttr, styles, parseExpressionInternal,
239
250
defaultAttributes {- , lineBeginContexts -}] ++ contexts ++ [contextCatchAll]
241
mkIdentifier :: String -> String
243
mkIdentifier ('-':x:xs) = toUpper x : mkIdentifier xs
244
mkIdentifier ('-':xs) = mkIdentifier xs
245
mkIdentifier (x:xs) = x : mkIdentifier xs
247
252
mkAlternatives :: [Doc] -> Doc
248
253
mkAlternatives docs =
249
254
let contents = vcat $ intersperse (text "<|>") docs
338
343
"JavaScript" -> "Javascript"
346
capitalize :: String -> String
347
capitalize (x:xs) = toUpper x : xs
341
350
nameFromPath :: FilePath -> String
342
351
nameFromPath = concat . map capitalize . words .
343
352
(map (\c -> if c == '-' then ' ' else c)) . takeFileName .