Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Regex Replace node #1893

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/src/packages/chaiNNer_standard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
Dependency(
display_name="ChaiNNer Extensions",
pypi_name="chainner_ext",
version="0.0.0",
size_estimate=1.1 * MB,
version="0.1.0",
size_estimate=1.5 * MB,
),
],
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from __future__ import annotations

from enum import Enum

from chainner_ext import MatchGroup, RustRegex

from nodes.properties.inputs import EnumInput, TextInput
from nodes.properties.outputs import TextOutput
from nodes.utils.replacement import ReplacementString

from .. import text_group


class ReplacementMode(Enum):
REPLACE_ALL = 0
REPLACE_FIRST = 1


@text_group.register(
schema_id="chainner:utility:regex_replace",
name="Regex Replace",
description="Replace occurrences of some regex with a replacement text. Either or all occurrences or the first occurrence will be replaced",
icon="MdTextFields",
inputs=[
TextInput("Text", min_length=0),
TextInput("Regex", min_length=0, placeholder=r'E.g. "\b\w+\b"'),
TextInput("Replacement Pattern", min_length=0, placeholder=r'E.g. "found {0}"'),
EnumInput(
ReplacementMode,
label="Replace mode",
default_value=ReplacementMode.REPLACE_ALL,
),
],
outputs=[
TextOutput(
"Text",
output_type="""
let count = match Input3 {
ReplacementMode::ReplaceAll => inf,
ReplacementMode::ReplaceFirst => 1,
};
regexReplace(Input0, Input1, Input2, count)
""",
).with_never_reason(
"Either the regex pattern or the replacement pattern is invalid"
),
],
)
def regex_replace(
text: str,
regex_pattern: str,
replacement_pattern: str,
mode: ReplacementMode,
) -> str:
# parse the inputs before we do any actual work
r = RustRegex(regex_pattern)
replacement = ReplacementString(replacement_pattern)

matches = r.findall(text)
if len(matches) == 0:
return text

if mode == ReplacementMode.REPLACE_FIRST:
matches = matches[:1]

def get_group_text(group: MatchGroup | None) -> str:
if group is not None:
return text[group.start : group.end]
else:
return ""

result = ""
last_end = 0
for match in matches:
result += text[last_end : match.start]

replacements: dict[str, str] = {}
for i in range(r.groups + 1):
replacements[str(i)] = get_group_text(match.get(i))
for name, i in r.groupindex.items():
replacements[name] = get_group_text(match.get(i))

result += replacement.replace(replacements)
last_end = match.end

result += text[last_end:]
return result
218 changes: 211 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"babel-loader": "^8.2.5",
"babel-plugin-i18next-extract": "^0.9.0",
"concurrently": "^7.2.1",
"copy-webpack-plugin": "^11.0.0",
"cross-env": "^7.0.3",
"css-loader": "^6.7.1",
"electron": "^23.0.0",
Expand Down Expand Up @@ -136,7 +137,7 @@
"react-markdown": "^8.0.3",
"react-time-ago": "^7.2.1",
"reactflow": "^11.5.6",
"rregex": "^1.5.1",
"rregex": "^1.7.0",
"scheduler": "^0.22.0",
"semver": "^7.3.7",
"systeminformation": "^5.11.16",
Expand Down
17 changes: 17 additions & 0 deletions src/common/rust-regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isRenderer } from './env';
import { log } from './log';

let imports;
if (isRenderer) {
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
imports = require('rregex/lib/browser') as typeof import('rregex');

// This is not good, but I can't think of a better way.
// We are racing loading the wasm module and using it.
imports.default().catch(log.error);
} else {
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
imports = require('rregex/lib/commonjs') as typeof import('rregex');
}

export class RRegex extends imports.RRegex {}
Loading