CurryHtml.hs 7.68 KB
Newer Older
Björn Peemöller 's avatar
Björn Peemöller committed
1
2
3
{- |
    Module      :  $Header$
    Description :  Generating HTML documentation
Björn Peemöller 's avatar
Björn Peemöller committed
4
    Copyright   :  (c) 2011 - 2014, Björn Peemöller
Björn Peemöller 's avatar
Björn Peemöller committed
5
6
7
8
9
10
11
12
13
    License     :  OtherLicense

    Maintainer  :  bjp@informatik.uni-kiel.de
    Stability   :  experimental
    Portability :  portable

    This module defines a function for generating HTML documentation pages
    for Curry source modules.
-}
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
14
15
module Html.CurryHtml (source2html) where

Björn Peemöller 's avatar
Björn Peemöller committed
16
17
18
import Control.Monad.Writer
import Control.Monad.Trans.Either

19
20
import Data.Char             (toLower)
import Data.Maybe            (fromMaybe, isJust)
21
import System.FilePath       ((</>), dropFileName, takeBaseName)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
22

23
import Curry.Base.Ident      (QualIdent (..), unqualify)
Björn Peemöller 's avatar
Björn Peemöller committed
24
import Curry.Base.Message
Björn Peemöller 's avatar
Björn Peemöller committed
25
import Curry.Base.Pretty     (text)
26
import Curry.Files.PathUtils (readModule, lookupCurryFile)
Björn Peemöller 's avatar
Björn Peemöller committed
27
import Curry.Syntax          (Module, lexSource, parseModule)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
28

Björn Peemöller 's avatar
Björn Peemöller committed
29
30
import Html.SyntaxColoring

Björn Peemöller 's avatar
Björn Peemöller committed
31
import Base.Messages
Björn Peemöller 's avatar
Björn Peemöller committed
32
33
34
import CompilerOpts          (Options(..), TargetType (..), defaultOptions)
import CurryBuilder          (buildCurry)
import Modules               (loadAndCheckModule, checkModuleHeader)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
35
36
37

--- translate source file into HTML file with syntaxcoloring
--- @param sourcefilename
Björn Peemöller 's avatar
Björn Peemöller committed
38
source2html :: Options -> FilePath -> CYIO ()
39
source2html opts f = do
40
41
42
  let baseName   = takeBaseName f
      outDir     = fromMaybe (dropFileName f) $ optHtmlDir opts
      outFile    = outDir </> baseName ++ "_curry.html"
Björn Peemöller 's avatar
Björn Peemöller committed
43
  srcFile <- liftIO $ lookupCurryFile (optImportPaths opts) f
44
  program <- filename2program opts (fromMaybe f srcFile)
45
  liftIO $ writeFile outFile (program2html baseName program)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
46
47
48
49

--- @param importpaths
--- @param filename
--- @return program
Björn Peemöller 's avatar
Björn Peemöller committed
50
filename2program :: Options -> String -> CYIO Program
51
filename2program opts f = do
Björn Peemöller 's avatar
Björn Peemöller committed
52
  mbModule <- liftIO $ readModule f
53
  case mbModule of
Björn Peemöller 's avatar
Björn Peemöller committed
54
    Nothing  -> left [message $ text $ "Missing file: " ++ f]
55
    Just src -> do
Björn Peemöller 's avatar
Björn Peemöller committed
56
57
      typed   <- liftIO $ fromIO $ fullParse opts f src
      checked <- liftIO $ fromIO $ fullParse (opts { optTargetTypes = [UntypedAbstractCurry]}) f src
58
59
60
      let parsed = parse f src
          lexed  = lexSource f src
      return $ genProgram src [typed, checked, parsed] lexed
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
61

Björn Peemöller 's avatar
Björn Peemöller committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{- |Return the result of a syntactical analysis of the source program 'src'.
    The result is the syntax tree of the program (type 'Module'; see Module
    "CurrySyntax").
-}
parse :: FilePath -> String -> MessageM Module
parse fn src = parseModule fn src >>= genCurrySyntax
  where
  genCurrySyntax mod1 = do
    checked <- lift $ runEitherT $ checkModuleHeader defaultOptions fn mod1
    case checked of
      Left hdrErrs -> failWith $ show $ head hdrErrs
      Right mdl    -> return mdl

