Module: Utilities

Defined in:
lib/AoBane/utilities.rb

Constant Summary collapse

AbbrHashTable =

Abbreviation proccessing##########################################################

Hash::new
AbbrPattern =
'\*\[(.+?)\]:(.*)\s*$'
StartDivMark =

paling proccessing##########################################################

'[\/\|]\-:b=(\d+?)\s(\w+?\s\w+?\s)' + # $1 $2
'(w=(\w+?)\s)??' +  # $3 $4
'(h=(\w+?)\s)??' +  # $5 $6
'(bg=((#)??\w+?)\s)??' + # $7 $8 $9
'(lh=([\w%]+?)\s)??' + # $10 $11
'(mg=(\d+?)\s)??' + # $12 $13
'(al=(\w+?)\s)??' + # $14 $15
'(rad=(\d+?))\-+[\|\/]' + # $16 $17
'({(.+?)})??'
EndDivMark =

$18 $19

'\|\_+\|'
AllChar =
'\w\s\!@\#\$%\^&\*\(\)\-\+=\[\{\}\];:\'"<>\,\.\/\?\\|'
InnerRepresent =
["@","/"]
BoldRegexp =

Pattern to match strong emphasis in Markdown text

%r{ (\*\*) (\S|\S.*?\S) \1 }x
ItalicRegexp =

Pattern to match normal emphasis in Markdown text

%r{ (\*) (\S|\S.*?\S) \1 }x
@@log =
Logger.new(STDOUT)

Class Method Summary collapse

Class Method Details

.abbrPostProcess(text) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/AoBane/utilities.rb', line 126

