Class: SiSU_XHTML_EPUB2_Tune::Tune

Inherits:
Object
  • Object
show all
Includes:
SiSU_Parts_XHTML
Defined in:
lib/sisu/xhtml_epub2_tune.rb

Instance Method Summary collapse

Methods included from SiSU_Parts_XHTML

#css, #home, #num_css, #paragraph_font_small, #paragraph_font_tiny, #set_fonts, #set_small, #set_tiny, #site, #the_font, #the_line_break, #the_margin, #the_nav, #the_table_close, #the_url, #the_url_decoration, #txt_0, #txt_1, #txt_2, #txt_3, #txt_close, #txt_concordance, #txt_manifest, #txt_open, #txt_toc_link, #xml_close, #xml_open

Methods included from SiSU_Parts_Generic

#footer_signature, #home, #home_txt, #i_choice, #i_home_button, #i_ico, #i_new, #rl_root, #root_http, #sisu, #sisu_txt, #sisudoc, #site, #the_icon, #the_text, #the_url, #txt_home, #txt_hp, #txt_hp_alias, #txt_signature, #urify, #url_close, #url_open

Constructor Details

#initialize(data, md) ⇒ Tune

Returns a new instance of Tune.



109
110
111
112
113
# File 'lib/sisu/xhtml_epub2_tune.rb', line 109

def initialize(data,md)
  @data,@md=data,md
  @sys=SiSU_Env::SystemCall.new
  @env=SiSU_Env::InfoEnv.new(@md.fns)
end

Instance Method Details

#amp_angle_bracketsObject



277
278
279
280
281
282
283
284
285
286
# File 'lib/sisu/xhtml_epub2_tune.rb', line 277

def amp_angle_brackets
  data,data_new=@data,[]
  data.each do |dob|
    dob.obj=dob.obj.
      gsub(/&/u,'&').
      gsub(/</u,'&lt;').gsub(/>/u,'&gt;')
    data_new << dob
  end
  data_new
end

#endnotes_htmlObject



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/sisu/xhtml_epub2_tune.rb', line 287

