class SiSU_sstFromXML::Convert

Public Class Methods

new(opt) click to toggle source
# File lib/sisu/sst_from_xml.rb, line 65
def initialize(opt)
  @opt=opt
  @sisu,@sisu_base=[],[]
  @ver=SiSU_Env::InfoVersion.instance.get_version
end

Public Instance Methods

dom() click to toggle source
# File lib/sisu/sst_from_xml.rb, line 134
def dom
  raise "#{__FILE__}::#{__LINE__} xml dom representation to sst not yet implemented (experimental simple xml representations sax and node to sst are in place)."
end
markup(text) click to toggle source
# File lib/sisu/sst_from_xml.rb, line 90
def markup(text)
  text.strip!
  text.gsub!(/(?:\s*\n|\s\s+)/,' ')
  text.gsub!(/<text class='h1'>(.+?)<\/text>/,':A~ \1')
  text.gsub!(/<text class='h2'>(.+?)<\/text>/,':B~ \1')
  text.gsub!(/<text class='h3'>(.+?)<\/text>/,':C~ \1')
  text.gsub!(/<text class='h4'>(.+?)<\/text>/,'1~ \1')
  text.gsub!(/<text class='h5'>(.+?)<\/text>/,'2~ \1')
  text.gsub!(/<text class='h6'>(.+?)<\/text>/,'3~ \1')
  text.gsub!(/<text class='norm'>(.+?)<\/text>/,'\1')
  text.gsub!(/<endnote symbol='norm'>(.+?)<\/endnote>/,'~{ \1 }~')
  text.gsub!(/<br ?\/>/,'<br>')
  text.gsub!(/<i>(.+?)<\/i>/,'/{\1}/')
  text.gsub!(/<b>(.+?)<\/b>/,'*{\1}*')
  text.gsub!(/<u>(.+?)<\/u>/,'_{\1}_')
  text.gsub!(/<sem:([a-z_]+)\s+depth=['"]zero['"]>(\s*.+?\s*)<\/sem:\1>/,';{ \2 };\1')
  text.gsub!(/<sem:([a-z_]+)\s+depth=['"]one['"]>(\s*.+?\s*)<\/sem:\1>/,':{ \2 }:\1')
  text.gsub!(/<sem:([a-z_]+)\s+depth=['"]many['"]>(\s*.+?\s*)<\/sem:\1>/,'\1:{ \2 }:\1')
  text.gsub!(/<sem:([a-z_]+)>(\s*.+?\s*)<\/sem:\1>/,'\1:{ \2 }:\1')
  text.gsub!(/\s +/,' ')
  text.strip!
  text + "\n\n"
end
markup_head(text) click to toggle source
# File lib/sisu/sst_from_xml.rb, line 80
def markup_head(text)
  text.strip!
  text.gsub!(/(?:\s*\n|\s\s+)/,' ')
  text.gsub!(/<header class=['"]\S+?['"]>(.+?)<\/header>/,'\1')
  text.gsub!(/<(\w+)>(.+?)<\/\w+>/,'@\1: \2')
  text.gsub!(/<header class=['"]\S+?['"]><(\w+)>(.+?)<\/\w+><\/header>/,'@\1: \2')
  text.gsub!(/\s +/,' ')
  text.strip!
  text + "\n\n"
end
node() click to toggle source
# File lib/sisu/sst_from_xml.rb, line 131
def node
  sax
end
read() click to toggle source
# File lib/sisu/sst_from_xml.rb, line 77
def read
  xml_to_sisu
end
sax() click to toggle source
# File lib/sisu/sst_from_xml.rb, line 113
def sax
  out_file=File.new(@output_file_name,'w')
  head=@doc.root.get_elements('//head/header')
  body=@doc.root.get_elements('//object/text')
  out_file.puts "% SiSU text #{@ver.version} (generated from a SiSU XML markup representation)\n\n"
  head.each do |x|
    if x.name=='header'
      head=markup_head(x.to_s)
      out_file.puts head
    end
  end
  body.each do |x|
    if x.name=='text'
      body=markup(x.to_s)
      out_file.puts body
    end
  end
end
tell(filename,type) click to toggle source
# File lib/sisu/sst_from_xml.rb, line 70
def tell(filename,type)
  SiSU_Screen::Ansi.new(
    @opt.act[:color_state][:set],
    "XML #{type} to SiSU sst",
    "#{filename} --> #{filename}.sst"
  ).green_hi_blue
end
xml_to_sisu() click to toggle source
# File lib/sisu/sst_from_xml.rb, line 137
def xml_to_sisu
  unless @opt.files.empty?
    @opt.files.each do |xml|
      @sisu_base=[]
      if xml =~/\.sx[sdn]\.xml$/
        begin
          @doc_str=IO.readlines(xml,'').join("\n")
          @output=File.new("#{xml}.sst",'w')
          @doc=REXML::Document.new(@doc_str)
          @output_file_name="#{Dir.pwd}/#{xml}.sst"
          @el=[]
        rescue REXML::ParseException
        end
      end
      if xml =~/\.sxs\.xml$/
        unless @opt.act[:quiet][:set]==:on
          tell(xml,'sax')
        end
        sax
      elsif xml =~/\.sxd\.xml$/
        unless @opt.act[:quiet][:set]==:on
          tell(xml,'dom')
        end
        dom
      elsif xml =~/\.sxn\.xml$/
        unless @opt.act[:quiet][:set]==:on
          tell(xml,'node')
        end
        node
      else puts "filename not recognised: << #{xml} >>"
      end
      @output << @sisu_base
    end
  else puts '.xml file for conversion to sisu expected'
  end
  puts @opt.files.inspect
end