Class: Aozora2Html::Header
- Inherits:
-
Object
- Object
- Aozora2Html::Header
- Defined in:
- lib/aozora2html/header.rb
Overview
ヘッダ生成用
Instance Method Summary collapse
- #build_header_info ⇒ Object
- #build_title(header_info) ⇒ Object
- #header_element_type(string) ⇒ Object
-
#initialize(css_files:) ⇒ Header
constructor
A new instance of Header.
- #out_header_info(hash, attr, true_name = nil) ⇒ Object
- #process_person(string, header_info) ⇒ Object
- #push(line) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(css_files:) ⇒ Header
Returns a new instance of Header.
6 7 8 9 |
# File 'lib/aozora2html/header.rb', line 6 def initialize(css_files:) @header = [] @css_files = css_files end |
Instance Method Details
#build_header_info ⇒ Object
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 |
# File 'lib/aozora2html/header.rb', line 72 def build_header_info header_info = { title: @header[0] } case @header.length when 2 process_person(@header[1], header_info) when 3 if header_element_type(@header[1]) == :original header_info[:original_title] = @header[1] process_person(@header[2], header_info) elsif process_person(@header[2], header_info) == :author header_info[:subtitle] = @header[1] else header_info[:author] = @header[1] end when 4 if header_element_type(@header[1]) == :original header_info[:original_title] = @header[1] else header_info[:subtitle] = @header[1] end if process_person(@header[3], header_info) == :author header_info[:subtitle] = @header[2] else header_info[:author] = @header[2] end when 5 header_info[:original_title] = @header[1] header_info[:subtitle] = @header[2] header_info[:author] = @header[3] if process_person(@header[4], header_info) == :author raise Aozora2Html::Error, 'parser encounted author twice' end when 6 header_info[:original_title] = @header[1] header_info[:subtitle] = @header[2] header_info[:original_subtitle] = @header[3] header_info[:author] = @header[4] if process_person(@header[5], header_info) == :author raise Aozora2Html::Error, 'parser encounted author twice' end end header_info end |
#build_title(header_info) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/aozora2html/header.rb', line 64 def build_title(header_info) buf = [:author, :translator, :editor, :henyaku, :title, :original_title, :subtitle, :original_subtitle].filter_map { |item| header_info[item] } buf_str = buf.join(' ') "<title>#{buf_str}</title>" end |
#header_element_type(string) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/aozora2html/header.rb', line 24 def header_element_type(string) original = true string.each_char do |ch| code = ch.unpack1('H*') if ((code >= '00') && (code <= '7f')) || # 1byte ((code >= '8140') && (code <= '8258')) || # 1-1, 3-25 ((code >= '839f') && (code <= '8491')) # 6-1, 7-81 # continue else original = false break end end if original :original elsif string.match(PAT_EDITOR) :editor elsif string.match(PAT_HENYAKU) :henyaku elsif string.match(PAT_TRANSLATOR) :translator end end |
#out_header_info(hash, attr, true_name = nil) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/aozora2html/header.rb', line 15 def out_header_info(hash, attr, true_name = nil) found = hash[attr] if found "<h2 class=\"#{true_name or attr}\">#{found}</h2>\r\n" else '' end end |
#process_person(string, header_info) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/aozora2html/header.rb', line 48 def process_person(string, header_info) type = header_element_type(string) case type when :editor header_info[:editor] = string when :translator header_info[:translator] = string when :henyaku header_info[:henyaku] = string else type = :author header_info[:author] = string end type end |
#push(line) ⇒ Object
11 12 13 |
# File 'lib/aozora2html/header.rb', line 11 def push(line) @header.push(line) end |
#to_html ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/aozora2html/header.rb', line 116 def to_html header_info = build_header_info # <title> 行を構築 html_title = build_title(header_info) # 出力 out_buf = [] out_buf.push("<?xml version=\"1.0\" encoding=\"Shift_JIS\"?>\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\r\n \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\" >\r\n<head>\r\n <meta http-equiv=\"Content-Type\" content=\"text/html;charset=Shift_JIS\" />\r\n <meta http-equiv=\"content-style-type\" content=\"text/css\" />\r\n") @css_files.each do |css| out_buf.push("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"#{css}\" />\r\n") end out_buf.push("\t#{html_title}\r\n <script type=\"text/javascript\" src=\"../../jquery-1.4.2.min.js\"></script>\r\n <link rel=\"Schema.DC\" href=\"http://purl.org/dc/elements/1.1/\" />\r\n <meta name=\"DC.Title\" content=\"#{header_info[:title]}\" />\r\n <meta name=\"DC.Creator\" content=\"#{header_info[:author]}\" />\r\n <meta name=\"DC.Publisher\" content=\"#{AOZORABUNKO}\" />\r\n</head>\r\n<body>\r\n<div class=\"metadata\">\r\n") out_buf.push("<h1 class=\"title\">#{header_info[:title]}</h1>\r\n" + out_header_info(header_info, :original_title) + out_header_info(header_info, :subtitle) + out_header_info(header_info, :original_subtitle) + out_header_info(header_info, :author) + out_header_info(header_info, :editor) + out_header_info(header_info, :translator) + out_header_info(header_info, :henyaku, 'editor-translator')) out_buf.push("<br />\r\n<br />\r\n</div>\r\n<div id=\"contents\" style=\"display:none\"></div><div class=\"main_text\">") out_buf.join end |