aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v3/html_tune.rb
blob: a1c9a8dcc8a9e8a1912dfc2bb53c0a6f08df664f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
217
218
219
220
221
222
223
224
225
226
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
277
278
279
280
281
282
283
284
285
286
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
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
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
# 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 <http://www.gnu.org/licenses/>.

   If you have Internet connection, the latest version of the GPL should be
   available at these locations:
   <http://www.fsf.org/licensing/licenses/gpl.html>
   <http://www.gnu.org/licenses/gpl.html>

   <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>

 * SiSU uses:
   * Standard SiSU markup syntax,
   * Standard SiSU meta-markup syntax, and the
   * Standard SiSU object citation numbering and system

 * Hompages:
   <http://www.jus.uio.no/sisu>
   <http://www.sisudoc.org>

 * Download:
   <http://www.sisudoc.org/sisu/en/SiSU/download.html>

 * Ralph Amissah
   <ralph@amissah.com>
   <ralph.amissah@gmail.com>

 ** Description: html generation, html pre-processing

=end
require_relative 'param'
module SiSU_HTML_Tune
  require_relative 'sysenv'                             # sysenv.rb
    include SiSU_Env; include SiSU_Screen
  require_relative 'html_format'                        # html_format.rb #watch
  @@line_mode=''
  @@endnote_array=[]
  @@endnote_call_counter=1
  @@table_align='<table summary='' width="96%" border="0" bgcolor="white" cellpadding="0" col="3">
<tr ...><td width="2%" align="right">
&nbsp\;</td>
<td width="94%" valign="top" align="justify">'
  @@table_align_close='</td>
