Class: Docbook::Mirror::Handlers::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/docbook/mirror/handlers/reference.rb

Class Method Summary collapse

Class Method Details

.refentry(element, context:) ⇒ Object



25
26
27
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/docbook/mirror/handlers/reference.rb', line 25

def self.refentry(element, context:)
  title = context.resolve_refentry_title(element)
  id = context.refentry_id(element)

  attrs = {
    xml_id: id,
    title: title,
  }.compact

  content = []

  # Build synopsis from refmeta
  if element.refmeta
    if element.refmeta.fieldsynopsis && !element.refmeta.fieldsynopsis.empty?
      fs = element.refmeta.fieldsynopsis.first
      parts = []
      if fs.type && !fs.type.content.join.empty?
        parts << Node::Text.new(
          text: fs.type.content.join, marks: [Mark::Code.new(role: "type")],
        )
      end
      parts << Node::Text.new(text: " ")
      if fs.varname && !fs.varname.content.join.empty?
        parts << Node::Text.new(
          text: fs.varname.content.join, marks: [Mark::Code.new(role: "varname")],
        )
      end
      if fs.initializer && !fs.initializer.content.join.empty?
        parts << Node::Text.new(text: " = ")
        parts << Node::Text.new(
          text: fs.initializer.content.join, marks: [Mark::Code.new(role: "literal")],
        )
      end
      unless parts.empty?
        content << Node::Paragraph.new(
          attrs: { class: "refmeta-synopsis" },
          content: parts,
        )
      end
    elsif element.refmeta.refentrytitle
      synopsis_parts = []
      reftitle = element.refmeta.refentrytitle.content.join
      manvol = element.refmeta.manvolnum
      synopsis_parts << "#{reftitle}(#{manvol})" if reftitle && manvol
      synopsis_parts << reftitle.to_s if reftitle && !manvol
      Array(element.refmeta.refmiscinfo).each do |info|
        synopsis_parts << info.content.join if info.content.any?
      end
      unless synopsis_parts.empty?
        content << Node::Paragraph.new(
          content: [Node::Text.new(
            text: synopsis_parts.join(""),
            marks: [Mark::Code.new],
          )],
        )
      end
    end
  end

  # refnamediv is a mapped attribute, not mixed content — process explicitly
  if element.refnamediv
    content.concat(refnamediv(element.refnamediv, context: context))
  end

  content.concat(context.extract_content(element))
  Node::RefEntry.new(attrs: attrs, content: content)
end

.reference(element, context:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/docbook/mirror/handlers/reference.rb', line 7

def self.reference(element, context:)
  attrs = {
    xml_id: element.xml_id || "elem-#{element.object_id}",
    title: context.resolve_title(element),
  }.compact

  content = []

  # refentry is a mapped attribute, not mixed content
  element.refentry.each do |re|
    entry = refentry(re, context: context)
    content << entry if entry
  end

  content.concat(context.extract_content(element))
  Node::Reference.new(attrs: attrs, content: content)
end

.refnamediv(element, context:) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/docbook/mirror/handlers/reference.rb', line 93

def self.refnamediv(element, context:)
  content = []

  # Render refnames (e.g. "$v:as-json")
  names = Array(element.refname).filter_map { |r| r.content.join }
  unless names.empty?
    content << Node::Paragraph.new(
      content: [Node::Text.new(
        text: names.join(", "),
        marks: [Mark::Code.new],
      )],
    )
  end

  # Render refpurpose
  if element.refpurpose&.content&.any?
    content << Node::Paragraph.new(
      attrs: { class: "refpurpose" },
      content: [Node::Text.new(text: element.refpurpose.content.join)],
    )
  end

  # Render refclass as a badge
  if element.refclass&.content&.any?
    content << Node::Paragraph.new(
      attrs: { class: "refclass" },
      content: [Node::Text.new(
        text: element.refclass.content.join,
        marks: [Mark::Code.new(role: "refclass")],
      )],
    )
  end

  content
end

.refsection(element, context:) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/docbook/mirror/handlers/reference.rb', line 129

def self.refsection(element, context:)
  title = context.resolve_title(element)
  id = element.xml_id || "elem-#{element.object_id}"
  attrs = { xml_id: id, title: title }.compact

  content = context.extract_content(element)
  Node::RefSection.new(attrs: attrs, content: content)
end