Class: SaxPrinter
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- SaxPrinter
- Defined in:
- lib/sax/sax_printer.rb
Constant Summary collapse
- CCS =
{ amp: {named: '&', hex: '&'}, lt: {named: '<', hex: '<'}, gt: {named: '>', hex: '>'}, }
- XMLDEC_ATTRS =
%w(version encoding standalone)
Instance Attribute Summary collapse
-
#instructions ⇒ Object
Returns the value of attribute instructions.
-
#pretty ⇒ Object
Returns the value of attribute pretty.
Instance Method Summary collapse
- #add_opening_tag(name, attrs) ⇒ Object
- #below_block? ⇒ Boolean
- #block?(name) ⇒ Boolean
- #block_or_compact?(name) ⇒ Boolean
- #cdata_block(string) ⇒ Object
- #characters(string) ⇒ Object
- #comment(string) ⇒ Object
- #compact?(name) ⇒ Boolean
- #end_document ⇒ Object
- #end_element(name) ⇒ Object
- #error(string) ⇒ Object
- #handle_whitespace(string) ⇒ Object
- #in_block? ⇒ Boolean
- #in_compact? ⇒ Boolean
- #in_inline? ⇒ Boolean
- #increment_space ⇒ Object
-
#initialize(options) ⇒ SaxPrinter
constructor
A new instance of SaxPrinter.
- #inline?(name) ⇒ Boolean
- #keep_linebreaks? ⇒ Boolean
- #nontextual_text(str, op, cl) ⇒ Object
- #processing_instruction(name, content) ⇒ Object
- #sanitize(string) ⇒ Object
- #self_closing?(name) ⇒ Boolean
- #set_control_vars(options) ⇒ Object
- #set_element_types(options) ⇒ Object
- #set_options_as_ivars(options) ⇒ Object
- #space_after_close(name) ⇒ Object
- #space_before_close(name) ⇒ Object
- #space_before_open(name) ⇒ Object
- #start_document ⇒ Object
- #start_element(name, attributes) ⇒ Object
- #start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ Object
- #tag_with_attrs(name, attrs) ⇒ Object
- #whitespace? ⇒ Boolean
- #ws_adder ⇒ Object
- #ws_only_in_block?(string) ⇒ Boolean
- #xmldec_attrs(args) ⇒ Object
- #xmldecl(*args) ⇒ Object
Constructor Details
#initialize(options) ⇒ SaxPrinter
Returns a new instance of SaxPrinter.
11 12 13 |
# File 'lib/sax/sax_printer.rb', line 11 def initialize() () end |
Instance Attribute Details
#instructions ⇒ Object
Returns the value of attribute instructions.
2 3 4 |
# File 'lib/sax/sax_printer.rb', line 2 def instructions @instructions end |
#pretty ⇒ Object
Returns the value of attribute pretty.
2 3 4 |
# File 'lib/sax/sax_printer.rb', line 2 def pretty @pretty end |
Instance Method Details
#add_opening_tag(name, attrs) ⇒ Object
135 136 137 138 |
# File 'lib/sax/sax_printer.rb', line 135 def add_opening_tag(name, attrs) tag = attrs.empty? ? "<#{name}>" : tag_with_attrs(name, attrs) pretty << tag end |
#below_block? ⇒ Boolean
170 171 172 |
# File 'lib/sax/sax_printer.rb', line 170 def below_block? in_inline? or in_compact? end |
#block?(name) ⇒ Boolean
61 62 63 |
# File 'lib/sax/sax_printer.rb', line 61 def block?(name) @block.include?(name) end |
#block_or_compact?(name) ⇒ Boolean
69 70 71 |
# File 'lib/sax/sax_printer.rb', line 69 def block_or_compact?(name) block?(name) or compact?(name) end |
#cdata_block(string) ⇒ Object
193 194 195 |
# File 'lib/sax/sax_printer.rb', line 193 def cdata_block(string) nontextual_text(string, '<![CDATA[', ']]>') end |
#characters(string) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/sax/sax_printer.rb', line 148 def characters(string) return false if ws_only_in_block?(string) handle_whitespace(string) unless string.empty? @open_tag = nil sanitize(string) pretty << string end end |
#comment(string) ⇒ Object
189 190 191 |
# File 'lib/sax/sax_printer.rb', line 189 def comment(string) nontextual_text(string, '<!--', '-->') end |
#compact?(name) ⇒ Boolean
65 66 67 |
# File 'lib/sax/sax_printer.rb', line 65 def compact?(name) @compact.include?(name) end |
#end_document ⇒ Object
145 146 |
# File 'lib/sax/sax_printer.rb', line 145 def end_document end |
#end_element(name) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sax/sax_printer.rb', line 110 def end_element(name) if self_closing?(name) pretty[-1] = '/>' else space_before_close(name) pretty << "</#{name}>" end @depth -= 1 space_after_close(name) @open_tag = nil @opens.pop end |
#error(string) ⇒ Object
203 204 205 |
# File 'lib/sax/sax_printer.rb', line 203 def error(string) fail Nokogiri::XML::SyntaxError.new(string) end |
#handle_whitespace(string) ⇒ Object
174 175 176 177 |
# File 'lib/sax/sax_printer.rb', line 174 def handle_whitespace(string) string.gsub!(/[\r\n]/, '') unless keep_linebreaks? string.gsub!(/^\s+|\s+$/, '') unless whitespace? end |
#in_block? ⇒ Boolean
85 86 87 |
# File 'lib/sax/sax_printer.rb', line 85 def in_block? block?(@opens[-1]) end |
#in_compact? ⇒ Boolean
81 82 83 |
# File 'lib/sax/sax_printer.rb', line 81 def in_compact? compact?(@opens[-1]) end |
#in_inline? ⇒ Boolean
77 78 79 |
# File 'lib/sax/sax_printer.rb', line 77 def in_inline? inline?(@opens[-1]) end |
#increment_space ⇒ Object
106 107 108 |
# File 'lib/sax/sax_printer.rb', line 106 def increment_space pretty << ws_adder end |
#inline?(name) ⇒ Boolean
73 74 75 |
# File 'lib/sax/sax_printer.rb', line 73 def inline?(name) @inline.include?(name) end |
#keep_linebreaks? ⇒ Boolean
166 167 168 |
# File 'lib/sax/sax_printer.rb', line 166 def keep_linebreaks? @preserve_linebreaks.include?(@open_tag) end |
#nontextual_text(str, op, cl) ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/sax/sax_printer.rb', line 179 def nontextual_text(str, op, cl) if in_block? @depth += 1 increment_space @depth -= 1 end pretty << "#{op}#{str}#{cl}" @open_tag = nil end |
#processing_instruction(name, content) ⇒ Object
32 33 34 |
# File 'lib/sax/sax_printer.rb', line 32 def processing_instruction(name, content) instructions << "<?#{name} #{content}?>" end |
#sanitize(string) ⇒ Object
197 198 199 200 201 |
# File 'lib/sax/sax_printer.rb', line 197 def sanitize(string) string.gsub!(/&/, CCS[:amp][@ccs]) string.gsub!(/</, CCS[:lt][@ccs]) string.gsub!(/>/, CCS[:gt][@ccs]) end |
#self_closing?(name) ⇒ Boolean
123 124 125 |
# File 'lib/sax/sax_printer.rb', line 123 def self_closing?(name) @open_tag == name and !.include?(name) end |
#set_control_vars(options) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/sax/sax_printer.rb', line 47 def set_control_vars() @whitespace = .include?(:preserve_whitespace) ? [:preserve_whitespace] : true = [:close_tags] ? Set.new([:close_tags]) : [] @preserve_linebreaks = [:preserve_linebreaks] ? Set.new([:preserve_linebreaks]) : [] @ccs = [:control_chars] || :named @use_ns = [:use_namespaces] @tab = [:tab] || ' ' end |
#set_element_types(options) ⇒ Object
41 42 43 44 45 |
# File 'lib/sax/sax_printer.rb', line 41 def set_element_types() @block = Set.new([:block]) @compact = Set.new([:compact]) @inline = Set.new([:inline]) end |
#set_options_as_ivars(options) ⇒ Object
56 57 58 59 |
# File 'lib/sax/sax_printer.rb', line 56 def () set_element_types() set_control_vars() end |
#space_after_close(name) ⇒ Object
131 132 133 |
# File 'lib/sax/sax_printer.rb', line 131 def space_after_close(name) increment_space if block_or_compact?(name) and compact?(@opens[-2]) end |
#space_before_close(name) ⇒ Object
127 128 129 |
# File 'lib/sax/sax_printer.rb', line 127 def space_before_close(name) increment_space if block?(name) and @depth != 0 end |
#space_before_open(name) ⇒ Object
102 103 104 |
# File 'lib/sax/sax_printer.rb', line 102 def space_before_open(name) increment_space if block?(name) or compact?(name) end |
#start_document ⇒ Object
36 37 38 39 |
# File 'lib/sax/sax_printer.rb', line 36 def start_document @depth = 0 @opens = [] end |
#start_element(name, attributes) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/sax/sax_printer.rb', line 94 def start_element(name, attributes) @depth += 1 space_before_open(name) add_opening_tag(name, attributes) @opens << name @open_tag = name end |
#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/sax/sax_printer.rb', line 15 def start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) if @use_ns super else start_element(name, attrs.map { |a| [a.localname, a.value] }) end end |
#tag_with_attrs(name, attrs) ⇒ Object
140 141 142 143 |
# File 'lib/sax/sax_printer.rb', line 140 def tag_with_attrs(name, attrs) attr_str = attrs.map { |n, v| "#{n}=\"#{v}\""}.join(' ') "<#{name} #{attr_str}>" end |
#whitespace? ⇒ Boolean
162 163 164 |
# File 'lib/sax/sax_printer.rb', line 162 def whitespace? @whitespace and below_block? end |
#ws_adder ⇒ Object
89 90 91 92 |
# File 'lib/sax/sax_printer.rb', line 89 def ws_adder ws = @tab * (@depth - 1) pretty.empty? ? ws : "\n#{ws}" end |
#ws_only_in_block?(string) ⇒ Boolean
158 159 160 |
# File 'lib/sax/sax_printer.rb', line 158 def ws_only_in_block?(string) string[/\A\s*\Z/] and in_block? end |
#xmldec_attrs(args) ⇒ Object
23 24 25 |
# File 'lib/sax/sax_printer.rb', line 23 def xmldec_attrs(args) XMLDEC_ATTRS.each_with_index.to_a.map { |t, i| " #{t}=\"#{args[i]}\"" if args[i] }.compact.join end |
#xmldecl(*args) ⇒ Object
27 28 29 30 |
# File 'lib/sax/sax_printer.rb', line 27 def xmldecl(*args) opts = xmldec_attrs(args) instructions << "<?xml#{opts}?>" end |