diff --git a/src/compiler/GF/Command/Commands.hs b/src/compiler/GF/Command/Commands.hs index a8a175f7c..72e57fcf5 100644 --- a/src/compiler/GF/Command/Commands.hs +++ b/src/compiler/GF/Command/Commands.hs @@ -445,6 +445,7 @@ pgfCommands = Map.fromList [ ], options = [ ("lines","return the list of lines, instead of the singleton of all contents"), + ("paragraphs","return the list of paragraphs, as separated by empty lines"), ("tree","convert strings into trees") ], exec = getEnv $ \ opts _ (Env pgf mos) -> do @@ -470,6 +471,7 @@ pgfCommands = Map.fromList [ _ | isOpt "tree" opts -> returnFromLines [(1::Int,s)] _ | isOpt "lines" opts -> return (fromStrings $ lines s) + _ | isOpt "paragraphs" opts -> return (fromStrings $ toParagraphs $ lines s) _ -> return (fromString s), flags = [("file","the input file name")] }), diff --git a/src/compiler/GF/Command/CommonCommands.hs b/src/compiler/GF/Command/CommonCommands.hs index 69ccaf325..0b698e79c 100644 --- a/src/compiler/GF/Command/CommonCommands.hs +++ b/src/compiler/GF/Command/CommonCommands.hs @@ -3,6 +3,7 @@ -- elsewhere module GF.Command.CommonCommands where import Data.List(sort) +import Data.Char (isSpace) import GF.Command.CommandInfo import qualified Data.Map as Map import GF.Infra.SIO @@ -116,11 +117,13 @@ commonCommands = fmap (mapCommandExec liftSIO) $ Map.fromList [ let (os,fs) = optsAndFlags opts trans <- optTranslit opts - if isOpt "lines" opts - then return $ fromStrings $ map (trans . stringOps (envFlag fs) (map prOpt os)) $ toStrings x - else return ((fromString . trans . stringOps (envFlag fs) (map prOpt os) . toString) x), + case opts of + _ | isOpt "lines" opts -> return $ fromStrings $ map (trans . stringOps (envFlag fs) (map prOpt os)) $ toStrings x + _ | isOpt "paragraphs" opts -> return $ fromStrings $ map (trans . stringOps (envFlag fs) (map prOpt os)) $ toParagraphs $ toStrings x + _ -> return ((fromString . trans . stringOps (envFlag fs) (map prOpt os) . toString) x), options = [ - ("lines","apply the operation separately to each input line, returning a list of lines") + ("lines","apply the operation separately to each input line, returning a list of lines"), + ("paragraphs","apply separately to each input paragraph (as separated by empty lines), returning a list of lines") ] ++ stringOpOptions, flags = [ @@ -269,3 +272,11 @@ trie = render . pptss . H.toTrie . map H.toATree -- ** Converting command input toString = unwords . toStrings toLines = unlines . toStrings + +toParagraphs = map (unwords . words) . toParas + where + toParas ls = case break (all isSpace) ls of + ([],[]) -> [] + ([],_:ll) -> toParas ll + (l, []) -> [unwords l] + (l, _:ll) -> unwords l : toParas ll