# encoding: utf-8 =begin * Name: SiSU * Description: a framework for document structuring, publishing and search * Author: Ralph Amissah * Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 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 * Hompages: * Download: * Ralph Amissah ** Description: epub formating, css template =end module SiSU_EPUB_Format include SiSU_Viz class ParagraphNumber def initialize(md,ocn) @md,@ocn=md,ocn.to_s @ocn ||='' vz=SiSU_Env::GetInit.instance.skin end def ocn_display @make=SiSU_Env::ProcessingSettings.new(@md) if @make.build.ocn? ocn_class='ocn' if @ocn.to_i==0 @ocn.gsub(/^(\d+|)$/, %{}) else @ocn.gsub(/^(\d+|)$/, %{}) end else ocn_class='ocn_off' @ocn.gsub(/^(\d+|)$/, %{}) end end def name %{} end def id #w3c? "tidy" complains about numbers as identifiers ! annoying %{id="o#{@ocn}"} end def goto %{} end end class CSS def css_epub_xhtml < WOK end =begin ~/epub |-- META-INF | `-- container.xml #✓ simple, make sure full-path of rootfile points to metadata.opf |-- content | |-- 1.xhtml | |-- 2.xhtml | |-- 3.xhtml | |-- ... .xhtml | |-- concordance.xhtml | |-- css | | `-- xhtml.css | |-- endnotes.xhtml | |-- image | | |-- arrow_next_red.png | | |-- arrow_prev_red.png | | |-- arrow_up_red.png | | `-- bullet_09.png | |-- index.xhtml | |-- meta.xhtml | |-- metadata.xhtml | `-- toc.xhtml |-- metadata.opf #(i) metadata dc; (ii) manifest (contents); (iii) spine (mimetypes) |-- mimetype #✓ application/epub+zip `-- toc.ncx #✓ (i) head (ii) doc title (iii) navmap, list of navigation points (like chapters) =end def doc_type doc_type_xhtml end def mimetype < WOK end def sections(dob,name) filename="#{name}#{Sfx[:epub_xhtml]}" dir_epub_cont="#{@md.env.processing_path.epub}/#{Ep[:d_oebps]}" segfilename="#{dir_epub_cont}/#{filename}" output_epub_cont_seg=File.new(segfilename,'w') output_epub_cont_seg << %{#{doc_type} #{dob.obj} - #{@md.html_title} #{@css.xhtml_epub} #{@vz.color_body}
8

