Class: MindmapDoc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s = nil, root: nil, debug: false) ⇒ MindmapDoc

Returns a new instance of MindmapDoc.



17
18
19
20
21
22
23
# File 'lib/mindmapdoc.rb', line 17

def initialize(s=nil, root: nil, debug: false)

  @root, @tree, @txtdoc, @svg, @debug = root, '', '', '', debug
  puts '@root' + @root.inspect if @debug
  import(s) if s
  
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



15
16
17
# File 'lib/mindmapdoc.rb', line 15

def root
  @root
end

Instance Method Details

#import(raw_s) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mindmapdoc.rb', line 25

def import(raw_s)
  
  s = raw_s.lstrip
  
  if s =~ /^#+ / then
    @txtdoc = s.gsub(/\r/,'')
    @tree, @html = parse_doc s
  else
    @tree, @txtdoc = parse_tree s
  end

  puts ('@tree: ' + @tree.inspect).debug if @debug
  @svg = build_svg(@tree)    
  
end

#load(s = 'mindmap.md') ⇒ Object



41
42
43
44
# File 'lib/mindmapdoc.rb', line 41

def load(s='mindmap.md')
  buffer = File.read(s)
  import(buffer)
end

#save(s = 'mindmap.md') ⇒ Object



188
189
190
191
# File 'lib/mindmapdoc.rb', line 188

def save(s='mindmap.md')
  File.write s, @txtdoc
  'mindmap written to file'
end

#to_htmlObject



115
116
117
# File 'lib/mindmapdoc.rb', line 115

def to_html()
  @html
end

#to_mdObject Also known as: to_doc



182
183
184
# File 'lib/mindmapdoc.rb', line 182

def to_md()
  @txtdoc
end

#to_mindmapdoc(s) ⇒ Object Also known as: to_mmd



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mindmapdoc.rb', line 119

def to_mindmapdoc(s)

  s2 = s.split(/(?=^--?mm-+)/).map do |raw_s|

    if raw_s =~ /^--?mm--/ then
      
      a2 = raw_s[/.*(?=^-{10,})/m].lines
      remaining = ($').lines[1..-1].join
      a2.shift
      content = a2.join
      rooted = content =~ /^# / ? true : false
      puts ('content: ' + content.inspect).debug if @debug
      tree = MindmapDoc.new(content, debug: @debug).to_tree(rooted: rooted)
      "<mindmap>\n%s\n\n----------\n\n%s</mindmap>\n\n%s" \
          % [tree, content, remaining]
      
    else
      raw_s
    end

  end.join()

end

#to_mindmapviz(id: '') ⇒ Object Also known as: to_mmv



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mindmapdoc.rb', line 145

def to_mindmapviz(id: '')
  
  lines = to_tree().lines.map do |raw_label|
    
    label = raw_label.chomp
    "%s # #%s" % [label, label.lstrip.downcase.gsub(' ', '-')]
  end
  
"<?mindmapviz root=\"#{root}\" fields='label, url' delimiter=' # ' " \
      + "id='mindmap#{id}'?>
  
#{lines.join("\n")}
"
  
end

#to_svgObject



163
164
165
# File 'lib/mindmapdoc.rb', line 163

def to_svg()
  @svg
end

#to_tree(rooted: false) ⇒ Object Also known as: to_s



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mindmapdoc.rb', line 167

def to_tree(rooted: false)
      
  if rooted then
    @tree
  else
    
    lines = @tree.lines      
    lines.shift  if lines.first[/^\S/]
    lines.map! {|x| x[2..-1]}.join
  end
  
end

#transform(s) ⇒ Object

parses a string containing 1 or more embedded <mindmap> elements and outputs the SVG and associated doc



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
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mindmapdoc.rb', line 49

def transform(s)
  
  data = []
  a = s.split(/(?=^<mindmap[^>]*>)/)
  puts ('transform: a: ' + a.inspect).debug if @debug
  return s unless a.length > 1
  
  count = 0
  
  a2 = a.map do |x|

    if x =~ /^<mindmap/ then
      
      count += 1
      
      mm, remaining = x[/<mindmap[^>]*>(.*)/m,1].split(/<\/mindmap>/,2)
      
      if @debug then
        puts ('mm: ' + mm.inspect + 
        ' remaining: ' + remaining.inspect).debug
      end
      
      raw_tree, raw_md = mm.split(/-{10,}/,2)
      
      
      if @debug then
        puts ('raw_md: ' + raw_md.inspect +
        ' raw_tree: ' + raw_tree.inspect + 
        ' raw_md: ' + raw_md.inspect).debug
      end
      
      mm1 = MindmapDoc.new raw_tree, debug: @debug

      if raw_md then
        mm2 = MindmapDoc.new raw_md, debug: @debug
        data << mm2.to_mmv(id: count)
      end

      docwidth = x[/<mindmap +docwidth=['"]([^'"]+)/,1]
      
      
      if @debug then
        
        puts ('docwidth: ' + docwidth.inspect +
        ' mm1.to_svg: ' + mm1.to_svg.inspect +
        ' mm2.to_doc: '  + mm2.to_doc.inspect).debug
        
      end
              
      mm_template("!s[](#mindmap#{count})\n\n", mm2.to_doc, 
                  count, docwidth) + remaining
      
    else
      x
    end

  end

  if data.any? then
    a2.join + "\n__DATA__\n\n" + data.join("\n")
  else
    s
  end
  
end