1
+ {-# LANGUAGE StrictData #-}
2
+
1
3
module ElementBlocker (
2
4
elemBlock
3
5
) where
4
6
import InputParser hiding (Policy (.. ))
5
7
import qualified InputParser
6
8
import PolicyTree
9
+ import ProgramOptions (DebugLevel (DebugLevel ))
7
10
import qualified Data.Map as Map
8
11
import Data.Maybe
9
12
import Utils
@@ -19,51 +22,45 @@ import Data.String.Utils (startswith)
19
22
type BlockedRulesTree = DomainTree [Pattern ]
20
23
data ElemBlockData = ElemBlockData [Pattern ] BlockedRulesTree deriving Show
21
24
22
- elemBlock :: String -> [String ] -> [Line ] -> IO ()
23
- elemBlock path info = writeElemBlock . elemBlockData
25
+ elemBlock :: String -> [String ] -> DebugLevel -> [Line ] -> IO ()
26
+ elemBlock path info debug = writeElemBlock . elemBlockData
24
27
where
25
28
writeElemBlock :: ElemBlockData -> IO ()
26
29
writeElemBlock (ElemBlockData flatPatterns rulesTree) =
27
30
do
28
- let filteredInfo = filter ( (||) <$> not . startswith " Url: " <*> startswith " Url: http " ) info
29
- -- debugPath = path </> "debug"
31
+ let debugPath = path </> " debug "
32
+ filteredInfo = filter ( (||) <$> not . startswith " Url: " <*> startswith " Url: http " ) info
30
33
createDirectoryIfMissing True path
31
34
cont <- getDirectoryContents path
32
- _ <- sequence $ removeOld <$> cont
33
- -- createDirectoryIfMissing True debugPath
34
- -- writeBlockTree path debugPath rulesTree
35
- writeBlockTree path rulesTree
36
- writePatterns_with_debug filteredInfo (path </> " ab2p.common.css" ) " " flatPatterns
37
- -- writePatterns_with_debug filteredInfo (path </> "ab2p.common.css") (debugPath </> "ab2p.common.css") flatPatterns
35
+ mapM_ removeOld cont
36
+ when (debug > DebugLevel 0 ) $ createDirectoryIfMissing True debugPath
37
+ writeBlockTree path debugPath rulesTree
38
+ writePatterns filteredInfo (path </> " ab2p.common.css" ) (if debug > DebugLevel 0 then debugPath </> " ab2p.common.css" else " " ) flatPatterns
38
39
removeOld entry' =
39
40
let entry = path </> entry'
40
41
in do
41
42
isDir <- doesDirectoryExist entry
42
43
if isDir then when (head entry' /= ' .' ) $ removeDirectoryRecursive entry
43
44
else when (takeExtension entry == " .css" ) $ removeFile entry
44
- -- writeBlockTree :: String -> String -> BlockedRulesTree -> IO ()
45
- -- writeBlockTree normalNodePath debugNodePath (Node name patterns children) =
46
- writeBlockTree :: String -> BlockedRulesTree -> IO ()
47
- writeBlockTree normalNodePath (Node name patterns children) =
45
+ writeBlockTree :: String -> String -> BlockedRulesTree -> IO ()
46
+ writeBlockTree normalNodePath debugNodePath (Node name patterns children) =
48
47
do
49
48
createDirectoryIfMissing True normalPath
50
- -- createDirectoryIfMissing True debugPath
51
- -- _ <- sequence (writeBlockTree normalPath debugPath <$> children)
52
- -- writePatterns ["See ab2p.common.css for sources info"] normalFilename debugFilename patterns
53
- _ <- sequence (writeBlockTree normalPath <$> children)
54
- writePatterns [" See ab2p.common.css for sources info" ] normalFilename patterns
49
+ when (debug > DebugLevel 1 ) $ createDirectoryIfMissing True debugPath
50
+ mapM_ (writeBlockTree normalPath debugPath) children
51
+ writePatterns [" See ab2p.common.css for sources info" ] normalFilename (if debug > DebugLevel 1 then debugFilename else " " ) patterns
55
52
where
56
53
normalPath
57
54
| null name = normalNodePath
58
55
| otherwise = normalNodePath </> name
59
- -- debugPath
60
- -- | null name = debugNodePath
61
- -- | otherwise = debugNodePath </> name
56
+ debugPath
57
+ | null name = debugNodePath
58
+ | otherwise = debugNodePath </> name
62
59
normalFilename = normalPath </> " ab2p.css"
63
- -- debugFilename = debugPath </> "ab2p.css"
64
- writePatterns_with_debug :: [String ] -> String -> String -> [Pattern ] -> IO ()
65
- writePatterns_with_debug _ _ _ [] = return ()
66
- writePatterns_with_debug info' normalFilename debugFilename patterns =
60
+ debugFilename = debugPath </> " ab2p.css"
61
+ writePatterns :: [String ] -> String -> String -> [Pattern ] -> IO ()
62
+ writePatterns _ _ _ [] = return ()
63
+ writePatterns info' normalFilename debugFilename patterns =
67
64
do
68
65
writeCssFile normalFilename $ intercalate " \n " ((++ Templates. blockCss) . intercalate " ," <$>
69
66
splitEvery 4000 patterns)
@@ -75,24 +72,7 @@ elemBlock path info = writeElemBlock . elemBlockData
75
72
do outFile <- openFile filename WriteMode
76
73
hSetEncoding outFile utf8
77
74
hPutStrLn outFile " /*"
78
- _ <- mapM (hPutStrLn outFile) info'
79
- hPutStrLn outFile " */"
80
- hPutStrLn outFile content
81
- hClose outFile
82
- writePatterns :: [String ] -> String -> [Pattern ] -> IO ()
83
- writePatterns _ _ [] = return ()
84
- writePatterns info' normalFilename patterns =
85
- do
86
- -- writeCssFile debugFilename $ intercalate "\n" $ (++ Templates.blockCss) <$> patterns
87
- writeCssFile normalFilename $ intercalate " \n " ((++ Templates. blockCss) . intercalate " ," <$>
88
- splitEvery 4000 patterns)
89
- where
90
- splitEvery n = takeWhile (not . null ) . unfoldr (Just . splitAt n)
91
- writeCssFile filename content =
92
- do outFile <- openFile filename WriteMode
93
- hSetEncoding outFile utf8
94
- hPutStrLn outFile " /*"
95
- _ <- mapM (hPutStrLn outFile) info'
75
+ mapM_ (hPutStrLn outFile) info'
96
76
hPutStrLn outFile " */"
97
77
hPutStrLn outFile content
98
78
hClose outFile
0 commit comments