Changeset b4685e7 in adblock2privoxy for adblock2privoxy


Ignore:
Timestamp:
Jan 2, 2015 6:21:55 PM (5 years ago)
Author:
zubr <a@…>
Branches:
master
Children:
ff7ee56
Parents:
828fba4
Message:

CSS domain, install file, options in task file

Location:
adblock2privoxy
Files:
6 added
12 edited

Legend:

Unmodified
Added
Removed
  • adblock2privoxy/adblock2privoxy.cabal

    r828fba4 rb4685e7  
    33cabal-version:       >= 1.10 
    44build-type:          Simple 
    5 tested-with:         GHC==7.8.3 
     5tested-with:          
     6                     GHC==7.8.3, 
     7                     GHC==7.6.3 
    68author:              Alexey Zubritsky <adblock2privoxy@zubr.me> 
    79data-files:           
     
    1012extra-source-files:   
    1113                README.rst 
     14                INSTALL.rst 
    1215                changelog 
    1316                man/man1/adblock2privoxy.1 
     17                distribution/rpmbuild/SPECS/adblock2privoxy.spec 
     18                distribution/buildWin.bat 
     19                distribution/makeFedoraRpm.sh 
    1420license:             GPL-3 
    1521maintainer:          Alexey Zubritskiy <adblock2privoxy@zubr.me> 
  • adblock2privoxy/changelog

    r6bfb8d3 rb4685e7  
     11.3.0 
     2                * Build and packaging process refactoring 
     3                * Build script for windows 
     4                * Installation documentation 
     5                * CSS server domain changed from hardcoded 'privoxy.zubr.me' to configurable one 
     6                * Element hiding CSS generation is skipped if the domain is not specified 
     7                * Task files contains output paths, so it's possible to run ab2p with single parameter: task file name 
     8                * Bugfixes for tickets  
    191.2.4 
    210            * Documentation and bugfixes 
  • adblock2privoxy/distribution/rpmbuild/SPECS/adblock2privoxy.spec

    r828fba4 rb4685e7  
    4343 
    4444%prep 
    45 %setup -q 
     45%setup -q -T -D -n root 
    4646cabal update 
    4747cabal install --user --only-dependencies --enable-optimization=2 
     
    5050%build 
    5151%global cabal_configure_options --user 
    52 %global ghc_user_conf 1. 
     52%global ghc_user_conf 1 
    5353%global ghc_without_dynamic 1 
    5454%ghc_bin_build 
  • adblock2privoxy/src/ElementBlocker.hs

    r6bfb8d3 rb4685e7  
    6868         writeCssFile filename content =  
    6969                do outFile <- openFile filename WriteMode 
     70                   hSetEncoding outFile utf8 
    7071                   hPutStrLn outFile "/*" 
    7172                   _ <- mapM (hPutStrLn outFile) info' 
  • adblock2privoxy/src/Main.hs

    r828fba4 rb4685e7  
    66import Task 
    77import Control.Applicative hiding (many) 
    8 import SourceInfo 
    9 import System.Console.GetOpt 
     8import SourceInfo as Source 
     9import ProgramOptions as Options 
    1010import System.Environment 
    1111import Templates 
     
    1616import Data.Text.Lazy (unpack) 
    1717import Network.Socket 
    18 import System.FilePath 
    19 import Paths_adblock2privoxy (version) 
    20 import Data.Version (showVersion) 
     18import System.Directory 
     19import Control.Monad 
    2120 
    22 data Options = Options 
    23      { _showVersion :: Bool 
    24      , _privoxyDir  :: FilePath 
    25      , _webDir      :: FilePath 
    26      , _taskFile    :: FilePath 
    27      , _forced    :: Bool 
    28      } deriving Show 
    29  
    30 options :: [OptDescr (Options -> Options)] 
    31 options = 
    32      [ Option "v" ["version"] 
    33          (NoArg (\ opts -> opts { _showVersion = True })) 
    34          "show version number" 
    35      , Option "p"   ["privoxyDir"] 
    36          (ReqArg (\ f opts -> opts { _privoxyDir = f }) 
    37                  "PATH") 
    38          "privoxy config output path (required)" 
    39      , Option "w"   ["webDir"] 
    40          (ReqArg (\ f opts -> opts { _webDir = f }) 
    41                  "PATH") 
    42          "css files output path (optional, privoxyDir is used by default)" 
    43      , Option "t"   ["taskFile"] 
    44          (ReqArg (\ f opts -> opts { _taskFile = f }) 
    45                  "PATH") 
    46          "path to task file containing urls to process" 
    47      , Option "f" ["forced"] 
    48          (NoArg (\ opts -> opts { _forced = True })) 
    49          "run even if no sources are expired" 
    50      ] 
    51  
    52 parseOptions :: [String] -> IO (Options, [String]) 
    53 parseOptions argv = 
    54    case getOpt Permute options argv of 
    55       (opts,nonOpts,[]  ) ->  
    56                 case foldl (flip id) (Options False "" "" "" False) opts of 
    57                         Options False "" _ _ _ -> writeError "Privoxy dir is not specified.\n" 
    58                         opts'@Options{_showVersion = True} -> return (opts', nonOpts) 
    59                         opts' -> return (setDefaults opts', nonOpts) 
    60       (_,_,errs) -> writeError $ concat errs 
    61    where 
    62         setDefaults opts@(Options _ privoxyDir "" _ _) = setDefaults opts{ _webDir = privoxyDir } 
    63         setDefaults opts@(Options _ privoxyDir _ "" _) = setDefaults opts{ _taskFile = privoxyDir </> "ab2p.task" } 
    64         setDefaults opts = opts   
    6521   
    66 writeError :: String -> IO a 
    67 writeError msg = ioError $ userError $ msg ++ usageInfo header options 
    68         where          
    69         header = "Usage: adblock2privoxy [OPTION...] [URL...]" 
    70  
    7122getResponse :: String -> IO String 
    7223getResponse url = do 
     
    7425        withSocketsDo $ unpack . decodeUtf8 <$> simpleHttp url 
    7526 
    76 processSources :: String -> String -> String -> [SourceInfo]-> IO () 
    77 processSources privoxyDir webDir taskFile sources = do  
     27processSources :: Options -> String -> [SourceInfo]-> IO () 
     28processSources options taskFile sources = do  
    7829        (parsed, sourceInfo) <- unzip <$> mapM parseSource sources    
    7930        let parsed' = concat parsed  
    80             infoText = showInfos  sourceInfo                
    81         writeTask taskFile infoText parsed' 
    82         elemBlock webDir infoText parsed' 
    83         urlBlock privoxyDir infoText parsed' 
    84         writeTemplateFiles privoxyDir 
    85         putStrLn $ "Run 'adblock2privoxy " ++ taskFile ++ "' every 1-2 days to process data updates" 
     31            sourceInfoText = showInfo sourceInfo 
     32            optionsText = logOptions options                
     33        writeTask taskFile (sourceInfoText ++ optionsText) parsed' 
     34        if null._cssDomain $ options 
     35                then putStrLn "WARNING: CSS generation is not run because webserver domain is not specified" 
     36                else elemBlock (_webDir options) sourceInfoText parsed' 
     37        urlBlock (_privoxyDir options) sourceInfoText parsed' 
     38        writeTemplateFiles (_privoxyDir options) (_cssDomain options) 
     39        putStrLn $ "Run 'adblock2privoxy -t " ++ taskFile ++ "' every 1-2 days to process data updates." 
    8640        where  
    8741        parseSource sourceInfo = do 
     
    10559        now <- getCurrentTime 
    10660        args <- getArgs 
    107         (Options printVersion privoxyDir webDir taskFile forced, urls) <- parseOptions args 
    108         let acton 
    109                 | printVersion = putStrLn $ "adblock2privoxy version " ++ showVersion version 
     61        (options@(Options printVersion _ _ taskFile _ forced), urls) <- parseOptions args 
     62        (options', task) <- do 
     63                fileExists <- doesFileExist taskFile 
     64                if fileExists 
     65                        then do task <- readTask taskFile  
     66                                return (fillFromLog options task, Just task) 
     67                        else return (options, Nothing)                         
     68        let  
     69            action 
     70                | printVersion = putStrLn versionText 
    11071                | not . null $ urls  
    111                    =    processSources privoxyDir webDir taskFile (makeInfo <$> urls) 
    112                 | not . null $ taskFile  
    113                    = do task <- readTask taskFile 
    114                         let sources = logInfo task                         
    115                         if forced || or (infoExpired now <$> sources)                                 
    116                                 then processSources privoxyDir webDir taskFile sources 
    117                                 else putStrLn "all sources are up to date" 
    118                 | otherwise = writeError "no input specified" 
    119         acton 
     72                   = processSources options' taskFile (makeInfo <$> urls) 
     73                | otherwise = case task of 
     74                        Nothing -> writeError "no input specified"   
     75                        (Just task') -> do 
     76                                let sources = Source.readLogInfos task' 
     77                                if forced || or (infoExpired now <$> sources)                                 
     78                                        then processSources options' taskFile sources 
     79                                        else putStrLn "all sources are up to date" 
     80                             
     81        action 
    12082        now' <- getCurrentTime 
    121         putStrLn $ concat ["done in ", show $ diffUTCTime now' now, " seconds"] 
     83        putStrLn $ concat ["Execution done in ", show $ diffUTCTime now' now, " seconds."] 
    12284 
  • adblock2privoxy/src/SourceInfo.hs

    r6bfb8d3 rb4685e7  
    22( 
    33SourceInfo(_url), 
    4 showInfos, 
     4showInfo, 
    55updateInfo, 
    66makeInfo, 
    7 logInfo, 
     7readLogInfos, 
    88infoExpired 
    99) where 
     
    3232endMark = "------- end ------" 
    3333 
    34 showInfos :: [SourceInfo] -> [String]  
    35 showInfos sourceInfos = (sourceInfos >>= showInfo) ++ [endMark ++ "\n"] 
     34showInfo :: [SourceInfo] -> [String]  
     35showInfo sourceInfos = (sourceInfos >>= showInfoItem) ++ [endMark ++ "\n"] 
    3636 
    37 showInfo :: SourceInfo -> [String]  
    38 showInfo sourceInfo@(SourceInfo _ url _ _ lastUpdated expires _ expired) =  
     37showInfoItem :: SourceInfo -> [String]  
     38showInfoItem sourceInfo@(SourceInfo _ url _ _ lastUpdated expires _ expired) =  
    3939        catMaybes [ Just separator, 
    4040                    optionalLine "Title: " _title, 
    41                     Just $ concat ["Url: ", url], 
    42                     Just $ concat ["Last modified: ", formatTime defaultTimeLocale "%d %b %Y %H:%M %Z" lastUpdated], 
     41                    Just $ "Url: " ++ url, 
     42                    Just $ "Last modified: " ++ formatTime defaultTimeLocale "%d %b %Y %H:%M %Z" lastUpdated, 
    4343                    Just $ concat ["Expires: ", show expires, " hours", expiredMark],  
    4444                    optionalLine "Version: " $ show . _version, 
     
    4949                | otherwise = "" 
    5050    optionalLine caption getter | getter sourceInfo == getter emptySourceInfo = Nothing 
    51                                 | otherwise = Just $ concat [caption, getter sourceInfo]  
     51                                | otherwise = Just $ caption ++ getter sourceInfo  
    5252 
    5353updateInfo :: UTCTime -> [Line] -> SourceInfo -> SourceInfo 
     
    6161makeInfo url = emptySourceInfo { _url = url } 
    6262 
    63 logInfo :: [String] -> [SourceInfo] 
    64 logInfo lns = chunkInfo <$> chunks 
     63readLogInfos :: [String] -> [SourceInfo] 
     64readLogInfos lns = chunkInfo <$> chunks 
    6565   where  
    6666   chunks = filter (not.null) . split [separator] . takeWhile (/= endMark) $ lns 
     
    6969infoExpired :: UTCTime -> SourceInfo -> Bool 
    7070infoExpired now (SourceInfo _ _ _ _ lastUpdated expires _ _ ) =  
    71         diffUTCTime now lastUpdated > (fromInteger $ expires * 60 * 60) 
     71        diffUTCTime now lastUpdated > fromInteger (expires * 60 * 60) 
    7272 
    7373lineComment :: Line -> String 
     
    8787            <$> (string "Last modified: " *> many1 anyChar) 
    8888        licenseParser = (\x -> info{_license = x})  
    89             <$> ((string "Licen" <|> string "Лицензия") *> (manyTill anyChar $ char ':')  
     89            <$> ((string "Licen" <|> string "Лицензия") *> manyTill anyChar (char ':')  
    9090                *> skipMany (char ' ') *> many1 anyChar) 
    9191        expiresParser = (\n unit -> info{_expires = unit * read n})  
  • adblock2privoxy/src/Task.hs

    r6bfb8d3 rb4685e7  
    33readTask 
    44) where 
     5import System.IO.Strict as Strict 
    56import System.IO 
    67import InputParser 
     
    2425readTask :: String -> IO [String]        
    2526readTask path = do  
    26         result <- lines <$> readFile path 
     27        result <- lines <$> Strict.readFile path 
    2728        return $ length result `seq` result --read whole file to allow its overwriting 
    2829 
  • adblock2privoxy/src/Templates.hs

    r6bfb8d3 rb4685e7  
    22import  {-# SOURCE #-}  UrlBlocker 
    33import Paths_adblock2privoxy 
    4 import System.Directory (copyFile) 
    54import System.FilePath ((</>)) 
     5import Data.String.Utils (replace, startswith) 
     6import Control.Applicative 
    67 
    78blockCss, ab2pPrefix, actionsFilePrefix, filtersFilePrefix :: String 
     
    2930terminalActionSwitch _ _ = ""  
    3031 
    31 writeTemplateFiles :: String -> IO () 
    32 writeTemplateFiles outDir = do 
     32writeTemplateFiles :: String -> String -> IO () 
     33writeTemplateFiles outDir cssDomain = do 
    3334        copySystem "ab2p.system.action" 
    3435        copySystem "ab2p.system.filter" 
    35         where  
     36        where 
     37        filterDomain content = unlines $ filterLine <$> lns 
     38                where 
     39                lns = lines content 
     40                replace' line (from, to) = replace from to line 
     41                filterLine line 
     42                        | null cssDomain && startswith "[?CSS_DOMAIN]" line = ""  
     43                        | otherwise = foldl replace' line [("[?CSS_DOMAIN]", ""), ("[CSS_DOMAIN]", cssDomain)]  
     44                                   
    3645        copySystem file = do 
    3746                dataDir <- getDataDir 
    38                 copyFile (dataDir  </> "templates" </> file) (outDir </> file) 
     47                content <- readFile $ dataDir  </> "templates" </> file 
     48                writeFile (outDir </> file) $ filterDomain content 
  • adblock2privoxy/src/UrlBlocker.hs

    r6bfb8d3 rb4685e7  
    4848    writeContent filename header content =  
    4949         do outFile <- openFile filename WriteMode 
    50             hPutStrLn outFile (header)  
     50            hSetEncoding outFile utf8 
     51            hPutStrLn outFile header  
    5152            _ <- mapM (hPutStrLn outFile) $ ('#':) <$> info 
    5253            hPutStrLn outFile $ intercalate "\n\n" $ show <$> content 
     
    5455 
    5556urlBlockData :: [Line] -> UrlBlockData  
    56 urlBlockData lns = filterBlockData $ result 
     57urlBlockData lns = filterBlockData result 
    5758    where 
    5859    result = mconcat [nodeResult node | node <- shortenNodes $ sortBy cmpPolicy $ filterNodesList blockLines] 
     
    7071     
    7172filterNodesList :: [FilteringNode] -> [FilteringNode] 
    72 filterNodesList nodes = Map.foldr (:) [] $ Map.fromListWith joinNodes $ list 
     73filterNodesList nodes = Map.foldr (:) [] $ Map.fromListWith joinNodes list 
    7374    where 
    7475    list = [(name node, node) | node <- nodes] 
     
    9091    where  
    9192    initialState = Map.empty :: Map.Map String String 
    92     shortenNode node = (\f -> node {_filters = f}) <$> ((mapM.mapM) shortenFilter $ _filters node)        
     93    shortenNode node = (\f -> node {_filters = f}) <$> (mapM.mapM) shortenFilter (_filters node)        
    9394    shortenFilter headerFilter@(HeaderFilter headerType flt)  
    9495        = let filterCode = _code flt  
     
    101102                    (start, rest) ->  
    102103                        let end = last $ split "]" rest  
    103                             shortenCode' = start ++ (show $ Map.size dictionary + 1) ++  end  
     104                            shortenCode' = start ++ show (Map.size dictionary + 1) ++  end  
    104105                        in do put $ Map.insert filterCode shortenCode' dictionary 
    105106                              return $ HeaderFilter headerType flt { _code = shortenCode' } 
     
    117118    requestType = _requestType requestOptions 
    118119    mainOptions = [requestOptions {_requestType = requestType { _positive = mainPosRequestTypes } }] 
    119     mainPosRequestTypes = filter (`notElem` [Subdocument]) <$> (_positive requestType) 
    120     boolOptions getter = case getter requestOptions of 
    121         False -> Nothing 
    122         True  -> Just requestOptions {_requestType = Restrictions Nothing [], _thirdParty = Nothing} 
     120    mainPosRequestTypes = filter (`notElem` [Subdocument]) <$> _positive requestType 
     121    boolOptions getter = if getter requestOptions 
     122        then Nothing 
     123        else Just requestOptions {_requestType = Restrictions Nothing [], _thirdParty = Nothing} 
    123124    singleTypeOptions singleType =  
    124125        do 
    125         foundTypes <- filter (== singleType) <$> (_positive requestType) 
     126        foundTypes <- filter (== singleType) <$> _positive requestType 
    126127        foundType <- listToMaybe foundTypes 
    127128        return requestOptions {_requestType = requestType { _positive = Just [foundType] } } 
     
    157158                                        (Switch True . TaggerAction <$> taggers), 
    158159                      _patterns   = patterns, 
    159                       _hasTag     = (nodeType == Nested) 
     160                      _hasTag     = nodeType == Nested 
    160161    taggers = filterTaggers <$> levelFilters 
    161162    filterTaggers flt@(HeaderFilter _ (Filter _ _ orEmpty))   
     
    183184filtersCode :: HeaderFilters -> ChainType -> Policy -> BlockMethod -> String -> String 
    184185filtersCode [] _ policy method rest  
    185     = join [Templates.ab2pPrefix, toLower <$> show policy, "-" ,toLower <$> show method,(if null rest then "" else "-"), rest] 
     186    = join [Templates.ab2pPrefix, toLower <$> show policy, "-" ,toLower <$> show method, if null rest then "" else "-", rest] 
    186187filtersCode (levelFilters : nextLevelFilters) chainType policy method rest  
    187     = filtersCode nextLevelFilters Nested policy method $ join [levelCode, (if null rest then "" else "-when-"), rest] 
    188     where  
    189     levelCode = (intercalate "-" $ filterCode <$> levelFilters) 
     188    = filtersCode nextLevelFilters Nested policy method $ join [levelCode, if null rest then "" else "-when-", rest] 
     189    where  
     190    levelCode = intercalate "-" $ filterCode <$> levelFilters 
    190191    filterCode (HeaderFilter HeaderType {_typeCode = typeCode} (Filter code _ orEmpty)) 
    191192        | chainType == Negate            = negateCode 
     
    230231        = intercalate " \\\n " $ mainText : (_forwarding tagger >>= cancelTaggerText) 
    231232        where  
    232         mainText = join [name enable, name $ _taggerType $ _headerType $ tagger, "{", name tagger,  "}" ] 
     233        mainText = join [name enable, name . _taggerType . _headerType $ tagger, "{", name tagger,  "}" ] 
    233234        cancelTaggerText (CancelTagger cancelTaggerCode)  
    234             = [join [name enable, name $ _taggerType $ _headerType $ tagger, "{", cancelTaggerCode,  "}" ]] 
     235            = [join [name enable, name . _taggerType . _headerType $ tagger, "{", cancelTaggerCode,  "}" ]] 
    235236        cancelTaggerText _ = []                 
    236237     
  • adblock2privoxy/src/Utils.hs

    r828fba4 rb4685e7  
    148148infixl 4 .*., <<$>, <<<$>, $>, $>>, $>>>, <<*>>, <<<*>>> 
    149149 
    150 (.*.) :: (c -> d) -> (a -> b -> c) -> (a -> b -> d) 
     150(.*.) :: (c -> d) ->  
     151         (a -> b -> c) ->  
     152          a -> b -> d 
    151153(.*.) = (.).(.) 
    152154 
  • adblock2privoxy/templates/ab2p.system.action

    r6bfb8d3 rb4685e7  
    22 
    33{+set-image-blocker{blank} \ 
    4  +filter{ab2p-elemhide-filter} \ 
     4[?CSS_DOMAIN] +filter{ab2p-elemhide-filter} \ 
    55 +client-header-tagger{ab2p-elemhide-check-debug} \ 
    66 +client-header-tagger{ab2p-handle-as-image-c} \ 
     
    88/ 
    99 
    10 {-filter{ab2p-elemhide-filter} \ 
    11  +filter{ab2p-elemhide-filter-debug}} 
    12 TAG:^ab2p-elemhide-filter-debug$ 
     10[?CSS_DOMAIN]{-filter{ab2p-elemhide-filter} \ 
     11[?CSS_DOMAIN] +filter{ab2p-elemhide-filter-debug}} 
     12[?CSS_DOMAIN]TAG:^ab2p-elemhide-filter-debug$ 
    1313 
    1414{+handle-as-image} 
  • adblock2privoxy/templates/ab2p.system.filter

    r6bfb8d3 rb4685e7  
    2828s@^cookie:.*ab2p-elemhide-filter-debug=true(?:;|$).*@ab2p-elemhide-filter-debug@Ti 
    2929 
    30 FILTER: ab2p-elemhide-filter 
    31 s@[^'"\s]\s*<head[^>]*>(?=\s*[^'"\s])@$&<link rel="stylesheet" type="text/css" href="http://privoxy.zubr.me/ab2p.common.css"></link><link rel="stylesheet" type="text/css" href="http://privoxy.zubr.me/$host/ab2p.css"></link>@iUD 
     30[?CSS_DOMAIN]FILTER: ab2p-elemhide-filter 
     31[?CSS_DOMAIN]s@[^'"\s]\s*<head[^>]*>(?=\s*[^'"\s])@$&<link rel="stylesheet" type="text/css" href="http://[CSS_DOMAIN]/ab2p.common.css"></link><link rel="stylesheet" type="text/css" href="http://[CSS_DOMAIN]/$host/ab2p.css"></link>@iUD 
    3232 
    33 FILTER: ab2p-elemhide-filter-debug 
    34 s@[^'"\s]\s*<head[^>]*>(?=\s*[^'"\s])@$&<link rel="stylesheet" type="text/css" href="http://privoxy.zubr.me/debug/ab2p.common.css"></link><link rel="stylesheet" type="text/css" href="http://privoxy.zubr.me/$host.debug/ab2p.css"></link>@iUD 
     33[?CSS_DOMAIN]FILTER: ab2p-elemhide-filter-debug 
     34[?CSS_DOMAIN]s@[^'"\s]\s*<head[^>]*>(?=\s*[^'"\s])@$&<link rel="stylesheet" type="text/css" href="http://[CSS_DOMAIN]/debug/ab2p.common.css"></link><link rel="stylesheet" type="text/css" href="http://[CSS_DOMAIN]/$host.debug/ab2p.css"></link>@iUD 
    3535 
    3636FILTER: ab2p-popup-filter 
Note: See TracChangeset for help on using the changeset viewer.