class TPS::TaskPaper::Node

Constants

DEFAULTS
TAG_REGEX

Attributes

children[R]
level[RW]
node_type[RW]
parent[R]
text[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/tps/taskpaper.rb, line 33
def initialize(options = {})
  options = DEFAULTS.merge(options)
  options.each { |k, v| instance_variable_set :"@#{k}", v }

  @children = options[:children].map { |data|
    Node.new data.merge(:parent => self)
  }
end
parse_line(line, root={}) click to toggle source

Returns a hash from a line.

The `root` reference serves as the context.

parse_line("\t- Hello", root)
#=> { node_type: :task, level: 2, text: "Hello" }
# File lib/tps/taskpaper.rb, line 115
def self.parse_line(line, root={})
  node = {}

  # find indentation level
  if line =~ /^(\s+)/
    if root[:indent]
      node[:level] = $1.length / root[:indent].length + 1
    else
      root[:indent] = $1
      node[:level] = 2
    end
  else
    node[:level] = 1
  end

  line = line.strip

  # GitHub-style task
  if line =~ /^- \[([ x])\] +(.*)$/
    node[:node_type] = :task
    node[:text] = $1
    node[:text], node[:tags] = parse_text($2)
    node[:tags] << "@done" if $1 == "x"

  elsif line =~ /^([\-x]) +(.*)$/
    node[:node_type] = :task
    node[:text] = $1
    node[:text], node[:tags] = parse_text($2)
    node[:tags] << "@done" if $1 == "x"

  # Project
  elsif line =~ /^(.*):((?:\s*#{TAG_REGEX})+)?$/m
    node[:node_type] = :project
    node[:text], node[:tags] = parse_text("#{$1}#{$2}")

  # GitHub-style project
  elsif line =~ /^#+ (.*)$/
    node[:node_type] = :project
    node[:text], node[:tags] = parse_text($1)

  # Note
  else
    node[:node_type] = :note
    node[:text] = line
  end

  node[:children] = Array.new
  node
end
parse_text(text) click to toggle source
# File lib/tps/taskpaper.rb, line 98
def self.parse_text(text)
  [
    text
      .gsub(TAG_REGEX, '')
      .gsub(/\s*:\s*$/, '')
      .strip,
    text.scan(TAG_REGEX).map(&:strip)
  ]
end

Public Instance Methods

breadcrumbs() click to toggle source
description() click to toggle source
# File lib/tps/taskpaper.rb, line 70
def description
  if children.length > 0
    first_child = children[0]
    return first_child.text  if first_child.note?
  end
end
note?() click to toggle source
# File lib/tps/taskpaper.rb, line 50
def note?
  node_type == :note
end
parent?() click to toggle source
# File lib/tps/taskpaper.rb, line 58
def parent?
  !! parent
end
project?() click to toggle source
# File lib/tps/taskpaper.rb, line 42
def project?
  node_type == :project
end
root?() click to toggle source
# File lib/tps/taskpaper.rb, line 54
def root?
  level == 0
end
tags() click to toggle source
# File lib/tps/taskpaper.rb, line 66
def tags
  @tags || []
end
task?() click to toggle source
# File lib/tps/taskpaper.rb, line 46
def task?
  node_type == :task
end
to_line_s() click to toggle source
# File lib/tps/taskpaper.rb, line 86
def to_line_s
  tags_str = tags.any? ? (tags.map { |t| " #{t}" }.join("")) : ""

  if project?
    "#{text}:#{tags_str}"
  elsif task?
    "- #{text}#{tags_str}"
  else
    "#{text}"
  end
end
to_s() click to toggle source

Returns a TaskPaper document.

# File lib/tps/taskpaper.rb, line 78
def to_s
  indent = root? ? "" : "\t"
  lines = ""
  lines << to_line_s + "\n"  unless root?
  children.each { |node| lines << node.to_s.gsub(/^/, indent) }
  lines
end
walk() { |self| ... } click to toggle source
# File lib/tps/taskpaper.rb, line 165
def walk(&blk)
  results = []
  results << self  if yield(self)
  children.each { |node| results += node.walk(&blk) }
  results
end