Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Climb the tree to build up the menu

  • Loading branch information...
commit b3364941f1270a0f0ccd553fdf45e3529b3eef46 1 parent cd43cbd
Kurt Werle authored September 22, 2012
38  lib/redcar-xiki.rb
@@ -12,10 +12,44 @@ def current_line_text
12 12
         doc.get_line(doc.cursor_line)
13 13
       end
14 14
       
  15
+      # This belongs in a xiki client.
  16
+      # Called when someone found something to execute.  We're going to crawl up the
  17
+      # document one line at a time and build up the tree.
  18
+      def build_up_menu(line, indent, menu_array)
  19
+        menu_array = Array(menu_array)
  20
+        return menu_array unless indent.length > 0 # If we're on the left edge, we have no parent
  21
+# $stdout << "working on: #{menu_array * ', '}\n"
  22
+        target_indent = indent.sub(/  $/,'') # The indent needs to be less than the previous one
  23
+        while (line = line - 1) >= 0
  24
+# $stdout << "line: #{line} #{doc.offset_at_line(line)}\n"
  25
+          # line_text = doc.get_line(doc.offset_at_line(line))
  26
+          line_text = doc.get_line(line) # let's get some text
  27
+# $stdout << "line0: #{line_text}\n"
  28
+          target_match = line_text.match(/^#{target_indent}\+\s*(\w+)/) # Trying to match "<some spaces>+ <menu>"
  29
+# $stdout << "line1: #{target_match}\n"
  30
+          return build_up_menu(line, target_indent, [target_match.captures.first] + menu_array) if target_match
  31
+# $stdout << "line2: #{line_text}\n"
  32
+          next if line_text.match(/^#{target_indent}[ |]/) # next one up if it has more spaces or starts with a |
  33
+        end
  34
+        return menu_array
  35
+      end
  36
+      
  37
+      # Here we are.  We have been been called.
  38
+      # We look for "[spaces]+[spaces]word".  That's probably not good enough, but it's a start.
  39
+      # If we find it, we call build_up_menu to see if we have a parent.
  40
+      #
  41
+      # This is not how this should work.  We should have a xiki client and we should pass
  42
+      # the document to it.
15 43
       def execute()
16 44
         current_line = doc.cursor_line
17  
-        text = 'world' if text.nil? || text == ''
18  
-        response = `xiki #{current_line_text}`
  45
+        matches = current_line_text.match(/^(\s*)\+\s*(\w+)/)
  46
+        return if matches.nil?
  47
+        indent, text = matches.captures
  48
+        return if text.nil?
  49
+        text = build_up_menu(current_line, indent, text) * '/'
  50
+        response = `xiki #{text}`
  51
+        response.gsub!(/^/, indent + '  ')
  52
+# $stdout << "text: #{text}\n"
19 53
         doc.insert(doc.offset_at_line(current_line + 1), response)
20 54
       end
21 55
     end
2  lib/redcar-xiki/version.rb
... ...
@@ -1,5 +1,5 @@
1 1
 module Redcar
2 2
   module Xiki
3  
-    VERSION = "0.0.1"
  3
+    VERSION = "0.0.2"
4 4
   end
5 5
 end

0 notes on commit b336494

Please sign in to comment.
Something went wrong with that request. Please try again.