Skip to content

Commit

Permalink
Issue 470 search replace (#82)
Browse files Browse the repository at this point in the history
* search and replace matching strings across all pages in a project

* added regex query support

* fixed lint issue
  • Loading branch information
kvchitrapu authored Mar 21, 2023
1 parent ce583cf commit 3a6c4ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
6 changes: 3 additions & 3 deletions ambuda/templates/proofing/projects/replace.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<div class="prose">
<h1>Replace</h1>
<p>Use this simple search and replace form to make edits across this project.</p>
<p>Use this simple search and replace form to make edits across this project. The search supports regex.s</p>
<form method="POST" class="bg-slate-100 p-4 my-4">

{{ field(form.query) }}
Expand Down Expand Up @@ -45,9 +45,9 @@ <h1>Matches</h1>
<div class="match" style="background-color: rgb(243, 239, 239);">
<input type="checkbox" name="match{{ page.slug }}-{{ match.line_num }}" id="match{{ page.slug }}-{{ match.line_num }}" value="selected">
<p>Page <a href="{{ page_url }}">{{ project.title }}/{{ page.slug }}:</a> Line {{ match.line_num }}</p>
<label for="match{{ page.slug }}-{{ match.line_num }}">{{ match.query }}</label>
<label for="match{{ page.slug }}-{{ match.line_num }}">{{ match.query|safe }}</label>
<br>
<label for="match{{ page.slug }}-{{ match.line_num }}-replace" style="color: rgb(97, 86, 66); background-color: rgb(219, 215, 215);"> {{ match.replace }} </label>
<label for="match{{ page.slug }}-{{ match.line_num }}-replace" style="color: rgb(97, 86, 66); background-color: rgb(219, 215, 215);"> {{ match.replace|safe }} </label>
<input type="hidden" name="match{{ page.slug }}-{{ match.line_num }}-replace" id="match{{ page.slug }}-{{ match.line_num }}-replace" value="{{ match.replace }}">
</div>
<br>
Expand Down
43 changes: 28 additions & 15 deletions ambuda/views/proofing/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ def _replace_text(project_, replace_form: ReplaceForm, query: str, replace: str)

results = []

query_pattern = re.compile(
query, re.UNICODE
) # Compile the regex pattern with Unicode support

LOG.debug(f"Search/Replace text with {query} and {replace}")
for page_ in project_.pages:
if not page_.revisions:
Expand All @@ -341,21 +345,30 @@ def _replace_text(project_, replace_form: ReplaceForm, query: str, replace: str)
latest = page_.revisions[-1]
LOG.debug(f"{__name__}: {page_.slug}")
for line_num, line in enumerate(latest.content.splitlines()):
# LOG.debug(f'Search/Replace > {page_.slug}: {line}')
if query in line:
LOG.debug(f"Search/Replace > appending search/replace {line}")
matches.append(
{
"query": escape(line).replace(
query, Markup(f"<mark>{escape(query)}</mark>")
),
"replace": escape(line).replace(
query, Markup(f"<mark>{escape(replace)}</mark>")
),
"checked": False,
"line_num": line_num,
}
)
if query_pattern.search(line):
try:
marked_query = query_pattern.sub(
lambda m: Markup(f"<mark>{escape(m.group(0))}</mark>"), line
)
marked_replace = query_pattern.sub(
Markup(f"<mark>{escape(replace)}</mark>"), line
)
LOG.debug(f"Search/Replace > marked query: {marked_query}")
LOG.debug(f"Search/Replace > marked replace: {marked_replace}")
matches.append(
{
"query": marked_query,
"replace": marked_replace,
"checked": False,
"line_num": line_num,
}
)
except TimeoutError:
# Handle the timeout for regex operation, e.g., log a warning or show an error message
LOG.warning(
f"Regex operation timed out for line {line_num}: {line}"
)

if matches:
results.append(
{
Expand Down

0 comments on commit 3a6c4ae

Please sign in to comment.