PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/hareview/Setup.hs

https://github.com/RefactoringTools/HaRe
Haskell | 89 lines | 61 code | 20 blank | 8 comment | 6 complexity | 712025d2fa3a954f8a58c2f161001ef2 MD5 | raw file
  1. #!/usr/bin/runhaskell
  2. import Distribution.Simple
  3. import Distribution.Simple.Setup (ConfigFlags (..))
  4. import Distribution.PackageDescription (emptyHookedBuildInfo,HookedBuildInfo(..))
  5. import Language.Haskell.HsColour (hscolour,Output(CSS))
  6. import Language.Haskell.HsColour.Colourise (defaultColourPrefs)
  7. import Control.Monad
  8. import Data.Maybe
  9. import Data.List
  10. main :: IO ()
  11. main = defaultMainWithHooks hooks
  12. hooks :: UserHooks
  13. hooks = simpleUserHooks { preConf = myPreConf }
  14. -- read template file with markers, call replaceOrEcho for each marker
  15. myPreConf :: Args -> ConfigFlags -> IO HookedBuildInfo
  16. myPreConf _ _ = do
  17. putStr "Generating custom html documentation... "
  18. -- file <- readFile "data/astview-tmpl.html"
  19. replaced <- mapM replaceOrEcho . lines =<< readFile "data/astview-tmpl.html"
  20. putStrLn " done."
  21. writeFile "data/astview.html" (unlines . concat $ replaced)
  22. return emptyHookedBuildInfo
  23. -- echoes the current line, or, if mymatch succeeds:
  24. -- replaces the line with colourized haskell code.
  25. replaceOrEcho :: String -> IO [String]
  26. replaceOrEcho s =
  27. if not $ match s
  28. then return [s]
  29. else do
  30. putStr $ (extract s)++" "
  31. file <- readFile ("data/"++(extract s)++".hs.txt")
  32. let replacement = lines $ hscolour
  33. CSS
  34. defaultColourPrefs
  35. False
  36. True
  37. (extract s)
  38. False
  39. file
  40. return (["<!-- Example "++(extract s)++" follows: -->"]
  41. ++ replacement
  42. ++ ["<!-- Example "++(extract s)++" done. -->"])
  43. -- interface that delegates to various implementations:
  44. -- recognizes Template marker of the form "%%asdf%%"
  45. match :: String -> Bool
  46. match = match0 "%%"
  47. --extracts the filename from the marker
  48. extract :: String -> String
  49. extract = extract1 "%%"
  50. -------- Implementations --------------
  51. match0 :: String -> String -> Bool
  52. match0 p s = take 2 s == p && take 2 (reverse s) == p
  53. match1 :: String -> String -> Bool
  54. match1 p = liftM2 (&&)
  55. (help p)
  56. (help p . reverse)
  57. where help q = (q ==) . (take (length q))
  58. match2 :: String -> String -> Bool
  59. match2 p s = p `isSuffixOf` s && (reverse p) `isPrefixOf` s
  60. extract1 :: String -> String -> String
  61. extract1 p s = let remainder = (drop (length p) s) in reverse (drop (length p) (reverse remainder) )
  62. extract2 :: String -> String -> String
  63. extract2 p s = reverse (drop (length p) (reverse (drop (length p) s)))
  64. extract3 :: String -> String -> String
  65. extract3 p s = reverse . drop (length p) $ reverse $ drop (length p) s
  66. extract4 :: String -> String
  67. extract4 = help . reverse . help
  68. where help :: String -> String
  69. help = fromJust . (stripPrefix "%%%")