Browse files

Append / if needed

Fix line end for end of file
Deal with @command
  • Loading branch information...
1 parent b336494 commit a338a394d535022cbc895ced11f1f34f294e9791 @kwerle committed Sep 23, 2012
Showing with 27 additions and 9 deletions.
  1. +27 −9 lib/redcar-xiki.rb
View
36 lib/redcar-xiki.rb
@@ -7,6 +7,14 @@ module Xiki
# Execute a xiki command
class Execute < ::Redcar::DocumentCommand
+ COMMAND_PATH = '/'
+ COMMAND_PARENT = '+'
+ COMMAND_CLOSED_PARENT = '-'
+ COMMAND_SHELL = '$'
+
+ COMMANDS = [COMMAND_PATH, COMMAND_PARENT, COMMAND_CLOSED_PARENT, COMMAND_SHELL]
+
+ COMMAND_REGEX = "([#{COMMANDS * '\\'}]) (.+)\s*$"
def current_line_text
doc.get_line(doc.cursor_line)
@@ -22,35 +30,45 @@ def build_up_menu(line, indent, menu_array)
target_indent = indent.sub(/ $/,'') # The indent needs to be less than the previous one
while (line = line - 1) >= 0
# $stdout << "line: #{line} #{doc.offset_at_line(line)}\n"
- # line_text = doc.get_line(doc.offset_at_line(line))
line_text = doc.get_line(line) # let's get some text
# $stdout << "line0: #{line_text}\n"
- target_match = line_text.match(/^#{target_indent}\+\s*(\w+)/) # Trying to match "<some spaces>+ <menu>"
+ target_match = line_text.match(/(^#{target_indent})#{COMMAND_REGEX}/) # Trying to match "<some spaces>+ <menu>"
# $stdout << "line1: #{target_match}\n"
- return build_up_menu(line, target_indent, [target_match.captures.first] + menu_array) if target_match
+ return process_match(line, target_indent, menu_array, target_match) if target_match
# $stdout << "line2: #{line_text}\n"
next if line_text.match(/^#{target_indent}[ |]/) # next one up if it has more spaces or starts with a |
end
return menu_array
end
+ def process_match(line_number, indent, menu_array, match)
+ indent, command, text = match.captures
+ return [text.gsub(/^@/, '')] + menu_array if text.start_with?('@')
+ return build_up_menu(line_number, indent, [text] + menu_array)
+ end
+
# Here we are. We have been been called.
- # We look for "[spaces]+[spaces]word". That's probably not good enough, but it's a start.
+ # We look for "[spaces]<command_type> word[s]". That's probably not good enough, but it's a start.
# If we find it, we call build_up_menu to see if we have a parent.
#
# This is not how this should work. We should have a xiki client and we should pass
# the document to it.
def execute()
current_line = doc.cursor_line
- matches = current_line_text.match(/^(\s*)\+\s*(\w+)/)
+ matches = current_line_text.match(/^(\s*)#{COMMAND_REGEX}/)
return if matches.nil?
- indent, text = matches.captures
- return if text.nil?
- text = build_up_menu(current_line, indent, text) * '/'
+ indent, command, text = matches.captures
+ # Make sure the command ends with '/'
+ unless text.end_with? '/'
+ text << '/'
+ end
+ doc.replace_line(doc.cursor_line, "#{indent}#{command} #{text}")
+ text = process_match(doc.cursor_line, indent, [], matches)
response = `xiki #{text}`
response.gsub!(/^/, indent + ' ')
+ doc.insert(doc.offset_at_line_end(doc.cursor_line), "\n") unless current_line_text.end_with?("\n")
+ doc.insert(doc.offset_at_line_end(doc.cursor_line), response)
# $stdout << "text: #{text}\n"
- doc.insert(doc.offset_at_line(current_line + 1), response)
end
end

0 comments on commit a338a39

Please sign in to comment.