Advertisement
Rafael_Sol_Maker

RAFAEL_SOL_MAKER's VX REGEXP NOTETAG SYSTEM v1.0

Nov 17th, 2011
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.67 KB | None | 0 0
  1. #===============================================================================
  2. #             RAFAEL_SOL_MAKER's VX REGEXP NOTETAG SYSTEM v1.0
  3. #-------------------------------------------------------------------------------
  4. #Description:   Function especially made to evaluate all the notetags on Power
  5. #               Pack using regular expressions.  This script makes it much
  6. #               easier and practical to obtain the wanted values between misc.
  7. #               text and empty space.  
  8. #               Multiple return values are supported when these are separated
  9. #               with ‘;’ in the same expression or even inanother; all these
  10. #               values are returned in an array.
  11. #               For Boolean expressions, it doesn’t care if ‘multi_values’ is
  12. #               set to ‘true’ or ’false’.
  13. #-------------------------------------------------------------------------------
  14. # How to Use:
  15. #                Sintax:
  16. #                eval_regexp (text, expr, [type_expected], [multi_values?])
  17. #                  * Parameters between "[]" are optional.
  18. #                Where:
  19. #                  text           -> Text where we will search for the tags.
  20. #                  expr           -> What word is contained in the expression?
  21. #                  type_espected  -> Expected variable type. See TYPES below.
  22. #                  multi_values?  -> The function will get more than one value?
  23. #
  24. #                Some using examples:
  25. #                  number = eval_regexp(text, "number", TYPES::Numbers, true)
  26. #                  number.nil? ?  @my_number = 0 : @my_number = number
  27. #                  # For expressions like: <number 123, 456, 789>
  28. #                
  29. #                  @switch = eval_regexp(text, "switch", TYPES::Boolean)
  30. #                  # For expressions like: < switch >, returns true or false
  31. #-------------------------------------------------------------------------------
  32. # Special Thanks: Yanfly, KGC, etc. for the original idea of the notetag system.
  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, "The given expression to the regular expression evaluator is invalid! \nPlease use a valid value that doesn't have any empty space!")
  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 # Use this like a switch or boolean tagnote: <expression>
  122.  Numbers       = 1 # Numbers, in general cases: <expression = 123> ou <expression = -456>
  123.  Percentage    = 2 # Use like percentage: <expression = 75%> # Returns 0.75F
  124.  Text          = 3 # For texts in general, etc: <expression = "c:/we/gotta/power123">
  125. end
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement