Skip to content
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
16 changes: 14 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ bitflags = { version = "2.4", features = ["serde"] }
ctor = "0.2"
convert_case = "0.8"
titlecase = "3.6"
fancy-regex = "0.18.0"
unicode-segmentation = "1.13.2"
indoc = "2.0.5"
derivative = "2.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,113 @@ fn document_node_definitions() -> HashMap<DefinitionIdentifier, DocumentNodeDefi
description: Cow::Borrowed("TODO"),
properties: None,
},
DocumentNodeDefinition {
identifier: "Regex Find",
Comment thread
Keavon marked this conversation as resolved.
category: "Text: Regex",
node_template: NodeTemplate {
document_node: DocumentNode {
implementation: DocumentNodeImplementation::Network(NodeNetwork {
exports: vec![
// Primary output: the whole match (String)
NodeInput::node(NodeId(1), 0),
// Secondary output: capture groups (Vec<String>)
NodeInput::node(NodeId(2), 0),
],
nodes: [
// Node 0: regex_find proto node — returns Vec<String> of [whole_match, ...capture_groups]
DocumentNode {
inputs: vec![
NodeInput::import(concrete!(String), 0),
NodeInput::import(concrete!(String), 1),
NodeInput::import(concrete!(f64), 2),
NodeInput::import(concrete!(bool), 3),
Comment thread
Keavon marked this conversation as resolved.
NodeInput::import(concrete!(bool), 4),
],
implementation: DocumentNodeImplementation::ProtoNode(text_nodes::regex::regex_find::IDENTIFIER),
..Default::default()
},
// Node 1: index_elements at index 0 — extracts the whole match as a String
DocumentNode {
inputs: vec![NodeInput::node(NodeId(0), 0), NodeInput::value(TaggedValue::F64(0.), false)],
Comment thread
Keavon marked this conversation as resolved.
implementation: DocumentNodeImplementation::ProtoNode(graphic::index_elements::IDENTIFIER),
..Default::default()
},
// Node 2: omit_element at index 0 — returns capture groups as Vec<String>
DocumentNode {
inputs: vec![NodeInput::node(NodeId(0), 0), NodeInput::value(TaggedValue::F64(0.), false)],
Comment thread
Keavon marked this conversation as resolved.
implementation: DocumentNodeImplementation::ProtoNode(graphic::omit_element::IDENTIFIER),
..Default::default()
},
]
.into_iter()
.enumerate()
.map(|(id, node)| (NodeId(id as u64), node))
.collect(),
..Default::default()
}),
inputs: vec![
NodeInput::value(TaggedValue::String(String::new()), true),
NodeInput::value(TaggedValue::String(String::new()), false),
NodeInput::value(TaggedValue::F64(0.), false),
Comment thread
Keavon marked this conversation as resolved.
NodeInput::value(TaggedValue::Bool(false), false),
NodeInput::value(TaggedValue::Bool(false), false),
],
..Default::default()
},
persistent_node_metadata: DocumentNodePersistentMetadata {
input_metadata: vec![
("String", "The string to search within.").into(),
("Pattern", "The regular expression pattern to search for.").into(),
(
"Match Index",
"Which non-overlapping occurrence of the pattern to return, starting from 0 for the first match. Negative indices count backwards from the last match.",
)
.into(),
("Case Insensitive", "Match letters regardless of case.").into(),
("Multiline", "Make `^` and `$` match the start and end of each line, not just the whole string.").into(),
],
output_names: vec!["Match".to_string(), "Captures".to_string()],
network_metadata: Some(NodeNetworkMetadata {
persistent_metadata: NodeNetworkPersistentMetadata {
node_metadata: [
DocumentNodeMetadata {
persistent_metadata: DocumentNodePersistentMetadata {
node_type_metadata: NodeTypePersistentMetadata::node(IVec2::new(0, 0)),
..Default::default()
},
..Default::default()
},
DocumentNodeMetadata {
persistent_metadata: DocumentNodePersistentMetadata {
node_type_metadata: NodeTypePersistentMetadata::node(IVec2::new(8, 0)),
..Default::default()
},
..Default::default()
},
DocumentNodeMetadata {
persistent_metadata: DocumentNodePersistentMetadata {
node_type_metadata: NodeTypePersistentMetadata::node(IVec2::new(8, 2)),
..Default::default()
},
..Default::default()
},
]
.into_iter()
.enumerate()
.map(|(id, node)| (NodeId(id as u64), node))
.collect(),
..Default::default()
},
..Default::default()
}),
..Default::default()
},
},
description: Cow::Borrowed(
r#"Finds a portion of the string matching a regular expression pattern. With "Match Index" at its default 0, it selects the first non-overlapping occurrence, but others may be selected. Capture groups, if any, are produced as a list in the "Captures" output."#,
),
properties: None,
},
// Aims for interoperable compatibility with:
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=levl%27%20%3D%20Levels-,%27curv%27%20%3D%20Curves,-%27expA%27%20%3D%20Exposure
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=Max%20input%20range-,Curves,-Curves%20settings%20files
Expand Down
35 changes: 35 additions & 0 deletions node-graph/libraries/graphic-types/src/graphic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,41 @@ impl<T: Clone> AtIndex for Table<T> {
}
}

