diff options
Diffstat (limited to 'src/dot.rs')
-rw-r--r-- | src/dot.rs | 39 |
1 files changed, 32 insertions, 7 deletions
@@ -53,7 +53,7 @@ impl<'a> Dot<'a> { write!(w, "{}{}", strict, &graph.graph_type())?; if let Some(id) = &graph.id { - write!(w, r#" "{}""#, escape_doublequotes(id))?; + write!(w, " {}", format_id(id))?; } writeln!(w, " {{")?; @@ -185,11 +185,11 @@ impl<'a> Dot<'a> { write!( w, - r#"{}"{}" {} "{}""#, + r#"{}{} {} {}"#, get_indentation(indentation_level), - escape_doublequotes(&edge_source), + format_id(&edge_source), edge_op, - escape_doublequotes(&edge_source) + format_id(&edge_target) )?; write!(w, "{}", fmt_attributes(&edge.attributes))?; writeln!(w, ";") @@ -638,7 +638,7 @@ impl<'a> Node<'a> { impl<'a> DotString<'a> for Node<'a> { fn dot_string(&self) -> Cow<'a, str> { - let mut dot_string = format!(r#""{}""#, escape_doublequotes(&self.id)); + let mut dot_string = format!("{}", format_id(&self.id)); dot_string.push_str(fmt_attributes(&self.attributes).as_str()); dot_string.push_str(";"); dot_string.into() @@ -966,6 +966,31 @@ fn get_indentation(indentation_level: usize) -> String { INDENT.repeat(indentation_level) } -fn escape_doublequotes(val: &String) -> String { - val.chars().map(|c| if c == '"' { format!("\\{}", c) } else { format!("{}", c)}).collect() +fn is_alphanum(val: &String) -> bool { + for byte in val.bytes() { + if !((byte >= b'a' && byte <= b'z') || (byte >= b'A' && byte <= b'Z') || + (byte >= b'0' && byte <= b'9') || byte == b'_' || byte >= 128) + { + return false; + } + } + true +} + +// According to https://graphviz.org/doc/info/lang.html we should wrap any strings containing +// non-alphanumerical characters, as escape double quotes within those strings. +// This probably needs to be more robust but I think for now it fixes a but around double-quoted +// strings +fn format_id(val: &String) -> String { + if is_alphanum(val) { + val.to_string() + } else { + format!("\"{}\"", val.chars().map( + |c| if c == '"' { + format!("\\{}", c) + } else { + format!("{}", c) + }).collect::<String>() + ) + } } |