CurryHtml.hs 6.77 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
16
17
module Html.CurryHtml (source2html) where

import Data.Maybe (fromMaybe, isJust)

18
19
20
21
22
import Curry.Base.Ident      (QualIdent (..), unqualify)
import Curry.Base.Message    (fromIO)
import Curry.Files.PathUtils
  (readModule, writeModule, lookupCurryFile, dropExtension, takeFileName)
import Curry.Syntax          (lexSource)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
23

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

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

--- translate source file into HTML file with syntaxcoloring
--- @param outputfilename
--- @param sourcefilename
source2html :: Options -> String -> IO ()
Björn Peemöller 's avatar
Björn Peemöller committed
34
source2html opts sourcefilename = do
35
36
  let imports = optImportPaths opts
      outputfilename = fromMaybe "" $ optOutput opts
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
37
38
39
40
41
      sourceprogname = dropExtension sourcefilename
      output' = if null outputfilename
                 then sourceprogname ++ "_curry.html"
                 else outputfilename
      modulname = takeFileName sourceprogname
42
  fullfname <- lookupCurryFile imports sourcefilename
43
  program <- filename2program opts (fromMaybe sourcefilename fullfname)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
44
45
46
47
48
49
50
  (if null outputfilename then writeModule True output'
                          else writeFile   output')
      (program2html modulname program)

--- @param importpaths
--- @param filename
--- @return program
51
52
53
54
55
56
filename2program :: Options -> String -> IO Program
filename2program opts filename = do
  mbModule <- readModule filename
  case mbModule of
    Nothing   -> abortWith ["Missing file: " ++ filename]
    Just cont -> do
Björn Peemöller 's avatar
Björn Peemöller committed
57
58
59
      typingParseRes <- fromIO $ fullParse opts filename cont
      fullParseRes   <- fromIO $ fullParse (opts { optTargetTypes = [UntypedAbstractCurry]}) filename cont
      let parseRes = parse filename cont
60
          lexRes   = lexSource filename cont
61
      return $ genProgram cont [typingParseRes, fullParseRes, parseRes] lexRes
Bjoern Peemoeller's avatar
Bjoern Peemoeller 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131



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

-- which code has which color
-- @param code
-- @return color of the code
code2class :: Code -> String
code2class (Keyword _) = "keyword"
code2class (Space _) = ""
code2class NewLine = ""
code2class (ConstructorName ConstrPattern _) = "constructorname_constrpattern"
code2class (ConstructorName ConstrCall _) = "constructorname_constrcall"
code2class (ConstructorName ConstrDecla _) = "constructorname_constrdecla"
code2class (ConstructorName OtherConstrKind _) = "constructorname_otherconstrkind"
code2class (Function InfixFunction _) = "function_infixfunction"
code2class (Function TypSig _) = "function_typsig"
code2class (Function FunDecl _) = "function_fundecl"
code2class (Function FunctionCall _) = "function_functioncall"
code2class (Function OtherFunctionKind _) = "function_otherfunctionkind"
code2class (ModuleName _) = "modulename"
code2class (Commentary _) = "commentary"
code2class (NumberCode _) = "numbercode"
code2class (StringCode _) = "stringcode"
code2class (CharCode _) = "charcode"
code2class (Symbol _) = "symbol"
code2class (Identifier IdDecl _) = "identifier_iddecl"
code2class (Identifier IdOccur _) = "identifier_idoccur"
code2class (Identifier UnknownId _) = "identifier_unknownid"
code2class (TypeConstructor TypeDecla _) = "typeconstructor_typedecla"
code2class (TypeConstructor TypeUse _) = "typeconstructor_typeuse"
code2class (TypeConstructor TypeExport _) = "typeconstructor_typeexport"
code2class (CodeWarning _ _) = "codewarning"
code2class (NotParsed _) = "notparsed"

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
  | isCall c && ownClass = maybe tag (addHtmlLink tag)   (getQualIdent c)
  | isDecl c && ownClass = maybe tag (addHtmlAnchor tag) (getQualIdent c)
  | otherwise = tag
  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>"

replace :: Char -> String -> String -> String
replace old new = foldr (\ x -> if x == old then (new ++) else ([x] ++)) ""

addHtmlAnchor :: String -> QualIdent -> String
Björn Peemöller 's avatar
Björn Peemöller committed
132
addHtmlAnchor str qualIdent = "<a name=\"" ++
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
133
  string2urlencoded (show (unqualify qualIdent)) ++
Björn Peemöller 's avatar
Björn Peemöller committed
134
  "\"></a>" ++ str
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
135
136

addHtmlLink :: String -> QualIdent -> String
137
138
addHtmlLink str qid =
   let (maybeModIdent, ident) = (qidModule qid, qidIdent qid) in
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
139
140
141
142
143
   "<a href=\"" ++
   maybe "" (\ x -> show x ++ "_curry.html") maybeModIdent ++
   "#" ++
   string2urlencoded (show ident) ++
   "\">" ++
Björn Peemöller 's avatar
Björn Peemöller committed
144
   str ++
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
   "</a>"

isCall :: Code -> Bool
isCall (TypeConstructor TypeExport _) = True
isCall (TypeConstructor _ _) = False
isCall (Identifier _ _) = False
isCall code = not (isDecl code) && isJust (getQualIdent code)

isDecl :: Code -> Bool
isDecl (ConstructorName ConstrDecla _) = True
isDecl (Function FunDecl _) = True
isDecl (TypeConstructor TypeDecla _) = True
isDecl _ = False

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