1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
132
133
134
135
136
137
138
139
140
141
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
----------------------------------------------------------------------
--- Operations to generate documentation in HTML format.
---
--- @author Michael Hanus, Jan Tikovsky
--- @version October 2020
----------------------------------------------------------------------

{-# OPTIONS_CYMAKE -Wno-incomplete-patterns #-}

module CurryDoc.Html where

import FilePath
import FileGoodies     ( getFileInPath )
import List
import Char
import Sort
import Time
import Markdown
import Maybe

import AbstractCurry.Types
import AbstractCurry.Files
import AbstractCurry.Select
import AbstractCurry.Build
import AbstractCurry.Pretty
import Analysis.TotallyDefined(Completeness(..))
import qualified FlatCurry.Types as FC
import qualified FlatCurry.Goodies as FCG
import HTML.Base
import HTML.Styles.Bootstrap3 ( bootstrapPage, glyphicon, homeIcon )
import HTML.CategorizedList
import System.CurryPath       ( getLoadPathForModule )
import System.FrontendExec    ( FrontendTarget (..), callFrontendWithParams
                              , defaultParams, setHtmlDir, setQuiet )
import Text.Pretty            ( showWidth, empty )

import CurryDoc.AnaInfo
import CurryDoc.Options
import CurryDoc.Read
import CurryDoc.Config

infixl 0 `withTitle`

--------------------------------------------------------------------------
-- Generates the documentation of a module in HTML format where the comments
-- are already analyzed.
generateHtmlDocs :: DocOptions -> AnaInfo -> String -> String
                 -> [(SourceLine,String)] -> IO String
generateHtmlDocs opts anainfo modname modcmts progcmts = do
  acyname <- getLoadPathForModule modname >>=
             getFileInPath (abstractCurryFileName modname) [""]
  putStrLn $ "Reading AbstractCurry program \""++acyname++"\"..."
  (CurryProg _ imports _ _ _ types functions ops) <-
                                                readAbstractCurryFile acyname
  let
    exptypes   = filter isExportedType types
    expfuns    = filter isExportedFun  functions
    propspecs  = map cmtFunc2Func
                     (filter (\fd -> isProperty fd || isSpecFunc fd) functions)
    navigation =
      [ bold [htxt "Exported names:"]
      , genHtmlExportIndex (map tName      exptypes)
                           (getExportedCons   types)
                           (getExportedFields types)
                           (map fName       expfuns)
      , anchored "imported_modules" [bold [htxt "Imported modules:"]]
      , ulist (map (\i -> [href (docURL opts i ++ ".html") [htxt i]]) imports)
        `addClass` "nav nav-sidebar"
      ]
    content =
      genHtmlModule opts modcmts ++
      [ h2 [htxt "Summary of exported operations:"]
      , borderedTable (map (genHtmlFuncShort opts progcmts anainfo) expfuns)
      ] ++
      ifNotNull exptypes (\tys ->
         [anchoredSection "exported_datatypes"
           (h2 [htxt "Exported datatypes:"] : hrule :
           concatMap (genHtmlType opts progcmts) tys)]) ++
      [anchoredSection "exported_operations"
         (h2 [htxt "Exported operations:"] :
          map (genHtmlFunc opts modname progcmts
                 (attachProperties2Funcs propspecs progcmts) anainfo ops)
              expfuns)
      ]
  mainPage title [htmltitle] (lefttopmenu types functions) rightTopMenu
           navigation content
 where
  title = "Module " ++ modname

  htmltitle = h1 [ htxt "Module "
                 , href (modname ++ "_curry.html") [htxt modname]
                 ]

  lefttopmenu :: [CTypeDecl] -> [CFuncDecl] -> [[HtmlExp]]
  lefttopmenu ts fs
    = [[href "?" [htxt title]], [href "#imported_modules" [htxt "Imports"]]]
   ++ ifNotNull ts (const [[href "#exported_datatypes"  [htxt "Datatypes" ]]])
   ++ ifNotNull fs (const [[href "#exported_operations" [htxt "Operations"]]])

  cmtFunc2Func fdecl = case fdecl of
                         CmtFunc _ qn a v tExp rs -> CFunc qn a v tExp rs
                         _                        -> fdecl

-- Datatype to classify the kind of information attached to a function:
data FuncAttachment = Property | PreCond | PostCond | SpecFun
 deriving Eq

-- Associate the properties or contracts (first argument)
-- to functions according to their positions and name in the source code
-- (we assume that they follow the actual function definitions).
-- Each property or contract is represented by its kind ('FuncAttachment'),
-- its name, and its documentation (HTML document).
attachProperties2Funcs :: [CFuncDecl] -> [(SourceLine,String)]
                       -> [(String,[(FuncAttachment,String,[HtmlExp])])]
attachProperties2Funcs _ [] = []
attachProperties2Funcs props ((sourceline,_) : slines) =
  case sourceline of
    FuncDef fn -> let (fprops,rslines) = span isPropFuncDef slines
                   in (fn, showContracts fn ++ concatMap showProp fprops) :
                      attachProperties2Funcs props rslines
    _          -> attachProperties2Funcs props slines
 where
  propNames = map (snd . funcName) props

  showProp (FuncDef fn,_) =
    let propdecl = fromJust (find (\fd -> snd (funcName fd) == fn) props)
     in if isProperty propdecl
        then map (\rhs -> (Property, fn,
                           [code [htxt $ prettyWith (ppCRhs empty) rhs]]))
                 (map ruleRHS (funcRules propdecl))
        else []

  showContracts fn =
    showContract (fn++"'pre")  showPreCond ++
    showContract (fn++"'post") showPostCond ++
    showContract (fn++"'spec") showSpec

  showContract fnsuff formatrule =
    maybe []
          (\contractdecl -> showRulesWith formatrule fnsuff contractdecl)
          (find (\fd -> snd (funcName fd) == fnsuff) props)

  showRulesWith formatrule fnsuff (CFunc qn@(mn,fn) ar _ ftype rules) =
    let stripSuffix = reverse . tail . dropWhile (/='\'') . reverse
     in map (formatrule fnsuff qn (mn,stripSuffix fn)
              . etaExpand ar (length (argTypes (typeOfQualType ftype)))) rules

  -- eta expand simple rules for more reasonable documentation
  etaExpand arity tarity rule = case rule of
    CRule ps (CSimpleRhs exp ldecls) ->
      if arity == tarity
      then rule
      else let evars = map (\i -> (i,"x"++show i)) [(arity+1) .. tarity]
            in CRule (ps ++ map CPVar evars)
                     (CSimpleRhs (foldl CApply exp (map CVar evars)) ldecls)
    _ -> rule -- don't do it for complex rules

  showPreCond fnpre qp qn rule = case rule of
   CRule _ (CSimpleRhs _ _) ->
     let (lhs,rhs) = break (=='=') (prettyRule qn rule)
      in (PreCond, fnpre, [code [htxt $ "(" ++ stripSpaces lhs ++ ")"],
                           italic [htxt " requires "],
                           code [htxt (safeTail rhs)]])
   _ -> -- we don't put must effort to format complex preconditions:
        (PreCond, fnpre, [code [htxt $ prettyRule qp rule]])

  showPostCond fnpost qp qn rule = case rule of
   CRule ps (CSimpleRhs _ _) ->
     let (_,rhs) = break (=='=') (prettyRule qn rule)
      in (PostCond, fnpost,
          [code [htxt $ prettyWith ppCPattern (last ps) ++ " = " ++
                        prettyWith ppCPattern
                                   (CPComb qn (take (length ps - 1) ps)) ],
                 italic [htxt " satisfies "],
                 code [htxt (safeTail rhs)]])
   _ -> -- we don't put must effort to format complex postconditions:
        (PostCond, fnpost, [code [htxt $ prettyRule qp rule]])

  showSpec fnspec qp qn rule = case rule of
   CRule _ (CSimpleRhs _ _) ->
     let (lhs,rhs) = break (=='=') (prettyRule qn rule)
      in (SpecFun, fnspec, [code [htxt $ "(" ++ stripSpaces lhs ++ ")"],
                            italic [htxt " is equivalent to "],
                            code [htxt (safeTail rhs)]])
   _ -> -- we don't put must effort to format complex specifications:
        (SpecFun, fnspec, [code [htxt $ prettyRule qp rule]])

  prettyWith ppfun = showWidth 78 . ppfun prettyOpts
  prettyRule qn rl = showWidth 78 (ppCRule prettyOpts qn rl)
  prettyOpts       = setNoQualification defaultOptions

  safeTail xs = if null xs then xs else tail xs

  isPropFuncDef (sline,_) =
    case sline of FuncDef fn -> fn `elem` propNames
                  _          -> False


--- Translate a documentation comment to HTML and use markdown translation
--- if necessary
--- @return: either a paragraph (`<p>`) element or an empty list.
docComment2HTML :: DocOptions -> String -> [HtmlExp]
docComment2HTML opts cmt
  | null cmt          = []
  | withMarkdown opts = markdownText2HTML (replaceIdLinks opts cmt)
  | otherwise         = [par [HtmlText (replaceIdLinks opts cmt)]]

-- replace identifier hyperlinks in a string (i.e., enclosed in single quotes)
-- by HTML hyperrefences:
replaceIdLinks :: DocOptions -> String -> String
replaceIdLinks opts str = case str of
  [] -> []
  ('\\':'\'':cs) -> '\'' : replaceIdLinks opts cs
  (c:cs) -> if c=='\'' then tryReplaceIdLink [] cs
                       else c : replaceIdLinks opts cs
 where
  tryReplaceIdLink ltxt [] = '\'' : reverse ltxt
  tryReplaceIdLink ltxt (c:cs)
   | isSpace c
   = '\'' : reverse ltxt ++ c : replaceIdLinks opts cs -- no space in id
   | c == '\''
   = checkId (reverse ltxt) ++ replaceIdLinks opts cs
   | otherwise
   = tryReplaceIdLink (c:ltxt) cs

  checkId s =
    if ' ' `elem` s
    then '\'' : s ++ ['\'']
    else let (md,dotfun) = break (=='.') s
          in "<code><a href=\"" ++
             (if null dotfun then '#':s
                             else docURL opts md ++ ".html#" ++ tail dotfun) ++
             "\">"++s++"</a></code>"

-- generate HTML index for all exported names:
genHtmlExportIndex :: [String] -> [String] -> [String] -> [String] -> HtmlExp
genHtmlExportIndex exptypes expcons expfields expfuns =
  HtmlStruct "ul" [("class","nav nav-sidebar")]
    (concatMap (\ (htmlnames,cattitle) ->
                 if null htmlnames
                 then []
                 else HtmlStruct "li" [("class","nav-header")] [htxt cattitle] :
                      map (HtmlStruct "li" []) htmlnames)
            [(htmltypes,"Datatypes:"),
             (htmlcons ,"Constructors:"),
             (htmlfields,"Fields:"),
             (htmlfuns ,"Operations:")])
 where
  htmltypes  = map (\n->[href ('#':n) [htxt n]])
                   (nub (sortStrings exptypes))
  htmlcons   = map (\n->[href ('#':n++"_CONS") [htxt n]])
                   (nub (sortStrings expcons))
  htmlfields = map (\n->[href ('#':n++"_FIELD") [htxt n]])
                  (nub (sortStrings expfields))
  htmlfuns   = map (\n->[href ('#':n) [htxt n]])
                   (nub (sortStrings expfuns))

tName :: CTypeDecl -> String
tName = snd . typeName

fName :: CFuncDecl -> String
fName = snd . funcName

cName :: CConsDecl -> String
cName = snd . consName

fldName :: CFieldDecl -> String
fldName (CField (_,name) _ _) = name

isExportedType :: CTypeDecl -> Bool
isExportedType = (== Public) . typeVis

isExportedCons :: CConsDecl -> Bool
isExportedCons = (== Public) . consVis

isExportedFun :: CFuncDecl -> Bool
isExportedFun = (== Public) . funcVis

isExportedField :: CFieldDecl -> Bool
isExportedField (CField _ vis _) = vis == Public

-- extract the names of all exported constructors
getExportedCons :: [CTypeDecl] -> [String]
getExportedCons = map cName . filter isExportedCons . concatMap typeCons

-- extract the names of all exported fields
getExportedFields :: [CTypeDecl] -> [String]
getExportedFields = map fldName . filter isExportedField . concatMap getFields
                  . concatMap typeCons
 where
  getFields (CCons   _ _ _ _ _ ) = []
  getFields (CRecord _ _ _ _ fs) = fs

-- Is a function definition a property to be documented?
isProperty :: CFuncDecl -> Bool
isProperty fdecl = fst (funcName fdecl)
                     `notElem` [easyCheckModule, propModule, propTypesModule]
                && isPropType (typeOfQualType (funcType fdecl))
 where
  isPropType :: CTypeExpr -> Bool
  isPropType ct =  ct == baseType (propTypesModule,"PropIO") -- I/O test?
                || resultType ct == baseType (propTypesModule,"Prop")

  easyCheckModule = "Test.EasyCheck"
  propModule      = "Test.Prop"
  propTypesModule = "Test.Prop.Types"

-- Is a function definition part of a specification, i.e.,
-- a full specification (suffix 'spec), a precondition (suffix 'pre),
-- or a postcondition (suffix 'post)?
isSpecFunc :: CFuncDecl -> Bool
isSpecFunc fdecl =
  let rfname = reverse (snd (funcName fdecl))
   in any (`isPrefixOf` rfname) ["ceps'","erp'","tsop'"]

--- generate HTML documentation for a module:
genHtmlModule :: DocOptions -> String -> [HtmlExp]
genHtmlModule docopts modcmts =
  let (maincmt,avcmts) = splitComment modcmts
   in docComment2HTML docopts maincmt ++
      map (\a->par [bold [htxt "Author: "], htxt a])
          (getCommentType "author" avcmts) ++
      map (\a->par [bold [htxt "Version: "], htxt a])
          (getCommentType "version" avcmts)

ulistOrEmpty :: [[HtmlExp]] -> [HtmlExp]
ulistOrEmpty items | null items = []
                   | otherwise  = [ulist items]

-- generate the html documentation for given comments ("param", "return",...)
ifNotNull :: [a] -> ([a] -> [b]) -> [b]
ifNotNull cmt genDoc
  | null cmt  = []
  | otherwise = genDoc cmt

--- generate HTML documentation for a datatype if it is exported:
genHtmlType :: DocOptions -> [(SourceLine,String)] -> CTypeDecl -> [HtmlExp]
genHtmlType docopts progcmts (CType (_,tcons) _ tvars constrs _) =
  let (datacmt,consfldcmts) = splitComment (getDataComment tcons progcmts)
   in    [ anchored tcons [style "typeheader" [htxt tcons]] ]
      ++ docComment2HTML docopts datacmt
      ++ [par [explainCat "Constructors:"]]
      ++ ulistOrEmpty (map (genHtmlCons docopts consfldcmts tcons tvars fldCons)
                           (filter isExportedCons constrs))
      ++ [hrule]
 where
  expFields = [f | CRecord _ _ _ _ fs <- constrs, f <- fs, isExportedField f]
  fldCons   = [ (fn,cn) | f@(CField (_,fn) _ _) <- expFields
              , CRecord _ _ (_,cn) _ fs <- constrs, f `elem` fs
              ]
genHtmlType docopts progcmts (CTypeSyn (tcmod,tcons) _ tvars texp) =
  let (typecmt,_) = splitComment (getDataComment tcons progcmts)
   in    [ anchored tcons [style "typeheader" [htxt tcons]] ]
      ++ docComment2HTML docopts typecmt
      ++ [ par [explainCat "Type synonym:"
         , nbsp
         ,
            if tcons=="String" && tcmod=="Prelude"
            then code [htxt "String = [Char]"]
            else code [HtmlText
                        (tcons ++ concatMap (\(i,_) -> [' ',chr (97+i)]) tvars ++
                         " = " ++ showType docopts tcmod False texp)]]
         , hrule
         ]
genHtmlType docopts progcmts t@(CNewType (_,tcons) _ tvars constr _) =
  let (datacmt,consfldcmts) = splitComment (getDataComment tcons progcmts)
   in if isExportedCons constr
        then    [anchored tcons [style "typeheader" [htxt tcons]] ]
             ++ docComment2HTML docopts datacmt
             ++ [par [explainCat "Constructor:"]]
             ++ ulistOrEmpty [genHtmlCons docopts consfldcmts tcons tvars fldCons constr]
             ++ [hrule]
        else []
 where
  cn      = cName constr
  fldCons = map (\fn -> (fn,cn)) (getExportedFields [t])

--- generate HTML documentation for a constructor if it is exported:
genHtmlCons :: DocOptions -> [(String,String)] -> String -> [CTVarIName]
             -> [(String,String)] -> CConsDecl -> [HtmlExp]
genHtmlCons docopts consfldcmts tcons tvars _
            (CCons _ _ (cmod,cname) _ argtypes) =
    anchored (cname ++ "_CONS")
      [code [opnameDoc [htxt cname],
             HtmlText (" :: " ++
                       concatMap (\t -> " "++showType docopts cmod True t++" -> ")
                                 argtypes ++
                       tcons ++ concatMap (\(i,_) -> [' ',chr (97+i)]) tvars)]] :
      maybe []
            (\ (_,cmt) -> htxt " : " : removeTopPar (docComment2HTML docopts
                                                    (removeDash cmt)))
            (getConsComment conscmts cname)
 where
  conscmts = getCommentType "cons" consfldcmts
genHtmlCons docopts consfldcmts tcons tvars fldCons
            (CRecord _ _ (cmod,cname) _ fields) =
    anchored (cname ++ "_CONS")
      [code [opnameDoc [htxt cname],
             HtmlText (" :: " ++
                       concatMap (\t -> " " ++ showType docopts cmod True t ++
                                        " -> ")
                                 argtypes ++
                       tcons ++ concatMap (\(i,_) -> [' ',chr (97+i)]) tvars)]] :
      (maybe []
            (\ (_,cmt) -> htxt " : " : removeTopPar (docComment2HTML docopts
                                                    (removeDash cmt)))
            (getConsComment conscmts cname)) ++
      par [explainCat "Fields:"] :
      ulistOrEmpty (map (genHtmlField docopts fldcmts cname fldCons)
                        (filter isExportedField fields))
 where
  argtypes = map (\(CField _ _ t) -> t) fields
  conscmts = getCommentType "cons" consfldcmts
  fldcmts  = getCommentType "field" consfldcmts

-- generate HTML documentation for record fields
genHtmlField :: DocOptions -> [String] -> String -> [(String,String)]
             -> CFieldDecl -> [HtmlExp]
genHtmlField docopts fldcmts cname fldCons (CField (fmod,fname) _ ty)
  | withAnchor fname = [anchored (fname ++ "_FIELD") html]
  | otherwise        = html
 where
  withAnchor f = maybe False (== cname) (lookup f fldCons)
  html         = [ code [opnameDoc [htxt fname]
                 , HtmlText (" :: " ++ showType docopts fmod True ty)]
                 ] ++ maybe []
                            (\ (_,cmt) -> htxt " : " : removeTopPar
                               (docComment2HTML docopts (removeDash cmt)))
                            (getConsComment fldcmts fname)

-- generate short HTML documentation for a function:
genHtmlFuncShort :: DocOptions -> [(SourceLine,String)] -> AnaInfo -> CFuncDecl
                 -> [[HtmlExp]]
genHtmlFuncShort docopts progcmts anainfo
                 (CFunc (fmod,fname) _ _ ftype _) =
 [[code [opnameDoc
            [anchored (fname ++ "_SHORT")
                      [href ('#':fname) [htxt (showId fname)]]],
         HtmlText (" :: " ++ showQualType docopts fmod ftype)],
     nbsp, nbsp]
     ++ genFuncPropIcons anainfo (fmod,fname) ++
  [breakline] ++
   removeTopPar
      (docComment2HTML docopts
         (firstSentence (fst (splitComment
                                (getFuncComment fname progcmts)))))]
genHtmlFuncShort docopts progcmts anainfo (CmtFunc _ n a vis ftype rules) =
  genHtmlFuncShort docopts progcmts anainfo (CFunc n a vis ftype rules)

-- generate HTML documentation for a function:
genHtmlFunc :: DocOptions -> String -> [(SourceLine,String)]
            -> [(String,[(FuncAttachment,String,[HtmlExp])])] -> AnaInfo
            -> [COpDecl] -> CFuncDecl -> HtmlExp
genHtmlFunc docopts modname progcmts funcattachments anainfo ops
            (CmtFunc _ n a vis ftype rules) =
  genHtmlFunc docopts modname progcmts funcattachments anainfo ops
              (CFunc n a vis ftype rules)
genHtmlFunc docopts modname progcmts funcattachments anainfo ops
            (CFunc (fmod,fname) _ _ ftype rules) =
  let (funcmt,paramcmts) = splitComment (getFuncComment fname progcmts)
   in anchoredDiv fname
       [borderedTable [[
         [par $
           [code [opnameDoc [showCodeHRef fname],
                  HtmlText (" :: "++ showQualType docopts fmod ftype)],
            nbsp, nbsp] ++
           genFuncPropIcons anainfo (fmod,fname)] ++
         docComment2HTML docopts funcmt ++
         genParamComment paramcmts ++
         -- show contracts and properties (if present):
         showAttachments "Precondition"  PreCond  ++
         showAttachments "Postcondition" PostCond ++
         showAttachments "Specification" SpecFun  ++
         showAttachments "Properties"    Property ++
         -- show further infos for this function, if present:
         (if null furtherInfos
          then []
          else [dlist [([explainCat "Further infos:"],
                        [ulist furtherInfos])]] )]]]
 where
  showCodeHRef fn = href (modname++"_curry.html#"++fn) [htxt (showId fn)]

  showAttachments aname attachkind =
   let attachfuns = filter (\ (k,_,_) -> k==attachkind)
                           (maybe [] id (lookup fname funcattachments))
    in if null attachfuns then [] else
        [dlist [([explainCat (aname++":")],
                 [par (intercalate [breakline]
                         (map (\ (_,pn,pc) -> pc ++
                                 [nbsp, htxt "(", showCodeHRef pn, htxt ")"])
                              attachfuns))])]]

  furtherInfos = genFuncPropComments anainfo (fmod,fname) rules ops

  genParamComment paramcmts =
    let params = map (span isIdChar) (getCommentType "param" paramcmts)
        ret    = getCommentType "return" paramcmts
     in  ifNotNull params (\parCmts ->
          [ dlist [([explainCat "Example call:"],
                    [code [htxt (showCall fname (map fst params))]])
                  ]
          , dlist [([explainCat "Parameters:"],
                    [ulist (map (\ (parid,parcmt) ->
                           [code [htxt parid], htxt " : "] ++
                           removeTopPar (docComment2HTML docopts
                                                         (removeDash parcmt)))
                       parCmts)])]
          ])
      ++ ifNotNull ret (\retCmt -> [dlist (map (\rescmt ->
          ([explainCat "Returns:"],
           removeTopPar (docComment2HTML docopts rescmt))) retCmt)])

  showCall f params =
    if isAlpha (head f) || length params /= 2
    then "(" ++ showId f ++ concatMap (" "++) params ++ ")"
    else "(" ++ params!!0 ++ " " ++ f ++ " " ++ params!!1 ++ ")"

-- remove initial dash sign (of a parameter comment)
removeDash :: String -> String
removeDash s = let ds = dropWhile isSpace s in
  if take 2 ds == "- " then dropWhile isSpace (drop 2 ds)
                       else ds

-- remove a single top-level paragraph in HTML expressions:
removeTopPar :: [HtmlExp] -> [HtmlExp]
removeTopPar hexps = case hexps of
  [HtmlStruct "p" [] hs] -> hs
  _ -> hexps

--------------------------------------------------------------------------
--- Generates icons for particular properties of functions.
genFuncPropIcons :: AnaInfo -> QName -> [HtmlExp]
genFuncPropIcons anainfo fname =
   [detPropIcon, nbsp]
 where
   --(non)deterministically defined property:
   detPropIcon =
    if getNondetInfo anainfo fname
    then href "index.html#nondet_explain" [nondetIcon]
    else href "index.html#det_explain"    [detIcon]

--------------------------------------------------------------------------
--- Generates further textual infos about particular properties
--- of a function. The result is a list of HTML expressions to be
--- formatted (if not empty) as some HTML list.
genFuncPropComments :: AnaInfo -> QName -> [CRule] -> [COpDecl] -> [[HtmlExp]]
genFuncPropComments anainfo fname rules ops =
   filter (not . null)
          [genFixityInfo fname ops,
           completenessInfo,
           indeterminismInfo,
           opcompleteInfo,
           externalInfo rules]
 where
   -- comment about the definitional completeness of a function:
   completenessInfo = let ci = getCompleteInfo anainfo fname in
     if ci == Complete
     then []
     else [htxt (if ci == InComplete
                 then "partially defined"
                 else
             "partially defined in each disjunction (but might be complete)")]

   -- comment about the indeterminism of a function:
   indeterminismInfo = if getIndetInfo anainfo fname
                       then [htxt "might behave indeterministically"]
                       else []

   -- comment about the indeterminism of a function:
   opcompleteInfo =
      if getOpCompleteInfo anainfo fname
      then [htxt "solution complete, i.e., able to compute all solutions"]
      else []

   -- comment about the external definition of a function:
   externalInfo []    = [htxt "externally defined"]
   externalInfo (_:_) = []


--- Generates a comment about the associativity and precedence
--- if the name is defined as an infix operator.
genFixityInfo :: QName -> [COpDecl] -> [HtmlExp]
genFixityInfo fname ops =
    concatMap (\(COp n fix prec)->
                  if n == fname
                  then [htxt ("defined as "++showFixity fix++
                              " infix operator with precedence "++show prec)]
                  else [])
              ops
 where
  showFixity CInfixOp  = "non-associative"
  showFixity CInfixlOp = "left-associative"
  showFixity CInfixrOp = "right-associative"


--------------------------------------------------------------------------
-- Pretty printer for qualified types in Curry syntax:
showQualType :: DocOptions -> String -> CQualTypeExpr -> String
showQualType opts mod (CQualType ctxt texp) =
  unwords [showContext opts mod ctxt, showType opts mod False texp]

showContext :: DocOptions -> String -> CContext -> String
showContext _ _ (CContext []) = ""
showContext opts mod (CContext [clscon]) =
  showConstraint opts mod clscon ++ " =>"
showContext opts mod (CContext ctxt@(_:_:_)) =
  brackets True (intercalate ", " (map (showConstraint opts mod) ctxt)) ++ " =>"

--- Pretty-print a single class constraint.
showConstraint :: DocOptions -> String -> CConstraint -> String
showConstraint opts mod (cn,texp) =
  showTypeCons opts mod cn ++ " " ++ showType opts mod True texp

-- Pretty printer for type expressions in Curry syntax:
-- second argument is True iff brackets must be written around complex types
showType :: DocOptions -> String -> Bool -> CTypeExpr -> String
showType opts mod nested texp = case texp of
  CTVar (i,_) -> [chr (97+i)] -- TODO: use name given in source program instead?
  CFuncType t1 t2 ->
    brackets nested (showType opts mod (isFunctionalType t1) t1 ++ " -&gt; " ++
                     showType opts mod False t2)
  CTCons tc -> showTConsType opts mod nested tc []
  CTApply t1 t2 ->
       maybe (brackets nested $
                showType opts mod True t1 ++ " " ++ showType opts mod True t2)
             (\ (tc,ts) -> showTConsType opts mod nested tc ts)
             (tconsArgsOfType texp)

showTConsType :: DocOptions -> String -> Bool -> QName -> [CTypeExpr] -> String
showTConsType opts mod nested tc ts
 | ts==[]  = showTypeCons opts mod tc
 | tc==("Prelude","[]") && (head ts == CTCons ("Prelude","Char"))
   = "String"
 | tc==("Prelude","[]")
   = "[" ++ showType opts mod False (head ts) ++ "]" -- list type
 | take 2 (snd tc) == "(,"                      -- tuple type
   = "(" ++ concat (intersperse "," (map (showType opts mod False) ts)) ++ ")"
 | otherwise
   = brackets nested
      (showTypeCons opts mod tc ++ " " ++
       concat (intersperse " " (map (showType opts mod True) ts)))

showTypeCons :: DocOptions -> String -> QName -> String
showTypeCons opts mod (mtc,tc) =
  if mtc == "Prelude"
  then tc --"<a href=\"Prelude.html#"++tc++"\">"++tc++"</a>"
  else
    if mod == mtc
    then "<a href=\"#"++tc++"\">"++tc++"</a>"
    else "<a href=\""++docURL opts mtc++".html#"++tc++"\">"++tc++"</a>"


--------------------------------------------------------------------------
-- translate source file into HTML file with syntax coloring
translateSource2ColoredHtml :: String -> String -> IO ()
translateSource2ColoredHtml docdir modname = do
    let output = docdir </> modname++"_curry.html"
    putStrLn ("Writing source file as HTML to \""++output++"\"...")
    callFrontendWithParams HTML
      (setQuiet True (setHtmlDir docdir defaultParams)) modname

-- translate source file into HTML file with anchors for each function:
translateSource2AnchoredHtml :: String -> String -> IO ()
translateSource2AnchoredHtml docdir modname =
 do putStrLn ("Writing source file as HTML to \""++docdir++"/"++modname++"_curry.html\"...")
    prog <- readFile (modname++".curry")
    writeFile (docdir </> modname++"_curry.html")
              (showPageWithDocStyle (modname++".curry")
                  [HtmlStruct "pre" []
                     [HtmlText (addFuncAnchors [] (lines prog))]])

-- add the anchors to the classified lines and translate back:
-- first argument: list of already added anchors
-- second argument: list of source lines
addFuncAnchors :: [String] -> [String] -> String
addFuncAnchors _ [] = ""
addFuncAnchors ancs (sl : sls) = let id1 = getFirstId sl in
  if id1=="" ||
     id1 `elem` ["data","type","import","module","infix","infixl","infixr"]
  then htmlQuote (sl++"\n") ++ addFuncAnchors ancs sls
  else if id1 `elem` ancs
       then (sl++"\n") ++ addFuncAnchors ancs sls
       else "<a name=\""++id1++"\"></a>"
            ++ htmlQuote (sl++"\n")
            ++ addFuncAnchors (id1:ancs) sls


--------------------------------------------------------------------------
-- generate the index page for the documentation directory:
genMainIndexPage :: DocOptions -> String -> [String] -> IO ()
genMainIndexPage docopts docdir modnames =
 do putStrLn ("Writing index page to \""++docdir++"/index.html\"...")
    simplePage "Documentation of Curry modules"
               (Just pageTitle)
               allConsFuncsMenu (indexPage modnames)
     >>= writeFile (docdir++"/index.html")
 where
  pageTitle = if not (null (mainTitle docopts))
                then [htxt (mainTitle docopts)]
                else if length modnames == 1
                      then [htxt "Documentation of the Curry program ",
                            href (head modnames ++ ".html")
                                 [htxt (head modnames)]]
                      else [htxt "Documentation of Curry programs"]

allConsFuncsMenu :: [[HtmlExp]]
allConsFuncsMenu =
  [[href "findex.html" [htxt "All operations"]],
   [href "cindex.html" [htxt "All constructors"]]]

indexPage :: [String] -> [HtmlExp]
indexPage modnames =
  (if null modnames
   then []
   else [h2 [htxt "Modules:"],
         ulist (map (\m->[href (m++".html") [htxt m]])
                    (mergeSortBy leqStringIgnoreCase modnames))])
  ++ [explainIcons]

-- Paragraph to explain the meaning of the icons:
explainIcons :: HtmlExp
explainIcons =
  anchoredSection "explain_icons"
    [h2 [htxt "Explanations of the icons used in the documentation:"],
     table
       [[[anchor "det_explain" [detIcon]],[nbsp],
         [htxt " Operation is deterministic, i.e., defined by exclusive rules",
          htxt " and depend only on deterministic operations"]]
       ,[[anchor "nondet_explain" [nondetIcon]],[nbsp],
         [htxt " Operation might be non-deterministic, i.e., it is defined by",
          htxt " overlapping rules or depend on non-deterministic operations"]]
--        ,[[anchor "rigid_explain" [rigidIcon]],[nbsp],
--          [htxt " Operation is rigid"]]
--        ,[[anchor "flex_explain" [flexibleIcon]],[nbsp],
--          [htxt " Operation is flexible"]]
--        ,[[anchor "flexrigid_explain" [flexrigidIcon]],[nbsp],
--          [htxt " Operation is partially flexible and partially rigid"]]
       ]
    ]

--------------------------------------------------------------------------
-- generate the function index page for the documentation directory:
genFunctionIndexPage :: DocOptions -> String -> [FC.FuncDecl] -> IO ()
genFunctionIndexPage opts docdir funs = do
  putStrLn ("Writing operation index page to \""++docdir++"/findex.html\"...")
  simplePage "Index to all operations" Nothing allConsFuncsMenu
             (htmlFuncIndex opts (sortNames expfuns))
    >>= writeFile (docdir++"/findex.html")
 where
   expfuns = map FCG.funcName $ filter ((== FC.Public) . FCG.funcVisibility) funs

htmlFuncIndex :: DocOptions -> [QName] -> [HtmlExp]
htmlFuncIndex opts = categorizeByItemKey . map (showModNameRef opts)

showModNameRef :: DocOptions -> QName -> (String,[HtmlExp])
showModNameRef opts (modname,name) =
  (name,
   [href (docURL opts modname ++ ".html#"++name) [htxt name], nbsp, nbsp,
    htxt "(", href (docURL opts modname ++ ".html") [htxt modname], htxt ")"]
  )

sortNames :: [(a,String)] -> [(a,String)]
sortNames names = mergeSortBy (\(_,n1) (_,n2)->leqStringIgnoreCase n1 n2) names


--------------------------------------------------------------------------
-- generate the constructor index page for the documentation directory:
genConsIndexPage :: DocOptions ->  String -> [FC.TypeDecl] -> IO ()
genConsIndexPage opts docdir types = do
  putStrLn ("Writing constructor index page to \""++docdir++"/cindex.html\"...")
  simplePage "Index to all constructors" Nothing allConsFuncsMenu
             (htmlConsIndex opts (sortNames expcons))
    >>= writeFile (docdir++"/cindex.html")
 where
   consDecls (FC.Type    _ _ _ cs) = cs
   consDecls (FC.TypeSyn _ _ _ _ ) = []
   expcons = map FCG.consName $ filter ((== FC.Public) . FCG.consVisibility) $
     concatMap consDecls types

htmlConsIndex :: DocOptions ->  [QName] -> [HtmlExp]
htmlConsIndex opts = categorizeByItemKey . map (showModNameRef opts)

--------------------------------------------------------------------------
-- generate the index page categorizing all system libraries of PAKCS/KICS2
genSystemLibsPage :: String -> [Category] -> [[ModInfo]] -> IO ()
genSystemLibsPage docdir cats modInfos = do
  putStrLn $ "Writing main index page for " ++ currySystem ++
             " to \"" ++ fname ++ "\"..."
  mainPage (currySystem ++ " Libraries")
           [h1 [htxt $ currySystem ++ ": System Libraries"]]
           syslibsLeftTopMenu
           syslibsRightTopMenu
           (syslibsSideMenu cats)
           ([infoTxt, hrule] ++ genHtmlLibCats modInfos ++ [hrule, explainIcons])
   >>= writeFile fname
 where
  fname = docdir ++ "/" ++ currySystem ++ "_libs.html"

syslibsLeftTopMenu :: [[HtmlExp]]
syslibsLeftTopMenu =
  [ [href (currySystemURL ++ "/Manual.pdf") [htxt "Manual (PDF)"]]
  , [href (currySystemURL ++ "/lib/") [htxt "Libraries"]]
  , [ehref currygleURL [extLinkIcon, htxt " API Search"]]
  , [href (currySystemURL ++ "/download.html") [htxt "Download"]]
  ]

syslibsRightTopMenu :: [[HtmlExp]]
syslibsRightTopMenu =
  [ curryHomeItem
  , [ehref (curryWikiURL ++ "/documentation/report")
           [extLinkIcon, htxt " Curry Report"]]
  ]

syslibsSideMenu :: [Category] -> [HtmlExp]
syslibsSideMenu cats = map par $
     [[ehref currygleURL [extLinkIcon, htxt " Search with Curr(y)gle"]]]
  ++ [[href ("#" ++ genCatLink c) [ htxt (showCategory c)]] | c <- cats]
  ++ [ [href "findex.html" [htxt "Index to all library functions"]]
     , [href "cindex.html" [htxt "Index to all library constructors"]]
     , [href "#explain_icons" [htxt "Icons used in the documentation"]]
     ]

infoTxt :: HtmlExp
infoTxt = par
  [ htxt "Here is the collection of libraries contained in the distribution of "
  , href currySystemURL [htxt currySystem]
  , htxt $ ". Most of these libraries have been implemented during the "
        ++ "development of larger Curry applications. If you have suggestions "
        ++ "for changes/improvements or if you want to contribute your own "
        ++ "library, please contact "
  , href "http://www.informatik.uni-kiel.de/~mh/" [htxt "Michael Hanus"]
  , htxt "."
  ]

-- Generate links for a category (system libraries page)
genCatLink :: Category -> String
genCatLink cat = getCategoryID cat

genHtmlLibCats :: [[ModInfo]] -> [HtmlExp]
genHtmlLibCats = concatMap gen
  where
  gen []              = []
  gen cat@((c,_,_):_) = [anchoredSection (getCategoryID c)
                        (h2 [htxt (showCategory c ++ ":")] : genHtmlLibCat cat)]

genHtmlLibCat :: [ModInfo] -> [HtmlExp]
genHtmlLibCat category =
  [dlist [(genHtmlName modname, docComment2HTML defaultCurryDocOptions modcmt)
  | (_,modname,modcmt) <- category ]
  ]
 where
  genHtmlName modname = [code [href (modname ++ ".html") [htxt modname]]]

--------------------------------------------------------------------------
-- Auxiliary operation for general page style.

--- Generate the main page with the default documentation style.
--- @param title - the title of the page
--- @param htmltitle - the title in HTML format (shown as h1)
--- @param lefttopmenu - the menu shown at left of the top
--- @param righttopmenu - the menu shown at right of the top
--- @param sidemenu - the menu shown at the left-hand side
--- @param maindoc - the main contents of the page
mainPage :: String -> [HtmlExp] -> [[HtmlExp]] -> [[HtmlExp]]
         -> [HtmlExp] -> [HtmlExp] -> IO String
mainPage title htmltitle lefttopmenu righttopmenu sidemenu maindoc = do
    time <- getLocalTime
    return $ showHtmlPage $
      bootstrapPage styleBaseURL cssIncludes title homeBrand
                    lefttopmenu righttopmenu 3 sidemenu htmltitle maindoc
                    (curryDocFooter time)

cssIncludes :: [String]
cssIncludes = ["bootstrap.min","currydoc"]

homeBrand :: (String,[HtmlExp])
homeBrand = (currySystemURL, [homeIcon, nbsp, htxt currySystem])

--- Generate a page with the default documentation style.
--- @param title - the title of the page
--- @param body  - the main contents of the page
showPageWithDocStyle :: String -> [HtmlExp] -> String
showPageWithDocStyle title body =
  showHtmlPage $
    HtmlPage title
             (map (\f -> pageCSS $ styleBaseURL++"/css/"++f++".css") cssIncludes)
             body

--- The standard right top menu.
rightTopMenu :: [[HtmlExp]]
rightTopMenu =
  [ curryHomeItem
  , [ehref (currySystemURL++"/lib/")
           [extLinkIcon, htxt $ " " ++ currySystem ++ " Libraries"]]
  , [ehref (curryWikiURL ++ "/tools/currydoc")
           [extLinkIcon, htxt " About CurryDoc"]]
  ]

--------------------------------------------------------------------------
-- Icons:

extLinkIcon :: HtmlExp
extLinkIcon = glyphicon "new-window"

detIcon :: HtmlExp
detIcon     = glyphicon "arrow-right"
                `withTitle` "This operation is deterministic"
nondetIcon :: HtmlExp
nondetIcon  = glyphicon "random"
                `withTitle` "This operation might be non-deterministic"
-- rigidIcon :: HtmlExp
-- rigidIcon     = italic [] `addClass` "fa fa-cogs"
--                   `withTitle` "This operation is rigid"
-- flexibleIcon :: HtmlExp
-- flexibleIcon  = italic [] `addClass` "fa fa-pagelines"
--                   `withTitle` "This operation is flexible"
-- flexrigidIcon :: HtmlExp
-- flexrigidIcon = italic [] `addClass` "fa fa-exclamation-triangle"
--     `withTitle` "This operation is partially flexible and partially rigid"

withTitle :: HtmlExp -> String -> HtmlExp
withTitle he t = he `addAttr` ("title",t)

--------------------------------------------------------------------------
-- Standard footer information for generated web pages:
curryDocFooter :: CalendarTime -> [HtmlExp]
curryDocFooter time =
  [italic [htxt "Generated by ",
           bold [htxt "CurryDoc"],
           htxt (" ("++currydocVersion++") at "),
           htxt (calendarTimeToString time)]]

curryHomeItem :: [HtmlExp]
curryHomeItem = [ehref curryHomeURL [extLinkIcon, htxt " Curry Homepage"]]

--- Generate a simple page with the default documentation style.
--- @param title - the title of the page
--- @param htmltitle - maybe a specific title for h1 header
--- @param lefttopmenu - the menu shown at left of the top
--- @param doc - the main contents of the page
simplePage :: String -> Maybe [HtmlExp] -> [[HtmlExp]] -> [HtmlExp] -> IO String
simplePage title htmltitle lefttopmenu maindoc = do
    time <- getLocalTime
    return $ showHtmlPage $
      bootstrapPage styleBaseURL cssIncludes title homeBrand lefttopmenu rightTopMenu 0 []
                    [h1 (maybe [htxt title] id htmltitle)]
                    maindoc
                    (curryDocFooter time)

--- An anchored section in the document:
anchoredSection :: String -> [HtmlExp] -> HtmlExp
anchoredSection tag doc = section doc `addAttr` ("id",tag)

--- An anchored element in the document:
anchored :: String -> [HtmlExp] -> HtmlExp
anchored tag doc = style "anchored" doc `addAttr` ("id",tag)

--- An anchored element in the document:
anchoredDiv :: String -> [HtmlExp] -> HtmlExp
anchoredDiv tag doc = block doc `addAttr` ("class", "anchored")
                                `addAttr` ("id",tag)

--- A bordered table:
borderedTable :: [[[HtmlExp]]] -> HtmlExp
borderedTable rows = table rows `addClass` "table table-bordered table-hover"

--- An external reference
ehref :: String -> [HtmlExp] -> HtmlExp
ehref url desc = href url desc `addAttr` ("target","_blank")

--------------------------------------------------------------------------
-- auxiliaries:

stripSpaces :: String -> String
stripSpaces = reverse . dropWhile isSpace . reverse . dropWhile isSpace

-- style for explanation categories, like "Constructors:", "Parameters:",...
explainCat :: String -> HtmlExp
explainCat s = textstyle "explaincat" s

-- style for function/constructor name shown in the documentation part:
opnameDoc :: [HtmlExp] -> HtmlExp
opnameDoc = style "opname"

-- Sorts a list of strings.
sortStrings :: [String] -> [String]
sortStrings strings = mergeSortBy leqStringIgnoreCase strings

-- Returns the first sentence in a string:
firstSentence :: String -> String
firstSentence s = let (fs,ls) = break (=='.') s in
  if null ls
  then fs
  else if tail ls /= "" && isWhiteSpace (head (tail ls))
       then fs ++ "."
       else fs ++ "." ++ firstSentence (tail ls)

firstPassage :: String -> String
firstPassage = unlines . takeWhile (\s -> s /= "" && not (all isWhiteSpace s))
             . lines

--------------------------------------------------------------------------