| 1 |
#!/usr/bin/env ruby |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
=begin |
| 5 |
|
| 6 |
= convbkmk Ver.0.30 |
| 7 |
|
| 8 |
2018.11.25 |
| 9 |
Takuji Tanaka |
| 10 |
ttk (at) t-lab.opal.ne.jp |
| 11 |
((<URL:http://www.t-lab.opal.ne.jp/tex/uptex_en.html>)) |
| 12 |
|
| 13 |
|
| 14 |
== Abstract |
| 15 |
|
| 16 |
((*convbkmk*)) is a tiny utility for making correct bookmarks in pdf files |
| 17 |
typesetted by pLaTeX/upLaTeX with the hyperref package. |
| 18 |
pLaTeX/upLaTeX + hyperref outputs data of bookmarks |
| 19 |
in their internal encodings (EUC-JP, Shift_JIS or UTF-8). |
| 20 |
On the other hand, the PostScript/PDF format requests that |
| 21 |
the data is written in a certain syntax with UTF-16 or PDFDocEncoding. |
| 22 |
Thus, data conversion is required to create correct bookmarks. |
| 23 |
|
| 24 |
In addition, pLaTeX outputs dvi files with special commands |
| 25 |
in its internal encoding (EUC-JP or Shift_JIS). |
| 26 |
It is not consistent with recent dviware and file systems |
| 27 |
which assume UTF-8. |
| 28 |
|
| 29 |
((*convbkmk*)) provides a function of |
| 30 |
the encoding conversion and formatting the data. |
| 31 |
|
| 32 |
== Requirement |
| 33 |
|
| 34 |
((*ruby*)) 1.9.x or later is required. |
| 35 |
((*ruby*)) 1.8.x is no longer supported. |
| 36 |
To support conversion of dvi special, |
| 37 |
((*dvispc*)) in dviout-util is required. |
| 38 |
|
| 39 |
== Examples |
| 40 |
|
| 41 |
=== for pdf bookmark |
| 42 |
|
| 43 |
pLaTeX (internal kanji code: euc) + hyperref + dvips : |
| 44 |
$ platex doc00.tex |
| 45 |
$ platex doc00.tex |
| 46 |
$ dvips doc00.dvi |
| 47 |
$ convbkmk.rb -e doc00.ps |
| 48 |
$ ps2pdf doc00-convbkmk.ps |
| 49 |
|
| 50 |
pLaTeX (kanji code: sjis) + hyperref + dvipdfmx : |
| 51 |
$ platex doc01.tex |
| 52 |
$ platex doc01.tex |
| 53 |
$ convbkmk.rb -s -o doc01.out |
| 54 |
$ platex doc01.tex |
| 55 |
$ dvipdfmx doc01.dvi |
| 56 |
|
| 57 |
upLaTeX + hyperref + dvips : |
| 58 |
$ uplatex doc02.tex |
| 59 |
$ uplatex doc02.tex |
| 60 |
$ dvips doc02.dvi |
| 61 |
$ convbkmk.rb doc02.ps |
| 62 |
$ ps2pdf doc02-convbkmk.ps |
| 63 |
|
| 64 |
upLaTeX + hyperref + dvipdfmx : |
| 65 |
$ uplatex doc03.tex |
| 66 |
$ uplatex doc03.tex |
| 67 |
$ convbkmk.rb -o doc03.out |
| 68 |
$ uplatex doc03.tex |
| 69 |
$ dvipdfmx doc03.dvi |
| 70 |
|
| 71 |
=== for dvi special (graphic file names) |
| 72 |
|
| 73 |
pLaTeX (internal kanji code: euc) + dvips : |
| 74 |
$ platex doc04.tex |
| 75 |
$ platex doc04.tex |
| 76 |
$ convbkmk.rb -e -d doc04.dvi |
| 77 |
$ dvips doc04-convbkmk.dvi |
| 78 |
$ ps2pdf doc04-convbkmk.ps |
| 79 |
|
| 80 |
pLaTeX (internal kanji code: sjis) + dvipdfmx : |
| 81 |
$ platex doc05.tex |
| 82 |
$ platex doc05.tex |
| 83 |
$ convbkmk.rb -s -d doc05.dvi |
| 84 |
$ dvipdfmx doc05.dvi |
| 85 |
|
| 86 |
((*convbkmk*)) executes ((*dvispc*)) command |
| 87 |
to extract dvi files. |
| 88 |
((*dvispc*)) command is designated by |
| 89 |
an environmental variable 'DVISPC'. |
| 90 |
By default, 'dvispc' is set. |
| 91 |
|
| 92 |
More examples are provided at the GitHub repository |
| 93 |
and by the upTeX source archive. |
| 94 |
|
| 95 |
== Repository |
| 96 |
|
| 97 |
convbkmk is maintained on GitHub: |
| 98 |
((<URL:https://github.com/t-tk/convbkmk>)) |
| 99 |
|
| 100 |
== License |
| 101 |
|
| 102 |
convbkmk |
| 103 |
|
| 104 |
Copyright (c) 2009-2018 Takuji Tanaka |
| 105 |
|
| 106 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 107 |
of this software and associated documentation files (the "Software"), to deal |
| 108 |
in the Software without restriction, including without limitation the rights |
| 109 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 110 |
copies of the Software, and to permit persons to whom the Software is |
| 111 |
furnished to do so, subject to the following conditions: |
| 112 |
|
| 113 |
The above copyright notice and this permission notice shall be included in |
| 114 |
all copies or substantial portions of the Software. |
| 115 |
|
| 116 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 117 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 118 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 119 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 120 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 121 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 122 |
THE SOFTWARE. |
| 123 |
|
| 124 |
== History |
| 125 |
|
| 126 |
: 2009.08.02 0.00 |
| 127 |
* Initial version. |
| 128 |
: 2011.05.02 0.01 |
| 129 |
* Bug fix: BOM was not correct. |
| 130 |
: 2012.05.08 0.02 |
| 131 |
* Bug fix: for a case of dvips with -z option and Ruby1.8. |
| 132 |
* Add conversion of /Creator and /Producer . |
| 133 |
: 2012.05.12 0.03 |
| 134 |
* Suppress halfwidth -> fullwidth katakana conversion and MIME decoding in Ruby1.8. |
| 135 |
: 2012.06.01 0.04 |
| 136 |
* Support escape sequences: \n, \r, \t, \b, \f, \\, \ddd (octal, PDFDocEncoding) and \0xUUUU (Unicode UTF-16BE). |
| 137 |
* Support sequences of end of line: '\' or other followed by "\n", "\r\n" or "\r" . |
| 138 |
* Set file IO to binary mode. |
| 139 |
: 2012.07.26 0.05 |
| 140 |
* Add -o option to support conversion of OUT files generated by dvipdfmx. |
| 141 |
: 2012.08.07 0.06 |
| 142 |
* Bug fix: Ver.0.05 does not work with Ruby1.9. |
| 143 |
: 2012.09.17 0.07 |
| 144 |
* Bug fix: An infinite loop occurs in Ver.0.05, 0.06 with -g option in some cases. |
| 145 |
* Add reference for PDFDocEncoding. |
| 146 |
: 2013.05.11 0.08 |
| 147 |
* Add -O option: overwrite output files onto input files instead of creating foo-convbkmk.ps . |
| 148 |
* Make comments rd/rdtool friendly. |
| 149 |
: 2014.03.02 0.09 |
| 150 |
* Bug fix: Conversion was not complete in some cases. |
| 151 |
: 2014.03.08 0.10 |
| 152 |
* Bug fix: Output of binary data might be broken in filter mode on Windows. |
| 153 |
: 2014.12.29 0.10a |
| 154 |
* Update the author's mail address and web site. |
| 155 |
: 2018.11.11 0.20 |
| 156 |
* Do not support Ruby1.8 anymore. |
| 157 |
: 2018.11.25 0.30 |
| 158 |
* Add -d option to support conversion of graphic file names in dvi special by pLaTeX. |
| 159 |
|
| 160 |
=end |
| 161 |
|
| 162 |
Version = "0.30" |
| 163 |
|
| 164 |
require "optparse" |
| 165 |
|
| 166 |
if RUBY_VERSION < "1.9" |
| 167 |
abort("Ruby 1.8 or earlier is no longer supported.") |
| 168 |
end |
| 169 |
|
| 170 |
class String |
| 171 |
def to_utf8(enc) |
| 172 |
self.force_encoding(enc.current).encode('UTF-8') |
| 173 |
end |
| 174 |
def utf16be_to_utf8 |
| 175 |
self.force_encoding('UTF-16BE').encode('UTF-8') |
| 176 |
end |
| 177 |
def utf8_to_utf16be |
| 178 |
self.force_encoding('UTF-8').encode('UTF-16BE') |
| 179 |
end |
| 180 |
end |
| 181 |
|
| 182 |
class TeXEncoding |
| 183 |
attr_accessor :current, :option, :status, :is_8bit |
| 184 |
attr_reader :list |
| 185 |
|
| 186 |
def initialize |
| 187 |
@current = false |
| 188 |
@option = false |
| 189 |
@status = false |
| 190 |
@is_8bit = false |
| 191 |
@list = ['Shift_JIS', 'EUC-JP', 'UTF-8'] |
| 192 |
end |
| 193 |
|
| 194 |
def set_process_encoding(enc) |
| 195 |
if @status == 'fixed' |
| 196 |
raise 'dupulicate definition' |
| 197 |
end |
| 198 |
if enc == 'guess' |
| 199 |
@option = 'guess' |
| 200 |
@status = 'guess' |
| 201 |
else |
| 202 |
@current = enc |
| 203 |
@option = enc |
| 204 |
@status = 'fixed' |
| 205 |
end |
| 206 |
return enc |
| 207 |
end |
| 208 |
end |
| 209 |
enc = TeXEncoding.new |
| 210 |
|
| 211 |
Opts = {} |
| 212 |
|
| 213 |
OptionParser.new do |opt| |
| 214 |
opt.on('-e', '--euc-jp', |
| 215 |
'set pTeX internal encoding to EUC-JP') {|v| |
| 216 |
enc.set_process_encoding('EUC-JP') |
| 217 |
} |
| 218 |
opt.on('-s', '--shift_jis', |
| 219 |
'set pTeX internal encoding to Shift_JIS') {|v| |
| 220 |
enc.set_process_encoding('Shift_JIS') |
| 221 |
} |
| 222 |
opt.on('-u', '--utf-8', |
| 223 |
'set upTeX internal encoding to UTF-8') {|v| |
| 224 |
enc.set_process_encoding('UTF-8') |
| 225 |
} |
| 226 |
opt.on('-g', '--guess', |
| 227 |
'guess pTeX/upTeX internal encoding') {|v| |
| 228 |
enc.set_process_encoding('guess') |
| 229 |
} |
| 230 |
enc_alias = Hash.new |
| 231 |
enc.list.each { |e| |
| 232 |
enc_alias[e] = e |
| 233 |
enc_alias[e[0]] = e |
| 234 |
enc_alias[e.downcase] = e |
| 235 |
} |
| 236 |
opt.on('--enc=ENC', enc_alias, |
| 237 |
'set pTeX/upTeX internal encoding to ENC') {|v| |
| 238 |
enc.set_process_encoding(v) |
| 239 |
} |
| 240 |
opt.on('-o', '--out', |
| 241 |
'treat OUT files') {|v| |
| 242 |
Opts[:mode] = :out |
| 243 |
Opts[:overwrite] = true |
| 244 |
require "fileutils" |
| 245 |
} |
| 246 |
opt.on('-d', '--dvi-special', |
| 247 |
'treat specials in DVI files') {|v| |
| 248 |
Opts[:mode] = :spc |
| 249 |
Dvispc = ENV["DVISPC"] ||= 'dvispc' |
| 250 |
require "fileutils" |
| 251 |
} |
| 252 |
opt.on('-O', '--overwrite', |
| 253 |
'overwrite output files') {|v| |
| 254 |
Opts[:overwrite] = true |
| 255 |
require "fileutils" |
| 256 |
} |
| 257 |
opt.banner += " file0.ps [file1.ps ...]\n" \ |
| 258 |
+ opt.banner.sub('Usage:',' ') + " < in_file.ps > out_file.ps\n" \ |
| 259 |
+ opt.banner.sub('Usage:',' ') + " -o file0.out [file1.out ...]\n" \ |
| 260 |
+ opt.banner.sub('Usage:',' ') + " -d file0.dvi [file1.dvi ...]\n" \ |
| 261 |
+ opt.banner.sub('Usage:',' ') + " -d file0.dvispc [file1.dvispc ...]" |
| 262 |
|
| 263 |
opt.parse! |
| 264 |
end |
| 265 |
|
| 266 |
# default encoding |
| 267 |
if enc.status == false |
| 268 |
enc.set_process_encoding('UTF-8') |
| 269 |
end |
| 270 |
if Opts[:mode] == :out |
| 271 |
OpenP, CloseP, OpenPEsc, ClosePEsc = '{', '}', '\{', '\}' |
| 272 |
FileSfx = 'out' |
| 273 |
elsif Opts[:mode] == :spc then |
| 274 |
FileSfx = '(dvi|dvispc)' |
| 275 |
else |
| 276 |
OpenP, CloseP, OpenPEsc, ClosePEsc = '(', ')', '\(', '\)' |
| 277 |
FileSfx = 'ps' |
| 278 |
end |
| 279 |
|
| 280 |
def try_guess_encoding(line, enc) |
| 281 |
return 'US-ASCII' if line.ascii_only? |
| 282 |
|
| 283 |
enc.is_8bit = true |
| 284 |
valid_enc = false |
| 285 |
count = 0 |
| 286 |
enc.list.each { |e| |
| 287 |
if line.dup.force_encoding(e).valid_encoding? |
| 288 |
count += 1 |
| 289 |
valid_enc = e |
| 290 |
end |
| 291 |
} |
| 292 |
if count == 1 |
| 293 |
enc.set_process_encoding(valid_enc) |
| 294 |
return valid_enc |
| 295 |
elsif count > 1 |
| 296 |
return false # ambiguous |
| 297 |
else |
| 298 |
raise 'Cannot guess encoding!' |
| 299 |
end |
| 300 |
end |
| 301 |
|
| 302 |
def os_legacy_encoding(enc) |
| 303 |
return if enc.status != 'guess' |
| 304 |
enc.is_8bit = true |
| 305 |
if (RUBY_PLATFORM =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/i) |
| 306 |
valid_enc = 'Shift_JIS' |
| 307 |
else |
| 308 |
valid_enc = 'EUC-JP' |
| 309 |
end |
| 310 |
enc.set_process_encoding(valid_enc) |
| 311 |
end |
| 312 |
|
| 313 |
def check_parentheses_balance(line, enc) |
| 314 |
depth = 0 |
| 315 |
count = 0 |
| 316 |
tmp_prev = '' |
| 317 |
tmp_rest = line |
| 318 |
|
| 319 |
if enc.status == 'guess' |
| 320 |
if tmp_enc = try_guess_encoding(line, enc) |
| 321 |
# succeeded in guess or ascii only |
| 322 |
tmp_rest = line.force_encoding(tmp_enc) |
| 323 |
else |
| 324 |
# ambiguous |
| 325 |
raise 'unexpected internal condition!' |
| 326 |
end |
| 327 |
else |
| 328 |
tmp_enc = enc.current |
| 329 |
tmp_rest = tmp_rest.force_encoding(tmp_enc) |
| 330 |
unless tmp_rest.valid_encoding? |
| 331 |
# illegal input |
| 332 |
$stdout = STDERR |
| 333 |
p 'parameters: ' |
| 334 |
p ' status: ' + enc.status |
| 335 |
p ' option: ' + enc.option |
| 336 |
p ' current: ' + enc.current |
| 337 |
p enc.is_8bit |
| 338 |
p ' [' + line + ']' |
| 339 |
raise 'encoding is not consistent' |
| 340 |
end |
| 341 |
end |
| 342 |
|
| 343 |
while tmp_rest.length>0 do |
| 344 |
if (tmp_rest =~ /\A(\\#{OpenPEsc}|\\#{ClosePEsc}|[^#{OpenP}#{CloseP}])*(#{OpenPEsc}|#{ClosePEsc})/o) # parenthis |
| 345 |
if $2 == OpenP |
| 346 |
depth += 1 |
| 347 |
count += 1 |
| 348 |
else |
| 349 |
depth -= 1 |
| 350 |
end |
| 351 |
tmp_prev += $& |
| 352 |
tmp_rest = $' |
| 353 |
else |
| 354 |
tmp_prev += tmp_rest |
| 355 |
tmp_rest = '' |
| 356 |
end |
| 357 |
if depth<1 |
| 358 |
break |
| 359 |
end |
| 360 |
end |
| 361 |
return depth, count, tmp_prev, tmp_rest |
| 362 |
end |
| 363 |
|
| 364 |
# PDFDocEncoding -> UTF-16BE |
| 365 |
# Ref. "PDF Reference, Sixth Edition, version 1.7", 2006, Adobe Systems Incorporated |
| 366 |
# http://www.adobe.com/devnet/pdf/pdf_reference_archive.html |
| 367 |
# http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf |
| 368 |
PDF2UNI = Array(0..255) |
| 369 |
PDF2UNI[0o030..0o037] = 0x02d8, 0x02c7, 0x02c6, 0x02d9, 0x02dd, 0x02db, 0x02da, 0x02dc |
| 370 |
PDF2UNI[0o200..0o207] = 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x0192, 0x2044 |
| 371 |
PDF2UNI[0o210..0o217] = 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, 0x2018 |
| 372 |
PDF2UNI[0o220..0o227] = 0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x0141, 0x0152, 0x0160 |
| 373 |
PDF2UNI[0o230..0o237] = 0x0178, 0x017d, 0x0131, 0x0142, 0x0153, 0x0161, 0x017e, 0xfffd |
| 374 |
PDF2UNI[0o240 ] = 0x20ac |
| 375 |
PDF2UNI[0o255 ] = 0xfffd |
| 376 |
|
| 377 |
def conv_string_to_utf16be(line, enc) |
| 378 |
if line !~ /(#{OpenPEsc})(.*)(#{ClosePEsc})/mo |
| 379 |
raise 'illegal input!' |
| 380 |
end |
| 381 |
pre, tmp, post = $`, $2, $' |
| 382 |
|
| 383 |
if tmp.ascii_only? && tmp !~ /\\0x[0-9A-F]{4}/i |
| 384 |
return line |
| 385 |
end |
| 386 |
|
| 387 |
conv = '' |
| 388 |
conv.force_encoding('UTF-8') |
| 389 |
tmp.force_encoding(enc.current) |
| 390 |
|
| 391 |
while tmp.length>0 do |
| 392 |
case tmp |
| 393 |
when /\A[^\\\n\r]+/ |
| 394 |
conv += $&.to_utf8(enc) |
| 395 |
when /\A\\([0-3][0-7][0-7])/ # PDFDocEncoding -> UTF-8 |
| 396 |
conv += [PDF2UNI[$1.oct]].pack("U*") |
| 397 |
when /\A\\0x(D[8-B][0-9A-F]{2})\\0x(D[C-F][0-9A-F]{2})/i # surrogate pair |
| 398 |
conv += [$1.hex, $2.hex].pack("n*").utf16be_to_utf8 |
| 399 |
when /\A\\0x([0-9A-F]{4})/i |
| 400 |
conv += [$1.hex].pack("U*") |
| 401 |
when /\A\\[nrtbf\\]/ |
| 402 |
conv += eval(%!"#{$&}"!) |
| 403 |
when /\A(\r\n|\r|\n)/ |
| 404 |
conv += "\n" |
| 405 |
when /\A\\([\r\n]{1,2})|\\/ |
| 406 |
# ignore |
| 407 |
else |
| 408 |
raise 'unexpected input!' |
| 409 |
end |
| 410 |
tmp = $' |
| 411 |
end |
| 412 |
|
| 413 |
buf = '' |
| 414 |
conv16be = "\xFE\xFF" # BOM U+FEFF |
| 415 |
conv16be.force_encoding('UTF-16BE') |
| 416 |
conv16be += conv.utf8_to_utf16be # UTF-16BE with BOM |
| 417 |
conv16be.each_byte {|byte| |
| 418 |
buf += (Opts[:mode] == :out ? '\%03o' : '%02X') % byte |
| 419 |
} |
| 420 |
buf = Opts[:mode] == :out ? '{' + buf + '}' : '<' + buf + '>' |
| 421 |
return pre + buf + post |
| 422 |
end |
| 423 |
|
| 424 |
def special_string_to_utf8(line, enc) |
| 425 |
if line.ascii_only? || line !~ /\Axxx[1-4]/mo |
| 426 |
return line, 0 |
| 427 |
end |
| 428 |
|
| 429 |
if line !~ /\Axxx(\d) (\d+) '(.*)'([^']*)\Z/mo |
| 430 |
raise 'illegal input!' |
| 431 |
end |
| 432 |
xxx, bytes, str, trail = $1.to_i, $2.to_i, $3, $4 |
| 433 |
if str.bytesize != bytes |
| 434 |
raise 'byte size is not consistent!' |
| 435 |
end |
| 436 |
if str !~ /\A((PS|ps)file=|pdf:image |pdf:epdf )/mo |
| 437 |
return line, 0 |
| 438 |
end |
| 439 |
|
| 440 |
conv = '' |
| 441 |
conv.force_encoding('UTF-8') |
| 442 |
|
| 443 |
os_legacy_encoding(enc) |
| 444 |
str.force_encoding(enc.current) |
| 445 |
str = str.to_utf8(enc) |
| 446 |
bytes_new = str.bytesize |
| 447 |
|
| 448 |
xxx_new = bytes_new <= 0xff ? 1 : 4 |
| 449 |
conv = 'xxx' + xxx_new.to_s + ' ' + bytes_new.to_s + " '" + str + "'" + trail |
| 450 |
return conv, bytes_new - bytes + xxx_new - xxx |
| 451 |
end |
| 452 |
|
| 453 |
def dvi_post_post(line, offset) |
| 454 |
if line !~ /\Apost_post (\d+) ([23])(?: 223){4,7}\Z/mo |
| 455 |
raise 'illegal input!' |
| 456 |
end |
| 457 |
bytes, id = $1.to_i, $2 |
| 458 |
padding = line.scan(' 223').count |
| 459 |
bytes += offset |
| 460 |
padding = (padding - offset) % 4 + 4 |
| 461 |
line = 'post_post ' + bytes.to_s + ' ' + id + ' 223' * padding + "\n" |
| 462 |
return line |
| 463 |
end |
| 464 |
|
| 465 |
def file_treatment(ifile, ofile, enc) |
| 466 |
ifile.set_encoding('ASCII-8BIT') |
| 467 |
ofile.set_encoding('ASCII-8BIT') |
| 468 |
|
| 469 |
line, offset = '', 0 |
| 470 |
while l = ifile.gets do |
| 471 |
line.force_encoding('ASCII-8BIT') |
| 472 |
line += l |
| 473 |
if Opts[:mode] == :out then |
| 474 |
reg = %r!(\{)! |
| 475 |
elsif Opts[:mode] == :spc then |
| 476 |
reg = %r!(\A(xxx|post_post))! |
| 477 |
else |
| 478 |
reg = %r!(/Title|/Author|/Keywords|/Subject|/Creator|/Producer)(\s+\(|$)! |
| 479 |
end |
| 480 |
if (line !~ reg ) |
| 481 |
ofile.print line |
| 482 |
line = '' |
| 483 |
next |
| 484 |
end |
| 485 |
|
| 486 |
if Opts[:mode] == :spc |
| 487 |
if (line =~ /\Axxx/) |
| 488 |
line, diff = special_string_to_utf8(line, enc) |
| 489 |
offset += diff |
| 490 |
else |
| 491 |
line = dvi_post_post(line, offset) |
| 492 |
end |
| 493 |
ofile.print line |
| 494 |
line = '' |
| 495 |
next |
| 496 |
end |
| 497 |
|
| 498 |
ofile.print $` |
| 499 |
line = $& + $' |
| 500 |
|
| 501 |
if Opts[:mode] != :out |
| 502 |
while line =~ %r!(/Title|/Author|/Keywords|/Subject|/Creator|/Producer)\Z! do |
| 503 |
line += ifile.gets |
| 504 |
end |
| 505 |
end |
| 506 |
|
| 507 |
if enc.status == 'guess' |
| 508 |
if tmp_enc = try_guess_encoding(line, enc) |
| 509 |
# succeeded in guess or ascii only |
| 510 |
line.force_encoding(tmp_enc) |
| 511 |
else |
| 512 |
# ambiguous |
| 513 |
next |
| 514 |
end |
| 515 |
end |
| 516 |
|
| 517 |
while line.length>0 do |
| 518 |
depth, count, tmp_prev, tmp_rest \ |
| 519 |
= check_parentheses_balance(line, enc) |
| 520 |
if depth<0 |
| 521 |
p depth, count, tmp_prev, tmp_rest |
| 522 |
raise 'illegal input! (depth<0)' |
| 523 |
elsif depth>0 |
| 524 |
break |
| 525 |
elsif count==0 |
| 526 |
ofile.print line |
| 527 |
line = '' |
| 528 |
break |
| 529 |
elsif count>0 |
| 530 |
ofile.print conv_string_to_utf16be(tmp_prev, enc) |
| 531 |
line = tmp_rest |
| 532 |
else |
| 533 |
p depth, count, tmp_prev, tmp_rest |
| 534 |
raise 'illegal input! (count<0)' |
| 535 |
end |
| 536 |
end |
| 537 |
|
| 538 |
end |
| 539 |
|
| 540 |
if enc.status == 'guess' && enc.is_8bit |
| 541 |
raise 'did not succeed in guess encoding!' |
| 542 |
end |
| 543 |
end |
| 544 |
|
| 545 |
|
| 546 |
### main |
| 547 |
if ARGV.size == 0 |
| 548 |
ifile = STDIN.binmode |
| 549 |
ofile = STDOUT.binmode |
| 550 |
file_treatment(ifile, ofile, enc) |
| 551 |
else |
| 552 |
ARGV.each {|fin| |
| 553 |
if (fin !~ /\.#{FileSfx}$/io) |
| 554 |
raise 'input file does not seem ' + FileSfx.upcase + ' file' |
| 555 |
end |
| 556 |
sfx = $& |
| 557 |
if (Opts[:mode] == :spc && fin =~ /\.dvi$/i) |
| 558 |
dvi_conversion = true |
| 559 |
fspc = fin.gsub(/\.dvi$/io, '.dvispc') |
| 560 |
if !(system Dvispc + ' -a ' + fin + ' ' + fspc) |
| 561 |
raise "fail to execute 'dvispc -a' command!" |
| 562 |
end |
| 563 |
fin = fspc |
| 564 |
sfx = '.dvispc' |
| 565 |
end |
| 566 |
fout = fin.gsub(/#{sfx}$/i, "-convbkmk#{sfx}") |
| 567 |
open(fin, 'rb') {|ifile| |
| 568 |
open(fout, 'wb') {|ofile| |
| 569 |
file_treatment(ifile, ofile, enc) |
| 570 |
} |
| 571 |
} |
| 572 |
if (Opts[:overwrite]) |
| 573 |
FileUtils.mv(fout, fin) |
| 574 |
fout = fin |
| 575 |
end |
| 576 |
if dvi_conversion |
| 577 |
fdvi = fout.gsub(/\.dvispc$/o, '.dvi') |
| 578 |
if !(system Dvispc + ' -x ' + fout + ' ' + fdvi) |
| 579 |
raise "fail to execute 'dvispc -x' command!" |
| 580 |
end |
| 581 |
end |
| 582 |
} |
| 583 |
end |
| 584 |
|