Class: AozoraZip::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/aozora_zip/text.rb

Constant Summary collapse

CREATED_DATE_PATTERN =
Regexp.new("^([0-9A-Za-z]+)年([0-9A-Za-z]+)月([0-9A-Za-z]+)日作成\r\n".encode("cp932"))
Regexp.new("\r\n\r\n底本:".encode("cp932"))

Instance Method Summary collapse

Constructor Details

#initialize(text_file, core) ⇒ Text

Returns a new instance of Text.



6
7
8
9
# File 'lib/aozora_zip/text.rb', line 6

def initialize(text_file, core)
  @text_file = text_file
  @core = core
end

Instance Method Details

#absolute_pathObject



11
12
13
# File 'lib/aozora_zip/text.rb', line 11

def absolute_path
  File.join(@core.dirname, @text_file)
end

#contentObject



15
16
17
# File 'lib/aozora_zip/text.rb', line 15

def content
  IO.binread(absolute_path).force_encoding("cp932")
end

#create_date_string(time) ⇒ Object



32
33
34
# File 'lib/aozora_zip/text.rb', line 32

def create_date_string(time)
  "#{time.year}#{time.month}#{time.day}日作成"
end

#find_created_dateObject



36
37
38
39
40
41
# File 'lib/aozora_zip/text.rb', line 36

def find_created_date
  m = CREATED_DATE_PATTERN.match(content, footer_pos)
  if m
    Time.new(m[1],m[2],m[3])
  end
end


23
24
25
26
27
28
29
30
# File 'lib/aozora_zip/text.rb', line 23

def footer_pos
  m = FOOTER_SEPARATOR_PATTERN.match(content)
  if m
    m.pre_match.size + "\r\n\r\n".size
  else
    nil
  end
end

#update(text) ⇒ Object



19
20
21
# File 'lib/aozora_zip/text.rb', line 19

def update(text)
  IO.binwrite(absolute_path, text)
end

#update_created_date(time) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/aozora_zip/text.rb', line 43

def update_created_date(time)
  m = CREATED_DATE_PATTERN.match(content, footer_pos)
  if m
    new_date = create_date_string(time).encode("cp932")
    m.pre_match + new_date + "\r\n" + m.post_match
  else
    nil
  end
end