Skip to content

Commit 8fb58c5

Browse files
author
scritch
committed
Add a little script for the future RHG v2
git-svn-id: http://rhg.rubyforge.org/svn@66 2ba632a7-620d-0410-bd84-d74392fff1da
1 parent caa34a1 commit 8fb58c5

File tree

3 files changed

+318
-1
lines changed

3 files changed

+318
-1
lines changed

lib/rhg_html_gen.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def generate_html htmlfile, txtfile
9999
if md = TranslatedByRE.match(r)
100100
$tags['translated by'] = md[1]
101101
r.sub!(TranslatedByRE, '')
102-
else
102+
elsif not $tags['translated by']
103103
STDERR.puts "error: no translator defined in file #{txtfile}"
104104
return
105105
end

wikigen/regroup.rb

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#!/usr/bin/env ruby
2+
# -*- coding: utf-8 -*- vim:set encoding=utf-8:
3+
# TODO:
4+
# - cleanup (and remove dependency with rhg_html_gen)
5+
# - images
6+
# - when generating the output data, if Japanese = English, add in the English something like "(To translate)"
7+
$KCODE = 'u'
8+
9+
ISOLanguage = 'en-US'
10+
11+
$LOAD_PATH.unshift('../lib')
12+
require 'rhg_html_gen'
13+
14+
COMMENT_RE = /\$comment\((.+?)\)\$/
15+
AUTOLINK_RE = %r{(^|[^:])\b((?:ht|f)tp://\S+?)([^\w\/;]*?)(?=\s|<|$)}
16+
NEW_CODE_RE = /`([^<]*?)`/m
17+
TAG_RE = /\$tag\((.+?)\)\$/
18+
19+
AUTO_CONV_ENDING=<<END
20+
<hr>
21+
22+
御意見・御感想・誤殖の指摘などは
23+
"青木峰郎 &lt;[email protected]&gt;":mailto:[email protected]
24+
までお願いします。
25+
26+
"『Rubyソースコード完全解説』
27+
はインプレスダイレクトで御予約・御購入いただけます (書籍紹介ページへ飛びます)。":http://direct.ips.co.jp/directsys/go_x_TempChoice.cfm?sh_id=EE0040&amp;spm_id=1&amp;GM_ID=1721
28+
29+
Copyright (c) 2002-2004 Minero Aoki, All rights reserved.
30+
END
31+
32+
TranslatedByRE = /^Translated by (.+)$/
33+
34+
def rhg_redcloth_replace(text)
35+
text = text.dup
36+
if md = TranslatedByRE.match(text)
37+
$tags['translated by'] = md[1]
38+
text.sub!(TranslatedByRE, '')
39+
end
40+
text.sub!(AUTO_CONV_ENDING, '') # remove the ending in the automatically generated Japanese files
41+
text.gsub!(COMMENT_RE) { |m| '' } # remove comments
42+
text.gsub(TAG_RE) do |m| # manages tags
43+
tag_name = $~[1]
44+
if $tags[tag_name]
45+
$tags[tag_name]
46+
else
47+
puts "Warning: The tag #{tag_name} is not defined"
48+
''
49+
end
50+
end
51+
fig_counter = 0
52+
text.gsub!(RedCloth::IMAGE_RE) do |m| # must be done before the `` replacement
53+
fig_counter += 1
54+
stln,algn,atts,url,title,href,href_a1,href_a2 = $~[1..8]
55+
#puts "Warning: the images used the the RHG should be PNGs, not JPEGs" if /\.jpe?g$/i.match(url)
56+
"\n\n<p style=\"text-align:center;\">\n#{m.gsub(/`/, '')}<br />Figure #{fig_counter}: #{title}\n</p>\n\n"
57+
end
58+
text.gsub!(NEW_CODE_RE) { |m| "<code>#{$~[1]}</code>" }
59+
text.gsub!(AUTOLINK_RE) do |m|
60+
before, address, after = $~[1..3]
61+
"#{before}\"#{address}\":#{address}#{after}"
62+
end
63+
text
64+
end
65+
66+
class Blocks
67+
def initialize(filename)
68+
@data = rhg_redcloth_replace(IO.read(filename)).split(/\n/).map { |l| l.rstrip }
69+
@boundaries = []
70+
71+
find_boundaries
72+
end
73+
74+
def length
75+
@boundaries.length
76+
end
77+
78+
def [](i)
79+
@data[@boundaries[i]].join("\n")
80+
end
81+
82+
def regroup_with_following(i)
83+
@data[@boundaries[i].last] << "\n<==================================>"
84+
@boundaries[i] = @boundaries[i].first..@boundaries[i+1].last
85+
@boundaries.delete_at(i+1)
86+
end
87+
88+
def each
89+
length.times { |i| yield self[i] }
90+
end
91+
92+
private
93+
def find_boundaries
94+
beginning = 0
95+
in_pre = false
96+
@data.each_with_index do |line, i|
97+
if line.empty? and not in_pre
98+
if i != beginning
99+
@boundaries.push(beginning..(i-1))
100+
beginning = i+1
101+
else
102+
beginning += 1
103+
end
104+
elsif i == @data.length - 1
105+
@boundaries.push(beginning..i)
106+
elsif /<pre/.match(line)
107+
@boundaries.push(beginning..(i-1)) if i > beginning
108+
beginning = i
109+
in_pre = true
110+
elsif /<\/pre/.match(line)
111+
@boundaries.push(beginning..i)
112+
beginning = i+1
113+
in_pre = false
114+
end
115+
end
116+
end
117+
end
118+
119+
chapter_num = sprintf("%02d", ARGV[0].to_i)
120+
121+
en_file_name = "../en/chapter#{chapter_num}.txt"
122+
ja_file_name = "../ja/chapter#{chapter_num}.txt"
123+
# if the English file does not exist yet, just use the Japanese one as source
124+
if File.exists?(en_file_name)
125+
blocks_en = Blocks.new(en_file_name)
126+
else
127+
$tags['translated by'] = '(not translated yet)'
128+
blocks_en = Blocks.new(ja_file_name)
129+
end
130+
blocks_ja = Blocks.new(ja_file_name)
131+
132+
BLOCK_REGROUPING_RE = /^(h[1-9]\.|<pre|▼)/
133+
134+
i = 0
135+
regroup_pos = 0
136+
while i < blocks_ja.length and i < blocks_en.length
137+
block_ja = blocks_ja[i]
138+
block_en = blocks_en[i]
139+
if BLOCK_REGROUPING_RE.match(block_ja)
140+
if BLOCK_REGROUPING_RE.match(block_en)
141+
regroup_pos = i
142+
i += 1
143+
else
144+
blocks_en.regroup_with_following(regroup_pos)
145+
end
146+
elsif BLOCK_REGROUPING_RE.match(block_en)
147+
blocks_ja.regroup_with_following(regroup_pos)
148+
else
149+
i += 1
150+
end
151+
end
152+
153+
# regroup the last blocks to have the same number of blocks in both
154+
blocks_en.regroup_with_following(blocks_en.length-2) while blocks_ja.length < blocks_en.length
155+
blocks_ja.regroup_with_following(blocks_ja.length-2) while blocks_en.length < blocks_ja.length
156+
157+
blocks_en.each do |b|
158+
if md = /h1\.\s*(.+)$/.match(b)
159+
$tags['title'] = md[1].gsub(/(<[^>]*>|`)/, '') # remove markup and backquotes from the title
160+
break
161+
end
162+
end
163+
if not $tags['title']
164+
STDERR.puts "error: no h1 section in source file"
165+
return
166+
end
167+
168+
File.open("chapter#{chapter_num}.txt", "w") do |f|
169+
f.puts "<table>"
170+
blocks_ja.length.times do |i|
171+
f.puts "<tr><td>"
172+
f.puts
173+
f.puts blocks_en[i]
174+
f.puts
175+
f.puts "</td><td>"
176+
f.puts
177+
f.puts blocks_ja[i]
178+
f.puts
179+
f.print "</td></tr>"
180+
end
181+
f.puts
182+
f.puts "</table>"
183+
end
184+
185+
FOOTER = <<EOS
186+
<hr>
187+
188+
The original work is Copyright &copy; 2002 - 2004 Minero AOKI.<br />
189+
Translated by #{$tags['translated by']}<br />
190+
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.5/"><img alt="Creative Commons License" border="0" src="images/somerights20.png"/></a><br/>This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/2.5/">Creative Commons Attribution-NonCommercial-ShareAlike2.5 License</a>.
191+
192+
</body>
193+
</html>
194+
EOS
195+
196+
RedClothRules = [ :textile ]
197+
198+
generate_html("chapter#{chapter_num}.html", "chapter#{chapter_num}.txt")

wikigen/rhg.css

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
3+
rhg.css
4+
5+
*/
6+
7+
body {
8+
background-color: white;
9+
color: black;
10+
text-align: left;
11+
line-height: 140%;
12+
margin-top: 5%;
13+
margin-left: 0;
14+
}
15+
16+
h1 {
17+
text-align: left;
18+
font-size: 200%;
19+
margin-top: 0em;
20+
margin-bottom: 1em;
21+
padding-left: 12px;
22+
border-left: 80px solid #33a;
23+
line-height: 80px;
24+
}
25+
26+
h2 {
27+
text-align: left;
28+
font-size: 150%;
29+
margin-top: 3em;
30+
margin-bottom: 1em;
31+
border-bottom: 2px solid #33a;
32+
}
33+
34+
h3 {
35+
text-align: left;
36+
font-size: 150%;
37+
margin-top: 1em;
38+
margin-bottom: 0.1em;
39+
padding-left: 8px;
40+
border-left: 12px solid #33a;
41+
}
42+
43+
h4 {
44+
text-align: left;
45+
font-size: 150%;
46+
margin-top: 1em;
47+
margin-bottom: 0.1em;
48+
}
49+
50+
ul {
51+
padding-left: 1em;
52+
}
53+
54+
li {
55+
padding-left: 0em;
56+
}
57+
58+
dl {
59+
margin-top: 0em;
60+
margin-bottom: 0em;
61+
margin-left: 0em;
62+
margin-right: 0em;
63+
}
64+
65+
dt {
66+
margin-top: 0.5em;
67+
margin-bottom: 0.2em;
68+
margin-left: 0em;
69+
margin-right: 0em;
70+
}
71+
72+
dd {
73+
margin-top: 0em;
74+
margin-bottom: 0em;
75+
margin-left: 4em;
76+
margin-right: 0em;
77+
}
78+
79+
p.caption {
80+
margin-botton: 0px;
81+
}
82+
83+
pre {
84+
font-size: 90%;
85+
line-height: 110%;
86+
padding: 8px;
87+
background: #eee;
88+
}
89+
90+
span.ami {
91+
color: black;
92+
background-color: #ccc;
93+
}
94+
95+
p.image {
96+
align: center;
97+
margin-top: 2em;
98+
margin-bottom: 2em;
99+
}
100+
101+
address {
102+
font-style: normal;
103+
line-height: 110%;
104+
text-align: right;
105+
}
106+
107+
a:link, a:visited {
108+
color: #33a;
109+
text-decoration: none;
110+
}
111+
112+
a:active, a:visited {
113+
color: #666;
114+
text-decoration: none;
115+
}
116+
117+
a:hover, a:focus {
118+
text-decoration: underline;
119+
}

0 commit comments

Comments
 (0)