Class: SiSU_XML_Fictionbook::Source::Scroll

Inherits:
SiSU_XML_Fictionbook::Source show all
Defined in:
lib/sisu/xml_fictionbook2.rb

Instance Method Summary collapse

Methods inherited from SiSU_XML_Fictionbook::Source

#read

Constructor Details

#initialize(data = '', md = '') ⇒ Scroll

Returns a new instance of Scroll.



116
117
118
119
120
121
# File 'lib/sisu/xml_fictionbook2.rb', line 116

def initialize(data='',md='')
  @data,@md=data,md
  @trans=SiSU_XML_Munge::Trans.new(@md)
  @particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(md.opt)
  @env=@particulars.env
end

Instance Method Details

#collapsedObject

collapsed –>



139
140
141
# File 'lib/sisu/xml_fictionbook2.rb', line 139

def collapsed
  %w[ 0 1 2 3 4 5 ]
end

#endnotes_build(endnotes, filename_fictionbook) ⇒ Object



298
299
300
301
302
303
304
305
# File 'lib/sisu/xml_fictionbook2.rb', line 298

def endnotes_build(endnotes,filename_fictionbook)
  if endnotes.length > 0
    filename_fictionbook.puts %{</body><body name="notes">}
    endnotes.each do |en|
      filename_fictionbook.puts SiSU_TextUtils::Wrap.new(en,80,6).line_wrap
    end
  end
end

#extract_endnotesObject

work on



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/sisu/xml_fictionbook2.rb', line 217

