Module: Osis2Html5
- Defined in:
- lib/osis2html5.rb,
lib/osis2html5/version.rb
Constant Summary collapse
- SUMMARY_NCHAR =
140- VERSION =
"0.1.1"
Class Method Summary collapse
- .body_text(book) ⇒ Object
- .book_list(doc) ⇒ Object
- .book_list_of_testament(title, table) ⇒ Object
- .book_tables(doc) ⇒ Object
- .convert_chapters(book) ⇒ Object
- .convert_linegroups(book, insert_br: true) ⇒ Object
- .convert_ruby(doc, rp: true) ⇒ Object
- .convert_verses(book) ⇒ Object
- .embed_variable(name) ⇒ Object
- .format_as_whole_doc(book, title, summary, erb: false) ⇒ Object
- .format_verse_number(number, id) ⇒ Object
- .generate_index(doc, outdir, lang: 'ja', erb: false) ⇒ Object
- .header(message) ⇒ Object
- .html5_footer ⇒ Object
- .html5_header(title, summary, lang: 'ja', erb: false) ⇒ Object
- .main(osis, outdir, **opts) ⇒ Object
- .osis_id_to_inner_id(osis_id) ⇒ Object
- .osis_id_to_verse_number(osis_id) ⇒ Object
- .process_book(book, outdir, erb: false) ⇒ Object
- .run ⇒ Object
- .testament_titles(doc) ⇒ Object
- .version(doc) ⇒ Object
- .xml_header ⇒ Object
Class Method Details
.body_text(book) ⇒ Object
94 95 96 97 98 |
# File 'lib/osis2html5.rb', line 94 def body_text(book) nodes = book.dup nodes.css('title').remove nodes.text.gsub(/\n+/, '') end |
.book_list(doc) ⇒ Object
264 265 266 267 268 269 270 |
# File 'lib/osis2html5.rb', line 264 def book_list(doc) titles = testament_titles(doc) tables = book_tables(doc) titles.zip(tables).map do |title, table| book_list_of_testament(title, table) end.join end |
.book_list_of_testament(title, table) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/osis2html5.rb', line 272 def book_list_of_testament(title, table) lis = table.map do |id, name| %(<li><a href="#{id}.html">#{name}</a></li>) end <<~EOS <h2>#{title}</h2> <ul> #{lis.join("\n")} </ul> EOS end |
.book_tables(doc) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/osis2html5.rb', line 244 def book_tables(doc) nnew = 27 # yes we know it pairs = doc.css('div[@type="book"]').map do |book| [book[:osisID].downcase, book.at_css('title[@type="main"]').content] end if pairs[0][0] == 'matt' # need to reorder pairs = pairs[nnew..-1] + pairs[0...nnew] end nold = pairs.size - nnew [pairs[0...nold], pairs[nold..-1]] end |
.convert_chapters(book) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/osis2html5.rb', line 108 def convert_chapters(book) book.css('chapter').each do |chapter| chapter.name = 'div' inner_id = osis_id_to_inner_id(chapter[:osisID]) chapter[:id] = inner_id chapter.remove_attribute('osisID') title = chapter.at_css('title') title.name = 'h2' title[:class] = 'chapter-title' title.remove_attribute('type') title.children.wrap(%(<a href="##{inner_id}">)) convert_verses(chapter) convert_linegroups(chapter) end end |
.convert_linegroups(book, insert_br: true) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/osis2html5.rb', line 161 def convert_linegroups(book, insert_br: true) book.css('lg').each do |lg| lg.name = 'span' lg[:class] = 'lg' lg.at_css('l').previous = '<br/>' if insert_br lg.css('l').each do |l| l.name = 'span' l[:class] = 'l' l << '<br/>' if insert_br end end end |
.convert_ruby(doc, rp: true) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/osis2html5.rb', line 54 def convert_ruby(doc, rp: true) ws = doc.css('w') ws.wrap '<ruby/>' ws.each do |w| rb, rt = w.text, w[:gloss] ruby = w.parent ruby.content = rb if rp ruby << "<rp>(</rp><rt>#{rt}</rt><rp>)</rp>" else ruby << "<rt>#{rt}</rt>" end end doc end |
.convert_verses(book) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/osis2html5.rb', line 129 def convert_verses(book) book.css('verse').each do |verse| verse.name = 'span' verse[:class] = 'verse' if verse.key?('eID') verse['data-e-id'] = osis_id_to_inner_id(verse['eID']) verse.remove_attribute('eID') next end osis_id = verse[:osisID] inner_id = osis_id_to_inner_id(osis_id) verse[:id] = inner_id verse.children = %(<span class="verse-content">#{verse.inner_html}</span>) verse_number = osis_id_to_verse_number(osis_id) formatted = format_verse_number(verse_number, inner_id) if verse.child verse.child.previous = formatted else verse.next_sibling.previous = formatted end verse.remove_attribute('osisID') if verse.key?('sID') verse['data-s-id'] = osis_id_to_inner_id(verse['sID']) verse.remove_attribute('sID') end end end |
.embed_variable(name) ⇒ Object
181 182 183 |
# File 'lib/osis2html5.rb', line 181 def (name) "<%= #{name} if binding.local_variable_defined?(:#{name}) %>" end |
.format_as_whole_doc(book, title, summary, erb: false) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/osis2html5.rb', line 174 def format_as_whole_doc(book, title, summary, erb: false) xml_header + html5_header(title, summary, lang: book.lang, erb: erb) + book.to_xml + end |
.format_verse_number(number, id) ⇒ Object
125 126 127 |
# File 'lib/osis2html5.rb', line 125 def format_verse_number(number, id) %(<span class="verse-number"><a href="##{id}">#{number}</a></span>) end |
.generate_index(doc, outdir, lang: 'ja', erb: false) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/osis2html5.rb', line 214 def generate_index(doc, outdir, lang: 'ja', erb: false) ver = version(doc) index = <<~EOS <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="#{lang}" lang="#{lang}"> <head> <meta charset="UTF-8"/> #{(:head) if erb} <title>#{ver}#{(:additional_title) if erb}</title> </head> <body> #{(:head_of_body) if erb} <main class="container"> <h1>#{ver}</h1> #{book_list(doc).chomp} </main> </body> </html> EOS filename = 'index.html' filename << '.erb' if erb path = File.join(outdir, filename) File.write(path, index) end |
.header(message) ⇒ Object
35 36 37 |
# File 'lib/osis2html5.rb', line 35 def header() STDERR.puts end |
.html5_footer ⇒ Object
207 208 209 210 211 212 |
# File 'lib/osis2html5.rb', line 207 def <<~EOS </body> </html> EOS end |
.html5_header(title, summary, lang: 'ja', erb: false) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/osis2html5.rb', line 189 def html5_header(title, summary, lang: 'ja', erb: false) lang_attrs = %( xml:lang="#{lang}" lang="#{lang}") <<~EOS <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"#{lang_attrs}> <head> <meta charset="UTF-8"/> #{(:head) if erb} <meta property="og:title" content="#{title}"/> <meta property="og:type" content="website"/> <meta property="og:description" content="#{summary}"/> <title>#{title}#{(:additional_title) if erb}</title> </head> <body> #{(:head_of_body) if erb} EOS end |
.main(osis, outdir, **opts) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/osis2html5.rb', line 39 def main(osis, outdir, **opts) Dir.mkdir(outdir) rescue nil # just ignore created dir header 'parsing OSIS' doc = Nokogiri::XML.parse(File.read(osis)) header 'processing each books' Parallel.each(doc.css('div[@type="book"]')) do |book| process_book(book, outdir, erb: opts[:erb]) end generate_index(doc, outdir, lang: doc.at('osisText').lang, erb: opts[:erb]) header '... done!' end |
.osis_id_to_inner_id(osis_id) ⇒ Object
100 101 102 |
# File 'lib/osis2html5.rb', line 100 def osis_id_to_inner_id(osis_id) osis_id.sub(/^[^\.]+\./, '').sub('.', ':') end |
.osis_id_to_verse_number(osis_id) ⇒ Object
104 105 106 |
# File 'lib/osis2html5.rb', line 104 def osis_id_to_verse_number(osis_id) osis_id.sub(/^.*\./, '') end |
.process_book(book, outdir, erb: false) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/osis2html5.rb', line 70 def process_book(book, outdir, erb: false) name = book[:osisID].downcase print name + ' ' book.name = 'main' book[:class] ='book container' book.remove_attribute('type') book.remove_attribute('osisID') summary = body_text(book)[0, SUMMARY_NCHAR-2] + '……' title = book.at_css('title') title.name = 'h1' title.remove_attribute('type') convert_ruby(book) convert_chapters(book) filename = name + '.html' filename << '.erb' if erb path = File.join(outdir, filename) File.write(path, format_as_whole_doc(book, title.content, summary, erb: erb)) end |
.run ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/osis2html5.rb', line 12 def run opts = {} OptionParser.new do |o| o. = "usage: osis2html5 [options] <input.osis> <output dirname>" o.on('-v', '--version', 'show version') do puts "osis2html5 #{VERSION}" exit end o.on('-h', '--help', 'prints this help') do puts o exit end o.on('--erb', 'enable erb mode') do opts[:erb] = true end end.parse! main(*ARGV, **opts) end |
.testament_titles(doc) ⇒ Object
256 257 258 259 260 261 262 |
# File 'lib/osis2html5.rb', line 256 def testament_titles(doc) gr = doc.css('div[@type="bookGroup"]') labels = [%w[x-OT 旧約聖書], %w[x-NT 新約聖書]] titles = labels.map do |st, deftitle| gr&.at(%(div[@subType="#{st}"] title))&.content || deftitle end end |
.version(doc) ⇒ Object
240 241 242 |
# File 'lib/osis2html5.rb', line 240 def version(doc) doc.at_css('work title').content end |
.xml_header ⇒ Object
185 186 187 |
# File 'lib/osis2html5.rb', line 185 def xml_header %(<?xml version="1.0" encoding="UTF-8"?>\n) end |