#{dob.obj}

} output_epub_cont_seg.close end def toc_ncx #list of navigation points (like chapters), table of contents, listing each navigation point (chapters and such) under the navigation map def structure open head_open head head_close doc_title doc_author navmap_open #navmap ... navmap_close close end def open < WOK end def close < WOK end def head_open < WOK end def head depth=@md.lvs[1] + @md.lvs[2] + @md.lvs[3] + @md.lvs[4] < #{@md.title.full} by #{@md.author} WOK end def head_close < WOK end def doc_title < #{@md.title.full} WOK end def doc_author < #{@md.author} WOK end def navmap_open < WOK end def navmap_sisu_toc(no) id_u=DISABLE[:epub][:ncx_navpoint_unique_id] \ ? '' : "-#{no}" < Table of Contents WOK end def navpoint(dob,no,name=nil) name=name ? name : dob.name id_u=DISABLE[:epub][:ncx_navpoint_unique_id] \ ? '' : "-#{no}" < #{dob.obj} WOK end def navpoint_close < WOK end def navmap_close < WOK end self end def metadata_opf #(i) metadata dc; (ii) manifest (contents); (iii) spine (mimetypes) def structure package_open metadata_open metadata_close manifest_open manifest_close spine_open spine_close guide_open guide_close package_close end def package_open < WOK end def package_close < WOK end def metadata #metadata dc author=if defined? @md.creator.author \ and @md.creator.author =~/\S+/ m='' @md.creator.author_detail.each do |x| surname=x[:the] \ ? x[:the] : '' other_names=x[:others] \ ? ', ' + x[:others] : '' m=(m.empty?) \ ? (surname + other_names) : (m + '; ' + surname + ', ' + other_names) m=m.gsub(//,'>'). gsub(/<br(?: \/)?>/,';') end x=@md.creator.author.dup x=x.gsub(//,'>'). gsub(/<br(?: \/)?>/,'
') %{\n #{x}} else '' end editor=if defined? @md.creator.editor \ and @md.creator.editor =~/\S+/ m='' @md.creator.editor_detail.each do |x| surname=x[:the] \ ? x[:the] : '' other_names=x[:others] \ ? ', ' + x[:others] : '' m=(m.empty?) \ ? (surname + other_names) : (m + '; ' + surname + ', ' + other_names) m=m.gsub(//,'>'). gsub(/<br(?: \/)?>/,';') end x=@md.creator.editor.dup x=x.gsub(//,'>'). gsub(/<br(?: \/)?>/,'
') %{\n #{x}} else '' end translator=if defined? @md.creator.translator \ and @md.creator.translator =~/\S+/ m='' @md.creator.translator_detail.each do |x| surname=x[:the] \ ? x[:the] : '' other_names=x[:others] \ ? ', ' + x[:others] : '' m=(m.empty?) \ ? (surname + other_names) : (m + '; ' + surname + ', ' + other_names) m=m.gsub(//,'>'). gsub(/<br(?: \/)?>/,';') end x=@md.creator.translator.dup x=x.gsub(//,'>'). gsub(/<br(?: \/)?>/,'
') %{\n #{x}} else '' end illustrator=if defined? @md.creator.illustrator \ and @md.creator.illustrator =~/\S+/ m='' @md.creator.illustrator_detail.each do |x| surname=x[:the] \ ? x[:the] : '' other_names=x[:others] \ ? ', ' + x[:others] : '' m=(m.empty?) \ ? (surname + other_names) : (m + '; ' + surname + ', ' + other_names) m=m.gsub(//,'>'). gsub(/<br(?: \/)?>/,';') end x=@md.creator.illustrator.dup x=x.gsub(//,'>'). gsub(/<br(?: \/)?>/,'
') %{\n #{x}} else '' end date_published=if defined? @md.date.published \ and @md.date.published =~/\S+/ x=@md.date.published.dup x=x.gsub(//,'>'). gsub(/<br(?: \/)?>/,'
') %{\n #{x}} else '' end subject=if defined? @md.classify.subject \ and @md.classify.subject =~/\S+/ x=@md.classify.subject.dup x=x.gsub(//,'>'). gsub(/<br(?: \/)?>/,'
') %{\n #{x}} else '' end language=if defined? @md.opt.lng \ and @md.opt.lng =~/\S+/ language=@md.opt.lng.gsub(/
/,'
') %{\n #{language}} else '' end rights=if defined? @md.rights.all \ and @md.rights.all =~/\S+/ rights=@md.rights.all.gsub(/
/,'
') %{\n #{rights}} else '' end f=SiSU_Env::FileOp.new(@md) < #{@md.title.full} #{author}#{editor}#{translator}#{illustrator}#{language}#{date_published}#{subject}#{rights} #{f.output_path.epub.url}/#{f.base_filename.epub} urn:uuid:#{@md.dgst[1]} WOK end def manifest_open < WOK end def manifest_content_sisu_toc < WOK end def manifest_content(dob,name=nil) name=name ? name : dob.name < WOK end def manifest_images(imgs) imgs=imgs + ['arrow_next_red.png','arrow_prev_red.png','arrow_up_red.png','bullet_09.png'] images=[" \n"] imgs.each do |i| image,type=/(\S+?)\.(png|jpg|gif)/.match(i)[1,2] images<<< WOK end images=images.join('') images end def manifest_close < WOK end def spine_open #spine: reading order of XHTML files from manifest, idref attribute refers back to id in manifest (exclude images, CSS etc.). < WOK end def spine_sisu_toc < WOK end def spine(dob,name=nil) name=name ? name : dob.name < WOK end def spine_close < WOK end def guide_open #guide: presentation order of XHTML files by reader). < WOK end def guide_sisu_toc < WOK end def guide(dob,name=nil) name=name ? name : dob.name < WOK end def guide_close < WOK end self end def toc_head_escript end def seg_head_escript end def table_close %{ #{@vz.table_close}} end def buttons_home %{ #{@vz.banner_home_and_index_buttons} } end def copyat %{