<td width="4%" align="right" valign="top">
<font size="1" color="#777777">
&nbsp;&nbsp;&nbsp;</font> </td></tr></table>'
  @@counter,@@column,@columns=0,0,0
  class Output
    def initialize(data,md)
      @data,@md=data,md
      @file=SiSU_Env::InfoFile.new(@md.fns)
      @cX=SiSU_Screen::Ansi.new(@md.opt.cmd).cX
    end
    def hard_output
      @filename_tune=@file.write_file_processing.html_tune
      data=[]
      @data.each do |x|
        unless x.obj.empty?
          x.obj=x.obj.strip
          data << x
        end
      end
      data.each do |dob|
        @filename_tune.puts dob, "\n"
      end
    end
    def marshal
      File.open(@file.marshal.html_tune,'w') {|f| Marshal.dump(@data.to_a,f)}
    end
  end
  class CleanHTML
    def initialize(html='')
      @html=html
    end
    def clean
      html=@html
      str=if html.class==String
        html
      else html.obj
      end
      str=str.gsub(/#{Mx[:gl_o]}(#[0-9]{3})#{Mx[:gl_c]}/u,'&\1;').
        gsub(/#{Mx[:gl_o]}#([a-z]{2,4})#{Mx[:gl_c]}/u,'&\1;').
        gsub(/¢/u,'&cent;').    # &#162;
        gsub(/£/u,'&pound;').   # &#163;
        gsub(/¥/u,'&yen;').     # &#165;
        gsub(/§/u,'&sect;').    # &#167;
        gsub(/©/u,'&copy;').    # &#169;
        gsub(/ª/u,'&ordf;').    # &#170;
        gsub(/«/u,'&laquo;').   # &#171;
        gsub(/®/u,'&reg;').     # &#174;
        gsub(/°/u,'&deg;').     # &#176;
        gsub(/±/u,'&plusmn;').  # &#177;
        gsub(/²/u,'&sup2;').    # &#178;
        gsub(/³/u,'&sup3;').    # &#179;
        gsub(/µ/u,'&micro;').   # &#181;
        gsub(/¶/u,'&para;').    # &#182;
        gsub(/¹/u,'&sup1;').    # &#185;
        gsub(/º/u,'&ordm;').    # &#186;
        gsub(/»/u,'&raquo;').   # &#187;
        gsub(/¼/u,'&frac14;').  # &#188;
        gsub(/½/u,'&frac12;').  # &#189;
        gsub(/¾/u,'&frac34;').  # &#190;
        gsub(/×/u,'&times;').   # &#215;
        gsub(/÷/u,'&divide;').  # &#247;
        gsub(/¿/u,'&iquest;').  # &#191;
        gsub(/À/u,'&Agrave;').  # &#192;
        gsub(/Á/u,'&Aacute;').  # &#193;
        gsub(/Â/u,'&Acirc;').   # &#194;
        gsub(/Ã/u,'&Atilde;').  # &#195;
        gsub(/Ä/u,'&Auml;').    # &#196;
        gsub(/Å/u,'&Aring;').   # &#197;
        gsub(/Æ/u,'&AElig;').   # &#198;
        gsub(/Ç/u,'&Ccedil;').  # &#199;
        gsub(/È/u,'&Egrave;').  # &#200;
        gsub(/É/u,'&Eacute;').  # &#201;
        gsub(/Ê/u,'&Ecirc;').   # &#202;
        gsub(/Ë/u,'&Euml;').    # &#203;
        gsub(/Ì/u,'&Igrave;').  # &#204;
        gsub(/Í/u,'&Iacute;').  # &#205;
        gsub(/Î/u,'&Icirc;').   # &#206;
        gsub(/Ï/u,'&Iuml;').    # &#207;
        gsub(/Ð/u,'&ETH;').     # &#208;
        gsub(/Ñ/u,'&Ntilde;').  # &#209;
        gsub(/Ò/u,'&Ograve;').  # &#210;
        gsub(/Ó/u,'&Oacute;').  # &#211;
        gsub(/Ô/u,'&Ocirc;').   # &#212;
        gsub(/Õ/u,'&Otilde;').  # &#213;
        gsub(/Ö/u,'&Ouml;').    # &#214;
        gsub(/Ø/u,'&Oslash;').  # &#216;
        gsub(/Ù/u,'&Ugrave;').  # &#217;
        gsub(/Ú/u,'&Uacute;').  # &#218;
        gsub(/Û/u,'&Ucirc;').   # &#219;
        gsub(/Ü/u,'&Uuml;').    # &#220;
        gsub(/Ý/u,'&Yacute;').  # &#221;
        gsub(/Þ/u,'&THORN;').   # &#222;
        gsub(/ß/u,'&szlig;').   # &#223;
        gsub(/à/u,'&agrave;').  # &#224;
        gsub(/á/u,'&aacute;').  # &#225;
        gsub(/â/u,'&acirc;').   # &#226;
        gsub(/ã/u,'&atilde;').  # &#227;
        gsub(/ä/u,'&auml;').    # &#228;
        gsub(/å/u,'&aring;').   # &#229;
        gsub(/æ/u,'&aelig;').   # &#230;
        gsub(/ç/u,'&ccedil;').  # &#231;
        gsub(/è/u,'&egrave;').  # &#232;
        gsub(/é/u,'&eacute;').  # &#233;
        gsub(/ê/u,'&ecirc;').   # &#234;
        gsub(/ë/u,'&euml;').    # &#235;
        gsub(/ì/u,'&igrave;').  # &#236;
        gsub(/í/u,'&iacute;').  # &#237;
        gsub(/î/u,'&icirc;').   # &#238;
        gsub(/ï/u,'&iuml;').    # &#239;
        gsub(/ð/u,'&eth;').     # &#240;
        gsub(/ñ/u,'&ntilde;').  # &#241;
        gsub(/ò/u,'&ograve;').  # &#242;
        gsub(/ó/u,'&oacute;').  # &#243;
        gsub(/ô/u,'&ocirc;').   # &#244;
        gsub(/õ/u,'&otilde;').  # &#245;
        gsub(/ö/u,'&ouml;').    # &#246;
        gsub(/ø/u,'&oslash;').  # &#248;
        gsub(/ù/u,'&ugrave;').  # &#250;
        gsub(/ú/u,'&uacute;').  # &#251;
        gsub(/û/u,'&ucirc;').   # &#252;
        gsub(/ü/u,'&uuml;').    # &#253;
        gsub(/þ/u,'&thorn;').   # &#254;
        gsub(/ÿ/u,'&yuml;').    # &#255;
        gsub(/ý/u,'&yacute;')
    end
  end
  class Tune
    def initialize(data,md)
      @data,@md=data,md
      @vz=SiSU_Env::GetInit.instance.skin
      @env=SiSU_Env::InfoEnv.new(@md.fns)
      @sys=SiSU_Env::SystemCall.new
      @env=SiSU_Env::InfoEnv.new(@md.fns)
      @brace_url=SiSU_Viz::Skin.new.url_decoration
    end
    def songsheet
      begin
        @cX=SiSU_Screen::Ansi.new(@md.opt.cmd).cX
        SiSU_Screen::Ansi.new(@md.opt.cmd,'Tune').txt_grey if @md.opt.cmd =~/[MVv]/
        songsheet_array(@data)
        #data=songsheet_array(@data)
        if @md.opt.cmd =~/M/  #Hard Output Tune Optional on/off here
          data=SiSU_HTML_Tune::Output.new(@data,@md).hard_output
          SiSU_HTML_Tune::Output.new(@data,@md).marshal
        end
        tuned=SiSU_HTML_Tune::Tune.new(@data,@md).output
      rescue
        SiSU_Errors::InfoError.new($!,$@,@md.opt.cmd,@md.fns).error do
          __LINE__.to_s + ':' + __FILE__
        end
      ensure
      end
    end
    def songsheet_array(data)
      data_tuned=[]
      #@tuned_file=[]
      data.each do |dob|
        dob=angle_brackets(dob)
        dob=endnotes_html(dob)
        dob=url_markup(dob)
        dob=markup(dob)
        data_tuned << dob
      end
      data_tuned
    end
    def urls(data)
      @words=[]
      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
            u="#{Xx[:html_relative2]}/" + u
          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.html_seg.rel_image
            #image_path=(@md.fns =~/\.-ss[tm]$/) \
            #? @env.url.images_external
            #: @env.url.images_local
            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.gsub(/(\S+)/,"#{Xx[:segment]}#\\1") if u !~/\// unless http_ #marker: in scroll remove; in seg replace
            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
    def url_markup(dob)
      unless dob.is==:code
        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 #consider change, do a while loop
        dob.obj=dob.obj.gsub(/\\copyright/i,%{<sup>&copy;</sup>})
        if (dob.obj !~/\<:ad\s+\.\.\//)
          dob.obj=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_local}/\\2" alt="\\3"></a></center>\n})
        else
          dob.obj=dob.obj.gsub(/\<:ad\s+(\S+)?\s+(\S+\.png)\s+(.+)?\;\s+(.+)?\;\s*\>/,
            %{\n<center><a href="\\1" target="_top"><img src="#{@env.url.images_local}/\\2" alt="\\3"></a></center>\n})
        end
        dob.obj=dob.obj.gsub(/!pick/,%{<img border="0" height="15" width="15" src="#{@env.url.images}/#{@vz.icon_choice}" alt="stellar">}).
          gsub(/!new/,%{&nbsp;<img border="0" height="15" width="15" src="#{@env.url.images}/#{@vz.icon_new}" alt="new">}).
          gsub(/<:h(.{1,7}?)>/,'<a href="#h\1">\1</a>').
          gsub(/<:to(\d{1,7}?)>/,'<a href="#to\1">to&nbsp;{&nbsp;\1&nbsp;}</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]}/,%{#{@brace_url.xml_open}<a href="mailto:\\1">\\1</a>#{@brace_url.xml_close}}).
          gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,%{#{@brace_url.xml_open}<a href="\\1" target="_top">\\1</a>#{@brace_url.xml_close}}) #http ftp matches with decoration
        if dob.obj =~/#{Xx[:html_relative2]}\/\S+/ \
        and dob.obj !~/(\"#{Xx[:html_relative2]}\/\S+?\"|>\s*#{Xx[:html_relative2]}\/\S+<)/
          dob.obj=dob.obj.gsub(/(#{Xx[:html_relative2]}\/\S+)/,'<a href="\1">\1</a>')
        end
        if dob.obj =~/..\/\S+/ \
        and dob.obj !~/(\"..\/\S+?\"|>\s*..\/\S+<)/
          dob.obj=dob.obj.gsub(/\.\.(\/\S+)/,%{<a href="#{Xx[:html_relative2]}\1">\1</a>})
        end
        dob.obj=dob.obj.gsub(/<a href=":/,%{<a href="#{@vz.url_site}/}).
          gsub(/<a href="\.\.\//,%{<a href="#{@vz.url_site}/}).
          gsub(/<a href="#{Xx[:html_relative2]}\//,%{<a href="#{@vz.url_site}/})
      else
        dob.obj=dob.obj.gsub(/</m,'&lt;').gsub(/>/m,'&gt;')
      end
      dob
    end
    def angle_brackets(dob)
      dob.obj=dob.obj.gsub(/<([a-z:\/]+)>/,"#{Dx[:lt_xml]}\\1#{Dx[:gt_xml]}")
      dob
    end
    def endnotes_html(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]})/,
            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2.</sup></a> \3 \4').   #endnote- note- (careful may have switched)
          gsub(/(#{Mx[:en_b_o]})([*+]\d+)\s+(.+?)(#{Mx[:en_b_c]})/,
            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2.</sup></a> \3 \4').   #endnote- note- (careful may have switched)
          gsub(/(#{Mx[:en_a_o]})([*+]+)\s+(.+?)(#{Mx[:en_a_c]})/,
            '&nbsp;<a name="-\2" href="#_\2">&nbsp;<sup>\2</sup>&nbsp;</a> ' + #note- endnote-
            '\1\2 <a name="_\2" href="#-\2">&nbsp;<sup>\2</sup></a> \3 \4')    #endnote- note- (careful may have switched)
      end
      dob
    end
    def markup(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]}/,'<a name="\1"></a>').
        gsub(/^#{Mx[:gl_bullet]}/m,'●&nbsp;&nbsp;').
        gsub(/#{Mx[:nbsp]}/,'&nbsp;').
        gsub(/<(p|br)>/,'<\1 />')
      dob=SiSU_HTML_Tune::CleanHTML.new(dob).clean
      dob
    end
    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
  end
end
__END__