@@ -1,3 +1,5 @@
1+{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
2+
13module Idris.Output where
24
35import Idris.Core.TT
@@ -13,10 +15,12 @@ import Util.ScreenSize (getScreenWidth)
1315
1416import Debug.Trace
1517
18+import Control.Monad.Error (throwError)
19+
1620import System.IO (stdout, Handle, hPutStrLn)
1721
1822import Data.Char (isAlpha)
19−import Data.List (nub)
23+import Data.List (nub, intersperse)
2024import Data.Maybe (fromMaybe)
2125
2226pshow :: IState -> Err -> String
@@ -170,3 +174,94 @@ printUndefinedNames :: [Name] -> Doc OutputAnnotation
170174printUndefinedNames ns = text "Undefined " <> names <> text "."
171175 where names = encloseSep empty empty (char ',') $ map ppName ns
172176 ppName = prettyName True True []
177+
178+renderExternal :: OutputFmt -> Int -> Doc OutputAnnotation -> Idris String
179+renderExternal fmt width doc
180+ | width < 1 = throwError . Msg $ "There must be at least one column for the pretty-printer."
181+ | otherwise =
182+ do ist <- getIState
183+ return . wrap fmt .
184+ displayDecorated (decorate fmt) .
185+ renderPretty 1.0 width .
186+ fmap (fancifyAnnots ist) $
187+ doc
188+ where
189+ decorate _ (AnnFC _) = id
190+
191+ decorate HTMLOutput (AnnName _ (Just TypeOutput) d _) =
192+ tag "idris-type" d
193+ decorate HTMLOutput (AnnName _ (Just FunOutput) d _) =
194+ tag "idris-function" d
195+ decorate HTMLOutput (AnnName _ (Just DataOutput) d _) =
196+ tag "idris-data" d
197+ decorate HTMLOutput (AnnName _ (Just MetavarOutput) d _) =
198+ tag "idris-metavar" d
199+ decorate HTMLOutput (AnnName _ (Just PostulateOutput) d _) =
200+ tag "idris-postulate" d
201+ decorate HTMLOutput (AnnName _ _ _ _) = id
202+ decorate HTMLOutput (AnnBoundName _ True) = tag "idris-bound idris-implicit" Nothing
203+ decorate HTMLOutput (AnnBoundName _ False) = tag "idris-bound" Nothing
204+ decorate HTMLOutput (AnnConst _) = id -- TODO
205+ decorate HTMLOutput (AnnData _ _) = tag "idris-data" Nothing
206+ decorate HTMLOutput (AnnType _ _) = tag "idris-type" Nothing
207+ decorate HTMLOutput AnnKeyword = tag "idris-keyword" Nothing
208+ decorate HTMLOutput (AnnTextFmt _) = id -- TODO
209+ decorate HTMLOutput (AnnTerm _ _) = id
210+ decorate HTMLOutput (AnnSearchResult _) = id
211+
212+ decorate LaTeXOutput (AnnName _ (Just TypeOutput) _ _) =
213+ latex "IdrisType"
214+ decorate LaTeXOutput (AnnName _ (Just FunOutput) _ _) =
215+ latex "IdrisFunction"
216+ decorate LaTeXOutput (AnnName _ (Just DataOutput) _ _) =
217+ latex "IdrisData"
218+ decorate LaTeXOutput (AnnName _ (Just MetavarOutput) _ _) =
219+ latex "IdrisMetavar"
220+ decorate LaTeXOutput (AnnName _ (Just PostulateOutput) _ _) =
221+ latex "IdrisPostulate"
222+ decorate LaTeXOutput (AnnName _ _ _ _) = id
223+ decorate LaTeXOutput (AnnBoundName _ True) = latex "IdrisImplicit"
224+ decorate LaTeXOutput (AnnBoundName _ False) = latex "IdrisBound"
225+ decorate LaTeXOutput (AnnConst _) = id -- TODO
226+ decorate LaTeXOutput (AnnData _ _) = latex "IdrisData"
227+ decorate LaTeXOutput (AnnType _ _) = latex "IdrisType"
228+ decorate LaTeXOutput AnnKeyword = latex "IdrisKeyword"
229+ decorate LaTeXOutput (AnnTextFmt _) = id -- TODO
230+ decorate LaTeXOutput (AnnTerm _ _) = id
231+ decorate LaTeXOutput (AnnSearchResult _) = id
232+
233+ tag cls docs str = "<span class=\""++cls++"\""++title++">" ++ str ++ "</span>"
234+ where title = maybe "" (\d->" title=\"" ++ d ++ "\"") docs
235+
236+ latex cmd str = "\\"++cmd++"{"++str++"}"
237+
238+ wrap HTMLOutput str =
239+ "<!doctype html><html><head><style>" ++ css ++ "</style></head>" ++
240+ "<body><!-- START CODE --><pre>" ++ str ++ "</pre><!-- END CODE --></body></html>"
241+ where css = concat . intersperse "\n" $
242+ [".idris-data { color: red; } ",
243+ ".idris-type { color: blue; }",
244+ ".idris-function {color: green; }",
245+ ".idris-keyword { font-weight: bold; }",
246+ ".idris-bound { color: purple; }",
247+ ".idris-implicit { font-style: italic; }"]
248+ wrap LaTeXOutput str =
249+ concat . intersperse "\n" $
250+ ["\\documentclass{article}",
251+ "\\usepackage{fancyvrb}",
252+ "\\usepackage[usenames]{xcolor}"] ++
253+ map (\(cmd, color) ->
254+ "\\newcommand{\\"++ cmd ++
255+ "}[1]{\\textcolor{"++ color ++"}{#1}}")
256+ [("IdrisData", "red"), ("IdrisType", "blue"),
257+ ("IdrisBound", "magenta"), ("IdrisFunction", "green")] ++
258+ ["\\newcommand{\\IdrisKeyword}[1]{{\\underline{#1}}}",
259+ "\\newcommand{\\IdrisImplicit}[1]{{\\itshape \\IdrisBound{#1}}}",
260+ "\n",
261+ "\\begin{document}",
262+ "% START CODE",
263+ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]",
264+ str,
265+ "\\end{Verbatim}",
266+ "% END CODE",
267+ "\\end{document}"]