copy @ #{@vz.txt_home}

} end def xhtml_close %{#{@vz.credits_sisu_epub} } end end class HeadToc < HeadInformation def initialize(md) super(md) @md=md @tocband_segtoc=make_seg end def seg_head_navigation_band firstseg=%{ #{@vz.epub_png_nav_nxt} } if @md.firstseg =~/\S+/ DISABLE[:epub][:internal_navigation] \ ? '' : %{

#{firstseg}

} end def seg_head_navigation_band_bottom firstseg=%{ #{@vz.epub_png_nav_nxt} } if @md.firstseg =~/\S+/ DISABLE[:epub][:internal_navigation] \ ? '' : %{

#{firstseg}

} end def manifest_link(text) %{ #{text}} end def concordance_link(text) if @md.concord_make %{ #{text} } else '' end end def head %{#{doc_type} #{@css.xhtml_epub} #{@vz.color_body} #{@vz.js_top}} end def concordance if @md.concord_make %{#{@vz.margin_css}

Concordance

#{@vz.table_close}} else %{#{@vz.margin_css} #{@vz.table_close}} end end def links_guide_open(type='horizontal') if type=='vertical'; links_guide_vertical_open else links_guide_horizontal_open end end def prefix_a end def rights def all rghts=@md.rights.all.gsub(/
/,'
') rghts=rghts.gsub(/^\s*Copyright\s+\(C\)/,'Copyright © ') %{

Rights: #{rghts}

} end self end def prefix_b %{

Prefix: #{@md.prefix_b}} end def make_seg concord=concordance_link(@vz.nav_txt_concordance) %{
#{@vz.nav_txt_toc_link} #{concord} #{@vz.table_close}} end def manifest #check structure manifest=manifest_link(@vz.nav_txt_manifest) %{#{@vz.margin_txt_3} #{@vz.paragraph_font_small} #{manifest} #{@vz.table_close}} end def concordance #check structure concord=concordance_link(@vz.nav_txt_concordance) %{#{@vz.margin_txt_3} #{@vz.paragraph_font_small} #{concord} #{@vz.table_close}} end def metadata %{#{@vz.margin_css}

MetaData

#{@vz.table_close}} end end class HeadSeg < HeadInformation def initialize(md) super(md) end def head %{#{doc_type} #{@seg_name_xhtml[@seg_name_xhtml_tracker]} - #{@md.html_title} #{@css.xhtml_epub} #{@vz.color_body}} end def endnote_mark %{
} end end class HeadScroll < HeadToc def initialize(md) super(md) end def toc_owner_details %{#{@vz.margin_txt_3} #{@vz.paragraph_font_small} Owner Details     #{@vz.table_close}} end end class FormatTextObject @vz=SiSU_Env::GetInit.instance.skin attr_accessor :md,:t_o,:txt,:ocn,:format,:table,:link,:linkname,:paranum,:p_num,:headname,:banner,:url def initialize(md,t_o) @md,@t_o=md,t_o if t_o.class==Hash @txt =t_o[:txt] || nil @ocn =t_o[:ocn] || nil @ocn_display =t_o[:ocn_display] || nil @headname =t_o[:headname] || nil @trailer =t_o[:trailer] || nil @endnote_part_a =t_o[:endnote_part_a] || nil @endnote_part_b =t_o[:endnote_part_b] || nil @lnk_url =t_o[:lnk_url] || nil @lnk_txt =t_o[:lnk_txt] || nil @format =t_o[:format] || nil @target =t_o[:target] || nil #occasionally passed but not used if @format and not @format.empty? if @format=~/^\d:(\S+)/ #need more reliable marker #if @format =~ /#{Rx[:lv]}/ headname=$1 #format[/\d~(\S+)/m,1] @headname=if headname =~/^[a-zA-Z]/; %{} #consider: h_#{headname} else %{} end @headname=if headname =~/^[a-zA-Z]/; %{} #consider: h_#{headname} else %{} end end end elsif t_o.class.inspect =~/Object/ @dob=t_o if defined? t_o.is @named=nametags_seg(@dob) @txt=((defined? t_o.obj) ? t_o.obj : nil) @ocn=((defined? t_o.ocn) ? t_o.ocn.to_s : nil) @headname=((t_o.is==:heading and defined? t_o.name) ? t_o.name : nil) else if @md.opt.cmd =~/M/ p __FILE__ +':'+ __LINE__.to_s p t_o.class p caller end end if @txt and not @txt.empty? @txt=@txt.gsub(/#{Mx[:mk_o]}[-~]##{Mx[:mk_c]}/,'') end @p_num=ParagraphNumber.new(@md,@ocn) @vz=SiSU_Env::GetInit.instance.skin end def nametags_seg(dob) #FIX tags='' if defined? dob.tags \ and dob.tags.length > 0 # insert tags "hypertargets" dob.tags.each do |t| tags=tags +%{} end end tags end def endnote_body %{

#{@txt}

} end def endnote_body_indent %{

#{@txt}

} end def no_paranum %{

#{@txt}

} end def para_form_css(tag,attrib,txt) # regular paragraphs shaped here ul=ulc='' ul,ulc="
    \n ","\n
