Module: CLIMarkdown::TaskPaper

Extended by:
Colors
Defined in:
lib/mdless/taskpaper.rb

Constant Summary collapse

TASK_RX =
/^(?<indent>(?:    |\t)*?)(?<marker>-)(?<task>\s+\S.*?)$/
PROJECT_RX =
/^(?<indent>(?:    |\t)*?)(?<project>[^- \t].*?:)(?<tags> +@\S+)*$/
NOTE_RX =
/^(?<indent>(?:    |\t)+)(?<note>(?<!- ).*?(?!:))$/

Constants included from Colors

Colors::COLORS, Colors::ESCAPE_REGEX

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Colors

blackout, c, last_color_code, remove_pre_post, size_clean, uncolor, uncolor!, unpad, wrap

Class Attribute Details

.theme=(value) ⇒ Object (writeonly)

Sets the attribute theme

Parameters:

  • value

    the value to set the attribute theme to.



11
12
13
# File 'lib/mdless/taskpaper.rb', line 11

def theme=(value)
  @theme = value
end

Class Method Details

.color(key) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mdless/taskpaper.rb', line 13

def color(key)
  val = nil
  keys = key.split(/[ ,>]/)
  if MDLess.theme.key?(keys[0])
    val = MDLess.theme[keys.shift]
  else
    @log.error("Invalid theme key: #{key}") unless keys[0] =~ /^text/
    return c([:reset])
  end
  keys.each do |k|
    if val.key?(k)
      val = val[k]
    else
      @log.error("Invalid theme key: #{k}")
      return c([:reset])
    end
  end
  if val.is_a? String
    val = "x #{val}"
    res = val.split(/ /).map(&:to_sym)
    c(res)
  else
    c([:reset])
  end
end

.highlight(input) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mdless/taskpaper.rb', line 103

def highlight(input)
  mc = color('taskpaper marker')
  tc = color('taskpaper task')
  pc = color('taskpaper project')
  nc = color('taskpaper note')

  if MDLess.options[:section]
    matches = []
    MDLess.options[:section].each do |sect|
      matches << section(input, sect)
    end
    input = matches.join("\n")
  end

  input.gsub!(/\t/, '    ')

  input.gsub!(PROJECT_RX) do
    m = Regexp.last_match
    "#{m['indent']}#{pc}#{m['project']}#{m['tags']}"
  end

  input.gsub!(TASK_RX) do
    m = Regexp.last_match
    "#{m['indent']}#{mc}- #{tc}#{m['task']}"
  end

  input.gsub!(NOTE_RX) do
    m = Regexp.last_match
    "#{m['indent']}#{nc}#{m['note']}"
  end

  input
end

.indent(input, idnt) ⇒ Object



74
75
76
77
78
# File 'lib/mdless/taskpaper.rb', line 74

def indent(input, idnt)
  input.split(/\n/).map do |line|
    line.sub(/^#{idnt}/, '')
  end.join("\n")
end

.is_taskpaper?(input) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mdless/taskpaper.rb', line 39

def is_taskpaper?(input)
  return true if MDLess.file =~ /\.taskpaper$/

  projects = sections(input)

  tasks = 0
  if projects.count > 1
    projects.each do |proj, content|
      tasks += content['content'].scan(TASK_RX).count
    end
  end

  if tasks >= 6
    return true
  else
    tst = input.dup.remove_meta
    tst = tst.gsub(PROJECT_RX, '')
    tst = tst.gsub(TASK_RX, '')
    tst = tst.gsub(NOTE_RX, '')
    tst = tst.gsub(/^ *\n$/, '')
    return tst.strip.length == 0
  end
end

.list_projects(input) ⇒ Object



96
97
98
99
100
101
# File 'lib/mdless/taskpaper.rb', line 96

def list_projects(input)
  projects = input.to_enum(:scan, PROJECT_RX).map { Regexp.last_match }
  projects.delete_if { |proj| proj['project'] =~ /^[ \n]*$/ }
  projects.map! { |proj| "#{color('taskpaper marker')}#{proj['indent']}- #{color('taskpaper project')}#{proj['project'].sub(/:$/, '')}" }
  projects.join("\n")
end

.section(input, string) ⇒ Object



63
64
65
66
# File 'lib/mdless/taskpaper.rb', line 63

def section(input, string)
  sects = sections(input)
  sects_to_s(sects.filter { |k, _| k.downcase =~ string.downcase.to_rx })
end

.sections(input) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mdless/taskpaper.rb', line 80

def sections(input)
  heirarchy = {}
  sects = input.to_enum(:scan, /(?mix)
                                (?<=\n|\A)(?<indent>(?:    |\t)*?)
                                (?<project>[^- \t\n].*?:)\s*(?=\n)
                                (?<content>.*?)
                                (?=\n\k<indent>\S.*?:|\Z)$/).map { Regexp.last_match }
  sects.each do |sect|
    heirarchy[sect['project']] = {}
    heirarchy[sect['project']]['content'] = indent(sect['content'], sect['indent'])
    heirarchy = heirarchy.merge(sections(sect['content']))
  end

  heirarchy
end

.sects_to_s(sects) ⇒ Object



68
69
70
71
72
# File 'lib/mdless/taskpaper.rb', line 68

def sects_to_s(sects)
  sects.map do |k, v|
    "#{k}#{v['content']}"
  end.join("\n")
end