lib,web: Replace regex functions with simple text replacement.

This commit is contained in:
Stephen Morgan 2020-08-31 16:25:28 +10:00
parent 2cd7877c46
commit 8dfffb1e61
3 changed files with 11 additions and 12 deletions

View File

@ -39,14 +39,14 @@ module Hledger.Data.AccountName (
) )
where where
import Data.List
import Data.List.Extra (nubSort) import Data.List.Extra (nubSort)
import qualified Data.List.NonEmpty as NE
#if !(MIN_VERSION_base(4,11,0)) #if !(MIN_VERSION_base(4,11,0))
import Data.Monoid import Data.Semigroup ((<>))
#endif #endif
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import Data.Tree import Data.Tree (Tree(..))
import Hledger.Data.Types import Hledger.Data.Types
import Hledger.Utils import Hledger.Utils
@ -115,7 +115,7 @@ expandAccountNames as = nubSort $ concatMap expandAccountName as
-- | "a:b:c" -> ["a","a:b","a:b:c"] -- | "a:b:c" -> ["a","a:b","a:b:c"]
expandAccountName :: AccountName -> [AccountName] expandAccountName :: AccountName -> [AccountName]
expandAccountName = map accountNameFromComponents . tail . inits . accountNameComponents expandAccountName = map accountNameFromComponents . NE.tail . NE.inits . accountNameComponents
-- | ["a:b:c","d:e"] -> ["a","d"] -- | ["a:b:c","d:e"] -> ["a","d"]
topAccountNames :: [AccountName] -> [AccountName] topAccountNames :: [AccountName] -> [AccountName]
@ -209,8 +209,10 @@ clipOrEllipsifyAccountName n = clipAccountName n
-- >>> putStr $ escapeName "First?!#$*?$(*) !@^#*? %)*!@#" -- >>> putStr $ escapeName "First?!#$*?$(*) !@^#*? %)*!@#"
-- First\?!#\$\*\?\$\(\*\) !@\^#\*\? %\)\*!@# -- First\?!#\$\*\?\$\(\*\) !@\^#\*\? %\)\*!@#
escapeName :: AccountName -> String escapeName :: AccountName -> String
escapeName = replaceAllBy (toRegex' "[[?+|()*\\\\^$]") ("\\" <>) -- PARTIAL: should not happen escapeName = T.unpack . T.concatMap escapeChar
. T.unpack where
escapeChar c = if c `elem` escapedChars then T.snoc "\\" c else T.singleton c
escapedChars = ['[', '?', '+', '|', '(', ')', '*', '$', '^', '\\']
-- | Convert an account name to a regular expression matching it and its subaccounts. -- | Convert an account name to a regular expression matching it and its subaccounts.
accountNameToAccountRegex :: AccountName -> Regexp accountNameToAccountRegex :: AccountName -> Regexp

View File

@ -106,16 +106,13 @@ addForm j today = identifyForm "add" $ \extra -> do
intercalate "," $ map ( intercalate "," $ map (
("{\"value\":" ++). ("{\"value\":" ++).
(++"}"). (++"}").
escapeJSSpecialChars .
drop 7 . -- "String "
show . show .
toJSON -- avoid https://github.com/simonmichael/hledger/issues/236
T.replace "</script>" "<\\/script>"
) ts, ) ts,
"]" "]"
] ]
where where
-- avoid https://github.com/simonmichael/hledger/issues/236
escapeJSSpecialChars = regexReplace (toRegexCI' "</script>") "<\\/script>"
validateTransaction :: validateTransaction ::
FormResult Day FormResult Day

View File

@ -72,7 +72,7 @@ writeJournalTextIfValidAndChanged f t = do
-- Ensure unix line endings, since both readJournal (cf -- Ensure unix line endings, since both readJournal (cf
-- formatdirectivep, #1194) writeFileWithBackupIfChanged require them. -- formatdirectivep, #1194) writeFileWithBackupIfChanged require them.
-- XXX klunky. Any equivalent of "hSetNewlineMode h universalNewlineMode" for form posts ? -- XXX klunky. Any equivalent of "hSetNewlineMode h universalNewlineMode" for form posts ?
let t' = T.pack $ regexReplace (toRegex' "\r") "" $ T.unpack t let t' = T.replace "\r" "" t
liftIO (readJournal def (Just f) t') >>= \case liftIO (readJournal def (Just f) t') >>= \case
Left e -> return (Left e) Left e -> return (Left e)
Right _ -> do Right _ -> do