yatoc 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/lib/yatoc.rb +79 -15
  4. data.tar.gz.sig +0 -0
  5. metadata +24 -4
  6. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3116bb2e59758cdae341cf67cc08c7de2ba860cdd55f35293e685c7649128f07
4
- data.tar.gz: 4f8b47dd477fcde31ce363e9d238a387f6c86d53a1107f509c0a4fb14617327d
3
+ metadata.gz: 5eb5256965dbce2125694049499d87d0e54b4b226ab93b9f8130a54cd3f3e907
4
+ data.tar.gz: 7ac46c4f4bdd682a737e5cfae82b26fe8a319f2f74ea58795e351357e59e8fd3
5
5
  SHA512:
6
- metadata.gz: 5f07baddeaa52820ddf5cf285649a8ea25e58a4ce87ab10cab0d06e27452782e2f20e60247d0aab1cf4aaa32d9040904072c236125ce1356b1bf1d1155170c68
7
- data.tar.gz: 7098a90f83ef5ffb04a8e995809dc2f01adf7f9faee875fa6062edf85d9e0e4e02470a63980e90397702b1d3ff94ed61ae493dcc4a4c5956c828fd4fe63135d5
6
+ metadata.gz: 2b3b8f8e1329c4ec69f14271483d696e1ef602ea05d9dfdca4729d25018d3a283983e775698b5ca29eb958b82bbd71a8f9ca742424398b28ba0cd8b500354bed
7
+ data.tar.gz: 5629d9fa2af81b478095397c994e1f15ec7ff691bf55895fdd18bfade656b79eb5653c914b1d5219aa159c5e3c3eae2bf50e35e4accb88734afdfa2c25cc1054
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/yatoc.rb CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  require 'pxindex'
8
8
  require 'line-tree'
9
+ require 'kramdown'
9
10
 
10
11
 
11
12
  class Yatoc
@@ -13,24 +14,32 @@ class Yatoc
13
14
 
14
15
  attr_reader :to_html, :to_toc, :to_index
15
16
 
16
- def initialize(html, min_sections: 3, numbered: nil, debug: false)
17
+ def initialize(content, min_sections: 3, numbered: nil, debug: false)
17
18
 
18
- @numbered, @debug, @html = numbered, debug, html
19
+ @numbered, @debug, @content = numbered, debug, content
19
20
 
20
- @to_html = if html =~ /<index[^>]+>/ then
21
+
22
+ if content =~ /<index[^>]+>/ then
21
23
 
22
24
  @numbered ||= false
23
- html2 = gen_index(html)
25
+ html2 = gen_index(content)
24
26
  puts 'html2: ' + html2.inspect
25
- "%s\n\n<div class='main'>%s</div>" % [html2, html.sub(/<index[^>]+>/, '')]
27
+ @to_html = "%s\n\n<div class='main'>%s</div>" % \
28
+ [html2, content.sub(/<index[^>]+>/, '')]
26
29
 
27
- elsif html.scan(/<h\d+/).length > min_sections
30
+ elsif content =~ /<ix[^>]+>/ then
31
+
32
+ @numbered ||= false
33
+ html2 = gen_index(content, threshold: nil)
34
+ puts 'html2: ' + html2.inspect
35
+ @to_html = "%s\n\n<div class='main'>%s</div>" % \
36
+ [html2, content.sub(/<ix[^>]+>/, '')]
28
37
 
29
- @unmbered ||= true
30
- gen_toc(html)
38
+ elsif content.scan(/<h\d+/).length > min_sections
39
+
40
+ @numbered ||= true
41
+ gen_toc(content)
31
42
 
32
- else
33
- html
34
43
  end
35
44
 
36
45
  end
@@ -90,9 +99,49 @@ CSS
90
99
  end
91
100
 
92
101
  def to_index(threshold: 5)
93
- gen_index(@html, threshold: threshold)
102
+ gen_index(@content, threshold: threshold)
94
103
  end
95
104
 
