Module: Rubyword::Writer::Part::Toc

Included in:
Document
Defined in:
lib/rubyword/writer/part/toc.rb

Instance Method Summary collapse

Instance Method Details

#write_toc(rubyword, xml) ⇒ Object



6
7
8
9
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
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
# File 'lib/rubyword/writer/part/toc.rb', line 6

def write_toc(rubyword, xml)
  return if rubyword.toc.nil? || !rubyword.toc[:open]
  @xml = xml
  rubyword.sections.each_with_index do |section, section_index|
    next if section.titles.nil?
    section.titles.each_with_index do |title, title_index|
      rid = title[:rid]
      indent = 0

      @xml.send('w:p') {
        @xml.send('w:pPr') {
          @xml.send('w:tabs') {
            @xml.send('w:tab', 'w:val' => 'right', 'w:leader' => 'dot', 'w:pos' => '9062')
          }

          # According title size to set tab index, detail: rubyword/element/text.rb
          if title[:indent] > 0
            @xml.send('w:ind', 'w:left' => title[:indent])
          end
        }
        
        # initailize in first loop
        if section_index == 0 && title_index == 0
          @xml.send('w:r') {
            @xml.send('w:fldChar', 'w:fldCharType' => 'begin')
          }
      
          @xml.send('w:r') {
            @xml.send('w:instrText', {'xml:space' => 'preserve'}, "TOC \\o 1-10 \\h \\z \\u") # What does '1-13' mean?
          }

          @xml.send('w:r') {
            @xml.send('w:fldChar', 'w:fldCharType' => 'separate')
          }
        end

        @xml.send('w:hyperlink', 'w:anchor' => "_Toc#{rid}", 'w:history' => 1) {
          @xml.send('w:r') {
            # add font style
            if rubyword.toc[:font_size]
              @xml.send('w:rPr') {
                @xml.send('w:sz', 'w:val' => rubyword.toc[:font_size])
                @xml.send('w:szCs', 'w:val' => rubyword.toc[:font_size])
              }
            end
            # add text
            @xml.send('w:t', title[:text])
          }

          @xml.send('w:r') {
            @xml.send('w:tab')
          }

          @xml.send('w:r') {
            @xml.send('fldChar', 'w:fldCharType' => 'begin')
          }

          @xml.send('w:r') {
            @xml.send('w:instrText', {'xml:space' => 'preserve'}, "PAGEREF _Toc#{rid} \\h")
          }

          # page numbers
          # @xml.send('w:r') {
          #   @xml.send('w:t', 1)
          # }

          @xml.send('w:r') {
            @xml.send('w:fldChar', 'w:fldCharType' => 'end')
          }
        }
      }
    end
  end
  # end of fldchar
  @xml.send('w:p') {
    @xml.send('w:r') {
      @xml.send('w:fldChar', 'w:fldCharType' => 'end')
    }
  }
end