Class: ENML_Listener
- Inherits:
-
Object
- Object
- ENML_Listener
- Includes:
- REXML::SAX2Listener
- Defined in:
- lib/enml-utils.rb
Instance Method Summary collapse
- #characters(text) ⇒ Object
- #end_document ⇒ Object
- #end_element(uri, localname, qname) ⇒ Object
- #files ⇒ Object
-
#initialize(resources, to_text, static_dirs, note_guid) ⇒ ENML_Listener
constructor
A new instance of ENML_Listener.
- #output ⇒ Object
- #process_file(attributes, type, subtype) ⇒ Object
- #start_document ⇒ Object
- #start_element(uri, localname, qname, attributes) ⇒ Object
Constructor Details
#initialize(resources, to_text, static_dirs, note_guid) ⇒ ENML_Listener
Returns a new instance of ENML_Listener.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/enml-utils.rb', line 19 def initialize(resources, to_text, static_dirs, note_guid) @to_text = to_text @files = [] @resources = resources || [] @dirs = static_dirs @note_guid = note_guid @resource_index = {} @resources.each do |res| hash = Digest.hexencode(res.data.bodyHash) @resource_index[hash] = res verbose "Stored index for resource with hash #{hash}" end end |
Instance Method Details
#characters(text) ⇒ Object
125 126 127 |
# File 'lib/enml-utils.rb', line 125 def characters(text) @stack[-1].add_text(text) end |
#end_document ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/enml-utils.rb', line 129 def end_document @output = "" @stack[-1].write(@output) decoder = HTMLEntities.new # One pass of entity decoding for HTML output... @output = decoder.decode(@output) if @to_text # Clean up some tags, extra empty lines, prettyfied characters, etc. @output.gsub!(/<\/?span[^>]*>/, '') @output.gsub!(/\t*<div[^>]*>/, '') @output.gsub!(/<\/div>/, "\n") @output.gsub!(/^\s+$/, '') @output.gsub!(/\n+/, "\n") @output.gsub!(/<br[^>]*\/>/, "\n") @output.gsub!(/“/, '"') @output.gsub!(/”/, '"') @output.gsub!(/‘/, "'") @output.gsub!(/’/, "'") @output.gsub!(/\u{a0}/, " ") # Unicode non-breaking space # ...two passes of decoding for text output. @output = decoder.decode(@output) end end |
#end_element(uri, localname, qname) ⇒ Object
120 121 122 123 |
# File 'lib/enml-utils.rb', line 120 def end_element(uri, localname, qname) new_elem = @stack.pop @stack[-1].add_element(new_elem) unless new_elem.nil? end |
#files ⇒ Object
156 157 158 |
# File 'lib/enml-utils.rb', line 156 def files @files end |
#output ⇒ Object
153 154 155 |
# File 'lib/enml-utils.rb', line 153 def output @output end |
#process_file(attributes, type, subtype) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/enml-utils.rb', line 38 def process_file(attributes, type, subtype) resource = @resource_index[attributes['hash']] if resource.nil? error "An error occurred - I don't have a resource with hash #{attributes['hash']}" return nil else new_file = {} if (!resource.attributes.nil? && !resource.attributes.fileName.nil?) new_file[:basename] = resource.attributes.fileName else new_file[:basename] = "#{attributes['hash']}.#{subtype}" end if @dirs[type] new_file[:fname] = "#{@dirs[type][0]}/#{attributes['hash']}/#{new_file[:basename]}" new_file[:url] = "#{@dirs[type][1]}/#{attributes['hash']}/#{new_file[:basename]}" end new_file[:data] = resource.data.body return new_file end end |
#start_document ⇒ Object
33 34 35 36 |
# File 'lib/enml-utils.rb', line 33 def start_document @outdoc = Document.new @stack = [@outdoc] end |
#start_element(uri, localname, qname, attributes) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 |
# File 'lib/enml-utils.rb', line 59 def start_element(uri, localname, qname, attributes) new_elem = nil if localname == 'en-note' # Convert <en-note> to <span> new_elem = Element.new('span') new_elem.add_attributes(attributes) elsif localname == 'en-todo' unless @to_text new_elem = Element.new('input') new_elem.add_attribute('type', 'checkbox') if attributes and attributes['checked'] == 'true' new_elem.add_attribute('checked', 'checked') end else if attributes and attributes['checked'] == 'true' @stack[-1].add_text("[x] ") else @stack[-1].add_text("[ ] ") end end elsif localname == 'en-media' if attributes['type'] =~ /^image\/(.+)/ subtype = $1 new_file = process_file(attributes, 'image', subtype) if new_file new_elem = Element.new('img') new_elem.add_attributes(attributes) new_elem.add_attribute('src', new_file[:url]) @files.push(new_file) end elsif attributes['type'] =~ /^(audio|video)\/(.*)/ type = $1 subtype = $2 new_file = process_file(attributes, type, subtype) if new_file new_elem = Element.new(type) new_elem.add_attribute('controls', "1") new_elem.add_element 'source', { 'src' => new_file[:url], 'type' => attributes['type'] } new_elem.add_text "Sorry, your browser does not support the #{type} tag." @files.push(new_file) end elsif attributes['type'] =~ /^(\S+)\/(\S+)/ type = $1 subtype = $2 new_file = process_file(attributes, 'files', subtype) if new_file new_elem = Element.new('a') new_elem.add_attribute('href', new_file[:url]) new_elem.add_text(new_file[:basename]) @files.push(new_file) end else error "Sorry, I don't know how to handle attachments of this type: #{attributes['type']}" end else new_elem = Element.new(localname) new_elem.add_attributes(attributes) end @stack.push(new_elem) end |