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: 'root', debug: false) ⇒ MindmapDoc

Returns a new instance of MindmapDoc.



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

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

  @root, @tree, @txtdoc, @svg, @debug = root, '', '', '', debug
  import(s) if s
  
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



11
12
13
# File 'lib/mindmapdoc.rb', line 11

def root
  @root
end

Instance Method Details

#import(raw_s) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mindmapdoc.rb', line 20

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

  @svg = build_svg(@tree)    
  
end

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



35
36
37
38
# File 'lib/mindmapdoc.rb', line 35

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

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



98
99
100
101
# File 'lib/mindmapdoc.rb', line 98

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

#to_htmlObject



69
70
71
# File 'lib/mindmapdoc.rb', line 69

def to_html()
  @html
end

#to_mdObject Also known as: to_doc



92
93
94
# File 'lib/mindmapdoc.rb', line 92

def to_md()
  @txtdoc
end

#to_svgObject



73
74
75
# File 'lib/mindmapdoc.rb', line 73

def to_svg()
  @svg
end

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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mindmapdoc.rb', line 77

def to_tree(rooted: false)
      
  if rooted then
    @root + "\n" + @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



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

def transform(s)
  
  a = s.split(/(?=<mindmap>)/)

  count = 0
  
  a2 = a.map do |x|

    if x =~ /^<mindmap>/ then
      
      count += 1
      
      mm, remaining = x[/<mindmap>(.*)/m,1].split(/<\/mindmap>/,2)
      raw_tree, raw_md = mm.split(/-{10,}/,2)
      mm1, mm2 = [raw_tree, raw_md].map {|x| MindmapDoc.new x}
      
      mm_template(mm1.to_svg, mm2.to_doc, count)    
      
    else
      x
    end

  end.join
  
end