CurryHtml.hs 6.11 KB
Newer Older
Björn Peemöller 's avatar
Björn Peemöller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
{- |
    Module      :  $Header$
    Description :  Generating HTML documentation
    Copyright   :  (c) 2011, Björn Peemöller (bjp@informatik.uni-kiel.de)
    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

16
17
import Data.Char             (toLower)
import Data.Maybe            (fromMaybe, isJust)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
18

19
20
import Curry.Base.Ident      (QualIdent (..), unqualify)
import Curry.Base.Message    (fromIO)
Björn Peemöller 's avatar
Björn Peemöller committed
21
import Curry.Base.Pretty     (text)
22
import Curry.Files.PathUtils
23
  (readModule, lookupCurryFile, dropExtension, takeFileName)
24
import Curry.Syntax          (lexSource)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
25

Björn Peemöller 's avatar
Björn Peemöller committed
26
27
import Html.SyntaxColoring

Björn Peemöller 's avatar
Björn Peemöller committed
28
import Base.Messages
29
30
import CompilerOpts  (Options(..), TargetType (..))
import Frontend      (parse, fullParse)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
31
32
33

--- translate source file into HTML file with syntaxcoloring
--- @param sourcefilename
Björn Peemöller 's avatar
Björn Peemöller committed
34
source2html :: Options -> FilePath -> CYIO ()
35
36
37
38
39
40
source2html opts f = do
  let baseName   = dropExtension f
      modulname  = takeFileName baseName
      outFileOpt = fromMaybe "" $ optOutput opts
      outFile    = if null outFileOpt then baseName ++ "_curry.html"
                                      else outFileOpt
Björn Peemöller 's avatar
Björn Peemöller committed
41
  srcFile <- liftIO $ lookupCurryFile (optImportPaths opts) f
42
  program <- filename2program opts (fromMaybe f srcFile)
Björn Peemöller 's avatar
Björn Peemöller committed
43
  liftIO $ writeFile outFile (program2html modulname program)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
44
45
46
47

--- @param importpaths
--- @param filename
--- @return program
Björn Peemöller 's avatar
Björn Peemöller committed
48
filename2program :: Options -> String -> CYIO Program
49
filename2program opts f = do
Björn Peemöller 's avatar
Björn Peemöller committed
50
  mbModule <- liftIO $ readModule f
51
  case mbModule of
Björn Peemöller 's avatar
Björn Peemöller committed
52
    Nothing  -> left [message $ text $ "Missing file: " ++ f]
53
    Just src -> do
Björn Peemöller 's avatar
Björn Peemöller committed
54
55
      typed   <- liftIO $ fromIO $ fullParse opts f src
      checked <- liftIO $ fromIO $ fullParse (opts { optTargetTypes = [UntypedAbstractCurry]}) f src
56
57
58
      let parsed = parse f src
          lexed  = lexSource f src
      return $ genProgram src [typed, checked, parsed] lexed
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

-- 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
82
  | isCall c && ownClass = maybe tag (addHtmlLink   tag) (getQualIdent c)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
83
  | isDecl c && ownClass = maybe tag (addHtmlAnchor tag) (getQualIdent c)
84
  | otherwise            = tag
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
85
86
87
88
89
90
91
  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>"

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
-- 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
115
116
117
118
replace :: Char -> String -> String -> String
replace old new = foldr (\ x -> if x == old then (new ++) else ([x] ++)) ""

addHtmlAnchor :: String -> QualIdent -> String
119
120
addHtmlAnchor str qid = "<a name=\"" ++ anchor ++ "\"></a>" ++ str
  where anchor = string2urlencoded (show (unqualify qid))
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
121
122

addHtmlLink :: String -> QualIdent -> String
123
124
addHtmlLink str qid =
   let (maybeModIdent, ident) = (qidModule qid, qidIdent qid) in
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
125
126
127
128
129
   "<a href=\"" ++
   maybe "" (\ x -> show x ++ "_curry.html") maybeModIdent ++
   "#" ++
   string2urlencoded (show ident) ++
   "\">" ++
Björn Peemöller 's avatar
Björn Peemöller committed
130
   str ++
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
131
132
133
134
   "</a>"

isCall :: Code -> Bool
isCall (TypeConstructor TypeExport _) = True
135
136
isCall (TypeConstructor          _ _) = False
isCall (Identifier               _ _) = False
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
137
138
139
140
isCall code = not (isDecl code) && isJust (getQualIdent code)

isDecl :: Code -> Bool
isDecl (ConstructorName ConstrDecla _) = True
141
142
143
isDecl (Function FunDecl            _) = True
isDecl (TypeConstructor TypeDecla   _) = True
isDecl _                               = False
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

-- 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