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

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

  @root, @tree, @txtdoc, @svg, @debug = root, '', '', '', 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



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

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



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

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

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



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

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

#to_htmlObject



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

def to_html()
  @html
end

#to_mdObject Also known as: to_doc



155
156
157
# File 'lib/mindmapdoc.rb', line 155

def to_md()
  @txtdoc
end

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mindmapdoc.rb', line 118

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



136
137
138
# File 'lib/mindmapdoc.rb', line 136

def to_svg()
  @svg
end

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



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mindmapdoc.rb', line 140

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



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

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

      if raw_md then
        mm2 = MindmapDoc.new raw_md
        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