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
import Data.List
import Data.List.Extra (nubSort)
import qualified Data.List.NonEmpty as NE
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
import Data.Semigroup ((<>))
#endif
import Data.Text (Text)
import qualified Data.Text as T
import Data.Tree
import Data.Tree (Tree(..))
import Hledger.Data.Types
import Hledger.Utils
@ -115,7 +115,7 @@ expandAccountNames as = nubSort $ concatMap expandAccountName as
-- | "a:b:c" -> ["a","a:b","a:b:c"]
expandAccountName :: AccountName -> [AccountName]
expandAccountName = map accountNameFromComponents . tail . inits . accountNameComponents
expandAccountName = map accountNameFromComponents . NE.tail . NE.inits . accountNameComponents
-- | ["a:b:c","d:e"] -> ["a","d"]
topAccountNames :: [AccountName] -> [AccountName]
@ -209,8 +209,10 @@ clipOrEllipsifyAccountName n = clipAccountName n
-- >>> putStr $ escapeName "First?!#$*?$(*) !@^#*? %)*!@#"
-- First\?!#\$\*\?\$\(\*\) !@\^#\*\? %\)\*!@#
escapeName :: AccountName -> String
escapeName = replaceAllBy (toRegex' "[[?+|()*\\\\^$]") ("\\" <>) -- PARTIAL: should not happen
. T.unpack
escapeName = T.unpack . T.concatMap escapeChar
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.
accountNameToAccountRegex :: AccountName -> Regexp

View File

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

View File

@ -72,7 +72,7 @@ writeJournalTextIfValidAndChanged f t = do
-- Ensure unix line endings, since both readJournal (cf
-- formatdirectivep, #1194) writeFileWithBackupIfChanged require them.
-- 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
Left e -> return (Left e)
Right _ -> do