Class: Yatoc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, min_sections: 3, numbered: nil, debug: false) ⇒ Yatoc

Returns a new instance of Yatoc.



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
# File 'lib/yatoc.rb', line 17

def initialize(content, min_sections: 3, numbered: nil, debug: false)
  
  @numbered, @debug, @content = numbered, debug, content

  
  @to_html = if content =~ /<index[^>]+>/ then

    @numbered ||= false
    puts '1. ready to gen_index'.info if @debug
    html2 = gen_index(content)
    puts 'html2: ' + html2.inspect
    "%s\n\n<div class='main'>%s</div>" % \
        [html2, content.sub(/<index[^>]+>/, '')]
    
  elsif content =~ /<ix[^>]+>/ then

    @numbered ||= false
    puts '2. ready to gen_index'.info if @debug
    html2 = gen_index(content, threshold: nil)
    puts 'html2: ' + html2.inspect
    "%s\n\n<div class='main'>%s</div>" % \
        [html2, content.sub(/<ix[^>]+>/, '')]      
    
  elsif content.scan(/<h\d+/).length > min_sections
    
    @numbered ||= true
    puts 'ready to gen_toc()'.info if @debug
    gen_toc(content)                 
    
  else
    
    content
    
  end      

  
  # note: @to_html is important because this gem is used by the 
  #       Martile gem which expect to pass HTML through to render any TOCs.
  
end

Instance Attribute Details

#to_htmlObject (readonly)

Returns the value of attribute to_html.



15
16
17
# File 'lib/yatoc.rb', line 15

def to_html
  @to_html
end

#to_index(threshold: 5) ⇒ Object (readonly)

Returns the value of attribute to_index.



15
16
17
# File 'lib/yatoc.rb', line 15

def to_index
  @to_index
end

#to_tocObject (readonly)

Returns the value of attribute to_toc.



15
16
17
# File 'lib/yatoc.rb', line 15

def to_toc
  @to_toc
end

Instance Method Details

#to_aztocObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/yatoc.rb', line 118

def to_aztoc()
  
  a = @content.split(/(?=^# )/).map {|x| x.scan(/^#+ +[^\n]+/)}

  a2 = a.group_by {|x| x.first[/# +(.)/,1]}.sort

  s = a2.map do |heading, body|
    lists = body.map do |x|
      x.map do |line| 
        line.sub(/^(#+)/) {|y| '  ' * (y.length - 1) + '*'}
      end.join("\n")
    end.join("\n") + "\n"
    ['# ' + heading, lists]
  end.join("\n")
  
  puts 'to_aztoc | s: ' + s if @debug
  
  doc = Rexle.new("<div>%s</div>" % Kramdown::Document.new(s).to_html)
  
  doc.root.xpath('//li').each do |li|

    pnode = li.parent.parent

    pg = ''

    pg = if pnode.name == 'li' then
      pnode.element('a/attribute::href').to_s.strip[/^[^#]+/] + '#' \
          + li.text.to_s.strip.gsub(/ /,'_').downcase
    else

      li.text.to_s.strip.gsub(/ /,'_')
    end
    
    puts 'pg: ' + pg.inspect if @debug

    e = Rexle::Element.new('a', attributes: {href: pg}, \
                           value: li.text.to_s.strip)
    li.children[0] = e
  end    
  
  doc.xml
  
end

#to_cssObject

use in conjunction with the <ix/> tag to render a sidebar



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/yatoc.rb', line 60

def to_css()
  
<<CSS
.sidenav {
border-top: 1px solid #9ef;
border-bottom: 1px solid #9ef;
width: 130px;
position: fixed;
z-index: 1;
top: 80px;
left: 10px;
background: transparent;
overflow-x: hidden;
padding: 8px 0;
}
.sidenav ul {
  background-color: transparent; margin: 0.3em 0.3em 0.3em 0.9em; 
  padding: 0.3em 0.5em;   color: #5af;
}
  
  .sidenav ul li {
    background-color: transparent; margin: 0.3em 0.1em; 
    padding: 0.2em
  }

.sidenav a {
color: #5af;
padding: 6px 8px 6px 8px;
text-decoration: none;
font-size: 0.9em;

}


.sidenav a:focus { color: #1e2; }
.sidenav a:hover { color: #1e2; }
.sidenav a:active { color: #1e2; }


a:link:active, a:visited:active {
color: (internal value);
}

.main {
margin-left: 140px; /* Same width as the sidebar + left position in px */
font-size: 1.0em; /* Increased text to enable scrolling */
padding: 0px 10px;
}

}
CSS

end