def extract_endnotes                                #work on
  endnotes,endnotes_raw,endnotes_b=[],[],[]
  @data.each do |para|
    endnotes_raw << para.obj.scan(/#{Mx[:en_a_o]}(.+?)#{Mx[:en_a_c]}/m)
    endnotes_b << para.obj.scan(/#{Mx[:en_b_o]}(.+?)#{Mx[:en_b_c]}/m)
  end
  endnotes_raw.flatten.each do |en|
    en=@trans.markup_fictionbook(en)
    endnotes << en.gsub(/([\d+*]+)\s+(.+)/m,
      %{<section id="footnote\\1">\n
<title><p>\\1.</p></title>\n
<p>\\2</p>\n
</section>})
  end
  endnotes_raw=[]
  endnotes
end

#extract_imagesObject

work on



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/sisu/xml_fictionbook2.rb', line 234

def extract_images                                #work on
  begin
    require 'base64'
  rescue LoadError
    SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
      error('base64 NOT FOUND (LoadError)')
  end
  images_raw,images_base64_fb2=[],[]
  images_base64={}
  if defined? @md.make.cover_image[:cover]
    images_raw << @md.make.cover_image[:cover]
  end
  @data.each do |para|
    images_raw << para.obj.scan(/#{Mx[:lnk_o]}\s*(\S+?\.(?:png|jpg|gif)).+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/m)
  end
  images_raw.flatten.sort.each do |img|
    imgpth=@env.url.images_local + '/' + img
    open(imgpth)
    if FileTest.file?(imgpth)
      images_base64[img]=Base64.encode64(File.read(imgpth))
    end
  end
  images_raw=[]
  images_base64.each_key do |k|
    imgtype=case k
    when /\.jpg/ then 'jpeg'
    when /\.png/ then 'png'
    when /\.gif/ then 'gif'
    else              'jpeg'
    end
    images_base64_fb2 << %{<binary content-type="image/#{imgtype}" id="#{k}">#{images_base64[k]}
</binary>
}
  end
  images_base64_fb2.join("\n")
end

#fictionbookObject



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/sisu/xml_fictionbook2.rb', line 142

def fictionbook
  [
    'section',
    'section',
    'section',
    'section',
    'section',
    'section',
    'section'
  ]
end

#headObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/sisu/xml_fictionbook2.rb', line 158

def head
  version=SiSU_Env::InfoVersion.instance.get_version
  rb_ver=SiSU_Env::InfoVersion.instance.rbversion
  date_available=if defined? @md.date.available; "\n     <p>#{@md.date.available} Initial version</p>"
  else ''
  end
  date_modified=if defined? @md.date.modified; "\n      <p>#{@md.date.modified} Last Modified</p>"
  else ''
  end
  coverpageimage=if defined? @md.make.cover_image[:cover]
    %{\n    <coverpage><image href="##{@md.make.cover_image[:cover]}" /></coverpage>}
  else ''
  end
  if defined? @md.authors \
  and @md.authors.length > 0
    authors=[]
    @md.authors.each do |author|
      authors << '    <author>'
      if not author[:others].empty?
        authors << %{      <first-name>#{author[:others]}</first-name>}
      end
      if not author[:the].empty?
        authors << %{      <last-name>#{author[:the]}</last-name>}
      end
      authors << '    </author>'
    end
    authors=authors.join("\n")
  end
  <<-WOK
<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns:xl="http://www.w3.org/1999/xlink"
 xmlns="http://www.gribuser.ru/xml/fictionbook/2.0">
<description>
  <title-info>
    <genre match="100">***</genre>
#{authors}
    <book-title>#{@md.title.full}</book-title>#{coverpageimage}
    <annotation>
    </annotation>
    <date value="#{@md.date.published}">#{@md.date.published}</date>
  </title-info>
     <document-info>
    <author>
<first-name/>
<last-name/>
<nickname/>
    </author>
    <program-used>#{version.project} #{version.version} and #{rb_ver}</program-used>
    <date value="#{version.date}">#{version.date}</date>
    <src-url>#{@md.file.output_path.manifest.url}/#{@md.file.base_filename.manifest}</src-url>
    <id></id>
    <version>1.0</version>
    <history>#{date_available}#{date_modified}
    </history>
  </document-info>
</description>
<body>
  WOK
end

#markup_text(data) ⇒ Object



270
271
272
273
274
275
276
277
# File 'lib/sisu/xml_fictionbook2.rb', line 270

def markup_text(data)
  data.each_with_index do |o,i|
    if o.is ==:heading || o.is ==:para
      o.obj=@trans.markup_fictionbook(o.obj,o.is) #unless o.obj==nil
    end
  end
  data
end

#output(o, comment = '') ⇒ Object



285
286
287
288
289
# File 'lib/sisu/xml_fictionbook2.rb', line 285

def output(o,comment='')
   puts o.lc == (0..6) \
   ? "#{spaces*o.lc}<#{o.lc}>[#{o.ocn}] #{o.ln} #{o.obj}</#{o.lc}>#{comment}"
   : "<#{o.lc}>[#{o.ocn}] #{o.ln} #{o.obj}</#{o.lc}>#{comment}"
end

#put(line) ⇒ Object



155
156
157
# File 'lib/sisu/xml_fictionbook2.rb', line 155

def put(line)
  puts line if @md.opt.act[:verbose_plus][:set]==:on
end

#songsheetObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sisu/xml_fictionbook2.rb', line 122

def songsheet
  @t='sisu'
  data=@data
  if @md.opt.act[:verbose_plus][:set]==:on
    structure_collapsed(data)
  end
  head
  endnotes=extract_endnotes
  images_base64=extract_images
  data=markup_text(data)
  structure_build_collapsed(data,endnotes,images_base64)
end

#spacesObject



134
135
136
# File 'lib/sisu/xml_fictionbook2.rb', line 134

def spaces
  Ax[:spaces]
end

#structure_build_collapsed(data, endnotes, images_base64) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/sisu/xml_fictionbook2.rb', line 306

def structure_build_collapsed(data,endnotes,images_base64)
  file=SiSU_Env::FileOp.new(@md)
  filename_fictionbook=file.write_file.xml_fictionbook
  h=0
  doc_position=:head
  filename_fictionbook.puts head
  data.each_with_index do |o,i|
    ocn=if @make.build.ocn?
      (defined? o.ocn and not o.ocn.nil?) \
      ? "\n#{Dx[:ocn_o]}#{o.ocn}#{Dx[:ocn_c]}"
      : ''
    else ''
    end
    if  o.is ==:heading
      unless doc_position==:head
        filename_fictionbook.puts structure_build_tag_close(o.lc,h)
      end
      doc_position=:body_and_tail
      filename_fictionbook.puts %{#{spaces*o.lc}<#{tags.fictionbook[o.lc]}>
#{spaces*o.lc}<title>
}
      filename_fictionbook.puts SiSU_TextUtils::Wrap.new("<p>#{o.obj}#{ocn}</p>",80,(o.lc*2+2)).line_wrap
      filename_fictionbook.puts %{#{spaces*o.lc}</title>}
      h=o.lc
    elsif  o.is ==:heading_insert \
    and o.obj =~/Endnotes/ \
    and o.ln == 1
      break
    elsif (o.of ==:para or o.of ==:block)
      filename_fictionbook.puts SiSU_TextUtils::Wrap.new("<p>#{o.obj}#{ocn}</p>",80,6).line_wrap
    end
  end
  filename_fictionbook.puts structure_build_tag_close(0,h)
  endnotes_build(endnotes,filename_fictionbook)
  filename_fictionbook.puts tail(images_base64)
  filename_fictionbook.close
end

#structure_build_tag_close(lc, h) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/sisu/xml_fictionbook2.rb', line 343

def structure_build_tag_close(lc,h)
  x=[]
  case h
  when 0
    x << "#{spaces*0}</#{tags.fictionbook[0]}>" if (lc <= 0)
  when 1
    x << "#{spaces*1}</#{tags.fictionbook[1]}>" if (lc <= 1)
    x << "#{spaces*0}</#{tags.fictionbook[0]}>" if (lc <= 0)
  when 2
    x << "#{spaces*2}</#{tags.fictionbook[2]}>" if (lc <= 2)
    x << "#{spaces*1}</#{tags.fictionbook[1]}>" if (lc <= 1)
    x << "#{spaces*0}</#{tags.fictionbook[0]}>" if (lc <= 0)
  when 3
    x << "#{spaces*3}</#{tags.fictionbook[3]}>" if (lc <= 3)
    x << "#{spaces*2}</#{tags.fictionbook[2]}>" if (lc <= 2)
    x << "#{spaces*1}</#{tags.fictionbook[1]}>" if (lc <= 1)
    x << "#{spaces*0}</#{tags.fictionbook[0]}>" if (lc <= 0)
  when 4
    x << "#{spaces*4}</#{tags.fictionbook[4]}>" if (lc <= 4)
    x << "#{spaces*3}</#{tags.fictionbook[3]}>" if (lc <= 3)
    x << "#{spaces*2}</#{tags.fictionbook[2]}>" if (lc <= 2)
    x << "#{spaces*1}</#{tags.fictionbook[1]}>" if (lc <= 1)
    x << "#{spaces*0}</#{tags.fictionbook[0]}>" if (lc <= 0)
  when 5
    x << "#{spaces*5}</#{tags.fictionbook[5]}>" if (lc <= 5)
    x << "#{spaces*4}</#{tags.fictionbook[4]}>" if (lc <= 4)
    x << "#{spaces*3}</#{tags.fictionbook[3]}>" if (lc <= 3)
    x << "#{spaces*2}</#{tags.fictionbook[2]}>" if (lc <= 2)
    x << "#{spaces*1}</#{tags.fictionbook[1]}>" if (lc <= 1)
    x << "#{spaces*0}</#{tags.fictionbook[0]}>" if (lc <= 0)
  when 6
    x << "#{spaces*6}</#{tags.fictionbook[6]}>" if (lc <= 6)
    x << "#{spaces*5}</#{tags.fictionbook[5]}>" if (lc <= 5)
    x << "#{spaces*4}</#{tags.fictionbook[4]}>" if (lc <= 4)
    x << "#{spaces*3}</#{tags.fictionbook[3]}>" if (lc <= 3)
    x << "#{spaces*2}</#{tags.fictionbook[2]}>" if (lc <= 2)
    x << "#{spaces*1}</#{tags.fictionbook[1]}>" if (lc <= 1)
    x << "#{spaces*0}</#{tags.fictionbook[0]}>" if (lc <= 0)
  end
  x.join("\n")
end

#structure_collapsed(data) ⇒ Object



290
291
292
293
294
295
296
297
# File 'lib/sisu/xml_fictionbook2.rb', line 290

def structure_collapsed(data)
  puts "\ncollapsed structure, heading outline --->\n\n"
  data.each_with_index do |o,i|
    if  (o.is ==:heading || o.is ==:heading_insert)
      output(o)
    end
  end
end

#tagsObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sisu/xml_fictionbook2.rb', line 137

def tags
  # collapsed -->
  def collapsed
    %w[ 0 1 2 3 4 5 ]
  end
  def fictionbook
    [
      'section',
      'section',
      'section',
      'section',
      'section',
      'section',
      'section'
    ]
  end
  self
end

#tail(images_base64_fb2) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/sisu/xml_fictionbook2.rb', line 278

def tail(images_base64_fb2)
  <<-WOK
</body>
#{images_base64_fb2}
</FictionBook>
  WOK
end