=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 Ralph Amissah All Rights Reserved.
* License: GPL 3 or later:
SiSU, a framework for document structuring, publishing and search
Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007 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: simple xml representation (dom style)
=end
module SiSU_simple_xml_model_dom
require "#{SiSU_lib}/defaults"
require "#{SiSU_lib}/param"
include SiSU_Param
include SiSU_Viz
require "#{SiSU_lib}/sysenv"
include SiSU_Env
require "#{SiSU_lib}/dal_doc_str"
require "#{SiSU_lib}/shared_xml"
require "#{SiSU_lib}/xml_format"
include SiSU_XML_format
include SiSU_XML_munge
require "#{SiSU_lib}/rexml"
include SiSU_Rexml
@@alt_id_count,@@tablehead,@@number_of_cols=0,0,0
@@tablefoot=''
class Convert
@@fns=nil
def initialize(opt)
@opt=opt
end
def read
begin
@md=SiSU_Param::Parameters.new(@opt).get #bug, relies on info persistence, assumes -m has previously been run
@env=SiSU_Env::Info_env.new(@opt.fns)
path=@env.path.output_tell
loc=@env.url.output_tell
tool=if @opt.cmd =~/[MV]/; "#{Dir.pwd}/#{@md.fn[:sxd]}\n\t#{@env.program.xml_viewer} #{path}/#{@md.fnb}/#{@md.fn[:sxd]}"
elsif @opt.cmd =~/v/; "#{@env.program.web_browser} #{Dir.pwd}/#{@md.fn[:sxd]}"
else ''
end
SiSU_Screen::Ansi.new(@opt.cmd,'invert','XML DOM',"#{@md.fns} -> #{@md.fn[:sxd]}").colorize unless @opt.cmd =~/q/
tell=SiSU_Screen::Ansi.new(@opt.cmd,@opt.fns,"#{Dir.pwd}/#{@md.fn[:sxd]}")
tell.flow if @opt.cmd =~/[MV]/
unless @@fns==@opt.fns
@@fns=@opt.fns
@@dal_array=[]
end
@dal_array=if @@dal_array.empty?; read_fnm
else @@dal_array.dup # check
end
SiSU_simple_xml_model_dom::Convert::Songsheet.new(@dal_array,@md,@env).songsheet
rescue; SiSU_Errors::Info_error.new($!,$@,@opt.cmd,@opt.fns).error
ensure #file closed in songsheet
end
end
def read_fnm
dal=[]
if FileTest.file?("#{Dir.pwd}/#{@opt.fns}")
dal=IO.readlines("#{Dir.pwd}/#{@opt.fns}","\n\n")
else puts 'Error'
end
end
private
class Songsheet
def initialize(data,md,dir)
@data,@md,@env=data,md,dir
end
def songsheet
begin
SiSU_simple_xml_model_dom::Convert::Scroll.new(@data,@md).songsheet
SiSU_simple_xml_model_dom::Convert::Tidy.new(@md,@env).xml if @md.cmd =~/[vVM]/ # test wellformedness, comment out when not in use
SiSU_Rexml::Rexml.new(@md,@md.fn[:sxd]).xml if @md.cmd =~/M/ # test rexml parsing, comment out when not in use #debug
rescue; SiSU_Errors::Info_error.new($!,$@,@md.cmd,@md.fns).error
ensure
end
end
end
class Scroll
require "#{SiSU_lib}/shared_txt"
require "#{SiSU_lib}/css"
include SiSU_text_utils
@@xml={ :body=>[],:open=>[],:close=>[],:head=>[] }
def initialize(data='',md=nil)
@data,@md=data,md
@vz=SiSU_Env::Get_init.instance.skin
@regx=/^(?:(?:<:p[bn]>\s*)?(?::?[A-C]|\d~)(?:(\S+))?\s+)?(.+)/
@tab="\t"
if @md
@env=SiSU_Env::Info_env.new(@md.fns)
@trans=SiSU_XML_munge::Trans.new(@md)
end
@sys=SiSU_Env::System_call.new
end
def songsheet
pre
markup
post
publish
end
protected
def embedded_endnotes(para='')
para.gsub!(/~\{(.+?)\}~/,
'\1 ')
para.gsub!(/~\[([*+])\s+(.+?)\]~/,
'\1\2 ')
end
def xml_markup(para='')
para.gsub!(/~\[([*+])\s+(.+?)\]~/,
'\1\2 ')
para.gsub!(/~\{([*+]+)\s+(.+?)\}~/,
'\1\2 ')
para.gsub!(/~\{(.+?)\}~/,
'\2 ')
end
def xml_head(meta)
txt=meta.text
txt.gsub!(/\/{(.+?)}\//,'\1')
txt.gsub!(/[*!]{(.+?)}[*!]/,'\1')
txt.gsub!(/_{(.+?)}_/,'\1')
txt.gsub!(/-{(.+?)}-/,'\1')
txt.gsub!(/ /,' ')
txt.gsub!(/ & /,' and ')
@@xml[:head] <<<
#{@tab*2}<#{meta.el}>
#{@tab*3}#{txt}
#{@tab*2}#{meta.el}>
#{@tab}
WOK
end
def xml_sc(md='')
sc=if @md.sc_info
<
#{@md.sc_filename}
#{@md.sc_number}
#{@md.sc_date}
WOK
else ''
end
@@xml[:sc]=sc
end
def xml_element(lv='',para='',hname='',tag='',xml_element='',xml_content='')
lv=lv.to_i
n=lv - 1
n1=lv
n2=lv + 1
n3=lv + 2
v=lv - 3
tag=''
tag="\n#{@tab*n3}#{hname}\n" if hname
@@xml[:body] <<<
#{@tab*n2}
#{@tab*n1}#{xml_content}
WOK
if lv == 4
@copen[1]=true
@copen[2]=@copen[3]=false
elsif lv == 5
@copen[2]=true
@copen[3]=false
elsif lv == 6
@copen[3]=true
end
end
def xml_structure(lv='',para='',hname='' )
puts lv if @md.cmd =~/[VM]/
lv=lv.to_i
n=lv - 1
n1=lv
n2=lv + 1
n3=lv + 2
v=lv - 3
tag=''
tag="\n#{@tab*n3}#{hname}\n" if hname !=nil
case lv
when 1..3
xml_element=""
3.downto(lv) do |x|
y=x - 1
@@xml[:body] << "#{@tab*5}\n" if @cont[1] or @cont[2] or @cont[3]
@cont[1]=false if @cont[1]
@cont[2]=false if @cont[2]
@cont[3]=false if @cont[3]
####### attempt to close contents
if @copen[3] # 6{
[3,2,1].each { |v| @@xml[:body] << "#{@tab*n}\n" }
@copen[1]=@copen[2]=@copen[3]=false
elsif @copen[2] # 5{
[2,1].each { |v| @@xml[:body] << "#{@tab*n}\n" }
@copen[1]=@copen[2]=@copen[3]=false
elsif @copen[1] # 4{
[1].each { |v| @@xml[:body] << "#{@tab*n}\n" }
@copen[1]=@copen[2]=@copen[3]=false
end
@@xml[:body] << "#{@tab*y}\n" if @level[x]
@level[x]=false
end
when 4..6
6.downto(lv) do |x|
y=x - 1
if @level[x] == true
u=x - 3;
@xml_contents_close[x]=''
end
end
cv=lv - 3
xml_element=""
xml_content="\n#{@tab*5}"
case lv
when 4
@@xml[:body] << "#{@tab*5}\n" if @cont[1]
if @copen[3] == true # 6{
[3,2,1].each { |v| @@xml[:body] << "#{@tab*n}\n" }
elsif @copen[2] == true # 5{
[2,1].each { |v| @@xml[:body] << "#{@tab*n}\n" }
elsif @copen[1] == true # 4{
[1].each { |v| @@xml[:body] << "#{@tab*n}\n" }
end
@cont[1]=true
when 5
@@xml[:body] << "#{@tab*5}\n" if @cont[2] or @cont[1]
if @copen[3] == true #6{
[3,2].each { |v| @@xml[:body] << "#{@tab*n}\n" }
elsif @copen[2] == true #5{
[2].each { |v| @@xml[:body] << "#{@tab*n}\n" }
end
@cont[2]=true
when 6
@@xml[:body] << "#{@tab*5}\n" if @cont[3] or @cont[2] or @cont[1]
[3].each { |v| @@xml[:body] << "#{@tab*n}\n" } if @copen[3]
@cont[3]=true
end
end
xml_element(lv,para,hname,tag,xml_element,xml_content)
@level[lv]=true
((lv+1)..6).each { |x| @level[x]=false }
end
def group_structure(para='')
para.gsub!(/<:group(?:-end)?>/,'')
para.strip!
@@xml[:body] << %{#{@tab*6}" << "\n"
end
def poem_structure(para='')
para.gsub!(/<:verse(?:-end)?>/,'')
para.strip!
@@xml[:body] << %{#{@tab*6}" << "\n"
end
def code_structure(para='')
para.gsub!(/<:code(?:-end)?>/,'')
para.strip!
@@xml[:body] << %{#{@tab*6}" << "\n"
end
def table_structure(table='')
@@xml[:body] << %{#{@tab*0}" << "\n"
@endnotes=[]
end
def tidywords(wordlist)
wordlist.each do |x|
x.gsub!(/&/,'&') unless x =~/&\S+;/
end
end
def markup
data=@data
dir=SiSU_Env::Info_env.new(@md.fns)
xml_sc(@md)
@rcdc=false
@level,@cont,@copen,@xml_contents_close=[],[],[],[]
(0..6).each { |x| @cont[x]=@level[x]=false }
(4..6).each { |x| @xml_contents_close[x]='' }
data.each do |para|
wordlist=para.scan(/\S+|\n/) #\n needed for tables, check though added 2005w17
para=tidywords(wordlist).join(' ').strip
para.gsub!(/<[-~]#>/,'')
para.gsub!(/ /,' ')
para.gsub!(/<:pb>\s*/,'')
para.gsub!(/\{(\S+?\.(?:png|jpg|gif)) .+?\}(?:(?:https?|file|ftp):\/\/\S+|image)/,
"#{dir.url.images_local}/\\1")
para.gsub!(/ /,' ')
para=SiSU_document_structure::Structure.new(@md,para).structure
@trans.char_enc.utf8(para) if @sys.locale =~/utf-?8/i #% utf8
if para =~/\A(?:@|0~)(\S+?):?\s+(.+?)\Z/m
d_meta=SiSU_text_utils::Header_scan.new(@md,para).meta
if d_meta; xml_head(d_meta)
end
end
@rcdc=true if @rcdc==false and (para =~/~metadata/ or para =~/^1~\s+Document Information/)
if para !~/(^0~|^@\S+?:||)/
@sto=SiSU_text_parts::Split_text_object.new(@md,para).lev_segname_para
unless @rcdc
if para !~/^(?:%\s|0~|@\S+?:)/
format_scroll=SiSU_XML_format::Format_scroll.new(@md,@sto.text) if @sto.format =~/i[12]|null/
case @sto.format
when /^(1)~(\S+)?/
xml_markup(para)
xml_structure($1,para,$2)
para=@sto.lev_para_ocn.heading_body1
when /^(2)~(\S+)?/
xml_markup(para)
xml_structure($1,para,$2)
para=@sto.lev_para_ocn.heading_body2
when /^(3)~(\S+)?/
xml_markup(para)
xml_structure($1,para,$2)
para=@sto.lev_para_ocn.heading_body3
when /^(4)~(\S+)?/
xml_markup(para)
xml_structure($1,para,$2)
para=@sto.lev_para_ocn.heading_body4
when /^(5)~(\S+)?/
xml_markup(para)
xml_structure($1,para,$2)
para=@sto.lev_para_ocn.heading_body5
when /^(6)~(\S+)?/
xml_markup(para)
xml_structure($1,para,$2)
para=@sto.lev_para_ocn.heading_body6
#when /^(i1)$/i
# #format_scroll.gsubBody
# #para=@sto.lev_para_ocn.scrIndent1
#when /^(i2)$/i
# format_scroll.gsubBody
# para=@sto.lev_para_ocn.scrIndent2
#when /^(center)$/i
# para.gsub!(/(.+)/,
# %{