module Utilities

Constants

AbbrHashTable

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

AbbrPattern
AllChar
BoldRegexp

Pattern to match strong emphasis in Markdown text

EndDivMark
InnerRepresent
ItalicRegexp

Pattern to match normal emphasis in Markdown text

StartDivMark

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

Public Class Methods

abbrPostProcess(text) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# File lib/AoBane/utilities.rb, line 202
def compressWSpaces(line)
  dup = if line =~ /\s+$/ then line.strip + " " else line end
  return dup
end
getNowTime() click to toggle source

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

# File lib/AoBane/utilities.rb, line 85
def getNowTime
  return Time.now.to_s
end
initNumberStack() click to toggle source

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

# File lib/AoBane/utilities.rb, line 257
def initNumberStack
  Stack.destroy
end
insertTimeStamp(text) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

abbrPostProcess(text) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# File lib/AoBane/utilities.rb, line 202
def compressWSpaces(line)
  dup = if line =~ /\s+$/ then line.strip + " " else line end
  return dup
end
getNowTime() click to toggle source

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

# File lib/AoBane/utilities.rb, line 85
def getNowTime
  return Time.now.to_s
end
initNumberStack() click to toggle source

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

# File lib/AoBane/utilities.rb, line 257
def initNumberStack
  Stack.destroy
end
insertTimeStamp(text) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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