File: section.rb

package info (click to toggle)
ruby-wikicloth 0.8.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 588 kB
  • ctags: 278
  • sloc: ruby: 2,548; makefile: 14
file content (85 lines) | stat: -rw-r--r-- 1,531 bytes parent folder | download | duplicates (4)
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
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
module WikiCloth

  class Section < String

    def initialize(title=nil, id=nil)
      self.title = title
      @children = []
      @id = id
      @template = nil
      @auto_toc = nil
    end

    def children
      @children
    end

    def id
      @id
    end

    def auto_toc=(val)
      @auto_toc = val
    end

    def template=(val)
      @template = val
    end

    def title=(val)
      if val =~ /^([=]{1,6})\s*(.*?)\s*(\1)/
        @depth = $1.length
        @clean_title = $2
        @title = val
      else
        @depth = 1
        @clean_title = val
        @title = val
      end
    end

    def title
      @clean_title
    end

    def depth
      @depth ||= 1
    end

    def get_section(id)
      return self.wikitext() if self.id == id
      @children.each do |child|
        ret = child.get_section(id)
        return ret unless ret.nil?
      end
      nil
    end

    def wikitext(opt={})
      options = { :replace => {} }.merge(opt)

      if options[:replace][self.id].nil?
        ret = "#{@title}#{self}"
        ret += @children.collect { |c| c.wikitext(options) }.join
        ret
      else
        options[:replace][self.id]
      end
    end

    def render(opt={})
      options = { :noedit => opt[:link_handler].nil? ? true : false }.merge(opt)
      if self.title.nil?
        ret = ""
      else
        ret = "\n#{@title}\n"
      end
      ret += self
      ret += "__TOC__" if @auto_toc
      ret += @children.collect { |c| c.render(options) } .join
      ret
    end

  end

end