def endnotes_html
  data=@data
  @tuned_file=[]
  a,s='_a','_s'
  ast,pls='&#042;','&#043;'
  data.each do |dob|
    unless dob.is ==:code
      dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]}|#{Mx[:en_b_o]})(\d+)\s+(.+?)(#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/,
          %{#{Mx[:nbsp]}<a href="##{Mx[:note]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}\\2">\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
          %{\\1\\2 <a href="##{Mx[:note_ref]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note]}\\2">\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
        gsub(/(#{Mx[:en_b_o]})[*](\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
          %{#{Mx[:nbsp]}<a href="##{Mx[:note_astx]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref_astx]}\\2">#{ast}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
          %{\\1#{ast}\\2 <a href="##{Mx[:note_ref_astx]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_astx]}\\2">#{ast}\\2.</sup></a> \\3 \\4}). #endnote- note- (careful may have switched)
        gsub(/(#{Mx[:en_b_o]})[+](\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
          %{#{Mx[:nbsp]}<a href="##{Mx[:note_plus]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref_plus]}\\2">#{pls}\\2</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
          %{\\1#{pls}\\2 <a href="##{Mx[:note_ref_plus]}\\2">#{Mx[:nbsp]}<sup id="#{Mx[:note_plus]}\\2">#{pls}\\2.</sup></a> \\3 \\4}) #endnote- note- (careful may have switched) # double-check there may here be a bug
      if dob.obj =~/#{Mx[:en_a_o]}([*+]+)\s+.+?#{Mx[:en_a_c]}/
        m=$1.length.to_i
        dob.obj=dob.obj.gsub(/(#{Mx[:en_a_o]})[*]+\s+(.+?)(#{Mx[:en_a_c]})/,
            %{#{Mx[:nbsp]}<a href="##{Mx[:note]}#{a*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}#{a*m}">#{ast*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
            %{\\1#{ast*m} <a href="##{Mx[:note_ref]}#{a*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note]}#{a*m}">#{ast*m}</sup></a> \\2 \\3}). #endnote- note- (careful may have switched)
          gsub(/(#{Mx[:en_a_o]})([+]+)\s+(.+?)(#{Mx[:en_a_c]})/,
            %{#{Mx[:nbsp]}<a href="##{Mx[:note]}#{s*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note_ref]}#{s*m}">#{pls*m}</sup>#{Mx[:nbsp]}</a> } +  #note- endnote-
            %{\\1#{pls*m} <a href="##{Mx[:note_ref]}#{s*m}">#{Mx[:nbsp]}<sup id="#{Mx[:note]}#{s*m}">#{pls*m}</sup></a> \\2 \\3}) #endnote- note- (careful may have switched)
      end
    end
    @tuned_file << dob
  end
end

#markupObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sisu/xhtml_epub2_tune.rb', line 141

def markup
  @tuned_file=[]
  @data.each do |dob|
    dob.obj=dob.obj.gsub(/#{Mx[:mk_o]}#([a-zA-Z]+)#{Mx[:mk_c]}/,'&\1;').
      gsub(/#{Mx[:mk_o]}(#[0-9]+)#{Mx[:mk_c]}/,'&\1;')
    dob.obj=dob.obj.gsub(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,'<br />') unless dob.is==:table
    dob.obj=dob.obj.gsub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,'<b>\1</b>').
      gsub(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'<i>\1</i>').
      gsub(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'<u>\1</u>').
      gsub(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'<sup>\1</sup>').
      gsub(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'<sub>\1</sub>').
      gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'<ins>\1</ins>').
      gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'<cite>\1</cite>').
      gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'<del>\1</del>').
      gsub(/#{Mx[:fa_monospace_o]}(.+?)#{Mx[:fa_monospace_c]}/,'<tt>\1</tt>'). # tt, kbd
      gsub(/#{Mx[:mk_o]}:name#(\S+?)#{Mx[:mk_c]}/,'').
      gsub(/#{Mx[:gl_bullet]}/m,"#{$ep[:hsp]*2}").
      gsub(/#{Dx[:url_o]}/,Dx[:url_o_xml]).gsub(/#{Dx[:url_c]}/,Dx[:url_c_xml]).
      gsub(/#{Mx[:nbsp]}/,$ep[:hsp]).
      gsub(/<(p|br)>/,'<\1 />')
    dob.obj=SiSU_XHTML_EPUB2_Tune::CleanXHTML.new(dob.obj).clean
    @tuned_file << dob
  end
end

#outputObject



316
317
318
319
320
321
322
323
324
325
# File 'lib/sisu/xhtml_epub2_tune.rb', line 316

def output
  data=@data
  @tuned_file=[]
  data.each do |dob|
    dob.obj=dob.obj.strip.chomp
    @tuned_file << dob
  end
  @tuned_file << "\n<EOF>" if (@md.fns =~/\.sst0/) #remove
  @tuned_file
end

#songsheetObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sisu/xhtml_epub2_tune.rb', line 114

def songsheet
  begin
    @cX=SiSU_Screen::Ansi.new(@md.opt.act[:color_state][:set]).cX
    if (@md.opt.act[:verbose][:set]==:on \
    || @md.opt.act[:verbose_plus][:set]==:on \
    || @md.opt.act[:maintenance][:set]==:on)
      SiSU_Screen::Ansi.new(
        @md.opt.act[:color_state][:set],
        'Tune'
      ).txt_grey
    end
    data=SiSU_XHTML_EPUB2_Tune::Tune.new(@data,@md).amp_angle_brackets
    data=SiSU_XHTML_EPUB2_Tune::Tune.new(data,@md).endnotes_html
    data=SiSU_XHTML_EPUB2_Tune::Tune.new(data,@md).url_markup
    data=SiSU_XHTML_EPUB2_Tune::Tune.new(data,@md).markup
    if @md.opt.act[:maintenance][:set]==:on #Hard Output Tune Optional on/off here
      data=SiSU_XHTML_EPUB2_Tune::Output.new(data,@md).hard_output
      SiSU_XHTML_EPUB2_Tune::Output.new(data,@md).marshal
    end
    SiSU_XHTML_EPUB2_Tune::Tune.new(@data,@md).output
  rescue
    SiSU_Errors::Rescued.new($!,$@,@md.opt.selections.str,@md.fns).location do
      __LINE__.to_s + ':' + __FILE__
    end
  ensure
  end
end

#url_markupObject



227
228
229
230
231
232
233
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
270
271
272
273
274
275
276
# File 'lib/sisu/xhtml_epub2_tune.rb', line 227

def url_markup
  data=@data
  @tuned_file=[]
  data.each do |dob|
    unless dob.is==:code
      if dob.obj =~/<::\s+/ #watch
        dob.obj=dob.obj.gsub(/<::\s+(\S+?)\s+!>/,
          %{<img src="#{@env.url.images_epub}/c_\\1.png" alt="\\1" width="14" height="14" align="bottom" border="0" />})
      end
      if dob.obj =~/<:image\s+/
        dob.obj=dob.obj.gsub(/<:image\s+(http\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+>/,
            %{<a href="\\1"><img src="#{@env.url.images_epub}/\\2" \\3 \\4 naturalsizeflag="0" align="bottom" border="0" /></a>}).
          gsub(/<:image\s+(http\S+)\s+(\S+)\s+>/,
            %{<a href="\\1"><img src="#{@env.url.images_epub}/\\2" naturalsizeflag="0" align="bottom" border="0" /></a>}).
          gsub(/<:image\s+(\S+)\s+(\S+)\s+(\S+)\s+>/,
            %{<img src="#{@env.url.images_epub}/\\1" \\2 \\3 naturalsizeflag="0" align="bottom" border="0" />}).
          gsub(/<:image\s+(\S+)\s+>/,
            %{<img src="#{@env.url.images_epub}/\\1" naturalsizeflag="0" align="bottom" border="0" />})
      end
      if dob.obj =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
        @word_mode=dob.obj.scan(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)[()\[\]]*[,.;:!?'"]{0,2}|(?:#{Mx[:gl_o]}\S+?#{Mx[:gl_c]})+|[^#{Mx[:lnk_o]}#{Mx[:lnk_c]}]+/mu)
        words=urls(@word_mode)
        dob.obj=dob.obj.gsub(/.+/m,words)
      end
      dob.obj=dob.obj.gsub(/\\copyright/i,%{<sup>&copy;</sup>})
      dob.obj=if (dob.obj !~/\<:ad\s+\.\.\//)
        dob.obj.gsub(/\<:ad\s+(\S+)?\s+(\S+\.png)\s+(.+)?\;\s+(.+)?\;\s*!\>/,
          %{\n<center><a href="http:\/\/\\1" target="external"><img src="#{@env.url.images_epub}/\\2" alt="\\3" /></a></center>\n})
      else
        dob.obj.gsub(/\<:ad\s+(\S+)?\s+(\S+\.png)\s+(.+)?\;\s+(.+)?\;\s*\>/,
          %{\n<center><a href="\\1" target="_top"><img src="#{@env.url.images_epub}/\\2" alt="\\3" /></a></center>\n})
      end
      dob.obj=dob.obj.gsub(/!pick/,%{<img border="0" height="15" width="15" src="#{@env.url.images_epub}/#{the_icon.i_choice}" alt="stellar" />}).
        gsub(/!new/,%{#{$ep[:hsp]}<img border="0" height="15" width="15" src="#{@env.url.images_epub}/#{the_icon.i_new}" alt="new" />}).
        gsub(/<:h(.{1,7}?)>/,'<a href="#h\1">\1</a>').
        gsub(/<:to(\d{1,7}?)>/,%{<a href="#to\\1">to#{$ep[:hsp]}\{#{$ep[:hsp]}\\1#{$ep[:hsp]}\}</a> }).
        gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/,'<a href="\1" target="_top">\1</a>'). #http ftp matches escaped, no decoration
        gsub(/#{Mx[:url_o]}([a-zA-Z0-9._-]+\@\S+?\.[a-zA-Z0-9._-]+)#{Mx[:url_c]}/,%{#{the_url_decoration.xml_open}<a href="mailto:\\1">\\1</a>#{the_url_decoration.xml_close}}).
        gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,%{#{the_url_decoration.xml_open}<a href="\\1" target="_top">\\1</a>#{the_url_decoration.xml_close}}) #http ftp matches with decoration
      if dob.obj =~/..\/\S+/ \
      and dob.obj !~/(\"..\/\S+?\"|>\s*..\/\S+<)/
        dob.obj=dob.obj.gsub(/(\.\.\/\S+)/,'<a href="\1">\1</a>')
      end
      dob.obj=dob.obj.gsub(/<a href="\.\.\//,%{<a href="#{the_url.site}/})
    else
      dob.obj=dob.obj.gsub(/</m,'&lt;').gsub(/>/m,'&gt;')
    end
    @tuned_file << dob
  end
end

#urls(data) ⇒ Object



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
217
218
219
220
221
222
223
224
225
226
# File 'lib/sisu/xhtml_epub2_tune.rb', line 165

def urls(data)
  @words=[]
  map_nametags=SiSU_Particulars::CombinedSingleton.instance.get_map_nametags(@md).nametags_map #p map_nametags
  data.each do |word|
    @words << if word=~/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}|image)/
      http_=true
      if word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/
        m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/.match(word).captures
      elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/
        http_=false
        m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]}/.match(word).captures
      elsif word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}image/
        m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}(image)/.match(word).captures
      end
      case m
      when /\.png|\.jpg|\.gif|c=|\s\d+x\d+/
        w,h=/\s(\d+)x(\d+)/.match(m).captures if m =~/\s\d+x\d+/
        w=%{width="#{w}"} if w
        h=%{height="#{h}"} if h
        c=m[/"(.+?)"/m,1]
        caption=%{<br /><p class="caption">#{c}</p>} if c
        png=m.scan(/\S+/)[0]
        image_path=@md.file.output_path.epub.rel_image #[email protected]_epub
        ins=if u \
        and u.strip !~/^image$/
          %{<a href="#{u}"><img src="#{image_path}/#{png}" #{w} #{h} naturalsizeflag="0" align="bottom" border="0" /></a>#{caption}}
        else %{<img src="#{image_path}/#{png}" #{w} #{h} naturalsizeflag="0" align="bottom" border="0" />#{caption}}
        end
        word=word.gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:#{Mx[:url_o]}\S+?#{Mx[:url_c]}|image)/,ins)
      else
        link=m[/(.+)/m]
        png=m.scan(/\S+/)[0].strip
        link=link.strip
        u=u.sub(/^#*/,'') #make neater
        if map_nametags[u] \
        and map_nametags[u][:segname]
          u=unless http_
            u=if u=~/^\d+$/
              u.gsub(/^(\d+)$/,"#{map_nametags[u][:segname]}#{Sfx[:xhtml]}#o\\1") if u !~/\//
            else
              u.gsub(/(\S+)/,"#{map_nametags[u][:segname]}#{Sfx[:xhtml]}#\\1") if u !~/\//
            end
          else u
          end
        elsif u =~/^:/
          u=u.gsub(/^:/,"#{@env.url.root}/")
        elsif u =~/^\.\.\//
          u=u.gsub(/^\.\.\//,"#{@env.url.root}/")
        elsif u =~/https?:\/\//
        else p "NOT FOUND name_tags: #{u}"
        end
        ins=%{<a href="#{u}">#{link}</a>}
        word=word.gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/,ins).
          gsub(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}\S+?#{Mx[:rel_c]}/,ins)
      end
      word
    else word
    end
    word
  end
  @words=@words.join(' ')
end