" if @tag =~/li/ %{
#{@p_num.ocn_display} #{ul}<#{tag} class="#{attrib}" #{@p_num.id}> #{@named}#{txt} #{ulc}
} end def para para_form_css('p','norm',@txt) end def group para_form_css('p','group',@txt) end def block para_form_css('p','block',@txt) end def alt para_form_css('p','alt',@txt) end def verse para_form_css('p','verse',@txt) end def code para_form_css('p','code',@txt) end def center para_form_css('p','center',@txt) end def bold para_form_css('p','bold',@txt) end def bullet para_form_css('li','bullet',@txt) end def table @txt=if @t_o.obj !~/^
'). gsub(/#{Mx[:br_obj]}/,'

') para_form_css('p','norm',@txt) end def format(tag,attrib) para_form_css(tag,attrib,@txt) end def title_heading(tag,attrib) %{
<#{tag} class="#{attrib}"> #{@named}#{@txt}
} end def title_heading1 id_u=DISABLE[:epub][:per_section_title] \ ? '' : title_heading('h1','tiny') end def title_heading2 id_u=DISABLE[:epub][:per_section_title] \ ? '' : title_heading('h2','tiny') end def title_heading3 id_u=DISABLE[:epub][:per_section_title] \ ? '' : title_heading('h3','tiny') end def title_heading4 '' end def seg_heading_sub(tag,attrib,txt) txt=txt.gsub(/(?:#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})\s*/m,' ') %{
#{@p_num.ocn_display} <#{tag} class="#{attrib}" #{@p_num.id}>#{@p_num.name} #{@named}#{@txt}
} end def seg_heading4 %{
#{@p_num.ocn_display}

#{@txt}

} end def seg_heading5 seg_heading_sub('p','bold',@txt) end def seg_heading6 seg_heading_sub('p','bold',@txt) end def dl #check :trailer "
#{@txt} #{@trailer}
" end def table_css_end '

' end def gsub_body #unused @txt=case @txt when /^(?:#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]}\s*)?\((i+|iv|v|vi+|ix|x|xi+)\)/ @txt.gsub(/^\((i+|iv|v|vi+|ix|x|xi+)\)/,'(\1)'). gsub(/^(#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]})\s*\((i+|iv|v|vi+|ix|x|xi+)\)/,'\1(\2)') when /^(?:#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]}\s*)?\(?(\d|[a-z])+\)/ @txt.gsub(/^\((\d+|[a-z])+\)/,'(\1)'). gsub(/^(#{Mx[:pa_o]}i[1-9]#{Mx[:pa_c]})\s*\((\d+|[a-z])+\)/,'\1(\2)') when /^\s*\d{1,3}\.\s/ @txt.gsub(/^\s*(\d+\.)/,'\1') when /^\s*[A-Z]\.\s/ @txt.gsub(/^\s*([A-Z]\.)/,'\1') else @txt end end def bold_para %{#{@vz.margin_txt_0}

#{@txt}

#{@vz.margin_num_css}     #{@vz.table_close}} end def bold_heading #unused @txt=@txt.gsub(/[1-9]~\S+/,''). gsub(/[1-9]~/,'') %{

#{@txt}

#{@vz.margin_num_css}     #{@vz.table_close}} end def toc_head_copy_at %{

#{@txt}

\n} end def center %{

#{@txt}

\n} end def bold %{

#{@txt}

\n} end def center_bold %{

#{@txt}

\n} end end class FormatScroll < FormatTextObject def initialize(md,txt) super(md,txt) @vz=SiSU_Env::GetInit.instance.skin end end class FormatSeg < FormatTextObject def initialize(md,txt) super(md,txt) end def endnote_seg_body(fn='') #FIX #url construction keep within single line... BUG WATCH 200408 fn='doc' if fn.to_s.empty? #you may wish to reconsider, sends to 'doc' where no segment info %{

#{@endnote_part_a}#{fn}#{Sfx[:epub_xhtml]}#{@endnote_part_b}

} end def clean(txt) txt=txt.gsub(/#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}/,''). gsub(/#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]}/,'') end def subtoc_lev(tag,attrib) @txt=clean(@txt) txt=if @txt \ and @txt =~/<\/?i>|/mi @txt.gsub(/<\/?i>|/mi,'') #removes name markers from subtoc, go directly to substantive text else @txt end note='' if txt =~/(#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})/m # had \s* at end note=$1 note=note.gsub(/[\n\s]+/m,' ') txt=txt.gsub(/(?:#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})\s*/m,' '). gsub(/ \d+<\/sup> /m,''). gsub(/#{Mx[:nbsp]}\d+<\/sup>#{Mx[:nbsp]}/m,'') #remove end %{<#{tag} class="#{attrib}"> #{txt} #{note} } end def subtoc_lev5 subtoc_lev('h5','subtoc') if @txt end def subtoc_lev6 subtoc_lev('h6','subtoc') if @txt end def heading_sub(tag,attrib,txt) txt=txt.gsub(/(?:#{Mx[:en_a_o]}.+?#{Mx[:en_a_c]}|#{Mx[:en_b_o]}.+?#{Mx[:en_b_c]})\s*/m,' ') %{
#{@p_num.ocn_display} <#{tag} class="#{attrib}" #{@p_num.id}> #{@headname} #{@txt}
} end def heading4 %{
#{@p_num.ocn_display}

#{@t_o[:format]} #{@txt}

} end def heading5 heading_sub('p','bold',@txt) end def heading6 heading_sub('p','bold',@txt) end def navigation_heading4 %{

#{@txt}

#{@vz.table_close}} end def navigation_heading5 %{

#{@txt}

} end def navigation_heading6 %{

#{@txt}

} end def navigation_center %{

#{@txt}

} end end class FormatToc < FormatTextObject def initialize(md,txt) super(md,txt) end def links_guide %{
  • #{@lnk_txt}
  • } end def lev(tag,attrib) if @txt %{<#{tag} class="#{attrib}"> #{@txt} } else '' end end def lev1 lev('h1','toc') end def lev2 lev('h2','toc') end def lev3 lev('h3','toc') end def lev4 lev('h4','toc') end def lev5 lev('h5','toc') end def lev6 lev('h6','toc') end def lev0 #docinfo lev('h0','toc') end end end __END__