Class: Livetext

Inherits:
Object show all
Includes:
Helpers, Standard
Defined in:
lib/livetext.rb,
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: GlobalHelpers, Handler, Helpers, Standard Classes: Expansion, Formatter, FunctionCaller, FunctionRegistry, Functions, HTML, LineParser, ParseGeneral, ParseSet, ParsingConstants, UserAPI, VariableManager, Variables

Constant Summary collapse

Vars =
Variables.new
TTY =
::File.open("/dev/tty", "w")
Disallowed =
i[ __binding__        __id__            __send__          class
clone              display           dup               enum_for
eql?               equal?            extend            freeze
frozen?            hash              inspect           instance_eval   
instance_exec      instance_of?      is_a?             kind_of?
method             methods           nil?              object_id          
pretty_inspect     private_methods   protected_methods public_method
public_methods     public_send       respond_to?       send
singleton_class    singleton_method  singleton_methods taint
tainted?           tap               to_enum           to_s
trust              untaint           untrust           untrusted?
define_singleton_method              instance_variable_defined?
instance_variable_get                instance_variable_set
remove_instance_variable             instance_variables ]
Path =
self.get_path
Plugins =
self.get_path("../plugin")
Imports =
self.get_path("../imports")
VERSION =
"0.9.65"

Constants included from Helpers

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

Constants included from Standard

Standard::SimpleFormats

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Standard

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#check_disallowed, #debug, #escape_html, #file_exists?, #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

Methods included from Standard

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

Methods included from GlobalHelpers

#check_disallowed, #check_file_exists, #cwd_root?, #grab_file, #search_upward

Constructor Details

#initialize(output = ::STDOUT) ⇒ Livetext

Livetext



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/livetext/core.rb', line 60

def initialize(output = ::STDOUT)  # Livetext
  @body = ""
  @indentation = [0]
  @function_registry = Livetext::FunctionRegistry.new
  @variables = Livetext::VariableManager.new(self)
  @formatter = Livetext::Formatter.new(self)
  @api = UserAPI.new(self)
  @output = ::Livetext.output = output
  @html = Livetext::HTML.new(@api)
  @sources = []
# puts "------ init: self = "
# p self
end

Class Attribute Details

.outputObject

bad solution?



32
33
34
# File 'lib/livetext/core.rb', line 32

def output
  @output
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#formatterObject (readonly)

Returns the value of attribute formatter.



27
28
29
# File 'lib/livetext/core.rb', line 27

def formatter
  @formatter
end

#function_registryObject (readonly)

Returns the value of attribute function_registry.



27
28
29
# File 'lib/livetext/core.rb', line 27

def function_registry
  @function_registry
end

#indentationObject

Returns the value of attribute indentation.



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

def indentation
  @indentation
end

#noparaObject

Returns the value of attribute nopara.



28
29
30
# File 'lib/livetext/core.rb', line 28

def nopara
  @nopara
end

#nopassObject

Returns the value of attribute nopass.



28
29
30
# File 'lib/livetext/core.rb', line 28

def nopass
  @nopass
end

#sourcesObject (readonly)

Returns the value of attribute sources.



27
28
29
# File 'lib/livetext/core.rb', line 27

def sources
  @sources
end

#variablesObject (readonly)

Returns the value of attribute variables.



27
28
29
# File 'lib/livetext/core.rb', line 27

def variables
  @variables
end

Class Method Details

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



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

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.handle_dotcmd(cmd) }  # Use handle_dotcmd for proper command parsing
  obj.api.setvars(vars)
  # Also set variables in global Livetext::Vars for backward compatibility
  vars.each {|var, val| Vars[var.to_sym] = val.to_s }
  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



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

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



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

def api
  @api
end

#api=(obj) ⇒ Object



157
158
159
# File 'lib/livetext/core.rb', line 157

def api=(obj)
  @api = obj
end

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



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

