Advertisement
Rafael_Sol_Maker

RAFAEL_SOL_MAKER's VX REGEXP NOTETAG SYSTEM v1.0

Nov 17th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.78 KB | None | 0 0
  1. #===============================================================================
  2. #             RAFAEL_SOL_MAKER's VX REGEXP NOTETAG SYSTEM v1.0
  3. #-------------------------------------------------------------------------------
  4. # Descrição:    Função feita especialmente para padronizar a avaliação de
  5. #               expressões regulares sob a forma de Tagnotes no Power Pack.                
  6. #               Esse script torna muito mais fácil e prática a obtenção dos  
  7. #               valores desejados em meio ao texto e espaços em branco.
  8. #               Suporta também múltiplos valores, que são retornados num array,
  9. #               quando os valores estão separados por vírgulas na mesma
  10. #               expressão, ou quando estão em outra expressão igual.
  11. #               O retorno de expressões do tipo 'Boolean' independe da opção
  12. #               'múltiplos valores' estar marcada com verdadeira ou não.
  13. #-------------------------------------------------------------------------------
  14. # Modo de usar:
  15. #                Sintaxe de uso:
  16. #                eval_regexp (texto, expressão, [tipo_esperado], [multi_valores?])
  17. #                  * Valores entre colchetes ("[]") são opcionais.
  18. #                Onde:
  19. #                  texto          -> Texto da onde as tags serão tiradas.
  20. #                  expressão      -> Qual a palavra contida na expressão?
  21. #                  tipo_esperado  -> Consulte o módule TYPES no fim do script.
  22. #                  multi_valores? -> Se a função capturará mais de um valor.
  23. #
  24. #                Alguns exemplos de uso:
  25. #                  número = eval_regexp(texto, "número", TYPES::Numbers, true)
  26. #                  número.nil? ?  @meu_número = 0 : @meu_número = número
  27. #                  # Para expressões do tipo: <número 123, 456, 789>
  28. #                
  29. #                  @switch = eval_regexp(texto, "switch", TYPES::Boolean)
  30. #                  # Para expressões do tipo: < switch >
  31. #-------------------------------------------------------------------------------
  32. # Agradecimentos Especiais: Yanfly, KGC, etc. pela idéia do sistema de notetags.
  33. #-------------------------------------------------------------------------------
  34. #===============================================================================
  35.  
  36. def eval_regexp (string, expression, type_expected = TYPES::Boolean, multi_values = false)
  37.  
  38.   if expression == "" or expression.include? (' ')
  39.     raise(ArgumentError, "A expressão informada para o avaliador de expressões regulares é inválida! Por favor informe um valor válido e que não possua\nespaços em branco!")
  40.   end
  41.  
  42.   values = ""; sign = ""
  43.   sign = "\\s*\\=\\s*" unless type_expected == TYPES::Boolean
  44.   if multi_values
  45.     case type_expected
  46.       when TYPES::Numbers
  47.       values = "[\\-]?\\d+(\\s*,\\s*[\\-]?\\d+)*"
  48.       when TYPES::Percentage
  49.       values = "\\d+%(\\s*,\\s*\\d+%)*"
  50.       when TYPES::Text
  51.       values = "\"(.*)+\"(\\s*,\\s*\"(.*)+\")*"
  52.     end
  53.   else
  54.     case type_expected
  55.       when TYPES::Numbers
  56.       values = "[\\-]?\\d+"
  57.       when TYPES::Percentage
  58.       values = "\\d+%"
  59.       when TYPES::Text
  60.       values = "\"(.*)+\""
  61.     end
  62.   end
  63.  
  64.   if type_expected == TYPES::Text
  65.     substring = []; startin = -1; lenght = -1
  66.     for n in 0..(string.size - 1)
  67.       if string[n, 1] == '<'
  68.         startin = n
  69.       elsif string[n, 1] == '>' and startin > 0
  70.         lenght = n - startin + 1
  71.       end
  72.       if startin > 0 and lenght > 0
  73.         substring.push string[startin, lenght]
  74.         startin = -1; lenght = -1
  75.       end
  76.     end
  77.     return nil if substring == []
  78.     string = substring
  79.   end
  80.  
  81.   result = []
  82.   for str in string
  83.     while true
  84.       /<\s*#{expression}#{sign}(#{values})\s*>/i.match (str)
  85.       break if $1.nil?
  86.       result.push $1
  87.       str = $~.post_match
  88.     end
  89.   end
  90.  
  91.   if type_expected == TYPES::Boolean
  92.     return true if result != []
  93.     return false
  94.   end
  95.   return nil if result == []  
  96.  
  97.   if multi_values
  98.     temp_res = []
  99.     for res in result
  100.       res.split(/\s*,\s*/).each { |str| temp_res.push str }
  101.     end
  102.     result = temp_res
  103.   end
  104.  
  105.   result.each_index {|index|
  106.     case type_expected
  107.       when TYPES::Numbers
  108.       result[index] = result[index].to_i
  109.       when TYPES::Percentage
  110.       result[index] = result[index].to_f / 100
  111.       when TYPES::Text
  112.       result[index].gsub! (/\"/) {}
  113.    end }
  114.    
  115.  return result[0] if !multi_values
  116.  return result
  117.  
  118. end
  119.  
  120. module TYPES
  121.  Boolean       = 0 # Somente como Tagnote de ativação (switch): <expressão>
  122.  Numbers       = 1 # Numeros em geral: <expressão = 123> ou <expressão = -456>
  123.  Percentage    = 2 # Usar como porcentagem: <expressão = 75%>
  124.  Text          = 3 # Para nomes em geral, etc: <expressão = "agora/com/équio">
  125. end
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement