Class: SiSU_sstFromXML::Convert

Inherits:
Object
  • Object
show all
Defined in:
lib/sisu/sst_from_xml.rb

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ Convert

Returns a new instance of Convert.



65
66
67
68
69
# 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

Instance Method Details

#domObject



134
135
136
# 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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# 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

#nodeObject



131
132
133
# File 'lib/sisu/sst_from_xml.rb', line 131

def node
  sax
end

#readObject



77
78
79
# File 'lib/sisu/sst_from_xml.rb', line 77

def read
  xml_to_sisu
end

#saxObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ Object



70
71
72
73
74
75
76
# 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_sisuObject



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
# 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