Comments (11)
The main difficulty seems to be finding a way to escape characters without sacrificing the polymorphic interface.
You're right ,that's a detail I overlooked entirely when I first reported this (I subsequently came up with concatMap (\x-> if elem x badChars then ["\\",x] else [x])
, felt pleased with myself, and thought all that was needed was plugging it into the right place).
Text.JSON.Escape
I don't know of a better approach; this looks reasonable but I'll do some looking around (probably Tomorrow) and see what else comes up.
from alga.
@jmtd Thanks! Indeed, this looks problematic.
Presumably, this is the right output?
digraph
{
"\"1\""
"\"2\""
"\"1\"" -> "\"2\""
}
from alga.
While this is being resolved, the workaround is to use exportAsIs
after converting all vertex labels to strings using the right escaping. For example:
> putStrLn (exportAsIs (path ["1", "2"]) :: String)
digraph
{
"1"
"2"
"1" -> "2"
}
from alga.
Hi,
Presumably, this is the right output?
Yep that's right.
While this is being resolved, the workaround is to use exportAsIs after converting all vertex labels to strings using the right escaping.
Very useful, thanks!
from alga.
This seems like a suitable piece of work for a beginner PR, so I am going to take a stab at this, however if anyone else wants to try it please don't let me stop you.
from alga.
@jmtd Sure, please give it a try!
The main difficulty seems to be finding a way to escape characters without sacrificing the polymorphic interface. At the moment users can export into String
, Text
or any other similar datatype. How do we implement the escaping logic generically?
Perhaps, we need to define a type class of 'escapable strings' with a couple of standard instances:
class Escape s where
escape :: s -> s
instance Escape String where
escape [] = []
escape (c:cs) = escapeChar c ++ escape cs
where
escapeChar :: Char -> String
escapeChar c = ...
instance Escape Text where ...
For example, I found exactly the same solution here.
Then we'll need to strengthen the constraint on exportViaShow
from Show s
to (Show s, Escape s)
.
Does this make sense? Or is there a better approach?
from alga.
Sorry for the radio silence here, slow progress, my WIP (not PR-worthy yet) is here https://github.com/jmtd/alga/tree/escape-typeclass
from alga.
@jmtd No problem, take you time :) Don't hesitate to send a draft PR -- happy to review and iterate.
from alga.
OK I'll open one (now :)) as I have something that works for the simple case above, but there's clearly more work to be done.
from alga.
A brief update. I thought it wise to look at what others had done already, and so I was reading through Data.GraphViz.Printing, in particular addEscapes
(anchor in that link). The de-facto rules appear to be considerably different to the theoretical ones (I was surprised for example to see escLetters = Set.fromList ['N', 'G', 'E', 'T', 'H', 'L', 'n', 'l', 'r']
. I wonder why they are escaped?)
I imagine that Data.GraphViz.Printing reflects the truth of the Dot format, but the real test would be whether dot
accepts it. So I was considering writing a simple test harness that would attempt an export of a given escaped string and see whether dot
complained or not, as part of developing a proper escape routine (and/or verifying the rules in Data.GraphViz.Printing)
from alga.
@jmtd Good idea to check what others are doing!
I was surprised for example to see
escLetters = Set.fromList ['N', 'G', 'E', 'T', 'H', 'L', 'n', 'l', 'r']
. I wonder why they are escaped?
I guess if we have a string Hello\nWorld
we don't want to mess with the \
but leave it as is? Not sure, this all is getting quite complicated ;)
Yes, we need to collect a dozen of examples/tests and make sure our translation agrees with dot
.
from alga.
Related Issues (20)
- How to handle goodness inherited from other libraries HOT 2
- Bump upper bound for base-compat for version 0.4 on Hackage HOT 4
- Unable to use ghcid HOT 6
- Algorithms should be in terms of typeclasses not data structures HOT 15
- Acyclic graphs with Int labels HOT 5
- Tests failed to build with QuickCheck 2.14.2
- Multitree HOT 3
- Monoid instance HOT 6
- support for GraphViz "HTML strings" for node or edge labels HOT 1
- Add benchmarks to CI HOT 2
- Supporting different node and edge types HOT 7
- v0.6 fails to build with GHC 8.0 and 8.2 HOT 3
- Doubt about decorating a Graph HOT 4
- Compatibility with mtl-2.3 HOT 2
- Smart constructor for Symbol rather than requiring -XOverloadedLists HOT 1
- Proposal : foldgM HOT 2
- Algebra.Graph.Labelled.AdjacencyMap.edges appears to be broken HOT 2
- Semigroup version of Algebra.Graph.Labelled.AdjacencyMap HOT 7
- Bump "deepseq" dependency bounds to 1.5.0.0 HOT 2
- Warnings when building with ghc-9.10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alga.