{- |Return the syntax tree of the source program 'src' (type 'Module'; see
    Module "CurrySyntax").after inferring the types of identifiers.
    'fullParse' always searches for standard Curry libraries in the path
    defined in the
    environment variable "PAKCSLIBPATH". Additional search paths can
    be defined using the argument 'paths'.
-}
fullParse :: Options -> FilePath -> String -> MessageIO Module
fullParse opts fn _ = do
  errs <- liftIO $ makeInterfaces opts fn
  if null errs
    then do
      checked <- liftIO $ runEitherT $ loadAndCheckModule opts fn
      case checked of
        Left errs'      -> failWith $ show $ head errs'
        Right (_, mod') -> return mod'
    else failWith $ show $ head errs

-- Generates interface files for importes modules, if they don't exist or
-- if they are not up-to-date.
makeInterfaces :: Options -> FilePath -> IO [Message]
makeInterfaces opts fn = do
  res <- runEitherT $ buildCurry opts fn
  case res of
    Left  errs -> return errs
    Right _    -> return []


Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-- generates htmlcode with syntax highlighting
-- @param modulname
-- @param a program
-- @return HTMLcode
program2html :: String -> Program -> String
program2html modulname codes =
  "<html>\n<head>\n<title>Module " ++
  modulname ++
  "</title>\n" ++
  "<link rel=\"stylesheet\" type=\"text/css\" href=\"currydoc.css\">" ++
  "</link>\n</head>\n" ++
  "<body style=\"font-family:'Courier New', Arial;\">\n<pre>\n" ++
  concatMap (code2html True . (\ (_, _, c) -> c)) codes ++
  "<pre>\n</body>\n</html>"

code2html :: Bool -> Code -> String
code2html ownClass code@(CodeWarning _ c) =
  (if ownClass then spanTag (code2class code) else id) (code2html False c)
code2html ownClass code@(Commentary _) =
  (if ownClass then spanTag (code2class code) else id)
    (replace '<' "<span>&lt</span>" (code2string code))
code2html ownClass c
125
  | isCall c && ownClass = maybe tag (addHtmlLink   tag) (getQualIdent c)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
126
  | isDecl c && ownClass = maybe tag (addHtmlAnchor tag) (getQualIdent c)
127
  | otherwise            = tag
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
128
129
130
131
132
133
134
  where tag = (if ownClass then spanTag (code2class c) else id)
                      (htmlQuote (code2string c))

spanTag :: String -> String -> String
spanTag [] str = str
spanTag cl str = "<span class=\"" ++ cl ++ "\">" ++ str ++ "</span>"

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
-- which code has which color
-- @param code
-- @return color of the code
code2class :: Code -> String
code2class (Keyword           _) = "keyword"
code2class (Space             _) = ""
code2class NewLine               = ""
code2class (ConstructorName k _) = "constructorname_"  ++ showLower k
code2class (Function        k _) = "function_" ++ showLower k
code2class (ModuleName        _) = "modulename"
code2class (Commentary        _) = "commentary"
code2class (NumberCode        _) = "numbercode"
code2class (StringCode        _) = "stringcode"
code2class (CharCode          _) = "charcode"
code2class (Symbol            _) = "symbol"
code2class (Identifier      k _) = "identifier_" ++ showLower k
code2class (TypeConstructor k _) = "typeconstructor_" ++ showLower k
code2class (CodeWarning     _ _) = "codewarning"
code2class (NotParsed         _) = "notparsed"

showLower :: Show a => a -> String
showLower = map toLower . show

Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
158
159
160
161
replace :: Char -> String -> String -> String
replace old new = foldr (\ x -> if x == old then (new ++) else ([x] ++)) ""

addHtmlAnchor :: String -> QualIdent -> String
162
163
addHtmlAnchor str qid = "<a name=\"" ++ anchor ++ "\"></a>" ++ str
  where anchor = string2urlencoded (show (unqualify qid))
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
164
165

addHtmlLink :: String -> QualIdent -> String
166
167
addHtmlLink str qid =
   let (maybeModIdent, ident) = (qidModule qid, qidIdent qid) in
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
168
169
170
171
172
   "<a href=\"" ++
   maybe "" (\ x -> show x ++ "_curry.html") maybeModIdent ++
   "#" ++
   string2urlencoded (show ident) ++
   "\">" ++
Björn Peemöller 's avatar
Björn Peemöller committed
173
   str ++
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
174
175
176
177
   "</a>"

isCall :: Code -> Bool
isCall (TypeConstructor TypeExport _) = True
178
179
isCall (TypeConstructor          _ _) = False
isCall (Identifier               _ _) = False
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
180
181
182
183
isCall code = not (isDecl code) && isJust (getQualIdent code)

isDecl :: Code -> Bool
isDecl (ConstructorName ConstrDecla _) = True
184
185
186
isDecl (Function FunDecl            _) = True
isDecl (TypeConstructor TypeDecla   _) = True
isDecl _                               = False
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

-- Translates arbitrary strings into equivalent urlencoded string.
string2urlencoded :: String -> String
string2urlencoded = id
{-
string2urlencoded [] = []
string2urlencoded (c:cs)
  | isAlphaNum c = c : string2urlencoded cs
  | c == ' '     = '+' : string2urlencoded cs
  | otherwise = show (ord c) ++ (if null cs then "" else ".") ++ string2urlencoded cs
-}

htmlQuote :: String -> String
htmlQuote [] = []
htmlQuote (c : cs)
  | c == '<'    = "&lt;"    ++ htmlQuote cs
  | c == '>'    = "&gt;"    ++ htmlQuote cs
  | c == '&'    = "&amp;"   ++ htmlQuote cs
  | c == '"'    = "&quot;"  ++ htmlQuote cs
  | c == '\228' = "&auml;"  ++ htmlQuote cs
  | c == '\246' = "&ouml;"  ++ htmlQuote cs
  | c == '\252' = "&uuml;"  ++ htmlQuote cs
  | c == '\196' = "&Auml;"  ++ htmlQuote cs
  | c == '\214' = "&Ouml;"  ++ htmlQuote cs
  | c == '\220' = "&Uuml;"  ++ htmlQuote cs
  | c == '\223' = "&szlig;" ++ htmlQuote cs
  | otherwise   = c : htmlQuote cs