Class: Docbook::Services::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/docbook/services/formatter.rb

Overview

Formats/prettifies DocBook XML documents.

Instance Method Summary collapse

Constructor Details

#initialize(input_path:, resolve_xinclude: false) ⇒ Formatter

Returns a new instance of Formatter.



7
8
9
10
# File 'lib/docbook/services/formatter.rb', line 7

def initialize(input_path:, resolve_xinclude: false)
  @input_path = input_path
  @resolve_xinclude = resolve_xinclude
end

Instance Method Details

#formatString

Returns pretty-printed XML.

Returns:

  • (String)

    pretty-printed XML



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/docbook/services/formatter.rb', line 13

def format
  xml_string = File.read(@input_path)

  if @resolve_xinclude
    xml_string = Docbook::XIncludeResolver.resolve_string(xml_string,
                                                          base_path: @input_path).to_xml
  end

  parsed = Docbook::Document.from_xml(xml_string)
  parsed.to_xml(pretty: true, declaration: true, encoding: "utf-8")
end