Class: Docbook::Document

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

Constant Summary collapse

ROOT_ELEMENT_MAP =
{
  "article" => Elements::Article,
  "book" => Elements::Book,
  "chapter" => Elements::Chapter,
  "appendix" => Elements::Appendix,
  "preface" => Elements::Preface,
  "part" => Elements::Part,
  "section" => Elements::Section,
  "refentry" => Elements::RefEntry,
  "bibliography" => Elements::Bibliography,
  "glossary" => Elements::Glossary,
  "set" => Elements::Set,
  "reference" => Elements::Reference,
  "topic" => Elements::Topic,
  "dedication" => Elements::Dedication,
  "acknowledgements" => Elements::Acknowledgements,
  "colophon" => Elements::Colophon,
  "index" => Elements::Index,
  "toc" => Elements::Toc,
  "sect1" => Elements::Sect1,
  "sect2" => Elements::Sect2,
  "sect3" => Elements::Sect3,
  "sect4" => Elements::Sect4,
  "sect5" => Elements::Sect5,
  "refsection" => Elements::RefSection,
  "refsect1" => Elements::RefSect1,
  "refsect2" => Elements::RefSect2,
  "refsect3" => Elements::RefSect3,
  "setindex" => Elements::SetIndex,
  "para" => Elements::Para,
  "simplesect" => Elements::Simplesect,
}.freeze

Class Method Summary collapse

Class Method Details

.from_xml(xml_string) ⇒ Docbook::Elements::Book, ...

Parse a DocBook XML string into the appropriate element object.

Parameters:

  • xml_string (String)

    DocBook 5 XML content

Returns:

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/docbook/document.rb', line 45

def from_xml(xml_string)
  root_name = extract_root_element(xml_string)
  raise Docbook::Error, "Empty or invalid XML document" unless root_name

  klass = ROOT_ELEMENT_MAP[root_name]
  unless klass
    raise Docbook::Error,
          "Unsupported DocBook root element: #{root_name}"
  end

  klass.from_xml(xml_string)
end

.supported_root_elementsArray<String>

List all supported root element names.

Returns:

  • (Array<String>)


68
69
70
# File 'lib/docbook/document.rb', line 68

def supported_root_elements
  ROOT_ELEMENT_MAP.select { |_, v| v }.keys
end

.supports?(root_name) ⇒ Boolean

Check if a given root element name is supported.

Parameters:

  • root_name (String)

    the root element name (e.g. "book", "article")

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/docbook/document.rb', line 61

def supports?(root_name)
  klass = ROOT_ELEMENT_MAP[root_name]
  !klass.nil?
end