Class: Heading

Inherits:
Paragraph show all
Defined in:
lib/almirah/doc_items/heading.rb

Constant Summary collapse

@@global_section_number =
''

Constants included from HtmlSafe

HtmlSafe::ALLOWED_URL_SCHEMES

Instance Attribute Summary collapse

Attributes inherited from Paragraph

#text

Attributes inherited from DocItem

#parent_doc, #parent_heading

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Paragraph

#getTextWithoutSpaces

Methods inherited from DocItem

#owner_document, #split_table_cells

Methods inherited from TextLine

#bold, #bold_and_italic, broken_links, #format_string, #inline_code, #italic, #link, link_registry, link_registry=, #literal_text, #owner_document, record_broken_link, reset_broken_links, #wiki_link

Methods included from HtmlSafe

#escape_attr, #escape_text, #safe_url

Methods inherited from TextLineBuilderContext

#bold, #bold_and_italic, #inline_code, #italic, #link, #literal_text, #wiki_link

Constructor Details

#initialize(doc, text, level) ⇒ Heading

Returns a new instance of Heading.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/almirah/doc_items/heading.rb', line 10

def initialize(doc, text, level)
  super(doc, text)
  @level = level

  if level != 0 # skip Doc Title
    if @@global_section_number == ''
      @@global_section_number = '1'
      (1..(level - 1)).each do |_n|
        @@global_section_number += '.1'
      end
    else
      previous_level = @@global_section_number.split('.').length

      if previous_level == level

        a = @@global_section_number.split('.')
        a[-1] = (a[-1].to_i + 1).to_s
        @@global_section_number = a.join('.')

      elsif level > previous_level

        a = @@global_section_number.split('.')
        a.push('1')
        @@global_section_number = a.join('.')

      else # level < previous_level

        a = @@global_section_number.split('.')
        delta = previous_level - level
        a.pop(delta)
        @@global_section_number = a.join('.')
        # increment
        a = @@global_section_number.split('.')
        a[-1] = (a[-1].to_i + 1).to_s
        @@global_section_number = a.join('.')
      end
    end
  end
  @section_number = @@global_section_number
  @anchor_id = get_anchor_text
end

Instance Attribute Details

#anchor_idObject

Returns the value of attribute anchor_id.



6
7
8
# File 'lib/almirah/doc_items/heading.rb', line 6

def anchor_id
  @anchor_id
end

#levelObject

Returns the value of attribute level.



6
7
8
# File 'lib/almirah/doc_items/heading.rb', line 6

def level
  @level
end

#section_numberObject

Returns the value of attribute section_number.



6
7
8
# File 'lib/almirah/doc_items/heading.rb', line 6

def section_number
  @section_number
end

Class Method Details

.reset_global_section_numberObject



108
109
110
# File 'lib/almirah/doc_items/heading.rb', line 108

def self.reset_global_section_number
  @@global_section_number = ''
end

Instance Method Details

#anchor_slugObject

The anchor is a generated identifier emitted into name/href attributes, so it must not carry the HTML-significant characters that author heading text may contain (ADR-188). Stripping them keeps the anchor inert and self-consistent across the heading, its self-link, and the table of contents.



78
79
80
# File 'lib/almirah/doc_items/heading.rb', line 78

def anchor_slug
  getTextWithoutSpaces.gsub(/[<>"'&]/, '')
end

#get_anchor_textObject



70
71
72
# File 'lib/almirah/doc_items/heading.rb', line 70

def get_anchor_text
  "#{@section_number}-#{anchor_slug}"
end

#get_markdown_anchor_textObject



82
83
84
# File 'lib/almirah/doc_items/heading.rb', line 82

def get_markdown_anchor_text
  getTextWithoutSpaces
end

#get_section_infoObject



52
53
54
55
56
57
58
# File 'lib/almirah/doc_items/heading.rb', line 52

def 
  if level.zero? # Doc Title
    @text
  else
    "#{@section_number} #{@text}"
  end
end

#get_section_info_htmlObject

As get_section_info but with the author-supplied text HTML-escaped for safe interpolation into rendered HTML (headings, TOC, traceability). ADR-188/SRS-096.



62
63
64
65
66
67
68
# File 'lib/almirah/doc_items/heading.rb', line 62

def 
  if level.zero? # Doc Title
    escape_text(@text)
  else
    "#{@section_number} #{escape_text(@text)}"
  end
end

#get_urlObject



104
105
106
# File 'lib/almirah/doc_items/heading.rb', line 104

def get_url
  "./specifications/#{parent_doc.id}/#{parent_doc.id}.html\##{@anchor_id}"
end

#to_htmlObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/almirah/doc_items/heading.rb', line 86

def to_html
  s = ''
  if @@html_table_render_in_progress
    s += "</table>\n"
    @@html_table_render_in_progress = false
  end
  heading_level = level.to_s
  heading_text = 
  if level.zero?
    heading_level = 1.to_s        # Render Doc Title as a regular h1
    heading_text = escape_text(@text) # Doc Title does not have a section number
  end
  s += "<a name=\"#{@anchor_id}\"></a>\n"
  s += "<h#{heading_level}> #{heading_text} <a href=\"\##{@anchor_id}\" class=\"heading_anchor\">"
  s += "&para;</a></h#{heading_level}>"
  s
end