Class: Asciidoctor::DocTest::IO::XML

Inherits:
Base
  • Object
show all
Defined in:
lib/asciidoctor/doctest/io/xml.rb

Overview

Subclass of Base for XML-based backends.

Examples:

Format of the example's header

<!-- .example-name
  Any text that is not the example's name or an option is considered
  as a description.
  :option_1: value 1
  :option_2: value 1
  :option_2: value 2
  :boolean_option:
-->
<p>The example's content in <strong>HTML</strong>.</p>

<div class="note">The trailing new line (below this) will be removed.</div>

Instance Attribute Summary

Attributes inherited from Base

#file_ext, #path

Instance Method Summary collapse

Methods inherited from Base

#group_names, #initialize, #pair_with, #read_examples, #update_examples, #write_examples

Constructor Details

This class inherits a constructor from Asciidoctor::DocTest::IO::Base

Instance Method Details

#parse(input, group_name) ⇒ void


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asciidoctor/doctest/io/xml.rb', line 28

def parse(input, group_name)
  examples = []
  current = create_example(nil)
  in_comment = false

  input.each_line do |line|
    line.chomp!
    if line =~ /^<!--\s*\.([^ \n]+)/
      name = $1
      current.content.chomp!
      examples << (current = create_example([group_name, name]))
      in_comment = true
    elsif in_comment
      if line =~ /^\s*:([^:]+):(.*)/
        current[$1.to_sym] = $2.blank? ? true : $2.strip
      else
        desc = line.rstrip.chomp('-->').strip
        (current.desc ||= '').concat!(desc, "\n") unless desc.empty?
      end
    else
      current.content.concat!(line, "\n")
    end
    in_comment &= !line.end_with?('-->')
  end

  examples
end

#serialize(examples) ⇒ void


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/asciidoctor/doctest/io/xml.rb', line 56

def serialize(examples)
  Array(examples).map { |exmpl|
    header = [
      ".#{exmpl.local_name}",
      exmpl.desc.presence,
      *format_options(exmpl.opts)
    ].compact

    header_str = header.one? ? (header.first + ' ') : (header.join("\n") + "\n")
    [ "<!-- #{header_str}-->", exmpl.content.presence ].compact.join("\n") + "\n"
  }.join("\n")
end