Class: Livetext::UserAPI

Inherits:
Object show all
Includes:
Standard
Defined in:
lib/livetext/userapi.rb

Overview

Encapsulate the UserAPI as a class

Constant Summary collapse

KBD =
File.new("/dev/tty", "r")
TTY =
File.new("/dev/tty", "w")
DotSpace =

Livetext::Sigil + Livetext::Space

". "

Constants included from Standard

Standard::SimpleFormats

Constants included from Helpers

Helpers::Comment, Helpers::DollarDot, Helpers::DotCmd, Helpers::ESCAPING, Helpers::Sigil, Helpers::Space

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Standard

#backtrace, #banner, #bits, #br, #cinclude, #cleanup, #comment, #copy, #dlist, #dot_def, #dot_include, #errout, #func, #h1, #h2, #h3, #h4, #h5, #h6, #heading, #heredoc, #heredoc!, #image, #import, #inherit, #link, #list, #list!, #mixin, #mono, #newpage, #nopara, #nopass, #para, #quit, #r, #raw, #reflection, #say, #seek, #set, #shell, #shell!, #table, #ttyout, #variables, #variables!, #xtable

Methods included from Helpers

#check_disallowed, #check_file_exists, #escape_html, #find_file, #friendly_error, #get_name_data, #grab_file, #graceful_error, #handle_dollar_dot, #handle_dotcmd, #handle_scomment, #invoke_dotcmd, #onoff, #process_file, #process_line, #read_variables, rx, #search_upward, #set_variables, #setfile, #setfile!, #showme

Constructor Details

#initialize(live) ⇒ UserAPI

Livetext::UserAPI



16
17
18
19
20
21
# File 'lib/livetext/userapi.rb', line 16

def initialize(live)  # Livetext::UserAPI
  @live = live
  @vars = live.vars
  @html = Livetext::HTML.new(self)
  @expander = Livetext::Expansion.new(live)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



14
15
16
# File 'lib/livetext/userapi.rb', line 14

def args
  @args
end

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/livetext/userapi.rb', line 14

def data
  @data
end

Instance Method Details

#apiObject



23
24
25
# File 'lib/livetext/userapi.rb', line 23

def api
  @live.api
end

#body(raw = false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/livetext/userapi.rb', line 128

def body(raw=false)
  lines = []
  end_found = false
  loop do
    @line = @live.nextline
    break if @line.nil?
    @line.chomp!
    break if end?(@line)
    next if comment?(@line)
    @line = format(@line) unless raw
    lines << @line
  end
  raise "Expected .end, found end of file" unless end?(@line)  # use custom exception
  optional_blank_line   # FIXME Delete this??
  return lines unless block_given?
  lines.each {|line| yield line }   # FIXME what about $. ?
end

#body_text(raw = false) ⇒ Object



146
147
148
# File 'lib/livetext/userapi.rb', line 146

def body_text(raw=false)
  raw_body.join("\n")
end

#check_existence(file, msg) ⇒ Object



70
71
72
# File 'lib/livetext/userapi.rb', line 70

def check_existence(file, msg)
  STDERR.puts msg unless File.exist?(file)
end

#comment?(str) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/livetext/userapi.rb', line 100

def comment?(str)
  str.index(DotSpace) == 0
end

#debug(*args) ⇒ Object



214
215
216
# File 'lib/livetext/userapi.rb', line 214

def debug(*args)
  TTY.puts *args if @live.debug
end

#debug=(val) ⇒ Object



210
211
212
# File 'lib/livetext/userapi.rb', line 210

def debug=(val)
  @live.debug = val
end

#dotObject



39
40
41
# File 'lib/livetext/userapi.rb', line 39

def dot
  @live
end

#dump(file = nil) ⇒ Object

not a dot command!



27
28
29
30
31
32
33
# File 'lib/livetext/userapi.rb', line 27

def dump(file = nil)   # not a dot command!
  file ||= ::STDOUT
  # TTY.puts "--- Writing body (#{@live.body.size} bytes)"
  file.puts @live.body
rescue => err
  TTY.puts "#dump had an error: #{err.inspect}"
end

#end?(str) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/livetext/userapi.rb', line 109

def end?(str)
  return true if str == ".end" || str =~ / *\$\.end/
  return false
