summaryrefslogtreecommitdiff
path: root/src/tools/Htmls.hs
blob: 6850a38a8f18f8bb81ae241d3543a04244c6ebfc (plain)
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
module Main where

import System

-- chop an HTML file into separate files, each linked to the next and previous.
-- the names of the files are n-file, with n = 01,02,...
-- the chopping is performed at each separator, here defined as "<!-- NEW -->"

-- AR 7/1/2002 for the Vinnova meeting in Linköping.

main :: IO ()
main = do
  file:_ <- getArgs
  htmls file

htmls :: FilePath -> IO ()
htmls file = do
  s  <- readFile file
  let ss = allPages s
  mapM (uncurry writeFile) (map (mkFile file (length ss)) ss)
  return ()

allPages :: String -> [(Int,String)]
allPages = zip [1..] . map unlines . chop . lines where
  chop ls = case span isNoSep ls of
      (s,_:ss) -> s : chop ss
      _ -> [ls]                 
  isNoSep = (/= separator)
  
mkFile :: FilePath -> Int -> (Int,String) -> (FilePath,String)
mkFile base mx (number,content) = 
 (fileName base number,
  unlines [
    begHTML,
    "<font size=1>",
    pageNum mx number,
    link base mx number,
    "</font>",
    "<p>",
    content,
    endHTML
   ]
  )

begHTML, endHTML, separator :: String
begHTML   = "<html><body bgcolor=\"#FFFFFF\" text=\"#000000\">"
endHTML   = "</body></html>"
separator = "<!-- NEW -->"

link :: FilePath -> Int -> Int -> String
link file mx n = 
  (if n >= mx then "" else ("   <a href=\"" ++ file' ++ "\">Next</a>")) ++
  (if n == 1  then "" else ("   <a href=\"" ++ file_ ++ "\">Previous</a>")) ++
                           ("   <a href=\"" ++ file1 ++ "\">First</a>") ++
                           ("   <a href=\"" ++ file2 ++ "\">Last</a>")
  where
    file_ = fileName file (n - 1)
    file' = fileName file (n + 1)
    file1 = fileName file 1
    file2 = fileName file mx

fileName :: FilePath -> Int -> FilePath
fileName file n = (if n < 10 then ('0':) else id) $ show n ++ "-" ++ file

pageNum mx num = "<p align=right>" ++ show num ++"/" ++ show mx ++ "</p>"