This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
splitter.rb
executable file
·206 lines (191 loc) · 6.26 KB
/
splitter.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# spliter.rb
# Copyright (c) UnaMesa Association 2004-2008
# License: Creative Commons Attribution ShareAlike 3.0 License http://creativecommons.org/licenses/by-sa/3.0/
require 'tiddler'
require 'iconv'
#require 'open-uri'
#require 'net/http'
class Splitter
def initialize(filename, outdir=nil, charset="ISO-8859-1")
@filename = filename
@conv = Iconv.new(charset,"UTF-8")
dirset = false
dirnum = 0;
dirname = outdir.nil? || outdir.empty? ? @filename : File.join(outdir, File.basename(@filename))
while !dirset do
@dirname = dirname + "." + dirnum.to_s
if(File.exists?(@dirname))
dirnum += 1
else
Dir.mkdir(@dirname)
dirset = true
end
end
end
def Splitter.usesubdirectories=(usesubdirectories)
@@usesubdirectories = usesubdirectories
end
def Splitter.tagsubdirectory=(tagsubdirectory)
@@tagsubdirectory = tagsubdirectory
end
def Splitter.quiet=(quiet)
@@quiet = quiet
end
def split
recipes = {"main" => "", "shadows" => "", "plugins" => "", "content" => "", "svg" => "", "feeds" => "", "themes" => "", "tags" => "", "templates" => ""}
tiddlerCount = readStoreArea(recipes)
if(tiddlerCount == 0)
puts "'#{@filename}' does not contain any tiddlers"
return
end
if(@@usesubdirectories)
toprecipe = ""
recipes.each_key do |k|
if(k != "tags" && k !="main")
if(recipes[k] != "")
dirname = File.join(@dirname, k)
File.open(File.join(dirname, "split.recipe"), File::CREAT|File::TRUNC|File::RDWR, 0644) do |recipefile|
recipefile << recipes[k]
end
toprecipe << "recipe: " + k + "/split.recipe\n"
end
end
end
if(recipes["tags"] != "")
dirname = File.join(@dirname, @@tagsubdirectory)
File.open(File.join(dirname, "split.recipe"), File::CREAT|File::TRUNC|File::RDWR, 0644) do |recipefile|
recipefile << recipes["tags"]
end
toprecipe << "recipe: #{@@tagsubdirectory}/split.recipe\n"
end
recipes["main"] = toprecipe
end
File.open(File.join(@dirname, "split.recipe"), File::CREAT|File::TRUNC|File::RDWR, 0644) do |recipefile|
recipefile << recipes["main"]
end
puts "\n'#{@filename}' processed, #{tiddlerCount.to_s} tiddlers written to '#{@dirname}'"
end
private
def writeTiddler(tiddler, recipes)
dirname = @dirname
tiddlerFilename = tiddler.title.to_s.gsub(/[ <>:]/,"_").gsub(/\t/,"%09").gsub(/#/,"%23").gsub(/%/,"%25").gsub(/\*/,"%2a").gsub(/,/,"%2c").gsub(/\//,"%2f").gsub(/</,"%3c").gsub(/>/,"%3e").gsub(/\?/,"%3f")
tiddlerFilename = @conv.iconv(tiddlerFilename)
if(tiddler.tags =~ /systemConfig/)
dirname = @dirname
if(@@usesubdirectories)
dirname = File.join(@dirname, "plugins")
if(!File.exists?(dirname))
Dir.mkdir(dirname)
end
end
targetfile = File.join(dirname, tiddlerFilename += ".js")
File.open(targetfile, File::CREAT|File::TRUNC|File::RDWR, 0644) do |out|
out << tiddler.contents
end
File.open(targetfile + ".meta", File::CREAT|File::TRUNC|File::RDWR, 0644) do |out|
out << tiddler.to_meta
end
recipes["plugins"] << "tiddler: #{tiddlerFilename}\n"
elsif(tiddler.tags =~ /svg/)
dirname = @dirname
if(@@usesubdirectories)
dirname = File.join(@dirname, "svg")
if(!File.exists?(dirname))
Dir.mkdir(dirname)
end
end
targetfile = File.join(dirname, tiddlerFilename += ".svg")
File.open(targetfile, File::CREAT|File::TRUNC|File::RDWR, 0644) do |out|
out << tiddler.contents
end
File.open(targetfile + ".meta", File::CREAT|File::TRUNC|File::RDWR, 0644) do |out|
out << tiddler.to_meta
end
recipes["svg"] << "tiddler: #{tiddlerFilename}\n"
else
if(tiddler.tags =~ /systemServer/)
writeTiddlerToSubDir(tiddler, tiddlerFilename, recipes["feeds"], "feeds")
elsif(tiddler.tags =~ /systemTheme/)
writeTiddlerToSubDir(tiddler, tiddlerFilename, recipes["themes"], "themes")
elsif(@@tagsubdirectory && tiddler.tags =~ Regexp.new(@@tagsubdirectory))
writeTiddlerToSubDir(tiddler, tiddlerFilename, recipes["tags"], @@tagsubdirectory)
elsif(Tiddler.isShadow?(tiddler.title))
writeTiddlerToSubDir(tiddler, tiddlerFilename, recipes["shadows"], "shadows")
elsif(tiddler.title =~ /Template:.*/)
writeTiddlerToSubDir(tiddler, tiddlerFilename, recipes["templates"], "templates")
else
writeTiddlerToSubDir(tiddler, tiddlerFilename, recipes["content"], "content")
end
end
recipes["main"] << "tiddler: #{tiddlerFilename}.tiddler\n"
if(!@@quiet)
puts "Writing: #{tiddler.title}"
end
end
def writeTiddlerToSubDir(tiddler, tiddlerFilename, recipe, subdir)
dirname = @dirname
if(@@usesubdirectories)
dirname = File.join(@dirname, subdir)
if(!File.exists?(dirname))
Dir.mkdir(dirname)
end
end
targetfile = File.join(dirname, tiddlerFilename += ".tiddler")
File.open(targetfile, File::CREAT|File::TRUNC|File::RDWR, 0644) do |out|
out << tiddler.to_div("tiddler",false)
end
recipe << "tiddler: #{tiddlerFilename}\n"
end
def readStoreArea(recipes)
open(@filename) do |file|
tiddlerCount = 0
start = false
line = file.gets
begin
line = file.gets
end while(line && line !~ /<div id="storeArea">/)
line = line.sub(/.*<div id="storeArea">/, "").strip
begin
if(line =~ /<div ti.*/)
tiddlerCount += 1
tiddler = Tiddler.new
line = tiddler.read_div(file,line)
writeTiddler(tiddler, recipes)
else
line = file.gets
end
end while(line && line !~ /<!--STORE-AREA-END-->/ && line !~ /<!--POST-BODY-START-->/ && line !~ /<div id="shadowArea">/)
return tiddlerCount
end
end
public
def Splitter.extractTiddlers(filename,titles)
out = Array.new
found = Array.new
open(filename) do |file|
start = false
line = file.gets
begin
line = file.gets
end while(line && line !~ /<div id="storeArea">/)
line = line.sub(/.*<div id="storeArea">/, "").strip
begin
if(line =~ /<div ti.*/)
tiddler = Tiddler.new
line = tiddler.read_div(file,line)
if titles.include?(tiddler.title)
out.push(tiddler)
found.push(tiddler.title)
end
else
line = file.gets
end
end while(line && line !~ /<!--STORE-AREA-END-->/ && line !~ /<!--POST-BODY-START-->/ && line !~ /<div id="shadowArea">/)
end
if out.length == titles.length
return out
else
STDERR.puts("Tiddlers #{(titles-found).to_s} not found in #{filename}")
end
end
end