/
outline.rb
47 lines (46 loc) · 1.49 KB
/
outline.rb
1
2
3
4
5
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
# encoding: utf-8
d 'Display the document outline'
command :outline do |c|
c.desc "Limit to level N"
c.flag :l, :level
c.desc "Show file names"
c.switch :f, :files
c.desc "Show titles"
c.switch :t, :titles
c.desc "Show IDs"
c.switch :i, :ids
c.action do |global_options, options, args|
levels = options[:l]
ids = options[:i]
files = options[:f]
titles = options[:t]
titles = true if !ids && !levels && !files || levels && !ids
Glyph['system.quiet'] = true
Glyph.run "generate:document"
Glyph['system.quiet'] = false
puts "====================================="
puts "#{Glyph['document.title']} - Outline"
puts "====================================="
Glyph.document.structure.descend do |n, level|
if n.is_a?(Glyph::MacroNode) then
case
when n[:name].in?(Glyph['system.structure.headers']) then
header = Glyph.document.header?(n[:header].code) rescue nil
next if !header || levels && header.level-1 > levels.to_i
last_level = header.level
h_id = ids ? "[##{header.code}]" : ""
h_title = titles ? "#{header.title} " : ""
text = (" "*(header.level-1))+"- "+h_title+h_id
puts text unless text.blank?
when n[:name] == :include then
if files && n.find_parent{|p| p[:name] == :document && p.is_a?(Glyph::MacroNode)} then
# When using the book or article macros, includes appear twice:
# * in the macro parameters
# * as children of the document macro
puts "=== #{n.param(0)}"
end
end
end
end
end
end