#encoding: utf-8
#- Name: SiSU
#
# - Description: documents, structuring, processing, publishing, search
# xml
#
# - Author: Ralph Amissah
#
#
# - Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
# 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019,
# 2020, 2021, Ralph Amissah,
# All Rights Reserved.
#
# - License: GPL 3 or later:
#
# SiSU, a framework for document structuring, publishing and search
#
# Copyright (C) Ralph Amissah
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see .
#
# If you have Internet connection, the latest version of the GPL should be
# available at these locations:
#
#
#
#
#
# - SiSU uses:
# - Standard SiSU markup syntax,
# - Standard SiSU meta-markup syntax, and the
# - Standard SiSU object citation numbering and system
#
# - Homepages:
#
#
# - Git
#
#
#
module SiSU_XML_Fictionbook
require_relative 'se_hub_particulars' # se_hub_particulars.rb
include SiSU_Particulars
require_relative 'ao' # ao.rb
require_relative 'se' # se.rb
include SiSU_Env
require_relative 'txt_shared' # txt_shared.rb
include SiSU_TextUtils
require_relative 'xml_shared' # xml_shared.rb
include SiSU_XML_Munge
class Source
def initialize(opt)
@opt=opt
@particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(opt)
end
def read
begin
@md,@ao_array=@particulars.md,@particulars.ao_array
@env=@particulars.env
report
SiSU_XML_Fictionbook::Source::Scroll.new(@ao_array,@md).songsheet
rescue
SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).location do
__LINE__.to_s + ':' + __FILE__
end
ensure
Dir.chdir(@opt.f_pth[:pth])
end
end
private
def report
unless @opt.act[:quiet][:set]==:on
tool=(@opt.act[:verbose][:set]==:on \
|| @opt.act[:verbose_plus][:set]==:on \
|| @opt.act[:maintenance][:set]==:on) \
? "#{@env.program.fictionbook_viewer} #{@md.file.output_path.xml_fictionbook.dir}/#{@md.file.base_filename.xml_fictionbook}"
: "[#{@opt.f_pth[:lng_is]}] #{@opt.fno}"
(@opt.act[:verbose][:set]==:on \
|| @opt.act[:verbose_plus][:set]==:on \
|| @opt.act[:maintenance][:set]==:on) \
? SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
'Fictionbook',
tool
).green_hi_blue
: SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
'Fictionbook',
tool
).green_title_hi
if (@opt.act[:verbose_plus][:set]==:on \
|| @opt.act[:maintenance][:set]==:on)
SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
@opt.fns,
"#{@md.file.output_path.xml_fictionbook.dir}/#{@md.file.base_filename.xml_fictionbook}"
).flow
end
end
end
class Scroll
def collapsed
%w[ 0 1 2 3 4 5 ]
end
def fictionbook
[
'section',
'section',
'section',
'section',
'section',
'section',
'section'
]
end
self
end
def put(line)
puts line if @md.opt.act[:verbose_plus][:set]==:on
end
def head
version=SiSU_Env::InfoVersion.instance.get_version
rb_ver=SiSU_Env::InfoVersion.instance.rbversion
date_available=if defined? @md.date.available; "\n
#{@md.date.available} Initial version
"
else ''
end
date_modified=if defined? @md.date.modified; "\n
#{@md.date.modified} Last Modified
"
else ''
end
coverpageimage=if defined? @md.make.cover_image[:cover]
%{\n }
else ''
end
if defined? @md.authors \
and @md.authors.length > 0
authors=[]
@md.authors.each do |author|
authors << ' '
if not author[:others].empty?
authors << %{ #{author[:others]}}
end
if not author[:the].empty?
authors << %{ #{author[:the]}}
end
authors << ' '
end
authors=authors.join("\n")
end
<<-WOK
***
#{authors}
#{@md.title.full}#{coverpageimage}
#{@md.date.published}#{version.project} #{version.version} and #{rb_ver}#{version.date}#{@md.file.output_path.manifest.url}/#{@md.file.base_filename.manifest}1.0#{date_available}#{date_modified}
WOK
end
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,
%{\n
\\1.
\n
\\2
\n
})
end
endnotes_raw=[]
endnotes
end
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 << %{#{images_base64[k]}
}
end
images_base64_fb2.join("\n")
end
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
def tail(images_base64_fb2)
<<-WOK
#{images_base64_fb2}
WOK
end
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
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
def endnotes_build(endnotes,filename_fictionbook)
if endnotes.length > 0
filename_fictionbook.puts %{
}
endnotes.each do |en|
filename_fictionbook.puts SiSU_TextUtils::Wrap.new(en,80,6).line_wrap
end
end
end
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}
}
filename_fictionbook.puts SiSU_TextUtils::Wrap.new("
#{o.obj}#{ocn}
",80,(o.lc*2+2)).line_wrap
filename_fictionbook.puts %{#{spaces*o.lc}}
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("
#{o.obj}#{ocn}
",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
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
end
end
end
__END__