Class: Block

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Block

Returns a new instance of Block.



11
12
13
14
15
16
# File 'lib/docme/block.rb', line 11

def initialize(block)
    @attributes = {}
    @is_empty = true

    parse_block(block)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/docme/block.rb', line 8

def attributes
  @attributes
end

#is_emptyObject (readonly)

Returns the value of attribute is_empty.



9
10
11
# File 'lib/docme/block.rb', line 9

def is_empty
  @is_empty
end

Instance Method Details

#parse_block(block) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/docme/block.rb', line 18

def parse_block(block)

    is_docme = 0
    block_flag = 0
    block_attr = ''
    multi_line = ''
    block_content = {}

    # each element in the block represents a line
    block.each do |line|
        strip_line = line.lstrip

        # if line begins with '+' then we are defining an attribute
        if strip_line.rindex('+', 0) == 0

            is_docme = 1
            parts = strip_line.split(':', 2)

            if parts[0].nil? || parts[1].nil?
                is_docme = 0
                next
            end

            # parts[0] == the attribute name
            attribute = clean_attribute(parts[0])

            content = clean_content(parts[1])

            # if the content begins with '{' then we have a regular block
            if content.rindex('{{', 0) == 0
                # go to the next line and look for '-', set block flag
                # add the attribute to the doc
                block_flag = 1
                block_attr = attribute
                next
            end

            # add content to the doc
            @attributes.store(attribute, content)
            next
        end

        # if line begins with a '-' then we are in a block, if  we are in a block but there are no sub attributes then do a multi-line
        if strip_line.rindex('-', 0) == 0 && is_docme == 1
            parts = strip_line.split(':')

            # parts[0] == the attribute name
            attribute = clean_attribute(parts[0])

            content = clean_content(parts[1])

            # if !var and !code, then process as regular attributes
            # put the attribute name
            # put the content
            block_content.store(attribute, content)
            next
        end

        if block_flag == 1 && strip_line.rindex('}}', 0) != 0 && is_docme == 1
            line = clean_code(line)
            multi_line.concat(line)
            next
        end

        # if the block flag is set and we reach the end of a block, then we reached the end of a regular block, unset flag
        if block_flag == 1 && strip_line.rindex('}}', 0) == 0 && is_docme == 1
            block_flag = 0

            if multi_line.length > 0
               @attributes.store(block_attr, multi_line)
            else
               @attributes.store(block_attr, block_content)
            end

            multi_line = ''
            block_attr = nil
            block_content = {}
            next
        end

    end

    @is_empty = @attributes.empty?
end