Method: Tree.replace_item

Defined in:
lib/xiki/tree.rb

.replace_item(txt) ⇒ Object

Replace last path item with this string.

Tree.replace_item “replaces whole line” Tree.replace_item “replaces after slash” # /hey Tree.replace_item “replaces after slash” # /hey/



1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
# File 'lib/xiki/tree.rb', line 1831

def self.replace_item txt

  new_ends_in_slash = txt =~ /\/$/   # If slash at end, remember to not add another one

  # If there's a slash (not counting end of line), replace after last slash

  if Line.value =~ /\/.+/
    (! new_ends_in_slash && Line.sub!(/(.+)\/.+\/$/, "\\1/#{txt}/")) ||
      Line.sub!(/(.+)\/.+/, "\\1/#{txt}")
  else   # else, just replace whole line minus bullet
    (! new_ends_in_slash && Line.sub!(/^([ +-]*).+\//, "\\1#{txt}\/")) ||
      Line.sub!(/^([ +-]*).+/, "\\1#{txt}")
  end

  nil
end