Class: Markdownator::Converters::Xml

Inherits:
Base
  • Object
show all
Defined in:
lib/markdownator/converters/xml.rb

Overview

Converts XML into an indented Markdown outline of elements and their text.

Instance Method Summary collapse

Instance Method Details

#accepts?(_io, stream_info) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/markdownator/converters/xml.rb', line 7

def accepts?(_io, stream_info)
  matches?(stream_info, extensions: %w[xml], mimetypes: %w[application/xml text/xml])
end

#convert(io, stream_info, **_options) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/markdownator/converters/xml.rb', line 11

def convert(io, stream_info, **_options)
  Markdownator.require_optional("nokogiri", feature: "XML conversion")
  doc = Nokogiri::XML(read_all(io, stream_info))
  raise FileConversionError, "Could not parse XML" if doc.root.nil?

  lines = []
  walk(doc.root, 0, lines)
  Result.new(markdown: lines.join("\n"))
end