summaryrefslogtreecommitdiff
path: root/scripts/template.gd
blob: a0bf7c1bfd325035167ceec452f225918bdebc24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
extends Node

var data = null
var regex = null

func _init(path):
  var file = File.new()
  file.open(path, File.READ)
  var content = file.get_as_text()
  file.close()
  self.data = parse_json(content)
  self.regex = RegEx.new()
  self.regex.compile("/(.*?)/")

func replace_all(base, values):
  var mtch = self.regex.search(base)
  while mtch != null:
    var key = mtch.get_string(1)
    var replace = ""
    if values.has(key):
      var from = values[key]
      if typeof(from) == TYPE_DICTIONARY:
        if from.size() > 0:
          replace = from[from.keys()[randi() % from.size()]]
      elif typeof(from) == TYPE_ARRAY:
        if from.size() > 0:
          replace = from[randi() % from.size()]
      else:
        replace = str(from)
    base = self.regex.sub(base, replace)
    mtch = self.regex.search(base)
  return base