Top Level Namespace

Defined Under Namespace

Modules: AbstractWikiService, ActiveRecord, ApplicationHelper, Chunk, ChunkManager, Diff, Engines, Enumerable, HTMLDiff, Instiki, Literal, PageLock, RDocSupport, WikiChunk Classes: AdminController, ApplicationController, Author, BlueCloth, Category, ChunkTest, FileController, FileYard, Include, LocalURIChunk, MadeleineServer, NoWiki, Page, PageSet, RedClothForTex, Revision, String, URIChunk, Web, WikiContent, WikiContentStub, WikiController, WikiService

Instance Method Summary collapse

Instance Method Details

#table_of_contents(text, pages) ⇒ Object



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

def table_of_contents(text, pages)
  text.gsub( /^([#*]+? .*?)$(?![^#*])/m ) do |match|
    lines = match.split( /\n/ )
    last_line = -1
    depth = []
    lines.each_with_index do |line, line_id|
      if line =~ /^([#*]+) (.*)$/m
        tl,content = $~[1..2]
        content.gsub! /[\[\]]/, ""
        content.strip!
        
        if depth.last
          if depth.last.length > tl.length
           (depth.length - 1).downto(0) do |i|
              break if depth[i].length == tl.length
              lines[line_id - 1] << "" # "\n\t\\end{#{ lT( depth[i] ) }}\n\t"
              depth.pop
            end
          end
          if !depth.last.nil? && !tl.length.nil? && depth.last.length == tl.length
            lines[line_id - 1] << ''
          end
        end
        
        depth << tl unless depth.last == tl
        
        subsection_depth = [depth.length - 1, 2].min
        
        lines[line_id] = "\n\\#{ "sub" * subsection_depth }section{#{ content }}"
        lines[line_id] += "\n#{pages[content]}" if pages.keys.include?(content)
        
        lines[line_id] = "\\pagebreak\n#{lines[line_id]}" if subsection_depth == 0
        
        last_line = line_id
        
      elsif line =~ /^\s+\S/
        last_line = line_id
      elsif line_id - last_line < 2 and line =~ /^\S/
        last_line = line_id
      end
      if line_id - last_line > 1 or line_id == lines.length - 1
        depth.delete_if do |v|
          lines[last_line] << "" # "\n\t\\end{#{ lT( v ) }}"
        end
      end
    end
    lines.join( "\n" )
  end
end