Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 112 lines (104 sloc) 4.713 kB
40dd210 Performance improvements suggested by Joachim Breitner.
jgm@berkeley.edu authored
1 {-# LANGUAGE CPP #-}
ede8650 Initial commit.
jgm@berkeley.edu authored
2 module Main where
3 import Text.Highlighting.Kate
40dd210 Performance improvements suggested by Joachim Breitner.
jgm@berkeley.edu authored
4 import System.IO (hPutStrLn, stderr)
ede8650 Initial commit.
jgm@berkeley.edu authored
5 import System.Environment
6 import Text.XHtml.Transitional
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
7 import System.Console.GetOpt
8 import System.Exit
cd1fd73 Use languagesByFilename in Highlight.
jgm@berkeley.edu authored
9 import System.FilePath (takeFileName)
17474ec Made module includes more explicit in Highlight.hs.
jgm@berkeley.edu authored
10 import Data.Maybe (listToMaybe)
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
11 import Data.Char (toLower)
12
13 data Flag = CssPath String
14 | Help
fcffd10 Added --fragment option to render html fragment.
jgm@berkeley.edu authored
15 | Fragment
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
16 | List
17 | NumberLines
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
18 | Syntax String
c6ca21c New, compressed format for classes.
jgm@berkeley.edu authored
19 | Detailed
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
20 | TitleAttributes
47a7ceb Added --version option and highlightingKateVersion constant.
jgm@berkeley.edu authored
21 | Version
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
22 deriving (Eq, Show)
23
24 options :: [OptDescr Flag]
25 options =
26 [ Option ['c'] ["css"] (ReqArg CssPath "PATH") "link CSS file"
fcffd10 Added --fragment option to render html fragment.
jgm@berkeley.edu authored
27 , Option ['f'] ["fragment"] (NoArg Fragment) "fragment, without document header"
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
28 , Option ['h'] ["help"] (NoArg Help) "show usage message"
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
29 , Option ['l'] ["list"] (NoArg List) "list available language syntaxes"
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
30 , Option ['n'] ["number-lines"] (NoArg NumberLines) "number lines"
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
31 , Option ['s'] ["syntax"] (ReqArg Syntax "SYNTAX") "specify language syntax to use"
c6ca21c New, compressed format for classes.
jgm@berkeley.edu authored
32 , Option ['d'] ["details"] (NoArg Detailed) "include detailed lexical information in classes"
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
33 , Option ['t'] ["title-attributes"] (NoArg TitleAttributes) "include structure in title attributes"
47a7ceb Added --version option and highlightingKateVersion constant.
jgm@berkeley.edu authored
34 , Option ['v'] ["version"] (NoArg Version) "print version"
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
35 ]
36
37 cssPathOf :: [Flag] -> Maybe String
38 cssPathOf [] = Nothing
39 cssPathOf (CssPath s : _) = Just s
40 cssPathOf (_:xs) = cssPathOf xs
ede8650 Initial commit.
jgm@berkeley.edu authored
41
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
42 syntaxOf :: [Flag] -> Maybe String
43 syntaxOf [] = Nothing
44 syntaxOf (Syntax s : _) = Just s
45 syntaxOf (_:xs) = syntaxOf xs
46
a6c8973 Reorganized API.
jgm@berkeley.edu authored
47 filterNewlines :: String -> String
48 filterNewlines ('\r':'\n':xs) = '\n' : filterNewlines xs
49 filterNewlines ('\r':xs) = '\n' : filterNewlines xs
50 filterNewlines (x:xs) = x : filterNewlines xs
51 filterNewlines [] = []
52
53 -- | Highlight source code in XHTML using specified syntax.
54 xhtmlHighlight :: [FormatOption] -- ^ Options
55 -> String -- ^ Name of syntax to use
56 -> String -- ^ Source code to highlight
57 -> Html
58 xhtmlHighlight opts lang code =
59 case highlightAs lang code of
60 Right result -> formatAsXHtml opts lang result
61 Left _ -> pre $ thecode << code
62
ede8650 Initial commit.
jgm@berkeley.edu authored
63 main = do
47a7ceb Added --version option and highlightingKateVersion constant.
jgm@berkeley.edu authored
64 (opts, fnames, errs) <- getArgs >>= return . getOpt Permute options
65 prg <- getProgName
66 let usageHeader = prg ++ " [options] [files...]"
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
67 if not (null errs)
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
68 then ioError (userError $ concat errs ++ usageInfo usageHeader options)
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
69 else return ()
70 if List `elem` opts
c38fa8a Highlight -l output to stdout, not stderr, and return success.
jgm@berkeley.edu authored
71 then putStrLn (unwords languages) >> exitWith ExitSuccess
ede8650 Initial commit.
jgm@berkeley.edu authored
72 else return ()
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
73 if Help `elem` opts
74 then hPutStrLn stderr (usageInfo usageHeader options) >>
75 exitWith (ExitFailure 1)
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
76 else return ()
47a7ceb Added --version option and highlightingKateVersion constant.
jgm@berkeley.edu authored
77 if Version `elem` opts
78 then putStrLn (prg ++ " " ++ highlightingKateVersion ++ " - (c) 2008 John MacFarlane") >>
79 exitWith ExitSuccess
80 else return ()
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
81 code <- if null fnames
a6c8973 Reorganized API.
jgm@berkeley.edu authored
82 then getContents >>= return . filterNewlines
83 else mapM readFile fnames >>= return . filterNewlines . concat
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
84 let lang' = case syntaxOf opts of
85 Just e -> Just e
cd1fd73 Use languagesByFilename in Highlight.
jgm@berkeley.edu authored
86 Nothing -> case fnames of
87 [] -> Nothing
88 (x:_) -> listToMaybe $ languagesByFilename $ takeFileName x
3a31b1c Specify language in Highlight using -s option, not argument; try to g…
jgm@berkeley.edu authored
89 lang <- if lang' == Nothing
90 then hPutStrLn stderr "No syntax specified." >>
91 hPutStrLn stderr (usageInfo usageHeader options) >>
92 exitWith (ExitFailure 5)
93 else do let (Just l) = lang'
94 return (map toLower l)
95 if not (lang `elem` (map (map toLower) languages))
96 then hPutStrLn stderr ("Unknown syntax: " ++ lang) >> exitWith (ExitFailure 4)
97 else return ()
c6ca21c New, compressed format for classes.
jgm@berkeley.edu authored
98 let highlightOpts = [OptTitleAttributes | TitleAttributes `elem` opts] ++
99 [OptDetailed | Detailed `elem` opts] ++
100 [OptNumberLines | NumberLines `elem` opts] ++
101 [OptLineAnchors | NumberLines `elem` opts]
9a74c51 Highlight now inserts css into document unless --css link specified.
jgm@berkeley.edu authored
102 let css = case cssPathOf opts of
d3ffa61 Added defaultHighlightingCss to Format; removed from Highlight.hs.
jgm@berkeley.edu authored
103 Nothing -> style ! [thetype "text/css"] $ primHtml defaultHighlightingCss
9a74c51 Highlight now inserts css into document unless --css link specified.
jgm@berkeley.edu authored
104 Just cssPath -> thelink ! [thetype "text/css", href cssPath, rel "stylesheet"] << noHtml
18300f3 Added options to Highlight executable.
jgm@berkeley.edu authored
105 let hcode = xhtmlHighlight highlightOpts lang code
8332ee7 Include title and metadata in Highlight's output.
jgm@berkeley.edu authored
106 let pageTitle = if null fnames then noHtml else thetitle << (takeFileName $ head fnames)
107 let metadata = meta ! [httpequiv "Content-Type", content "text/html; charset=UTF-8"] +++
108 meta ! [name "generator", content "highlight-kate"]
fcffd10 Added --fragment option to render html fragment.
jgm@berkeley.edu authored
109 if Fragment `elem` opts
110 then putStrLn $ renderHtmlFragment hcode
8332ee7 Include title and metadata in Highlight's output.
jgm@berkeley.edu authored
111 else putStrLn $ renderHtml $ header << [pageTitle, metadata, css] +++ body << hcode
Something went wrong with that request. Please try again.