Class: Livetext

Inherits:
Object show all
Includes:
Helpers
Defined in:
lib/livetext/core.rb,
lib/livetext/paths.rb,
lib/livetext/version.rb,
lib/livetext/skeleton.rb

Overview

Class Livetext skeleton (top level).

Defined Under Namespace

Modules: Formatter, GlobalHelpers, Handler, Helpers, Standard Classes: Expansion, Functions, HTML, LineParser, ParseGeneral, ParseSet, ParsingConstants, UserAPI, Variables

Constant Summary collapse

Vars =
Variables.new
TTY =
::File.open("/dev/tty", "w")
Path =
self.get_path
Plugins =
self.get_path("../plugin")
Imports =
self.get_path("../imports")
VERSION =
"0.9.48"

Constants included from Helpers

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

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

Constructor Details

#initialize(output = ::STDOUT) ⇒ Livetext

Livetext



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/livetext/core.rb', line 50

def initialize(output = ::STDOUT)  # Livetext
  @source = nil
  @_mixins = []
  @_imports = []
  @_outdir = "."
  @no_puts = output.nil?
  @body = ""
  @main = Processor.new(self, output)  # nil = make @main its own parent??
  @indentation = [0]
  @_vars = Livetext::Vars
  @api = UserAPI.new(self)
  initial_vars
# puts "------ init: self = "
# p self
end

Class Attribute Details

.outputObject

bad solution?



16
17
18
# File 'lib/livetext/core.rb', line 16

def output
  @output
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



13
14
15
# File 'lib/livetext/core.rb', line 13

def body
  @body
end

#indentationObject

Returns the value of attribute indentation.



13
14
15
# File 'lib/livetext/core.rb', line 13

def indentation
  @indentation
end

#mainObject (readonly)

Returns the value of attribute main.



11
12
13
# File 'lib/livetext/core.rb', line 11

def main
  @main
end

#noparaObject

Returns the value of attribute nopara.



12
13
14
# File 'lib/livetext/core.rb', line 12

def nopara
  @nopara
end

#nopassObject

Returns the value of attribute nopass.



12
13
14
# File 'lib/livetext/core.rb', line 12

def nopass
  @nopass
end

#sourcesObject (readonly)

Returns the value of attribute sources.



11
12
13
# File 'lib/livetext/core.rb', line 11

def sources
  @sources
end

Class Method Details

.customize(mix: [], call: [], vars: {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/livetext/core.rb', line 66

def self.customize(mix: [], call: [], vars: {})
  obj = self.new
  mix  = Array(mix)
  call = Array(call)
  mix.each do |lib| 
    obj.invoke_dotcmd(:mixin, lib.dup)
  end
  call.each {|cmd| obj.main.send(cmd[1..-1]) }  # ignores leading dot, no param
  obj.api.setvars(vars)
  obj
end

.get_path(dir = "") ⇒ Object



4
5
6
7
# File 'lib/livetext/paths.rb', line 4

def self.get_path(dir = "")
  path = File.join(File.dirname(__FILE__), dir)
  File.expand_path(path)
end

.interpolate(str) ⇒ Object



23
24
25
26
27
28
# File 'lib/livetext/core.rb', line 23

def self.interpolate(str)
  expand = Livetext::Expansion.new(self) 
  str2 = expand.expand_variables(str)
  str3 = expand.expand_function_calls(str2)
  str3
end

Instance Method Details

#apiObject



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

def api
  @api
end

#api=(obj) ⇒ Object



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

def api=(obj)
  @api = obj
end

#customize(mix: [], call: [], vars: {}) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/livetext/core.rb', line 78

def customize(mix: [], call: [], vars: {})
  mix  = Array(mix)
  call = Array(call)
  mix.each {|lib| mixin(lib) }
  call.each {|cmd| @main.send(cmd[1..-1]) }  # ignores leading dot, no param
  # vars.each_pair {|var, val| @api.set(var, val.to_s) }
  api.setvars(vars)
  self
end

#initial_varsObject



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

def initial_vars
  # Other predefined variables (see also setfile)
  @api.setvar(:User, `whoami`.chomp)
  @api.setvar(:Version, Livetext::VERSION)
end

#inspectObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/livetext/core.rb', line 88

def inspect
  "Livetext:\n" + 
  "  source = #{@source.inspect}\n" +
  "  mixins = #{@_mixins.inspect}\n" + 
  "  import = #{@_mixins.inspect}\n" + 
  "  main   = (not shown)\n" + 
  "  indent = #{@indentation.inspect}\n" + 
  "  vars   = #{@_vars.inspect}\n" + 
  "  api    = (not shown)\n" +
  "  body   = (#{@body.size} bytes)"
end

#nextlineObject



34
35
36
# File 'lib/livetext/core.rb', line 34

def nextline
  @main.nextline       # delegate
end

#peek_nextlineObject



30
31
32
# File 'lib/livetext/core.rb', line 30

def peek_nextline
  @main.peek_nextline  # delegate
end

#save_locationObject



42
43
44
# File 'lib/livetext/core.rb', line 42

def save_location
  @save_location  # delegate
end

#save_location=(where) ⇒ Object



46
47
48
# File 'lib/livetext/core.rb', line 46

def save_location=(where)
  @save_location = where  # delegate
end

#transform(text) ⇒ Object



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

def transform(text)
  setfile!("(string)")
  enum = text.each_line
  front = text.match(/.*?\n/).to_a.first.chomp rescue ""
  @main.source(enum, "STDIN: '#{front}...'", 0)
  loop do 
    line = @main.nextline
    break if line.nil?
    process_line(line)
  end
  result = @body
#   @body = ""
  result
end

#varsObject



19
20
21
# File 'lib/livetext/core.rb', line 19

def vars
  @_vars
end

#xform(*args, file: nil, text: nil, vars: {}) ⇒ Object

EXPERIMENTAL and incomplete



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

def xform(*args, file: nil, text: nil, vars: {})
  case
    when file && text.nil?
      xform_file(file)
    when file.nil? && text
      transform(text)
    when file.nil? && text.nil?
      raise "Must specify file or text"
    when file && text
      raise "Cannot specify file and text"
  end
  self.process_file(file)
  self.body
end

#xform_file(file, vars: nil) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/livetext/core.rb', line 145

def xform_file(file, vars: nil)
  Livetext::Vars.replace(vars) unless vars.nil?
  @_vars.replace(vars) unless vars.nil?
# checkpoint! "Calling process_file..."
  self.process_file(file)
# checkpoint! "...returned"
  self.body
end