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, 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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mdless/taskpaper.rb', line 112

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



83
84
85
86
87
# File 'lib/mdless/taskpaper.rb', line 83

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

.is_taskpaper?(input) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mdless/taskpaper.rb', line 49

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 = remove_meta(input)
    tst = tst.gsub(PROJECT_RX, '')
    tst = tst.gsub(TASK_RX, '')
    tst = tst.gsub(/^ *\n$/, '')
    return tst.strip.length == 0
  end
end

.list_projects(input) ⇒ Object



105
106
107
108
109
110
# File 'lib/mdless/taskpaper.rb', line 105

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

.remove_meta(input) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/mdless/taskpaper.rb', line 39

def remove_meta(input)
  first_line = input.split("\n").first
  if first_line =~ /(?i-m)^---[ \t]*?$/
    input.sub!(/(?im)^---[ \t]*\n([\s\S]*?)\n[-.]{3}[ \t]*\n/, '')
  elsif first_line =~ /(?i-m)^[\w ]+:\s+\S+/
    input.sub!(/(?im)^([\S ]+:[\s\S]*?)+(?=\n\n)/, '')
  end
  input
end

.section(input, string) ⇒ Object



72
73
74
75
# File 'lib/mdless/taskpaper.rb', line 72

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

.sections(input) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mdless/taskpaper.rb', line 89

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



77
78
79
80
81
# File 'lib/mdless/taskpaper.rb', line 77

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