source: adblock2privoxy/adblock2privoxy/src/Templates.hs @ e2b555c

Last change on this file since e2b555c was e2b555c, checked in by Alexey Zubritskiy <a.zubritskiy@…>, 4 years ago

Adapted to GHC 7.10, introduced stack build

  • Property mode set to 100644
File size: 2.0 KB
Line 
1module Templates where
2import  {-# SOURCE #-}  UrlBlocker
3import Paths_adblock2privoxy
4import System.FilePath ((</>))
5import Data.String.Utils (replace, startswith)
6
7blockCss, ab2pPrefix, actionsFilePrefix, filtersFilePrefix :: String
8blockCss = "{display:none!important;visibility:hidden!important}"
9ab2pPrefix = "ab2p-"
10actionsFilePrefix = "#AbBlock generated actions -- don't edit --"
11filtersFilePrefix = "#AbBlock generated filters -- don't edit --"
12
13terminalActionSwitch :: Bool -> BlockMethod -> String
14terminalActionSwitch True Request =
15 "+block{ adblock rules } \\\n\
16 \+server-header-tagger{ab2p-block-s}"
17terminalActionSwitch False Request =
18 "-block \\\n\
19 \-server-header-tagger{ab2p-block-s} \\\n\
20 \+server-header-tagger{ab2p-unblock-d} \\\n\
21 \+server-header-tagger{ab2p-unblock-s} \\\n\
22 \+client-header-tagger{ab2b-unblock-u}"
23terminalActionSwitch True Xframe = "+server-header-filter{ab2p-xframe-filter}"
24terminalActionSwitch False Xframe = "-server-header-filter{ab2p-xframe-filter}"
25terminalActionSwitch False Elem = "-filter{ab2p-elemhide-filter}"
26terminalActionSwitch True Xpopup = "+filter{ab2p-popup-filter}"
27terminalActionSwitch False Xpopup = "-filter{ab2p-popup-filter}"
28terminalActionSwitch True Dnt = "+add-header{DNT: 1}"
29terminalActionSwitch _ _ = ""
30
31writeTemplateFiles :: String -> String -> IO ()
32writeTemplateFiles outDir cssDomain = do
33        copySystem "ab2p.system.action"
34        copySystem "ab2p.system.filter"
35        where
36        filterDomain content = unlines $ filterLine <$> lns
37                where
38                lns = lines content
39                replace' line (from, to) = replace from to line
40                filterLine line
41                        | null cssDomain && startswith "[?CSS_DOMAIN]" line = ""
42                        | otherwise = foldl replace' line [("[?CSS_DOMAIN]", ""), ("[CSS_DOMAIN]", cssDomain)]
43
44        copySystem file = do
45                dataDir <- getDataDir
46                content <- readFile $ dataDir  </> "templates" </> file
47                writeFile (outDir </> file) $ filterDomain content
Note: See TracBrowser for help on using the repository browser.