def listings_mix_references( template = @text )
def scan_source( source, r = /[(TeXlabel)|(LABEL)]\{(.*?)?\}/ )
Logger.add(4, self.class, "Read File #{source} for cross-references")
labels = Hash.new( 0 )
if !File.exist?( source )
Logger.add(3, self.class, "Sourcecode #{source} nicht gefunden" )
return labels
end
File.open(source){ |f|
linenumber = 0
f.each{|l|
linenumber += 1
while l =~ r
labels[$1] = linenumber
l.sub!( r ,'')
end
}
}
Logger.add(5, self.class, "Found labels in #{source}: #{labels.inspect}")
return labels
end
def get_label( hash, key, source )
pos = hash.fetch(key, 0)
Logger.add(3, self.class, "Label #{key} not found in #{source}") if pos == 0
return pos
end
@text.gsub!(
/%(lstinputlisting\[.*?\]\{(.*?)\})\n.lstinputlisting\[.*?\]\{.*?\}/m,
'\\\\\1'
)
@text.gsub!(
/%(ABAPinput\[.*?\]\{(.*?)\})\n.ABAPinpu\[.*?\]\{.*?\}/m,
'\\\\\1'
)
sources = Hash.new()
r = Regexp.new('\\\\(lstinputlisting|ABAPinput)\[.*(?:fromlabel=(.*?))(,.*)*(?:tolabel=(.*?))(,.*)*\]{(.*)}')
while m = r.match(@text)
old = m[0]
source = m[6]
Logger.add(5, self.class, "Found lstinputlisting #{source}")
if ! sources.has_key?(source)
sources[source] = scan_source( source )
end
pos1 = get_label(sources[source], m[2], source ) + 1
pos2 = get_label(sources[source], m[4], source ) - 1
new = '\\' + m[1] + '[' + 'firstline=' + pos1.to_s + m[3] +
'lastline=' + pos2.to_s + m[5].to_s + "]{" + source + '}'
Logger.add(5, self.class, "Command old:\t#{old}\nCommand new\t#{new}" )
@text = m.pre_match + "%#{old[1..-1]}\n" + new + m.post_match
end
return @text
end