pub trait OmitIndex {
fn omit_index(&self, index: usize) -> Self;
fn omit_index_from_end(&self, index: usize) -> Self;
}
impl<T: Clone> OmitIndex for Vec<T> {
fn omit_index(&self, index: usize) -> Self {
self.iter().enumerate().filter(|(i, _)| *i != index).map(|(_, v)| v.clone()).collect()
}

fn omit_index_from_end(&self, index: usize) -> Self {
if index == 0 || index > self.len() {
return self.clone();
}
self.omit_index(self.len() - index)
}
}
impl<T: Clone> OmitIndex for Table<T> {
fn omit_index(&self, index: usize) -> Self {
let mut result = Self::default();
for (i, row) in self.iter().enumerate() {
if i != index {
result.push(row.into_cloned());
}
}
result
}

fn omit_index_from_end(&self, index: usize) -> Self {
if index == 0 || index > self.len() {
return self.clone();
}
self.omit_index(self.len() - index)
}
}

// TODO: Eventually remove this migration document upgrade code
pub fn migrate_graphic<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Table<Graphic>, D::Error> {
use serde::Deserialize;
Expand Down
33 changes: 33 additions & 0 deletions node-graph/nodes/graphic/src/graphic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ where
.unwrap_or_default()
}

/// Returns the collection with the element at the specified index removed.
/// If no value exists at that index, the collection is returned unchanged.
#[node_macro::node(category("General"))]
pub fn omit_element<T: graphic_types::graphic::OmitIndex + Clone + Default>(
_: impl Ctx,
/// The collection of data, such as a list or table.
#[implementations(
Vec<f64>,
Vec<u32>,
Vec<u64>,
Vec<DVec2>,
Vec<String>,
Table<Artboard>,
Table<Graphic>,
Table<Vector>,
Table<Raster<CPU>>,
Table<Raster<GPU>>,
Table<Color>,
Table<GradientStops>,
)]
collection: T,
/// The index of the item to remove, starting from 0 for the first item. Negative indices count backwards from the end of the collection, starting from -1 for the last item.
index: SignedInteger,
) -> T {
let index = index as i32;

if index < 0 {
collection.omit_index_from_end(-index as usize)
} else {
collection.omit_index(index as usize)
}
Comment thread
Keavon marked this conversation as resolved.
}

#[node_macro::node(category("General"))]
async fn map<Item: AnyHash + Send + Sync + std::hash::Hash>(
ctx: impl Ctx + CloneVarArgs + ExtractAll,
Expand Down
1 change: 1 addition & 0 deletions node-graph/nodes/text/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ log = { workspace = true }
serde_json = { workspace = true }
convert_case = { workspace = true }
titlecase = { workspace = true }
fancy-regex = { workspace = true }
unicode-segmentation = { workspace = true }

# Optional workspace dependencies
Expand Down
1 change: 1 addition & 0 deletions node-graph/nodes/text/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod font_cache;
pub mod json;
mod path_builder;
pub mod regex;
mod text_context;
mod to_path;

Expand Down
Loading
Loading