Module: XOXO
- Defined in:
- lib/xoxo.rb
Overview
XOXO is a Ruby XOXO parser and generator. It provides a Ruby API similar to Marshal and YAML (though more specific) to load and dump XOXO, an simple, open outline format written in standard XHTML and suitable for embedding in (X)HTML, Atom, RSS, and arbitrary XML.
Defined Under Namespace
Constant Summary collapse
- VERSION =
xoxo.rb version number
"1.2.0"
Class Method Summary collapse
-
.dump(struct, options = {}) ⇒ Object
Return a XOXO string corresponding to the Ruby object
struct
, translated to the following rules:. -
.load(xoxo) ⇒ Object
Load and return a XOXO structure from the String, IO or StringIO or xoxo.
-
.make_xoxo(struct, class_name = nil) ⇒ String
private
Serialize an object in XOXO format.
Class Method Details
.dump(struct, options = {}) ⇒ Object
Return a XOXO string corresponding to the Ruby object struct
, translated to the following rules:
-
Arrays become ordered lists
<ol>
. -
Hashes become definition lists
<dl>
, keys are stringified withto_s
. -
Everything else becomes stringified with
to_s
and wrapped in appropriate list elements (<li>
or<dt>
/<dd>
).
Additionally, you can pass these options on the options hash:
:html_wrap
=>true
-
Wrap the XOXO with a basic XHTML 1.0 Transitional header.
:css
=> css-
Reference css as stylesheet for the wrapped XOXO document.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/xoxo.rb', line 50 def self.dump(struct, ={}) struct = [struct] unless struct.kind_of? Array if [:html_wrap] result = <<EOF.strip <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> EOF if [:css] result << %Q[<style type="text/css" >@import "#{[:css]}";</style>] end result << "</head><body>" << make_xoxo(struct, 'xoxo') << "</body></html>" else result = make_xoxo(struct, 'xoxo') end result end |
.load(xoxo) ⇒ Object
Load and return a XOXO structure from the String, IO or StringIO or xoxo.
27 28 29 30 31 32 33 |
# File 'lib/xoxo.rb', line 27 def self.load(xoxo) structs = Parser.new(xoxo).parse.structs while structs.kind_of?(Array) && structs.size == 1 structs = structs.first end structs end |
.make_xoxo(struct, class_name = nil) ⇒ String (private)
Serialize an object in XOXO format.
Note that Hash’s with a ‘url’ key are converted to ‘<a>` tags instead of `<dl>` tags. If you need to work around this you can convert the key to a Symbol instead of a String.
TODO: Should it be the other way around, Symbol instead of String key?
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/xoxo.rb', line 82 def self.make_xoxo(struct, class_name=nil) s = '' case struct when Assoc unless struct.empty? s << "<dl>" struct.each do |(key, value)| s << "<dt>" << key.to_s << "</dt><dd>" << make_xoxo(value) << "</dd>" end s << "</dl>" end when Array if class_name s << %Q[<ol class="#{class_name}">] else s << "<ol>" end struct.each { |item| s << "<li>" << make_xoxo(item) << "</li>" } s << "</ol>" when Hash if struct.has_key? 'url' d = struct.dup u = d.delete('url') t = d.delete('text') { d.delete('title', u) } s << '<a href="' << u.to_s << '" ' d.each do |tag, value| s << tag.to_s << '="' << make_xoxo(d.delete(tag)) << '" ' end s << '>' << make_xoxo(t) << '</a>' else unless struct.empty? s << "<dl>" struct.each do |key, value| s << "<dt>" << key.to_s << "</dt><dd>" << make_xoxo(value) << "</dd>" end s << "</dl>" end end when String s << struct when Numeric s << struct.to_s when Struct a = Assoc.new struct.each_pair do |k,v| a << [k,v] end s = make_xoxo(a, class_name) else a = Assoc.new struct.instance_variables.each do |iv| key = iv.to_s.sub(/^@/, '') a << [key, struct.instance_variable_get(iv)] end s = make_xoxo(a, class_name) end s end |