105
+ def to_aztoc()
106
+
107
+ a = @content.split(/(?=^# )/).map {|x| x.scan(/^#+ +[^\n]+/)}
108
+
109
+ a2 = a.group_by {|x| x.first[/# +(.)/,1]}.sort
110
+
111
+ a3 = a2.map do |heading, body|
112
+ lists = body.map do |x|
113
+ x.map do |line|
114
+ line.sub(/^(#+)/) {|y| ' ' * (y.length - 1) + '*'}
115
+ end.join("\n")
116
+ end
117
+ ['# ' + heading, lists]
118
+ end.join("\n\n")
119
+
120
+ doc = Rexle.new("<div>%s</div>" % Kramdown::Document.new(a3).to_html)
121
+
122
+ doc.root.xpath('//li').each do |li|
123
+
124
+ pnode = li.parent.parent
125
+
126
+ pg = ''
127
+
128
+ pg = if pnode.name == 'li' then
129
+ pnode.element('a/attribute::href').to_s.strip[/^[^#]+/] + '#' \
130
+ + li.text.to_s.strip.gsub(/ /,'_').downcase
131
+ else
132
+
133
+ li.text.to_s.strip.gsub(/ /,'_')
134
+ end
135
+
136
+ e = Rexle::Element.new('a', attributes: {href: pg}, \
137
+ value: li.text.to_s.strip)
138
+ li.children[0] = e
139
+ end
140
+
141
+ doc.xml
142
+
143
+ end
144
+
96
145
  private
97
146
 
98
147
  def build_html(a)
@@ -109,12 +158,27 @@ CSS
109
158
 
110
159
  end
111
160
 
161
+ def gen_aztoc(html)
162
+
163
+ a = scan_headings html, 1
164
+ puts ('_a: ' + a.inspect).debug if @debug
165
+
166
+ s = make_tree(a,0, 1)
167
+ puts ('s: ' + s.inspect).debug if @debug
168
+
169
+ px = PxIndex.new
170
+ px.import(s)
171
+
172
+ @to_index = "<div id='azindex' class='sidenav'>\n%s\n</div>\n\n" \
173
+ % px.build_html
174
+ end
175
+
112
176
  def gen_index(html, threshold: 5)
113
177
 
114
178
  a = html.split(/(?=<h2)/)
115
179
  puts ('gen_index a: ' + a.inspect).debug if @debug
116
180
 
117
- if a.length < threshold then
181
+ if threshold.nil? or a.length < threshold then
118
182
 
119
183
  index = build_html(a)
120
184
 
@@ -178,7 +242,7 @@ CSS
178
242
  end
179
243
 
180
244
 
181
- def make_tree(a, indent=0)
245
+ def make_tree(a, indent=0, hn=2)
182
246
 
183
247
  if @debug then
184
248
  puts 'inside make_tree'.debug
@@ -193,11 +257,11 @@ CSS
193
257
 
194
258
  puts 'before make_tree()'.info if @debug
195
259
 
196
- make_tree(x, indent+1)
260
+ make_tree(x, indent+1, hn)
197
261
 
198
262
  else
199
263
 
200
- next unless x =~ /<h[2-4]/
264
+ next unless x =~ /<h[#{hn}-4]/
201
265
  space = i == 0 ? indent-1 : indent
202
266
  heading = (' ' * space) + x[/(?<=\>)[^<]+/]
203
267
  puts ('heading: ' + heading.inspect).debug if @debug
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yatoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  7RVV+J+oYjvww0tYfGq6N2tECM5thrQYj3cKLzAEEPYBjoWcPA5bqRXBTNta+k+K
36
36
  tc7DELyeAZoKgZBO2wAc4FQ6
37
37
  -----END CERTIFICATE-----
38
- date: 2019-01-19 00:00:00.000000000 Z
38
+ date: 2019-02-02 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: pxindex
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: '0.7'
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
- version: 0.7.1
69
+ version: 0.7.2
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -76,7 +76,27 @@ dependencies:
76
76
  version: '0.7'
77
77
  - - ">="
78
78
  - !ruby/object:Gem::Version
79
- version: 0.7.1
79
+ version: 0.7.2
80
+ - !ruby/object:Gem::Dependency
81
+ name: kramdown
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.1.0
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.1.0
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '2.1'
80
100
  description:
81
101
  email: james@jamesrobertson.eu
82
102
  executables: []
metadata.gz.sig CHANGED
Binary file