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.



67
68
69
70
71
# File 'lib/sisu/sst_from_xml.rb', line 67

def initialize(opt)
  @opt=opt
  @sisu,@sisu_base=[],[]
  @ver=SiSU_Env::InfoVersion.instance.get_version
end

Instance Method Details

#domObject



136
137
138
# File 'lib/sisu/sst_from_xml.rb', line 136

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/sisu/sst_from_xml.rb', line 92

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



82
83
84
85
86
87
88
89
90
91
# File 'lib/sisu/sst_from_xml.rb', line 82

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



133
134
135
# File 'lib/sisu/sst_from_xml.rb', line 133

def node
  sax
end

#readObject



79
80
81
# File 'lib/sisu/sst_from_xml.rb', line 79

def read
  xml_to_sisu
end

#saxObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sisu/sst_from_xml.rb', line 115

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



72
73
74
75
76
77
78
# File 'lib/sisu/sst_from_xml.rb', line 72

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



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
# File 'lib/sisu/sst_from_xml.rb', line 139

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