-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kxml.rb
94 lines (83 loc) · 1.92 KB
/
Kxml.rb
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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'nokogiri'
class Kxml
def initialize(path)
#открываем файл
@path = path
@filename=path.rpartition('\\')
f = File.open(path)
@doc = Nokogiri::XML(f,nil,'UTF-8')
if not File.exist?("t_"+@path)
f.seek(0, IO::SEEK_SET)
@doc_copy = Nokogiri::XML(f,nil,'UTF-8')
f.close
else
f.close
File.open("t_"+@path) {|f| @doc_copy = Nokogiri::XML(f,nil,'UTF-8')}
end
#создаём глобальный массив узлов-сцен
@scenes = @doc.search 'scene'
@scenes_t = @doc_copy.search 'scene'
end
#функция для вывода списка сцен
def list_scenes
arr = @scenes.map do |scene|
scene['name']
end
arr
end
def make_tree(opt)
if opt
scenes = @scenes
else
scenes = @scenes_t end
tree = scenes.map do |scene|
name = scene['name']
lines = scene.children.map do |child|
[child.content, child.attributes['insertedText'].to_s]
end
[name,lines] #=tree
end
tree
end
#выбор активной сцены по имени из списка, active.class == element
def chose_scene(name)
@content = nil
@active = (@scenes.search "scene[@name=#{name}]")[0]
end
#вывод списка диалогов в сцене
def list_active
if not @content
@content = []
i = 0
children = @active.children #children.class == text or node
children.each do |child|
if /\n/.match(child.content)
@content[i]=nil
else
@content[i] = child.content
end
i+=1
end
puts '\n'
end
@content.each do |entry|
puts entry
end
end
def replace_copy(path, text)
scene = path[0]
line =path[1]
inserted = path[2]
target = @scenes_t[scene].children[line]
if inserted
target.set_attribute('insertedText', text)
else
target.content=text
end
end
def save_copy
File.open(@filename[0]+@filename[1]+"t_"+@filename[2],"w") do |f|
f.write(@doc_copy.to_xml)
end
end
end #Kxml end