end

#err(*args) ⇒ Object



196
197
198
# File 'lib/livetext/userapi.rb', line 196

def err(*args)
  STDERR.puts *args
end

#expand_functions(str) ⇒ Object



54
55
56
# File 'lib/livetext/userapi.rb', line 54

def expand_functions(str)
  @expander.expand_functions(str)
end

#expand_variables(str) ⇒ Object



50
51
52
# File 'lib/livetext/userapi.rb', line 50

def expand_variables(str)
  @expander.expand_variables(str)
end

#format(line) ⇒ Object



162
163
164
165
# File 'lib/livetext/userapi.rb', line 162

def format(line)
  line2 = @expander.format(line)
  line2
end

#handle_escapes(str, set) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/livetext/userapi.rb', line 154

def handle_escapes(str, set)
  str = str.dup
  set.each_char do |ch|
    str.gsub!("\\#{ch}", ch)
  end
  str
end

#htmlObject



35
36
37
# File 'lib/livetext/userapi.rb', line 35

def html
  @html
end

#include_file(file) ⇒ Object



43
44
45
46
47
48
# File 'lib/livetext/userapi.rb', line 43

def include_file(file)
# checkpoint "DATA = #{file.inspect}"
  api.data = file
  api.args = [file]
  dot_include
end

#optional_blank_lineObject



93
94
95
96
97
98
# File 'lib/livetext/userapi.rb', line 93

def optional_blank_line
  peek = @live.peek_nextline  # ???
  return if peek.nil?
  @line = @live.nextline if peek =~ /^ *$/
  return true  # This way, dot commands will usually return true
end

#out(str = "", file = nil) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/livetext/userapi.rb', line 180

def out(str = "", file = nil)
  return if str.nil?
  return file.puts str unless file.nil?
  str.gsub!("\b ", "")   # ignore spaces
  @live.body << str
  @live.body << "\n" unless str.end_with?("\n")
end

#out!(str = "") ⇒ Object



188
189
190
# File 'lib/livetext/userapi.rb', line 188

def out!(str = "")
  @live.body << str  # no newline
end

#passthru(line) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/livetext/userapi.rb', line 167

def passthru(line)
  return if @live.nopass
  if line == "\n"
    unless @live.nopara
      out "<p>" 
      out
    end
  else
    text = @expander.format(line.chomp)
    out text
  end
end


205
206
207
208
# File 'lib/livetext/userapi.rb', line 205

def print(*args)
  # @live.output.print *args
  @live.api.out! *args
end

#puts(*args) ⇒ Object



200
201
202
203
# File 'lib/livetext/userapi.rb', line 200

def puts(*args)
  # @live.output.puts *args
  @live.api.out *args
end

#raw_body(tag = "__EOF__") ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/livetext/userapi.rb', line 114

def raw_body(tag = "__EOF__")
  lines = []
  @live.save_location = @live.sources.last
  loop do
    @line = @live.nextline
    break if @line.nil?
    break if @line.chomp.strip == tag
    lines << @line
  end
  optional_blank_line
  return lines unless block_given?
  lines.each {|line| yield line }
end

#raw_body!Object



150
151
152
# File 'lib/livetext/userapi.rb', line 150

def raw_body!
  raw_body(Livetext::Sigil).join("\n")
end

#setvar(var, val) ⇒ Object

FIXME



58
59
60
# File 'lib/livetext/userapi.rb', line 58

def setvar(var, val)   # FIXME
  @live.vars.set(var, val)
end

#setvars(pairs) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/livetext/userapi.rb', line 62

def setvars(pairs)
  # STDERR.puts "#{__method__}: pairs = #{pairs.inspect} (#{pairs.class})"
  pairs = pairs.to_a   # could be Hash or Variables
  pairs.each do |var, value|
    @live.vars.set(var, value)
  end
end

#trailing?(char) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
# File 'lib/livetext/userapi.rb', line 104

def trailing?(char)
  return true if ["\n", " ", nil].include?(char)
  return false
end

#tty(*args) ⇒ Object



192
193
194
# File 'lib/livetext/userapi.rb', line 192

def tty(*args)
  TTY.puts *args
end

#varsObject



89
90
91
# File 'lib/livetext/userapi.rb', line 89

def vars
  @vars
end