Class: Evertils::Helper::ApiEnmlHandler
- Inherits:
-
Object
- Object
- Evertils::Helper::ApiEnmlHandler
- Includes:
- Nokogiri::XML
- Defined in:
- lib/evertils/helpers/api-enml-handler.rb
Instance Method Summary collapse
- #clear_empty ⇒ Object
- #convert_to_xml(enml) ⇒ Object (also: #to_xml)
-
#fix_dtd ⇒ Object
Sometimes, the Doctype declaration gets borked by the XML parser lets replace it with a new DTD if that is the case.
- #from_str(str) ⇒ Object
-
#initialize(conf) ⇒ ApiEnmlHandler
constructor
A new instance of ApiEnmlHandler.
- #prepare ⇒ Object
- #to_enml(hash) ⇒ Object
Constructor Details
#initialize(conf) ⇒ ApiEnmlHandler
Returns a new instance of ApiEnmlHandler.
8 9 10 11 |
# File 'lib/evertils/helpers/api-enml-handler.rb', line 8 def initialize(conf) @config = conf self end |
Instance Method Details
#clear_empty ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/evertils/helpers/api-enml-handler.rb', line 45 def clear_empty @xml.css('div').each do |node| children = node.children if children.size == 1 && children.first.is_a?(Nokogiri::XML::Text) node.remove if node.text.strip.empty? end end @xml end |
#convert_to_xml(enml) ⇒ Object Also known as: to_xml
27 28 29 30 |
# File 'lib/evertils/helpers/api-enml-handler.rb', line 27 def convert_to_xml(enml) @xml = from_str(enml) self end |
#fix_dtd ⇒ Object
Sometimes, the Doctype declaration gets borked by the XML parser lets replace it with a new DTD if that is the case
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/evertils/helpers/api-enml-handler.rb', line 60 def fix_dtd node = @xml.children[0] # the node we are looking at is actually the XML node, skip it if node.is_a?(ProcessingInstruction) node = node.next end # remove the existing broken DTD node.remove # create a new one (note: output is overridden in DTD class defined # below ApiEnmlHandler) dtd = DTD.new('DOCTYPE', @xml) @xml.children.first.before(dtd) end |
#from_str(str) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/evertils/helpers/api-enml-handler.rb', line 15 def from_str(str) str.sub!("\n", '') @xml = DocumentFragment.parse(str) do |conf| conf.noblanks end fix_dtd clear_empty end |
#prepare ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/evertils/helpers/api-enml-handler.rb', line 35 def prepare note_xml = @xml.search('en-note') # remove <br> tags note_xml.search('br').each(&:remove) note_xml.inner_html.to_s end |
#to_enml(hash) ⇒ Object
79 80 81 |
# File 'lib/evertils/helpers/api-enml-handler.rb', line 79 def to_enml(hash) Evertils::Helper::EvernoteENML.with_list(hash) end |