# coding: utf-8
=begin
* Name: SiSU
* Description: a framework for document structuring, publishing and search
* Author: Ralph Amissah
* Copyright: (C) 1997 - 2009 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: html generation, html pre-processing
=end
require "#{SiSU_lib}/param"
module SiSU_HTML_Tune
require "#{SiSU_lib}/sysenv"
require "#{SiSU_lib}/html_format" #watch
include SiSU_Env
include SiSU_Screen
@@line_mode=''
@@endnote_array=[]
@@endnote_call_counter=1
@@table_align='
 \; |
'
@@table_align_close=' |
|
'
@@counter,@@column,@columns=0,0,0
class Output
def initialize(data,md)
@data,@md=data,md
@cX=SiSU_Screen::Ansi.new(@md.cmd).cX
end
def hard_output
my_make=SiSU_Env::Create_file.new(@md.cmd,@md.fns)
@filename_tune=my_make.file_tune
data=[]
@data.each{|x| x.strip; data << x if not x.empty?} #1.9 array?
data.each do |para|
@filename_tune.puts para, "\n" #if para !~/¡/u
end
end
def marshal
my_make=SiSU_Env::Create_file.new(@md.cmd,@md.fns)
@marshal_tune=my_make.marshal_tune
File.open(@marshal_tune,'w') {|f| Marshal.dump(@data.to_a,f)}
end
end
class Clean_html
def initialize(html='')
@html=html
end
def clean
@html.gsub!(/#{Mx[:gl_o]}(#[0-9]{3})#{Mx[:gl_c]}/u,'&\1;')
@html.gsub!(/#{Mx[:gl_o]}#([a-z]{2,4})#{Mx[:gl_c]}/u,'&\1;')
@html.gsub!(/¢/u,'¢') # ¢
@html.gsub!(/£/u,'£') # £
@html.gsub!(/¥/u,'¥') # ¥
@html.gsub!(/§/u,'§') # §
@html.gsub!(/©/u,'©') # ©
@html.gsub!(/ª/u,'ª') # ª
@html.gsub!(/«/u,'«') # «
@html.gsub!(/®/u,'®') # ®
@html.gsub!(/°/u,'°') # °
@html.gsub!(/±/u,'±') # ±
@html.gsub!(/²/u,'²') # ²
@html.gsub!(/³/u,'³') # ³
@html.gsub!(/µ/u,'µ') # µ
@html.gsub!(/¶/u,'¶') # ¶
@html.gsub!(/¹/u,'¹') # ¹
@html.gsub!(/º/u,'º') # º
@html.gsub!(/»/u,'»') # »
@html.gsub!(/¼/u,'¼') # ¼
@html.gsub!(/½/u,'½') # ½
@html.gsub!(/¾/u,'¾') # ¾
@html.gsub!(/×/u,'×') # ×
@html.gsub!(/÷/u,'÷') # ÷
@html.gsub!(/¿/u,'¿') # ¿
@html.gsub!(/À/u,'À') # À
@html.gsub!(/Á/u,'Á') # Á
@html.gsub!(/Â/u,'Â') # Â
@html.gsub!(/Ã/u,'Ã') # Ã
@html.gsub!(/Ä/u,'Ä') # Ä
@html.gsub!(/Å/u,'Å') # Å
@html.gsub!(/Æ/u,'Æ') # Æ
@html.gsub!(/Ç/u,'Ç') # Ç
@html.gsub!(/È/u,'È') # È
@html.gsub!(/É/u,'É') # É
@html.gsub!(/Ê/u,'Ê') # Ê
@html.gsub!(/Ë/u,'Ë') # Ë
@html.gsub!(/Ì/u,'Ì') # Ì
@html.gsub!(/Í/u,'Í') # Í
@html.gsub!(/Î/u,'Î') # Î
@html.gsub!(/Ï/u,'Ï') # Ï
@html.gsub!(/Ð/u,'Ð') # Ð
@html.gsub!(/Ñ/u,'Ñ') # Ñ
@html.gsub!(/Ò/u,'Ò') # Ò
@html.gsub!(/Ó/u,'Ó') # Ó
@html.gsub!(/Ô/u,'Ô') # Ô
@html.gsub!(/Õ/u,'Õ') # Õ
@html.gsub!(/Ö/u,'Ö') # Ö
@html.gsub!(/Ø/u,'Ø') # Ø
@html.gsub!(/Ù/u,'Ù') # Ù
@html.gsub!(/Ú/u,'Ú') # Ú
@html.gsub!(/Û/u,'Û') # Û
@html.gsub!(/Ü/u,'Ü') # Ü
@html.gsub!(/Ý/u,'Ý') # Ý
@html.gsub!(/Þ/u,'Þ') # Þ
@html.gsub!(/ß/u,'ß') # ß
@html.gsub!(/à/u,'à') # à
@html.gsub!(/á/u,'á') # á
@html.gsub!(/â/u,'â') # â
@html.gsub!(/ã/u,'ã') # ã
@html.gsub!(/ä/u,'ä') # ä
@html.gsub!(/å/u,'å') # å
@html.gsub!(/æ/u,'æ') # æ
@html.gsub!(/ç/u,'ç') # ç
@html.gsub!(/è/u,'è') # è
@html.gsub!(/é/u,'é') # é
@html.gsub!(/ê/u,'ê') # ê
@html.gsub!(/ë/u,'ë') # ë
@html.gsub!(/ì/u,'ì') # ì
@html.gsub!(/í/u,'í') # í
@html.gsub!(/î/u,'î') # î
@html.gsub!(/ï/u,'ï') # ï
@html.gsub!(/ð/u,'ð') # ð
@html.gsub!(/ñ/u,'ñ') # ñ
@html.gsub!(/ò/u,'ò') # ò
@html.gsub!(/ó/u,'ó') # ó
@html.gsub!(/ô/u,'ô') # ô
@html.gsub!(/õ/u,'õ') # õ
@html.gsub!(/ö/u,'ö') # ö
@html.gsub!(/ø/u,'ø') # ø
@html.gsub!(/ù/u,'ù') # ú
@html.gsub!(/ú/u,'ú') # û
@html.gsub!(/û/u,'û') # ü
@html.gsub!(/ü/u,'ü') # ý
@html.gsub!(/þ/u,'þ') # þ
@html.gsub!(/ÿ/u,'ÿ') # ÿ
@html.gsub!(/ý/u,'ý')
@html
end
end
class Tune
@@dp=nil
def initialize(data,md)
@data,@md=data,md
@vz=SiSU_Env::Get_init.instance.skin
@env=SiSU_Env::Info_env.new(@md.fns)
@sys=SiSU_Env::System_call.new
@dp=@@dp ||=SiSU_Env::Info_env.new.digest.pattern
@env=SiSU_Env::Info_env.new(@md.fns)
@url_brace=SiSU_Viz::Skin.new.url_decoration
#@utf8=SiSU_character_encode::UTF8 #.new
end
def songsheet
begin
@cX=SiSU_Screen::Ansi.new(@md.cmd).cX
tell=SiSU_Screen::Ansi.new(@md.cmd,'Tune')
tell.txt_grey unless @md.cmd =~/q/
data=Tune.new(@data,@md).endnotes_html
data=Tune.new(data,@md).url_markup
data=Tune.new(data,@md).markup
if @md.cmd =~/M/ #Hard Output Tune Optional on/off here
data=Output.new(data,@md).hard_output
Output.new(data,@md).marshal
end
tuned=Tune.new(@data,@md).output
rescue; SiSU_Errors::Info_error.new($!,$@,@md.cmd,@md.fns).error
ensure
end
end
def para_numbers
data=@data
@tuned_file=[]
data.each do |para|
para.gsub!(/#{Mx[:lv_o]}\d:(\S?)#{Mx[:lv_c]}/,'\0#\1. ')
@tuned_file << para
end
end
def markup
@tuned_file=[]
@data.each do |para|
#@utf8.new(para).html
#@utf8.html(@para)
para.gsub!(/#{Mx[:mk_o]}#([a-zA-Z]+)#{Mx[:mk_c]}/,'&\1;')
para.gsub!(/#{Mx[:mk_o]}(#[0-9]+)#{Mx[:mk_c]}/,'&\1;')
para.gsub!(/#{Mx[:br_line]}|#{Mx[:br_nl]}/,'
')
para.gsub!(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/,'\1')
para.gsub!(/#{Mx[:fa_italics_o]}(.+?)#{Mx[:fa_italics_c]}/,'\1')
para.gsub!(/#{Mx[:fa_underscore_o]}(.+?)#{Mx[:fa_underscore_c]}/,'\1')
para.gsub!(/#{Mx[:fa_superscript_o]}(.+?)#{Mx[:fa_superscript_c]}/,'\1')
para.gsub!(/#{Mx[:fa_subscript_o]}(.+?)#{Mx[:fa_subscript_c]}/,'\1')
para.gsub!(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'\1')
para.gsub!(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'\1')
para.gsub!(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'\1')
para.gsub!(/#{Mx[:mk_o]}:name#(\S+?)#{Mx[:mk_c]}/,'')
para.gsub!(/#{Mx[:nbsp]}/,' ')
para.gsub!(/<(p|br)>/,'<\1 />')
para=SiSU_HTML_Tune::Clean_html.new(para).clean
@tuned_file << para
end
end
def urls(data)
@words=[]
data.each do |word|
@words << if word=~/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}((?:https?|file|ftp)\S+|image)/
if word =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:(?:https?|file|ftp):\/\/\S+?\.[^'"><\s]+?|image)[;.,]?(?:\s|$)/
m,u,d=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}((?:https?|file|ftp):\/\/\S+?\.[^'"><\s]+?|image)([;.,]?(?:\s|$))/.match(word).captures
else m,u=/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}((?:https?|file|ftp)\S+|image)/.match(word).captures
d=''
end
case m
when /\.png|\.jpg|\.gif|c=|\d+x\d+/
w,h=/(\d+)x(\d+)/.match(m).captures if m =~/\d+x\d+/
w=%{width="#{w}"} if w
h=%{height="#{h}"} if h
c=m[/"(.+?)"/m,1]
caption=%{
#{c}
} if c
png=m.scan(/\S+/)[0]
image_path=if @md.fns =~/\.-ss[tm]$/; @env.url.images_external
else @env.url.images_local
end
ins=if u \
and u.strip !~/^image$/
%{#{caption}}
else %{#{caption}}
end
word.gsub!(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}((?:https?|file|ftp)\S+|image)/,ins)
else
link=m[/(.+)/m]
png=m.scan(/\S+/)[0].strip
link=link.strip
ins=%{#{link}#{d}}
word.gsub!(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:https?|file|ftp)\S+/,ins)
end
word
else word
end
word
end
@words=@words.join(' ')
end
def url_markup
data=@data
@tuned_file=[]
data.each do |para|
#para.gsub!(/#{Mx[:mk_o]}name#(\S+?)#{Mx[:mk_c]}/,'')
para.gsub!(/<-#>/,'')
#para.gsub!(/<(p|br)>/,'<\1 />')
unless para =~/^#{Mx[:gr_o]}code#{Mx[:gr_c]}/
if para =~/<::\s+/ #watch
para.gsub!(/<::\s+(\S+?)\s+!>/,
%{})
end
if para =~/<:image\s+/
para.gsub!(/<:image\s+(http\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+>/,
%{})
para.gsub!(/<:image\s+(http\S+)\s+(\S+)\s+>/,
%{})
para.gsub!(/<:image\s+(\S+)\s+(\S+)\s+(\S+)\s+>/,
%{})
para.gsub!(/<:image\s+(\S+)\s+>/,
%{})
end
if para =~/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}((?:https?|file|ftp)\S+|image)/
@word_mode=para.scan(/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}(?:(?:https?|file|ftp)\S+|image)|(?:#{Mx[:gl_o]}\S+?#{Mx[:gl_c]})+|\S+/u)
words=urls(@word_mode)
para.gsub!(/.+/m,words)
end
if (para !~/^#{Mx[:meta_o]}|^#{Mx[:gr_o]}code#{Mx[:gr_c]}/)
para.gsub!(/\\copyright/i,%{©})
if (para !~/\<:ad\s+\.\.\//)
para.gsub!(/\<:ad\s+(\S+)?\s+(\S+\.png)\s+(.+)?\;\s+(.+)?\;\s*!\>/,
%{\n\n})
else
para.gsub!(/\<:ad\s+(\S+)?\s+(\S+\.png)\s+(.+)?\;\s+(.+)?\;\s*\>/,
%{\n\n})
end
para.gsub!(/!pick/, %{})
para.gsub!(/!new/, %{ })
para.gsub!(/<:h(.{1,7}?)>/,'\1')
para.gsub!(/<:to(\d{1,7}?)>/,'to { \1 } ')
if para =~/\b\S+\@\S+?\.\S+/ \
and para !~/(\"\S+\@\S+?\.\S+\"|>\S+\@\S+?\.\S+?<)/
para.gsub!(/([a-zA-Z0-9._-]+\@\S+?\.[a-zA-Z0-9._-]+)/,'<\1>')
end
para.gsub!(/\b[_\\]((?:https?|file|ftp):\/\/\S+?\.[^'"><\s]+?)([;.,]?(?:\s|$))/,'\1\2') #http ftp matches escaped, no decoration
para.gsub!(/((?:^|\s)#{Mx[:lnk_c]})((?:https?|file|ftp):\/\/\S+?\.[^'"><\s]+?)([;.,]?(?:\s|$))/,'\1\2\3') #special case \{ e.g. \}http://url
para.gsub!(/(^|#{Mx[:gl_c]}|\s)((?:https?|file|ftp):\/\/\S+?\.[^'"><\s]+?)([;.,]?(?=\s|$))/,%{\\1#{@url_brace.xml_open}\\2#{@url_brace.xml_close}\\3}) #http ftp matches with decoration
if para =~/..\/\S+/ \
and para !~/(\"..\/\S+?\"|>\s*..\/\S+<)/
para.gsub!(/(\.\.\/\S+)/,'\1')
end
para.gsub!(//m,'\1>') #code-block: angle brackets special characters
para.gsub!(/(^|[^}])_/m,'\1>')
end
@tuned_file << para
end
end
def endnotes_html
data=@data
@tuned_file=[]
data.each do |para|
unless para =~/^#{Mx[:gr_o]}code#{Mx[:gr_c]}/
para.gsub!(/(#{Mx[:en_a_o]}|#{Mx[:en_b_o]})(\d+)\s+(.+?) #{Mx[:id_o]}#@dp#{Mx[:id_c]}(#{Mx[:en_a_c]}|#{Mx[:en_b_c]})/,
' \2 ' + #note- endnote-
'\1\2 \2. \3 \4') #endnote- note- (careful may have switched)
para.gsub!(/(#{Mx[:en_b_o]})([*+]\d+)\s+(.+?) #{Mx[:id_o]}#@dp#{Mx[:id_c]}(#{Mx[:en_b_c]})/,
' \2 ' + #note- endnote-
'\1\2 \2. \3 \4') #endnote- note- (careful may have switched)
para.gsub!(/(#{Mx[:en_a_o]})([*+]+)\s+(.+?) #{Mx[:id_o]}#@dp#{Mx[:id_c]}(#{Mx[:en_a_c]})/,
' \2 ' + #note- endnote-
'\1\2 \2 \3 \4') #endnote- note- (careful may have switched)
end
@tuned_file << para
end
end
def output
data=@data
@tuned_file=[]
data.each do |para|
para.strip!
para.chomp!
@tuned_file << para
end
@tuned_file << "\n" if (@md.fns =~/\.sst0/) #remove
@tuned_file
end
end
end
__END__