svgsym 0.1.0.0 → 0.1.1.0
raw patch · 4 files changed
+46/−13 lines, 4 filesdep +arraydep +regex-basedep ~base
Dependencies added: array, regex-base
Dependency ranges changed: base
Files
- CHANGELOG.md +8/−1
- README.md +1/−1
- src-exe/Main.hs +33/−9
- svgsym.cabal +4/−2
CHANGELOG.md view
@@ -1,5 +1,12 @@ # Revision history for svgsym -## 0.1.0.0 -- YYYY-mm-dd+## 0.1.1.0 -- 2022-05-07++* Expand allowed versions of the base library.+ Mainly to support builds using GHC 8.10.++* Validate symbol ID regular expression before use.++## 0.1.0.0 -- 2022-05-01 * First version. Released on an unsuspecting world.
README.md view
@@ -21,7 +21,7 @@ symbol regular expression consist of a single capture group that captures the actual symbol identifier. -``` sh-session+``` console $ svgsym css-gg/all.svg \ --content './src/**/*.hs' \ --symbol '"(gg-[a-z]+)"' \
src-exe/Main.hs view
@@ -1,11 +1,14 @@ {-# LANGUAGE BlockArguments #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} -module Main where+module Main (main) where import Control.Monad (forM, when)+import Data.Array ((!))+import qualified Data.Array as Arr import qualified Data.ByteString.Lazy as LB import Data.Foldable (Foldable (fold), find, for_) import Data.List (isPrefixOf)@@ -21,10 +24,19 @@ import System.FilePath ((</>)) import System.FilePattern.Directory (FilePattern, getDirectoryFiles) import System.IO (hPutStrLn, stderr)-import Text.Regex.TDFA (AllTextMatches (getAllTextMatches), (=~))+import Text.Regex.TDFA (AllTextMatches (getAllTextMatches), Regex, RegexContext (match), RegexLike (matchAll, matchAllText), makeRegexM, (=~), (=~~))+import Text.Regex.TDFA.Common (Regex (regex_groups)) import Text.XML.Light (Attr (Attr), Element (Element, elContent, elName), QName (..), parseXMLDoc, ppTopElement, showElement) import qualified Text.XML.Light as Xml +type Re = String++newtype Errorable a = Errorable (Either String a)+ deriving (Functor, Applicative, Monad)++instance MonadFail Errorable where+ fail = Errorable . Left+ qnSvg :: QName qnSvg = QName {qName = "svg", qURI = Just "http://www.w3.org/2000/svg", qPrefix = Nothing} @@ -54,13 +66,12 @@ } -- | Find all symbols using the given pattern-findSymbols :: String -> String -> Set String-findSymbols pat haystack = Set.fromList $ go haystack+findSymbols :: Regex -> String -> Set String+findSymbols regex = Set.fromList . concatMap go . lines where+ go :: String -> [String] go "" = []- go s =- let (_, _, rest, match) = s =~ pat :: (String, String, String, [String])- in match <> go rest+ go s = map (fst . (! 1)) $ matchAllText regex s findFiles :: [FilePattern] -> IO [FilePath] findFiles patterns = do@@ -71,7 +82,7 @@ absPats = makeAbsolute <$> patterns getDirectoryFiles "/" absPats -findFileSymbols :: String -> [FilePath] -> IO (Set String)+findFileSymbols :: Regex -> [FilePath] -> IO (Set String) findFileSymbols regex files = do fmap fold . forM files $ \f -> do s <- readFile f@@ -98,13 +109,26 @@ main :: IO () main = do Opts.Options {..} <- execParser Opts.parser++ symbolRegex <- case makeRegexM symbolPat of+ Errorable (Left err) -> do+ hPutStrLn stderr "Errorable parsing symbol regular expression:"+ hPutStrLn stderr err+ exitFailure+ Errorable (Right re) -> do+ let (_, n) = Arr.bounds $ regex_groups re+ when (n /= 1) do+ hPutStrLn stderr "Expect exactly one capture group in symbol regular expression."+ exitFailure+ pure re+ files <- findFiles contentPats when debug do hPutStrLn stderr "Matched content files:" for_ files \f -> hPutStrLn stderr (" " <> f) - symbols <- findFileSymbols symbolPat files+ symbols <- findFileSymbols symbolRegex files when debug do hPutStrLn stderr "Matched symbols:"
svgsym.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: svgsym-version: 0.1.0.0+version: 0.1.1.0 synopsis: A tool to prune unused symbols from icon SVG files. homepage: https://git.sr.ht/~rycee/svgsym bug-reports: https://lists.sr.ht/~rycee/public-inbox@@ -22,13 +22,15 @@ Svgsym.Options build-depends:- , base ^>=4.15.1+ , array ^>=0.5+ , base >=4.14 && <4.17 , bytestring ^>=0.10 , containers ^>=0.6 , directory ^>=1.3 , filepath ^>=1.4 , filepattern ^>=0.1.2 , optparse-applicative ^>=0.16+ , regex-base ^>=0.94 , regex-tdfa ^>=1.3.1 , xml ^>=1.3