Skip to content

Commit

Permalink
use read BOM|utf-8 flag for opening files, instead of string replacing (
Browse files Browse the repository at this point in the history
#574)

* use read BOM|utf-8 flag for opening files, instead of string replacing
* use :mode
  • Loading branch information
kmuto authored and takahashim committed Apr 20, 2016
1 parent f95a830 commit 1d81349
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 13 deletions.
6 changes: 2 additions & 4 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ def parse_chapters

def mkpart_from_namelistfile(path)
chaps = []
File.read(path).split.each_with_index do |name, idx|
name.sub!(/\A\xEF\xBB\xBF/u, '') # remove BOM
File.read(path, :mode => 'r:BOM|utf-8').split.each_with_index do |name, idx|
if path =~ /PREDEF/
chaps << mkchap(name)
else
Expand Down Expand Up @@ -404,9 +403,8 @@ def mkchap_ifexist(name, idx = nil)

def read_FILE(filename)
res = ""
File.open("#{@basedir}/#{filename}") do |f|
File.open("#{@basedir}/#{filename}", 'r:BOM|utf-8') do |f|
while line = f.gets
line.sub!(/\A\xEF\xBB\xBF/u, '') # remove BOM
if /\A#/ =~ line
next
end
Expand Down
2 changes: 1 addition & 1 deletion lib/review/book/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(book, number, name, path, io = nil)
@content = nil
end
if !@content && @path && File.exist?(@path)
@content = File.read(@path).sub(/\A\xEF\xBB\xBF/u, '')
@content = File.read(@path, :mode => 'r:BOM|utf-8')
@number = nil if ['nonum', 'nodisp', 'notoc'].include?(find_first_header_option)
end
@list_index = nil
Expand Down
4 changes: 2 additions & 2 deletions lib/review/book/part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# $Id: book.rb 4315 2009-09-02 04:15:24Z kmuto $
#
# Copyright (c) 2002-2008 Minero Aoki
# 2009-2014 Minero Aoki, Kenshi Muto
# 2009-2016 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand All @@ -26,7 +26,7 @@ def initialize(book, number, chapters, name = "", io = nil)
if io
@content = io.read
elsif @path && File.exist?(@path)
@content = File.read(@path).sub(/\A\xEF\xBB\xBF/u, '')
@content = File.read(@path, :mode => 'r:BOM|utf-8')
else
@content = nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/review/preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def parse_git_blob(g_obj)
end

def parse_file(fname)
File.open(fname) {|f|
File.open(fname, 'r:BOM|utf-8') {|f|
init_ErrorUtils f
return _parse_file(f)
}
Expand Down
7 changes: 2 additions & 5 deletions lib/review/tocparser.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#
# $Id: tocparser.rb 4268 2009-05-27 04:17:08Z kmuto $
#
# Copyright (c) 2002-2007 Minero Aoki
# 2008-2009 Minero Aoki, Kenshi Muto
# 2008-2016 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand All @@ -18,7 +16,7 @@ module ReVIEW

class TOCParser
def TOCParser.parse(chap)
f = StringIO.new(chap.content)
f = StringIO.new(chap.content, 'r:BOM|utf-8')
stream = Preprocessor::Strip.new(f)
new.parse(stream, chap).map do |root|
root.number = chap.number
Expand All @@ -39,7 +37,6 @@ def parse(f, chap)
node_stack = []
filename = chap.path
while line = f.gets
line.sub!(/\A\xEF\xBB\xBF/u, '') # remove BOM
case line
when /\A\#@/
;
Expand Down

0 comments on commit 1d81349

Please sign in to comment.