Class: MindmapDoc
- Inherits:
-
Object
- Object
- MindmapDoc
- Includes:
- RXFReadWriteModule
- Defined in:
- lib/mindmapdoc.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #import(raw_s) ⇒ Object
-
#initialize(s = nil, root: nil, debug: false) ⇒ MindmapDoc
constructor
A new instance of MindmapDoc.
- #load(s = 'mindmap.md') ⇒ Object
- #save(s = 'mindmap.md') ⇒ Object
- #to_html ⇒ Object
- #to_md ⇒ Object (also: #to_doc)
-
#to_mindmapdoc(s) ⇒ Object
(also: #to_mmd)
used for finding and parsing mindmapdoc blocks within a Markdown document.
- #to_mindmapviz(id: '') ⇒ Object (also: #to_mmv)
- #to_svg ⇒ Object
- #to_tree(rooted: false) ⇒ Object (also: #to_s)
-
#transform(s) ⇒ Object
parses a string containing 1 or more embedded <mindmap> elements and outputs the SVG and associated doc.
Constructor Details
#initialize(s = nil, root: nil, debug: false) ⇒ MindmapDoc
Returns a new instance of MindmapDoc.
18 19 20 21 22 23 24 |
# File 'lib/mindmapdoc.rb', line 18 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
#root ⇒ Object
Returns the value of attribute root.
16 17 18 |
# File 'lib/mindmapdoc.rb', line 16 def root @root end |
Instance Method Details
#import(raw_s) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mindmapdoc.rb', line 26 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(to_tree(rooted: true)) end |
#load(s = 'mindmap.md') ⇒ Object
42 43 44 45 |
# File 'lib/mindmapdoc.rb', line 42 def load(s='mindmap.md') buffer = FileX.read(s) import(buffer) end |
#save(s = 'mindmap.md') ⇒ Object
191 192 193 194 |
# File 'lib/mindmapdoc.rb', line 191 def save(s='mindmap.md') FileX.write s, @txtdoc 'mindmap written to file' end |
#to_html ⇒ Object
116 117 118 |
# File 'lib/mindmapdoc.rb', line 116 def to_html() @html end |
#to_md ⇒ Object Also known as: to_doc
185 186 187 |
# File 'lib/mindmapdoc.rb', line 185 def to_md() @txtdoc end |
#to_mindmapdoc(s) ⇒ Object Also known as: to_mmd
used for finding and parsing mindmapdoc blocks within a Markdown document
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mindmapdoc.rb', line 122 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/mindmapdoc.rb', line 148 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_svg ⇒ Object
166 167 168 |
# File 'lib/mindmapdoc.rb', line 166 def to_svg() @svg end |
#to_tree(rooted: false) ⇒ Object Also known as: to_s
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/mindmapdoc.rb', line 170 def to_tree(rooted: false) if rooted then @root.chomp + "\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
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 114 |
# File 'lib/mindmapdoc.rb', line 50 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 |