Skip to content

Commit 0f2ffe1

Browse files
author
Steven Lu
committed
1. REPLACE-ALL added.
1 parent 2743add commit 0f2ffe1

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

ailice/modules/ABrowser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def ModuleInfo(self):
3333
"SEARCH-UP-BROWSER": {"func": "SearchUp", "prompt": "Search content upward from the current location.", "type": "supportive"},
3434
"GET-LINK": {"func": "GetLink", "prompt": "Get the url on the specified text fragment. The text needs to be one of those text fragments enclosed by square brackets on the page (excluding the square brackets themselves).", "type": "supportive"},
3535
"EXECUTE-JS": {"func": "ExecuteJS", "prompt": "Execute js code on the current web page, especially suitable for form operations such as entering text, clicking buttons, etc. Use triple quotes on your code.", "type": "supportive"},
36-
"REPLACE": {"func": "Replace", "prompt": "Replace and edit content within the current page. When regexMode==True, you can use regular expressions to represent the pattern and replacement. This function is a simple wrapper for re.sub() in this mode. When regexMode==False, pattern and replacement represent literal strings. Use triple quotes to represent pattern and replacement.", "type": "supportive"},
36+
"REPLACE": {"func": "Replace", "prompt": "Replace the matching content within the current page. When regexMode==True, you can use regular expressions to represent the pattern and replacement. This function is a simple wrapper for re.sub() in this mode. When regexMode==False, pattern and replacement represent literal strings. Use triple quotes to represent pattern and replacement.", "type": "supportive"},
37+
"REPLACE-ALL": {"func": "ReplaceAll", "prompt": "Replace all matching content in the entire document. When regexMode==True, you can use regular expressions to represent the pattern and replacement. This function is a simple wrapper for re.sub() in this mode. When regexMode==False, pattern and replacement represent literal strings. Use triple quotes to represent pattern and replacement.", "type": "supportive"},
3738
"SAVETO": {"func": "SaveTo", "prompt": "Save the modified content to a file. If the dstPath parameter is an empty string, save it to the original file.", "type": "supportive"},
3839
}}
3940

@@ -140,6 +141,9 @@ def GetLink(self, text: str, session: str) -> str:
140141
def Replace(self, pattern: str, replacement: str, regexMode: bool, session: str) -> str:
141142
return self.sessions[session].Replace(pattern, replacement, regexMode) if hasattr(self.sessions[session], "Replace") else "Replace not supported in current browser."
142143

144+
def ReplaceAll(self, pattern: str, replacement: str, regexMode: bool, session: str) -> str:
145+
return self.sessions[session].ReplaceAll(pattern, replacement, regexMode) if hasattr(self.sessions[session], "ReplaceAll") else "ReplaceAll not supported in current browser."
146+
143147
def SaveTo(self, dstPath: str, session: str) -> str:
144148
return self.sessions[session].SaveTo(dstPath) if hasattr(self.sessions[session], "SaveTo") else "SaveTo not supported in current browser."
145149

@@ -154,7 +158,7 @@ def main():
154158
makeServer(ABrowser,
155159
{"pdfOutputDir": (args.pdfOutputDir if "" != args.pdfOutputDir.strip() else tmpdir)},
156160
args.addr,
157-
["ModuleInfo", "Browse", "Edit", "ScrollDown", "ScrollUp", "SearchDown", "SearchUp", "GetFullText", "GetLink", "ExecuteJS", "Replace", "SaveTo"]).Run()
161+
["ModuleInfo", "Browse", "Edit", "ScrollDown", "ScrollUp", "SearchDown", "SearchUp", "GetFullText", "GetLink", "ExecuteJS", "Replace", "ReplaceAll", "SaveTo"]).Run()
158162

159163
if __name__ == '__main__':
160164
main()

ailice/modules/ATextBrowser.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ def __init__(self, functions: dict[str, str]):
1111
self.prompt = \
1212
'''
1313
The document is in editable mode. You can edit the content using the following functions:
14-
#Replace and edit content within the current page. When regexMode==True, you can use regular expressions to represent the pattern and replacement. This function is a simple wrapper for re.sub() in this mode. When regexMode==False, pattern and replacement represent literal strings. Use triple quotes to represent pattern and replacement.
14+
#Replace the matching content within the current page. When regexMode==True, you can use regular expressions to represent the pattern and replacement. This function is a simple wrapper for re.sub() in this mode. When regexMode==False, pattern and replacement represent literal strings. Use triple quotes to represent pattern and replacement.
1515
REPLACE<!|pattern: str, replacement: str, regexMode: bool, session: str|!> -> str
16+
#Replace all matching content in the entire document. The parameters are the same as REPLACE.
17+
REPLACE-ALL<!|pattern: str, replacement: str, regexMode: bool, session: str|!> -> str
1618
#Save the modified content to a file. If the dstPath parameter is an empty string, save it to the original file.
1719
SAVETO<!|dstPath: str, session: str|!> -> str
1820
@@ -58,8 +60,22 @@ def Replace(self, pattern: str, replacement: str, regexMode: bool) -> str:
5860
textNew = re.sub(pattern, replacement, self(prompt=False))
5961
else:
6062
textNew = self(prompt=False).replace(pattern, replacement)
63+
msg = "Pattern NOT FOUND in current visible page. Please check: 1. If the pattern you entered is correct, such as whether you forgot to properly escape characters within the quotes. 2. Ensure that the content to be replaced is within the currently visible page (you can use the SEARCHDOWN/SEARCHUP to locate it, or directly use the REPLACE-ALL to replace all matching content).\n\n---\n\n"
64+
if (self(prompt=False) != textNew) or ("" == pattern):
65+
msg = "The matching contents has been replaced. \n\n---\n\n"
6166
self.ReplaceText(textNew, replaceAll=False)
62-
return textNew + self.prompt
67+
return msg + self() + self.prompt
68+
69+
def ReplaceAll(self, pattern: str, replacement: str, regexMode: bool) -> str:
70+
if regexMode:
71+
textNew = re.sub(pattern, replacement, self.txt)
72+
else:
73+
textNew = self.txt.replace(pattern, replacement)
74+
msg = "Pattern NOT FOUND in the entire document. Please check if the pattern you entered is correct, such as whether you forgot to properly escape characters within the quotes.\n\n---\n\n"
75+
if (self.txt != textNew) or ("" == pattern):
76+
msg = "The matching contents has been replaced. \n\n---\n\n"
77+
self.ReplaceText(textNew, replaceAll=True)
78+
return msg + self() + self.prompt
6379

6480
def SaveTo(self, dstPath: str) -> str:
6581
try:

0 commit comments

Comments
 (0)