Yamlt?

Hi all.

A short question.

XML has XSLT (which is XML itself)
JSON has less-known JSONT (JsonT - Transforming Json), which
also
is JSON itself.

What about YAMLT? Is it exists? Can it be useful? How can it look?

More practically: is it worse trying to write YAMLT implementation in
Ruby?

V.

On 6/17/06, Victor ‘Zverok’ Shepelev [email protected] wrote:

Hi all.

A short question.

XML has XSLT (which is XML itself)
JSON has less-known JSONT (JsonT - Transforming Json), which also
is JSON itself.

What about YAMLT? Is it exists? Can it be useful? How can it look?

I don’t think there’s anything like that. Can’t you just manipulate
the data in-memory?

From: Simen E. [mailto:[email protected]]
Sent: Sunday, June 18, 2006 2:44 PM

What about YAMLT? Is it exists? Can it be useful? How can it look?

I don’t think there’s anything like that. Can’t you just manipulate
the data in-memory?

Of course, I can (as well, as manipulate XML DOM without XSLT).
But in-memory manipulations code can lack declarative look.
Honestly speaking, Ruby’s data manipluations (all those sexy
iterators/enumerators, you know) IS declarative enough.

But, if it were YAML subset which can be interpreted as YAML
transformations
declaration, it can be useful. Right?

More practically: is it worse trying to write YAMLT implementation in
Ruby?

V.

  • Simen

V.

On 6/18/06, Victor ‘Zverok’ Shepelev [email protected] wrote:

is JSON itself.
iterators/enumerators, you know) IS declarative enough.

But, if it were YAML subset which can be interpreted as YAML transformations
declaration, it can be useful. Right?

More practically: is it worse trying to write YAMLT implementation in
Ruby?

Additionally, this would be wonderful to have for Web-based projects.
I know quite a few folks implement their sites in XML. Going for
Ruby->XML>XSLT chain is an overkill imho

I would think you could use marshaling to accomplish this. Just write
your own dumper/loader class:

Create a sample YAML file.

require ‘yaml’
tree = { :name => ‘ruby’,
:uses => [ ‘scripting’, ‘web’, ‘testing’, ‘etc’ ] }
File.open(“tree.yaml”, “w”) {|f| YAML.dump(tree, f)}

A transformer.

require ‘nezbit’
require ‘yaml’
tree = YAML.load_file(“tree.yaml”)
File.open(“tree.nzb”, “w”) { |f| Nezbit.dump(tree, f)}

And finally, a routine to use the transformed data.

require ‘nezbit’
tree = Nezbit.load_file(“tree.nzb”)

Regards,
JJ

On 17-Jun-2006, at 08:51, Victor ‘Zverok’ Shepelev wrote:

More practically: is it worse trying to write YAMLT implementation in
Ruby?

V.


“America goes not abroad in search of monsters to destroy.” – John
Quincy Adams

Victor ‘Zverok’ Shepelev wrote:

XML has XSLT (which is XML itself)
JSON has less-known JSONT (JsonT - Transforming Json), which also
is JSON itself.

What about YAMLT? Is it exists? Can it be useful? How can it look?

If JSON is YAML (which it pretty much is), then JSONT is YAMLT.

http://redhanded.hobix.com/inspect/yamlIsJson.html
http://redhanded.hobix.com/inspect/jsonCloserToYamlButNoCigarThanksAlotWhitespace.html

Cheers,
Dave

From: John J. [mailto:[email protected]]
Sent: Thursday, June 22, 2006 5:30 AM

also
I would think you could use marshaling to accomplish this. Just write
require ‘yaml’
tree = YAML.load_file(“tree.yaml”)
File.open(“tree.nzb”, “w”) { |f| Nezbit.dump(tree, f)}

And finally, a routine to use the transformed data.

require ‘nezbit’
tree = Nezbit.load_file(“tree.nzb”)

Regards,
JJ

Sorry, but I can’t understand the example :frowning:
What is ‘Nezbit’ ? How it all works?

V.


No matter how far down the wrong road you’ve gone, turn back. -
Turkish Proverb

On 21-Jun-2006, at 23:04, Victor ‘Zverok’ Shepelev wrote:

require ‘nezbit’

Sorry, but I can’t understand the example :frowning:
What is ‘Nezbit’ ? How it all works?

V.

Maybe this will be easier to understand:

#!/usr/bin/env ruby
require ‘yaml’
require ‘pp’

This class will save and load Hash objects using a made-up file

format.

It is for an example of transforming YAML to another format.

class MyFormat

Dump a Hash object to a file.

def MyFormat.dump(obj, file)
fail “This example only handles Hashes” if ! obj.is_a?(Hash)

 obj.each_pair { |key, value|
   MyFormat.dump(value, file) if value.is_a?(Hash)
   file << "<#{key}>\n"
   value.each { |val|
     file << "\t<#{val}>\n"
   }
 }

end

A regular expression to match keys and values of the form:

KEY_VALUE_RE = /
<(.+?)> (?# A key name surrounded by < > . )
\n (?# Key names are followed by a newline. )
( (?# Begin capturing. )
(?: (?# Begin a group. )
\t (?# Values have a tab character in front of
them. )
<.+?> (?# The value is surrounded by < > . )
\n (?# Values are followed by a newline. )
) (?# End the group. )
+ (?# There should be one or more values. )
) (?# End capturing. )
/xm # x means this is an extended regexp.
# m means it will match across multiple lines.

Process the contents of filename, and return a Hash

object with the key and value pairs.

def MyFormat.load_file(filename)
obj = {}
File.open(filename) { |file|
lines = file.read
lines.scan(KEY_VALUE_RE) { |key, values|
# Keys are symbols
key = key.to_sym
# Extract the values, removing extraneous characters.
vals = values.split(/\n/)
vals.collect! { |val|
val.gsub!(/^[\t<]+/, ‘’)
val.gsub!(/>$/, ‘’)
}
if vals.size == 1
obj[key] = vals[0]
else
obj[key] = vals
end
}
}
return obj
end
end

Files to play with.

YAML_FILENAME = Dir.pwd + ‘/tree.yaml’
MYFORMAT_FILENAME = Dir.pwd + ‘/tree.myformat’

A sample object to dump and load back.

tree = { :name => ‘ruby’,
:uses => [ ‘scripting’, ‘web’, ‘etc’ ]
}

Save the tree object to a file using YAML.

File.open(YAML_FILENAME, ‘w’) { |file|
YAML.dump(tree, file)
}

Now we can transform the YAML file into a MyFormat file.

File.open(MYFORMAT_FILENAME, ‘w’) { |file|
a_tree = YAML.load_file(YAML_FILENAME)
MyFormat.dump(a_tree, file)
}

Now get the object from the MyFormat file.

tree2 = MyFormat.load_file(MYFORMAT_FILENAME)

Show the original and the transformed objects.

pp tree
pp tree2