Class: W2Tags::Parser
- Inherits:
-
Object
- Object
- W2Tags::Parser
- Defined in:
- lib/w2tags/parser.rb
Overview
Copyright © 2008 - 2009 Widi Harsojo
Instance Attribute Summary collapse
-
#dbg ⇒ Object
change debuging ex: obj.dbg = true.
-
#ext ⇒ Object
set extention for ext auto loading HOT files.
-
#silent ⇒ Object
Returns the value of attribute silent.
-
#spc ⇒ Object
readonly
Returns the value of attribute spc.
Instance Method Summary collapse
-
#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.
- #end_tags(arr) ⇒ Object
-
#initialize(mrg = nil) ⇒ Parser
constructor
initiall create instance object, default if no arguments will be target for html.
-
#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.
-
#parse_file(src, init_start = true, chk_date = false) ⇒ Object
parse one w2tags, result will be name of the target file created.
-
#parse_files(srcs, init_start = true) ⇒ Object
parse multiple w2tags files, result will be array of target file created.
-
#parse_init ⇒ Object
it use to clean all the definition and reloading the hot file.
-
#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.
-
#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.
-
#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).
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# 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.('.')) @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= [] @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 }] @tagr = proc do |this| @key.strip! = "<#{@key}" << " #{@mem_var["*all*"].strip}" if @mem_var["*all*"]!='' #tags_created << " #{@att}" if @att!='' if @txt=='/' << "/>\n" else << '>' @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*"]!='' << @mem_var["*code*"].gsub('$*',@txt) else << @txt.gsub(/^ +/,'') #remove gsub if don't want auto trim left end end end 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
#dbg ⇒ Object
change debuging ex: obj.dbg = true
6 7 8 |
# File 'lib/w2tags/parser.rb', line 6 def dbg @dbg end |
#ext ⇒ Object
set extention for ext auto loading HOT files
8 9 10 |
# File 'lib/w2tags/parser.rb', line 8 def ext @ext end |
#silent ⇒ Object
Returns the value of attribute silent.
4 5 6 |
# File 'lib/w2tags/parser.rb', line 4 def silent @silent end |
#spc ⇒ Object (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
236 237 238 239 |
# File 'lib/w2tags/parser.rb', line 236 def chg_mem_hot(nxt) @mem_hot = nxt @mem_tag["^"] = nxt if nxt end |
#end_tags(arr) ⇒ Object
227 228 229 |
# File 'lib/w2tags/parser.rb', line 227 def (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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/w2tags/parser.rb', line 244 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 |
#parse_file(src, init_start = true, chk_date = false) ⇒ Object
parse one w2tags, result will be name of the target file created
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/w2tags/parser.rb', line 206 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
220 221 222 223 224 225 |
# File 'lib/w2tags/parser.rb', line 220 def parse_files(srcs,init_start=true) srcs.collect do |src| src.gsub!(/\n/,'') parse_file(src,init_start) end.compact end |
#parse_init ⇒ Object
it use to clean all the definition and reloading the hot file
155 156 157 158 159 160 161 |
# File 'lib/w2tags/parser.rb', line 155 def parse_init @tg_end = [] #momorize tag end from regex function @doc_src = [] @doc_out = [] @tg_hot = {} @mrg ? @mrg : @ext 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.
165 166 167 168 169 170 171 172 173 |
# File 'lib/w2tags/parser.rb', line 165 def parse_line row,init=true parse_init if init dbg[:parse]= false @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.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/w2tags/parser.rb', line 179 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!="" p "#####> #{@row}" if @dbg[:parse] @doc_out << @row.rstrip+"\n" 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)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/w2tags/parser.rb', line 119 def parsing(src,tgt,init_start=true) puts ">>#{src}" puts ">>#{tgt}" parse_init @src_path= File.dirname(src) @doc_src = IO.read(src).delete("\r").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 |