By | Chris Done |
At | 2011-06-09 |
Title | Lots of languages for syntax highlighting. |
Description |
Edit file src/Amelie/View/Paste.hs 33188 → 33188
10 10 ,pasteLink
11 11 ,pasteRawLink)
12 12 where
13 13 14 14 import Amelie.Types
+ 15 import Amelie.View.Highlight (highlightPaste)
15 16 import Amelie.View.Html
16 17 import Amelie.View.Layout
17 18 18 19 import Control.Applicative ((<$>),(<*>),pure)
19 20 import Control.Monad (when)
20 21 import Data.ByteString.UTF8 (toString)
21 22 import qualified Data.Map as M
22 23 import Data.Monoid.Operator ((++))
23 24 import Data.String (fromString)
24 25 import Data.Text (Text)
- 25 import Data.Text.Encoding (encodeUtf8)
26 26 import Data.Text.Lazy (fromStrict)
27 27 import Data.Time.Show (showDateTime)
28 28 import Data.Traversable
29 29 import Prelude hiding ((++))
30 30 import Safe (readMay)
31 31 import Text.Blaze.Html5 as H hiding (map)
32 32 import qualified Text.Blaze.Html5.Attributes as A
33 33 import Text.Blaze.Html5.Extra
34 34 import Text.Formlet
- 35 import Text.Highlighter.Formatters.Html (format)
- 36 import Text.Highlighter.Lexer (runLexer)
- 37 import Text.Highlighter.Lexers.Haskell (lexer)
38 35 39 36 -- | A formlet for paste submission / editing.
40 37 pasteFormlet :: PasteFormlet -> (Formlet PasteSubmit,Html)
41 38 pasteFormlet PasteFormlet{..} =
42 39 let form = postForm ! A.action "/new" $ do
… … … … 86 83 87 84 -- | View a paste's details and content.
88 85 viewPaste :: [Channel] -> [Language] -> Paste -> Html
89 86 viewPaste chans langs paste@Paste{..} = do
90 87 pasteDetails chans langs paste
- 91 pasteContent paste
+ 88 pasteContent langs paste
92 89 93 90 -- | List the details of the page in a dark section.
94 91 pasteDetails :: [Channel] -> [Language] -> Paste -> Html
95 92 pasteDetails chans langs paste@Paste{..} =
96 93 darkSection (fromStrict pasteTitle) $ do
… … … … 105 102 106 103 where detail title content = do
107 104 li $ do strong (title ++ ":"); toHtml content
108 105 109 106 -- | Show the paste content with highlighting.
- 110 pasteContent :: Paste -> Html
- 111 pasteContent Paste{..} =
- 112 lightNoTitleSection $ do
- 113 case runLexer lexer (encodeUtf8 (pastePaste ++ "\n")) of
- 114 Right tokens -> format True tokens
- 115 _ -> pre $ toHtml pastePaste
+ 107 pasteContent :: [Language] -> Paste -> Html
+ 108 pasteContent langs paste =
+ 109 lightNoTitleSection $ highlightPaste langs paste
116 110 117 111 -- | The href link to a paste.
118 112 pasteLink :: ToHtml html => Paste -> html -> Html
119 113 pasteLink Paste{..} inner = href ("/" ++ show pasteId) inner
120 114 … … … … Edit file src/Amelie/View/Style.hs 33188 → 33188
163 163 tokenColor "kr" "#397460"
164 164 tokenColor "s" "#366354"
165 165 tokenColor "sc" "#366354"
166 166 tokenColor "se" "#743838"
167 167 tokenColor "kt" "#4F4371"
+ 168 tokenColor "nv" "#4F4371"
168 169 tokenColor "ow" "#333"
169 170 tokenColor "o" "#3E394D"
170 171 tokenColor "n" "#343634"
171 172 tokenColor "nf" "#222"
172 173 -- This is a weird one.
… … … … Add file src/Amelie/View/Highlight.hs 33188
+ 1 {-# OPTIONS -Wall -fno-warn-name-shadowing #-} + 2 {-# LANGUAGE OverloadedStrings #-} + 3 {-# LANGUAGE RecordWildCards #-} + 4 + 5 -- | Code highlighting. + 6 + 7 module Amelie.View.Highlight + 8 -- (highlightPaste) + 9 where + 10 + 11 import Amelie.Types + 12 + 13 import Data.Char + 14 import Data.List (find) + 15 import Data.Monoid.Operator ((++)) + 16 import Data.Text (unpack) + 17 import Data.Text.Encoding (encodeUtf8) + 18 import Prelude hiding ((++)) + 19 import Text.Blaze.Html5 as H hiding (map) + 20 import Text.Highlighter.Formatters.Html (format) + 21 import Text.Highlighter.Lexer (runLexer) + 22 import Text.Highlighter.Lexers + 23 import Text.Highlighter.Types + 24 + 25 -- | Syntax highlight the paste. + 26 highlightPaste :: [Language] -> Paste -> Html + 27 highlightPaste langs Paste{..} = + 28 case lang >>= ((`lookupLang` (map snd lexers)) . unpack . languageName) of + 29 Nothing -> pre $ toHtml pastePaste + 30 Just lexer -> + 31 case runLexer lexer (encodeUtf8 (pastePaste ++ "\n")) of + 32 Right tokens -> format True tokens + 33 _ -> pre $ toHtml pastePaste + 34 + 35 where lang = find ((==pasteLanguage) . Just . languageId) langs + 36 lookupLang name = find $ \lexer -> lower (lName lexer) == lower name + 37 lower = map toLower