CurryHtml.hs 6.02 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
23
import Curry.Files.Filenames (dropExtension, takeFileName)
import Curry.Files.PathUtils (readModule, lookupCurryFile)
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
source2html opts f = do
  let baseName   = dropExtension f
      modulname  = takeFileName baseName
38
      outFile    = baseName ++ "_curry.html"
Björn Peemöller 's avatar
Björn Peemöller committed
39
  srcFile <- liftIO $ lookupCurryFile (optImportPaths opts) f
40
  program <- filename2program opts (fromMaybe f srcFile)
Björn Peemöller 's avatar
Björn Peemöller committed
41
  liftIO $ writeFile outFile (program2html modulname program)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
42
43
44
45

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

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

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

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

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

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

isDecl :: Code -> Bool
isDecl (ConstructorName ConstrDecla _) = True
139
140
141
isDecl (Function FunDecl            _) = True
isDecl (TypeConstructor TypeDecla   _) = True
isDecl _                               = False
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
142
143
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

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