def customize(mix: [], call: [], vars: {})
  mix  = Array(mix)
  call = Array(call)
  mix.each {|lib| mixin(lib) }
  call.each {|cmd| handle_dotcmd(cmd) }  # Use handle_dotcmd for proper command parsing
  # vars.each_pair {|var, val| @api.set(var, val.to_s) }
  api.setvars(vars)
  # Also set variables in global Livetext::Vars for backward compatibility
  vars.each {|var, val| Vars[var.to_sym] = val.to_s }
  self
end

#disallowed?(name) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/livetext/core.rb', line 117

def disallowed?(name)
  flag = Disallowed.include?(name.to_sym)
  flag
end

#error(*args) ⇒ Object



113
114
115
# File 'lib/livetext/core.rb', line 113

def error(*args)
  ::STDERR.puts *args
end

#htmlObject



126
127
128
# File 'lib/livetext/core.rb', line 126

def html
  @html
end

#inspectObject



100
101
102
103
104
105
106
107
# File 'lib/livetext/core.rb', line 100

def inspect
 api_abbr  = @api ? "(non-nil)" : "(not shown)"
  "Livetext:\n" + 
  "  indent = #{@indentation.inspect}\n" + 
  "  vars   = #{@variables.inspect}\n" + 
  "  api    = #{api_abbr}\n" +
  "  body   = (#{@body.size} bytes)"
end

#nextlineObject



147
148
149
150
151
152
153
154
155
# File 'lib/livetext/core.rb', line 147

def nextline
  return nil if @sources.empty?
  line = @sources.last[0].next
  @sources.last[2] += 1
  line
rescue StopIteration
  @sources.pop
  nil
end

#output=(io) ⇒ Object



122
123
124
# File 'lib/livetext/core.rb', line 122

def output=(io)
  @output = io
end

#peek_nextlineObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/livetext/core.rb', line 134

def peek_nextline
  return nil if @sources.empty?
  source = @sources.last
  line = source[0].peek
  line
rescue StopIteration
  @sources.pop
  nil
rescue => err
  TTY.puts "#{__method__}: RESCUE err = #{err.inspect}"
  nil
end

#process(text: nil, file: nil, vars: {}) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/livetext/core.rb', line 161

def process(text: nil, file: nil, vars: {})
  # Set variables first
  @variables.set_multiple(vars) unless vars.empty?
  
  # Set api on Livetext::Functions so all functions can access it
  Livetext::Functions.api = @api
  
  # Process based on input type
  case
  when file && text.nil?
    process_file(file)
  when file.nil? && text
    transform_text(text)
  when file.nil? && text.nil?
    raise "Must specify file or text"
  when file && text
    raise "Cannot specify file and text"
  end
  
  [self.body, @variables.to_h]
end

#save_locationObject



52
53
54
# File 'lib/livetext/core.rb', line 52

def save_location
  @save_location  # delegate
end

#save_location=(where) ⇒ Object



56
57
58
# File 'lib/livetext/core.rb', line 56

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

#source(enum, file, line) ⇒ Object



130
131
132
# File 'lib/livetext/core.rb', line 130

def source(enum, file, line)
  @sources.push([enum, file, line])
end

#transform(text) ⇒ Object

Keep for backward compatibility



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

def transform(text)
  transform_text(text)
end

#varsObject



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

def vars
  @variables
end

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

Keep for backward compatibility



206
207
208
209
# File 'lib/livetext/core.rb', line 206

def xform(*args, file: nil, text: nil, vars: {})
  body, _vars = process(file: file, text: text, vars: vars)
  body
end

#xform_file(file, vars: nil) ⇒ Object

Keep for backward compatibility



212
213
214
215
216
# File 'lib/livetext/core.rb', line 212

def xform_file(file, vars: nil)
  vars_hash = vars.nil? ? {} : vars
  body, _vars = process(file: file, vars: vars_hash)
  body
end