Module: MdInc::Commands

Defined in:
lib/md_inc/md_inc_commands.rb

Instance Method Summary collapse

Instance Method Details

#between(re1, re2, lines) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/md_inc/md_inc_commands.rb', line 106

def between(re1, re2, lines)
  state = :outside
  output = []
  lines.each do |l|
    if state == :outside && re1 =~ l
      state = :inside
    elsif state == :inside && re2 =~ l
      state = :outside
    else
      output << l if state==:inside
    end
  end
  STDERR.puts "Warning: no output from included file" if output.empty?
  output
end

#code(language, lines) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/md_inc/md_inc_commands.rb', line 98

def code(language, lines)
  if language.nil?
    lines.map {|l| l.rstrip.prepend('    ')}
  else
    ["```#{language}"] + lines + ["```"]
  end
end

#code_inc(path, language = nil, re1 = nil, re2 = nil) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/md_inc/md_inc_commands.rb', line 90

def code_inc(path, language=nil, re1=nil, re2=nil)
  if re1
    code(language, normalize_indent(between(re1, re2, inc(path))))
  else
    code(language, normalize_indent(inc(path)))
  end
end

#contentObject



11
12
13
# File 'lib/md_inc/md_inc_commands.rb', line 11

def content
  @content
end

#evaluate(string) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/md_inc/md_inc_commands.rb', line 53

def evaluate(string)
  begin
    instance_eval(string)
  rescue
    puts "Error evaluating #{string}"
    puts $!
    puts caller
  end
end

#full_path(path) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/md_inc/md_inc_commands.rb', line 3

def full_path(path)
  if options[:base_dir]
    File.join(options[:base_dir], path)
  else
    path
  end
end

#inc(path, recursive = true) ⇒ Object



84
85
86
87
88
# File 'lib/md_inc/md_inc_commands.rb', line 84

def inc(path, recursive=true)
  lines = File.readlines(full_path(path))
  lines.map! &:rstrip
  recursive ? process_lines(lines) : lines
end

#line_type(line) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/md_inc/md_inc_commands.rb', line 41

def line_type(line)
  if %{. ..}.include?(line)
    :text
  elsif line[0,2] == '..'
    :multi_line_cmd
  elsif line[0,1] == '.'
    :single_line_cmd
  else
    :text
  end
end

#normalize_indent(lines) ⇒ Object



130
131
132
133
# File 'lib/md_inc/md_inc_commands.rb', line 130

def normalize_indent(lines)
  min_indent = min_indent(lines)
  lines.map {|l| l[min_indent..-1]}
end

#process(content) ⇒ Object



19
20
21
22
# File 'lib/md_inc/md_inc_commands.rb', line 19

def process(content)
  output = process_lines(content.split("\n"))
  output.flatten.join("\n")
end

#process_file(file_name) ⇒ Object



15
16
17
# File 'lib/md_inc/md_inc_commands.rb', line 15

def process_file(file_name)
  process(File.read(file_name))
end

#process_lines(lines) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/md_inc/md_inc_commands.rb', line 24

def process_lines(lines)
  result = []
  until lines.empty?
    line = lines.shift
    ltype = line_type(line)

    if ltype == :multi_line_cmd
      result +=process_multiline_cmd(line, lines)
    elsif ltype == :single_line_cmd
      result << process_single_line_cmd(line)
    else
      result << line
    end
  end
  result
end

#process_multiline_cmd(line, lines) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/md_inc/md_inc_commands.rb', line 67

def process_multiline_cmd(line, lines)
  content_lines = []
  until lines.empty? || (lines.first =~ /^..end/)
    content_lines << lines.shift
  end
  lines.shift unless lines.empty?
  save_content = @content
  @content = content_lines
  result = evaluate(line[2..-1])
  @content = @save_content
  result
end

#process_single_line_cmd(line) ⇒ Object



63
64
65
# File 'lib/md_inc/md_inc_commands.rb', line 63

def process_single_line_cmd(line)
  evaluate(line[1..-1])
end

#skip(re, lines) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/md_inc/md_inc_commands.rb', line 122

def skip(re, lines)
  output = []
  lines.each do |l|
    output << l unless l =~ re
  end
  output
end

#upcase_contentObject



135
136
137
# File 'lib/md_inc/md_inc_commands.rb', line 135

def upcase_content
  content.map {|l| l.upcase}
end

#x(*args) ⇒ Object



80
81
82
# File 'lib/md_inc/md_inc_commands.rb', line 80

def x(*args)
  []
end