Class: W2Tags::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/w2tags/parser.rb

Overview

Copyright © 2008 - 2009 Widi Harsojo

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mrg = nil) ⇒ Parser

initiall create instance object, default if no arguments will be target for html



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/w2tags/parser.rb', line 15

def initialize(mrg = nil)
  @dbg={
    :hot      =>nil,
    :stack    =>nil,
    :parse    =>nil,
    :constanta=>nil
  }
  
  #regex for w2tags   
  @rg_tag = [
  /^[ \t]*(%)([!]?[\w\-&\/:#.\[\]]+\{.*\}[=]*)!([^\n]*)\n/,
  /^[ \t]*(%)([!]?[\w\-&\/:#.\[\]=]+)!([^\n]*)([\n])/]
  
  #regex for function tags    
  @rg_hot = [
  /(%)([!]?[ \t\$\w\-&\/:#.\[\]%=]+\{.*\}[=]*)~([^\n]*)\n/,
  /(%)([!]?[ \t\$\w\-&\/:#.\[\]%=]+)~([^\n]*)\n/    ]
  @rgx    =  nil   #current regular expression
  @mrg    =  mrg   #another hot to include
  @ext    = 'erb'  #target extension 
  @hot    = 'hot'  #source of file hot
  @src_path= ''    #path for source file
  @silent = false  #for test

  @ron    = 0      #strip current source line if size it empty or not
  @spc    = ''     #current begining space of current source line
  @ind    = '  '   #indentation size
  @row    = 'row'  #current source line
  @key    = 'key'  #key extracted from regex function

  @mem_hot= nil    #@tg_nex will be use (for "%") if this var == nil
  @mem_tag= {'^'=>"%div$*!"} #get memorize of w2tag 
  @mem_var= {'$me'=>"wharsojo"}
  @mem_var['$basepath'] = File.basename(File.expand_path('.'))

  @tg_hot = {}     #{'div'=>[proc{|this|"%div$*!"},nil]} #collection of tag_hot after reading from source hot
  @tg_nex = {}     #tag next activate on shortcut tag "%"
  @tg_end = []     #momorize tag end from regex function
  @doc_src= []
  @doc_out= []
  
  @tagr = proc do |this|
    @key.strip!
    tags_created =  "<#{@key}"
    tags_created << " #{@mem_var["*all*"].strip}" if @mem_var["*all*"]!='' 
    #tags_created << " #{@att}" if @att!=''
    if @txt=='/'
      tags_created << "/>\n"
    else
      tags_created << '>'
      @ln_end = " "            
      if @txt=='' 
        @tg_end.push "#{@spc}</#{@key}>#{@ln_end}"
        p "Stack: #{@tg_end}" if @dbg[:stack]
      else
        if @txt.gsub!(/\<$/,'')
          @tg_end.push "#{@spc}</#{@key}>#{@ln_end}"
        else
          @ln_end = "</#{@key}>#{@ln_end}"
        end
        if @mem_var["*code*"] && @mem_var["*code*"]!=''
          tags_created << @mem_var["*code*"].gsub('$*',@txt)
        else
          tags_created << @txt.gsub(/^ +/,'') #remove gsub if don't want auto trim left
        end
      end
    end
    tags_created
  end
  @skiper= []
  public_methods.each do |f|
    send(f) if /_initialize$/ =~ (meth= f.to_s)
    @skiper << [$1,'_skip'].join.to_sym if /(.*)_skip$/ =~ meth
  end
end

Instance Attribute Details

#dbgObject

change debuging ex: obj.dbg = true



6
7
8
# File 'lib/w2tags/parser.rb', line 6

def dbg
  @dbg
end

#extObject

set extention for ext auto loading HOT files



8
9
10
# File 'lib/w2tags/parser.rb', line 8

def ext
  @ext
end

#silentObject

Returns the value of attribute silent.



4
5
6
# File 'lib/w2tags/parser.rb', line 4

def silent
  @silent
end

#spcObject (readonly)

Returns the value of attribute spc.



9
10
11
# File 'lib/w2tags/parser.rb', line 9

def spc
  @spc
end

Instance Method Details

#chg_mem_hot(nxt) ⇒ Object

call from proc hot before it render hots line see in source code “w2tags.rb::read_filehot” default behaviour for tag_nex will not execute since @mem_hot will assigned w/o nil assigned nil using “%!” in w2tags



247
248
249
250
# File 'lib/w2tags/parser.rb', line 247

def chg_mem_hot(nxt)
  @mem_hot      = nxt      
  @mem_tag["^"] = nxt if nxt
end

#end_tags(arr) ⇒ Object



238
239
240
# File 'lib/w2tags/parser.rb', line 238

def end_tags(arr)
  @tg_end = @tg_end + arr
end

#multi_end(ttls) ⇒ Object

pop up end tags from the stack result will be string contains end tags from stack who’s space inside each end tags > current space



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/w2tags/parser.rb', line 255

def multi_end(ttls)
  rpls = ''
  ttl  = @tg_end.size-1
  ttl  = ttls-1 if ttls
  ttl.downto(0) do |i|
    sz = @tg_end[i][/^ +/].to_s.size
    if ttls || @spc.size <= sz
      send = @tg_end.pop.to_s
      if send.strip[0,5]=="!run!"
        scrpt = send.gsub("\n","\n#{@spc}").split("\n")
        @doc_src = scrpt[1,99]+@doc_src
      else
        spc = send[/(^[ \t]*)/,1].to_s
        rpls << send.gsub(/\n/,"\n#{spc}") + "\n" 
      end
    end
  end
  p "End..: #{rpls.strip}" if @dbg[:parse] && rpls!= ''
  rpls
end

#next_tag_initObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/w2tags/parser.rb', line 101

def next_tag_init
  @tg_nex['html'  ]= [0,proc { @mem_tag["^"] = "%head $*\n"}]
  @tg_nex['head'  ]= [0,proc { @mem_tag["^"] = "%body $*\n"}]
  @tg_nex['ol'    ]= [0,proc { @mem_tag["^"] = "%li $0\n"  }]
  @tg_nex['ul'    ]= [0,proc { @mem_tag["^"] = "%li $0\n"  }]
  @tg_nex['dl'    ]= [0,proc { @mem_tag["^"] = "%dt $0\n"  }]
  @tg_nex['dd'    ]= [0,proc { @mem_tag["^"] = "%dt $0\n"  }]
  @tg_nex['form'  ]= [0,proc { @mem_tag["^"] = "%input $0\n"}]
  @tg_nex['select']= [0,proc { @mem_tag["^"] = "%option $0\n"}]
  @tg_nex['table' ]= [0,proc { @tg_nex['tr'][0]= 0 }]
  @tg_nex['tr'    ]= [0,proc { 
     @tg_nex['tr'   ][0]+= 1
     if @tg_nex['tr'][0]== 1
        @mem_tag["^"] = "%th $0\n" 
     else
        @mem_tag["^"] = "%td $0\n"
     end
  }]
end

#parse_file(src, init_start = true, chk_date = false) ⇒ Object

parse one w2tags, result will be name of the target file created



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/w2tags/parser.rb', line 217

def parse_file(src,init_start=true,chk_date=false)
  tgt,@ext = [src[/(.+\.)w2(\w+)$/,1]<<$2,$2]
  
  return nil if !File.exist?(src) #p "src: #{src} not found..."
  if chk_date && File.exist?(tgt)
    return nil if File.mtime(src) <= File.mtime(tgt)
  end
  
  puts "\nParsing W2Tags File:#{src}" 
  parsing(src,tgt,init_start)
  tgt
end

#parse_files(srcs, init_start = true) ⇒ Object

parse multiple w2tags files, result will be array of target file created



231
232
233
234
235
236
# File 'lib/w2tags/parser.rb', line 231

def parse_files(srcs,init_start=true)
  srcs.collect do |src|
    src.gsub!(/\n/,'')
    parse_file(src,init_start)
  end.compact
end

#parse_initObject

it use to clean all the definition and reloading the hot file



92
93
94
95
96
97
98
99
# File 'lib/w2tags/parser.rb', line 92

def parse_init
  @tg_end   = [] #momorize tag end from regex function
  @doc_src  = []
  @doc_out  = []
  @tg_hot   = {} 
  merge_tags @mrg ? @mrg : @ext
  next_tag_init
end

#parse_line(row, init = true) ⇒ Object

to test parsing on source line and return will be the result, everytime it execude, it clean up and reloading the HOT files.



173
174
175
176
177
178
179
180
181
182
# File 'lib/w2tags/parser.rb', line 173

def parse_line row,init=true
  parse_init if init
  dbg[:parse]= false
  @doc_src   = read_w2tags(row).split("\n") << ",/"
  #@doc_src   = row.delete("\r").split("\n") << ",/"
  while (@row= @doc_src.shift) do  #;p "row:#{@row}"
    parse_row
  end
  @doc_out
end

#parse_row(row = nil) ⇒ Object

the actual parsing on row, but it use for parsing the files not to test the source line, since some of the row have a command that has effect on source, result and the row will be empty, please use parse_line if you want to test interactively on IRB.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/w2tags/parser.rb', line 188

def parse_row row=nil
    @row = row if row
    @row<<"\n"           #;p "row:#{@row}"
    p "_____> #{@row}" if @dbg[:parse] && @plt == 99 && @rmk == 99

    parse_spc
    
    @ln_end = ""        #imediate ends tag
    @row.gsub!(/\\\\/,'')  #esc char \\
    @row.gsub!('\}','')    #esc char \}
    @row.gsub!('\;','')    #esc char \;
    while (parse_all) do; end  
    @row.gsub!('','\\')
    @row.gsub!('','}')
    @row.gsub!('',';')
    if @ln_end!=""
      @row.gsub!(/([\n\t ]*$)/,'')
      @row << "#{@ln_end.strip}\n"
    end
    if @row.strip!="" # responsible for generating good output
      p "#####> #{@row}" if @dbg[:parse] 
      @doc_out << (@row.rstrip + "\n") 
      #@doc_out << @row.rstrip # not working for 
      #@doc_out << "\n"        # shortcut if elsif else 
    end
    @row
end

#parsing(src, tgt, init_start = true) ⇒ Object

parsing from fullpath source to the fullpath target/result with the option of auto add ‘initialize’ and ‘finalize’ for source file in w2tags only fill with LF it will not translate, since behaviour for “nnn”.split(“n”) will result in empty array if hot available it will add w2tags in: firstline with @initialize() lastline with @finalize() to not add, supply this function with init_start=false ex: parsing(“t.w2tst”,“t.tst”,false)



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/w2tags/parser.rb', line 136

def parsing(src,tgt,init_start=true)
  puts ">>#{src}"
  puts ">>#{tgt}"
  parse_init
  @src_path= File.dirname(src)
  @doc_src = read_w2tags(IO.read(src)).split("\n")
  @doc_src<< "%finallize" if @tg_hot['finallize' ]
  
  while (@row = @doc_src.shift) do  #;p "row:#{@row}"
    if init_start && !(/!hot!/ =~ @row)
      @doc_src,@row = [[@row]+@doc_src,"%initialize"] if @tg_hot['initialize']
      p "HEAD:#{init_start}"
      init_start  = false
    end
    parse_row
  end
  if @dbg[:constanta] 
    p "const_ "
    @mem_var.keys.sort.each {|k|p "#{k.ljust 10} : #{@mem_var[k]}"}
  end
  
  if @dbg[:hot]
    @tg_hot.keys.sort.each_with_index do |v,i|
      puts "hot keys: #{i}. #{v}"
    end
  end
  
  open(tgt,'w') do |f|
    @doc_out.each do |row|
      f << row
    end
  end
  
end

#read_w2tags(src, spc = nil) ⇒ Object



121
122
123
124
125
# File 'lib/w2tags/parser.rb', line 121

def read_w2tags(src,spc=nil)
  src = src.delete("\r").split(/\n\>\>\>W2TAGS\>\>\>/)
  @tg_hot.merge!(W2Tags.parsing_hot(src.shift)) if src.length>1
  (spc ? src[0].gsub("\n","\n"+@spc) : src[0]) 
end