CurryHtml.hs 6.03 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
21
import Curry.Base.Ident      (QualIdent (..), unqualify)
import Curry.Base.Message    (fromIO)
import Curry.Files.PathUtils
22
  (readModule, lookupCurryFile, dropExtension, takeFileName)
23
import Curry.Syntax          (lexSource)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
24

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

27
import Base.Messages (abortWith)
28
29
import CompilerOpts  (Options(..), TargetType (..))
import Frontend      (parse, fullParse)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
30
31
32

--- translate source file into HTML file with syntaxcoloring
--- @param sourcefilename
33
34
35
36
37
38
39
40
41
42
source2html :: Options -> FilePath -> IO ()
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
  srcFile <- lookupCurryFile (optImportPaths opts) f
  program <- filename2program opts (fromMaybe f srcFile)
  writeFile outFile (program2html modulname program)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
43
44
45
46

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

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

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

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

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

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

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

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