YAML !omap
Why did we use an !omap
in the
source YAML? This Wiki will eventually support any kind of YAML.
Short-term, we only need pages that present lists of test cases, and
!omap
s are the only ordered maps in
YAML. So, short-term, this project will only use !omap
nodes containing !omap
and !str
nodes. Future iterations can easily add
other types. This Short Cut focuses on Ajax, not elaborate HTML, and if we
can convert two YAML types then the rest will be easy.
This new code passes those tests:
class YarWiki def initialize(page_name = 'FrontPage') @page_name = page_name.to_s end attr_reader :page_name def wiki_path wp = File.expand_path(File.join(RAILS_ROOT, 'wiki')) Dir.mkdir(wp) rescue nil return wp end def yaml_path File.join(wiki_path, page_name + '.yaml') end def save_page(contents) File.open(yaml_path, 'w'){|f| f.write(contents) } end def format_yaml(x) return unless File.exist?(yaml_path) seq = YAML::parse_file(yaml_path) x.ul do seq.children.each do |kid| x.li do hash = kid.value x.strong( hash.keys.first.value ) x.text! ': ' + hash.values.first.value end end end end end
YAML::Syck
expresses an !omap
as a Seq
object containing a list of Map
objects, so the method format_yaml
works in two phases. We grab the
Seq
and iterate through its children;
each one is a Map
with only one key and
one value. We print the key in strong
markup, and we print the value after a colon.
Both the key and the value are Scalar
objects, and we get their raw data from
their .value
members. Our preliminary ...
Get Test Driven Ajax (on Rails) now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.