def abbrPostProcess(text)
  if AbbrHashTable.size == 0 then return text 
  else
    keywords = AbbrHashTable.keys.join('|')
    text.gsub!(/(#{keywords})/){
      word = if $1.nil? then '' else $1 end
      '<abbr title="' + AbbrHashTable[word] +'">' + word + '</abbr>' 
    }
    return text
  end
end

.abbrPreProcess(text) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/AoBane/utilities.rb', line 92

def abbrPreProcess(text)
  output = ''
  if text.nil? then return '' end 
  text.lines{ |line|
    if line =~ /\{abbrnote:(.+?)\}/i then #1
      if $1.nil? then '' #1.5
      else 
        File::open($1){|file| #2
          file.each{|line| #3
            if /^#.*\n/ =~ line then
              next
            elsif /#{AbbrPattern}/ =~ line 
              storeAbbr($1,$2)
            end
          } #3
        }
        
      end #1.5
    elsif line =~ /#{AbbrPattern}/ then
      @@log.debug $~
      storeAbbr($1,$2)
    else output << line
    end #
  }
  
  @@log.debug AbbrHashTable
  return output
end

.calcSectionNo(startNo = 1, range = 0, size = 0, dep = 1, str = '', outerStack) ⇒ Object

Return a caluculated section number and string.############################



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/AoBane/utilities.rb', line 262

def calcSectionNo(startNo=1, range=0, size=0, dep=1, str='', outerStack)
    stack = outerStack #Stack.instance
    i = dep.to_i
    counter = 0
    numberStr = [["%",i,counter],["%%",i,counter],["%%%",i,counter],
                 ["%%%%",i,counter],["%%%%%",i,counter],["%%%%%%",i,counter]]
    number = ""
    headNo = size.to_i

    if (headNo > $MAX_H) || (headNo <= 0) then 
      @@log.error("AoBane Syntax Error: Header shortage!") 
      raise SyntaxError,"Headder shortage!"
    else
      (1..headNo).each_with_index{|k| #h1 to h6
        p k
       if (k < headNo) then
         p "+++" # #{k},#{stack.sizeofStack}"
         if k >= stack.size  then
           stack.push(numberStr[k])
         end
       elsif k == headNo then
         p "---"
         if stack.size == 0 then
           stack.push(numberStr[k-1])
         end
         if stack.last[$S_SLOT].size > numberStr[k-1][$S_SLOT].size then
           loop do
             stack.pop
             if stack.last[$S_SLOT].size == numberStr[k-1][$S_SLOT].size then
               break
             end
           end
         end
       else
         p "~~~~"
         stack.push(numberStr[k])
       end #if...elsif 
     }
=begin
    else
      @@log.error("AoBane Syntax Error: Header Number Overflow!")
      raise SyntaxError,"Header Number Overflow!"
    end #case
=end
  end #if...else
    p "$$$$" 
    number = ""
    stack.each { |item|
      if item == stack.last then
        item[$N_SLOT] += item[$C_SLOT]
        item[$C_SLOT] = 1
      end
      number << (item[$N_SLOT]).to_s + '.'
      @@log.debug number
    }
 
    h = "#"
    times = startNo.to_i + size.to_i - 1
  return  h*times + number + str
end

.compressWSpaces(line) ⇒ Object



202
203
204
205
# File 'lib/AoBane/utilities.rb', line 202

def compressWSpaces(line)
  dup = if line =~ /\s+$/ then line.strip + " " else line end
  return dup
end

.getNowTimeObject

get Now Timestamp#################################################################



85
86
87
# File 'lib/AoBane/utilities.rb', line 85

def getNowTime
  return Time.now.to_s
end

.initNumberStackObject

Initialize a Stack class ############################



257
258
259
# File 'lib/AoBane/utilities.rb', line 257

def initNumberStack
  Stack.destroy
end

.insertTimeStamp(text) ⇒ Object

Insert Timestamp#################################################################



76
77
78
79
80
81
82
83
# File 'lib/AoBane/utilities.rb', line 76

def insertTimeStamp(text)
  if /\$date/i =~ text then
    text.gsub!(/\$date/i){
      getNowTime
    }
  else text 
  end
end

.isDigit(str) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/AoBane/utilities.rb', line 220

def isDigit(str)
  if /\d+/ =~ str then 
    return true
  else
    return false
  end
end

.italic_and_bold(str) ⇒ Object



214
215
216
217
218
# File 'lib/AoBane/utilities.rb', line 214

def italic_and_bold(str)
  str.
    gsub( BoldRegexp, %{<strong>\\2</strong>} ).
    gsub( ItalicRegexp, %{<em>\\2</em>} )
end

.postProcFence(text) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/AoBane/utilities.rb', line 228

def postProcFence(text)
  output = text.split("\n")
  output.each_with_index{|line,index|
    if /#{StartDivMark}/ =~ line then
      output[index] = '<div style="border:' + $1 + 'px ' + $2 + ';' +
        if $4.nil? then '' else 'width:' + if Utilities::isDigit($4) then $4 + 'px;' else $4 + ';'  end end  + 
        if $6.nil? then '' else 'height:' + if Utilities::isDigit($6) then $6 + 'px;' else $6 + ';' end end + 
        if $8.nil? then '' else 'background-color:' + $8 + ';' end + 
        if $11.nil? then 'line-height:100%;' else 'line-height:' + $11 + ';' end +
        if $13.nil? then '' else 'margin:' + $13 + 'px;' end +
        if $15.nil? then '' else 'text-align:' + $15 + ';' end +
        'border-radius:' + 
        if $17.nil? then '' else $17 end + 'px;"' + 
        if $19.nil? then '' else 'class="#{$19}"' end + 
        '>'
      output.each_with_index{|l,i = index|
        if /\/@\// =~ l then
          output[i] = '</div>'
          index = i
          break
        end
        i += 1
      }
    end
  }
  return output.join("\n")
end

.preProcFence(text, startPoint) ⇒ Object



152
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/AoBane/utilities.rb', line 152

def preProcFence(text,startPoint)
  output = []
  dup = []
  isInFence = [false]
  isInPre = false
  exclude = '(?!^\|_+|\|\-:)^\||^[#]{1,6}\s|^\s+\*|^\s+\-'
  if !text.instance_of?(Array) then output = text.split("\n") else output = text end

  output.each_with_index{|line,index|
    if index < startPoint then next
    elsif /#{StartDivMark}/ =~ line then
      start = line.split("|")
      dup <<  "/" + start[1] + "/" 
      if start.size >= 2 then dup << start[2..-1] end 
      isInFence.push(true)
      next
    elsif /#{EndDivMark}/ =~ line then
      dup << '/@/'
      next
    else 
      if isInFence.last then
        if dup.last.nil? then 
          dup << compressWSpaces(line)
        else 
          if dup.last =~ /#{exclude}/i || line =~ /#{exclude}/i then
            if line =~ /#{exclude}/i then dup << line
            else dup << compressWSpaces(line) end
           else
            if line == "" then 
              dup << '<br />'
            else
              if line =~ /<pre>|<\/pre>/ || isInPre then
                isInPre = true
                dup <<  line
                if line =~ /<\/pre>/ then isInPre = false end
              else
                dup.last << compressWSpaces(line)
              end
              next
            end
          end
        end
      else
        dup << if !line.nil? then line else "" end
      end     
    end
  }
  return dup
end

.storeAbbr(key, val) ⇒ Object

def



121
122
123
124
# File 'lib/AoBane/utilities.rb', line 121

def storeAbbr(key,val)
  val = if val.nil? then '' else val end
  AbbrHashTable.store(key,val)
end

.transformSpecialChar(text) ⇒ Object



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
# File 'lib/AoBane/utilities.rb', line 19

def transformSpecialChar(text)
  #output = text.split("\n")
  specialChar =  {
    "\-\-" => "&mdash;",
    "<=" => "&hArr;",
    "<\->" => "&harr;",
    "\->" =>"&rarr;",
    "<\-" =>"&larr;",
    "=>" => "&rArr;",
    "<=" => "&lArr;",
    "\|\|\^" => "&uArr;",
    "\|\|\/" => "&dArr;",
    "\|\/" => "&darr;",
    "\|\^" => "&uarr;",
    "+_" => "&plusmn;",
    "!=" => "&ne;",
    "~=" => "&cong;",
    "<_" => "&le;",
    ">_" => "&ge",
    "\|FA" => "&forall;",
    "\|EX" => "&exist;",
    "\|=" => "&equiv;",
    "\(\+\)" => "&oplus;",
    "\(\-\)" => "&ominus;",
    "\(X\)" => "&otimes;",
    "\(c\)" => "&copy;",
    "\(R\)" =>"&reg;",
    "\(SS\)" => "&sect;",
    "\(TM\)" => "&trade;",
    "!in" => "&notin;"}
  
  entry = '(?!\-+\|)\-\-|<=>|<\->|\->|<\-|=>|<=|\|\^|\|\|\/|\|\/|\^|' +
    '\+_|!=|~=|>_|<_|\|FA|\|EX|\|=|\(\+\)|\(\-\)|\(X\)|\(c\)|\(R\)|\(SS\)|\(TM\)|!in'
  
  
  
  zoneofPre = ["<pre>","<\/pre>"] 
  dup = []
  doc = text.split("\n")
  index = 0
  doc.each{
    if doc[index] =~ /#{zoneofPre[0]}/i
      until doc[index] =~ /#{zoneofPre[1]}/i
        dup[index] = doc[index]
        index += 1
      end
      dup[index] = doc[index]
    else
      dup[index] = if !doc[index].nil? then doc[index].gsub(/#{entry}/,specialChar) end
      index += 1
    end
  }
  
  #Insert by set.minami
  return dup.join("\n")
end