Class: Tags2w

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

Instance Method Summary collapse

Constructor Details

#initializeTags2w

Returns a new instance of Tags2w.



5
6
7
8
9
10
11
# File 'lib/tags2w.rb', line 5

def initialize
  @cod_if = false
  @doc_out= []
  @level  = 0
  @line   = ''
  @tags   = %w[h1]
end

Instance Method Details

#element(el) ⇒ Object



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
# File 'lib/tags2w.rb', line 22

def element(el) 
  if el.class==Hpricot::Text
    tag= el.name
  elsif el.class==Hpricot::Comment
    tag= "<!--#{el.name}-->"
  else
    #p el.html
    at = el.respond_to?(:attributes) ? el.attributes : {}
    tag= "%#{el.name}"
    if at['name']
      tag<<":#{at['name']}" 
      at.delete 'name'
    end
    if at['id']
      tag<<"##{at['id']}" 
      at.delete 'id'
    end
    if at['class']
      tag<<".#{at['class'].split(' ').join('.')}" 
      at.delete 'class'
    end
    if at.length>0
      ex= at.collect{|k,v|[k,'="',v,'"'].join}.join(' ')
      tag<<"{#{ex}}"
    end
    if /\%div([#.])/ =~ tag
      tag.gsub!(/\%div([#.])/,$1) 
    else
      tag.gsub!(/\%input\:/,':') 
    end
    if /\%cmd\./ =~ tag
      #puts tag
      tag.gsub!(/\%cmd\.if\{(.*?)\}/    ,    "-if #{$1}") if /\%cmd\.if\{att="(.*?)"\}/    =~ tag
      tag.gsub!(/\%cmd\.for\{(.*?)\}/   ,   "-for #{$1}") if /\%cmd\.for\{att="(.*?)"\}/   =~ tag
      tag.gsub!(/\%cmd\.unless\{(.*?)\}/,"-unless #{$1}") if /\%cmd\.unless\{att="(.*?)"\}/=~ tag
      tag.gsub!(/\%cmd\.elsif\{(.*?)\}/ , "-elsif #{$1}") if /\%cmd\.elsif\{att="(.*?)"\}/ =~ tag
      tag.gsub!(/\%cmd\.else\{(.*?)\}/  ,  "-else #{$1}") if /\%cmd\.else\{att="(.*?)"\}/  =~ tag
      tag.gsub!(/\%cmd\.each\{(.*?)\}/  ,  "-each #{$1}") if /\%cmd\.each\{att="(.*?)"\}/  =~ tag
      tag.gsub!(/\%cmd\.each2\{(.*?)\}/ , "-each2 #{$1}") if /\%cmd\.each2\{att="(.*?)"\}/ =~ tag
      tag.gsub!(/\%cmd\.form_tag\{(.*?)\}/,"-form_tag #{$1}") if /\%cmd\.form_tag\{att="(.*?)"\}/=~ tag
      tag.gsub!(/\%cmd\.form_for\{(.*?)\}/,"-form_for #{$1}") if /\%cmd\.form_for\{att="(.*?)"\}/=~ tag
    else  
      tag.gsub!(/\%\<\?xml/             ,        "<?xml") if /^\%\<\?xml/                  =~ tag
      tag.gsub!(/\%\<\!DOCTYPE/         ,    "<!DOCTYPE") if /^\%\<\!DOCTYPE/              =~ tag
    end                                                
  end
  tag  
end

#equal(line) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tags2w.rb', line 71

def equal(line)
  rtn = []
  line.split("\n").each do |ln|
    if /\<%=(.*)[-]?%\>/=~ ln
      rtn<< ('= '<< $1.lstrip.gsub(/\-$/,'') )
    else
      rtn<< (' ' << ln.rstrip)
    end
  end
  rtn.join("\n")
end

#in_block(blk) ⇒ Object



92
93
94
95
96
# File 'lib/tags2w.rb', line 92

def in_block(blk)
  spc1= '  '*(@level)
  spc2= '  '*(@level+1)
  [spc1,blk.gsub(/\n +/,"\n#{spc2}")].join
end

#in_block2(blk) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tags2w.rb', line 98

def in_block2(blk)
  les= 999
  spc= '  '*(@level+1)
  #blk= ['  ',blk].join
  lns= blk.split("\n")
  lns.each do |l|
    x=(/ */=~l)
    les=x if x<les
  end
  lns.collect {|l|[spc,l[les,999]].join}.join("\n").sub(/^  /,'')
end

#line_codeObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/tags2w.rb', line 153

def line_code
  @doc_med= []
  @doc_out.join("\n").split("\n").each_with_index do |ln,i|
    doc= ''
    if /[^"] *\<%(=.*)[-]?%\>/=~ ln
      ln.gsub!(/[^"] *\<%=.*[-]?%\>/,$1.lstrip.gsub(/\-$/,'')) 
      @doc_med<< ln
    elsif /[^"] *\<%[-]?(.*)[-]?%\>/ =~ ln
      code= $1.lstrip.gsub(/\-$/,'')
      part= code.split(/ +/)[0]
      if %w[if elsif else].index(part)
        @cod_if = true
        code= "-#{code}" 
      else
        code= "- #{code}" 
      end
      if @cod_if && (%w[end].index(part))
        @cod_if= false
      else
        ln.gsub!(/\<%[-]?.*[-]?%\>/,code) 
        @doc_med<< ln 
      end
    else
      @doc_med<< ln
    end
  end
  @doc_out= @doc_med
end

#resultObject



18
19
20
# File 'lib/tags2w.rb', line 18

def result
  @doc_out
end

#ruby_equal(el) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/tags2w.rb', line 83

def ruby_equal(el)
  rtn = equal(element(el).strip).gsub(/ *\%w2code_equ/,'=') 
  ch= el.respond_to?(:children) ? el.children : nil
  if ch && ch.length==1
    rtn<< ruby_equal(el.children[0])
  end
  rtn
end

#start(el) ⇒ Object



13
14
15
16
# File 'lib/tags2w.rb', line 13

def start(el)
  initialize
  sub_element(el) 
end

#sub_element(parent) ⇒ Object



110
111
112
113
114
115
116
117
118
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
# File 'lib/tags2w.rb', line 110

def sub_element(parent)
  if parent.children
    parent.children.each do |el|
      @line= element(el).strip
      if @line!=''
        if el.class==Hpricot::Comment
          @doc_out << ['  '*(@level),@line].join 
        elsif el.class==Hpricot::Text
          @doc_out << ['  '*(@level),@line].join 
        else
          if el.name=='script'
            @doc_out << in_block2(@line << el.html)
          elsif el.name=='w2comment'
            @doc_out << in_block2("%comment"<< el.html)
          elsif el.name=='w2code_equ'
            @doc_out << in_block2("="<< el.html)
          else
            ch= el.respond_to?(:children) ? el.children : nil
            if @line[0,1]!='-' && ch && ch.length==1
                inline= ruby_equal(el.children[0]) 
                inline= " (#{inline.lstrip}%)" if /\%\w/ =~ inline
                @doc_out << ['  '*@level,@line,inline].join 
            else
              if /\%cmd/ !~ @line
                rtn= ['  '*@level,@line].join 
                rtn.gsub!('<w2code_equ>','<%=')
                rtn.gsub!('</w2code_equ>','%>')
                @doc_out << rtn
                if ch
                  @level+=1
                  sub_element(el) 
                  @level-=1
                end
              end
            end
          end
        end
      end
    end
  end
  self
end