summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/ruby/base/ctx.rb
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/scripts/context/ruby/base/ctx.rb')
-rw-r--r--Master/texmf-dist/scripts/context/ruby/base/ctx.rb117
1 files changed, 96 insertions, 21 deletions
diff --git a/Master/texmf-dist/scripts/context/ruby/base/ctx.rb b/Master/texmf-dist/scripts/context/ruby/base/ctx.rb
index 852c3f7046e..0baf5a88b1b 100644
--- a/Master/texmf-dist/scripts/context/ruby/base/ctx.rb
+++ b/Master/texmf-dist/scripts/context/ruby/base/ctx.rb
@@ -23,6 +23,8 @@ class CtxRunner
attr_reader :environments, :modules, :filters
+ @@suffix = 'prep'
+
def initialize(jobname=nil,logger=nil)
if @logger = logger then
def report(str='')
@@ -52,17 +54,42 @@ class CtxRunner
end
if not @ctxname then
- report('provide ctx file')
+ report('no ctx file specified')
return
end
- if not FileTest.file?(@ctxname) and defaultname and FileTest.file?(defaultname) then
- @ctxname = defaultname
- end
-
+ # name can be kpse:res-make.ctx
if not FileTest.file?(@ctxname) then
- report('provide ctx file')
- return
+ fullname, done = '', false
+ if @ctxname =~ /^kpse:/ then
+ begin
+ if fullname = Kpse.found(@ctxname.sub(/^kpse:/,'')) then
+ @ctxname, done = fullname, true
+ end
+ rescue
+ # should not happen
+ end
+ else
+ ['..','../..'].each do |path|
+ begin
+ fullname = File.join(path,@ctxname)
+ if FileTest.file?(fullname) then
+ @ctxname, done = fullname, true
+ end
+ rescue
+ # probably strange join
+ end
+ break if done
+ end
+ end
+ if ! done && defaultname && FileTest.file?(defaultname) then
+ report("using default ctxfile #{defaultname}")
+ @ctxname = defaultname
+ end
+ if not done then
+ report('no ctx file found')
+ return false
+ end
end
@xmldata = IO.read(@ctxname)
@@ -101,16 +128,44 @@ class CtxRunner
@modules << justtext(mod)
end
REXML::XPath.each(@xmldata.root,"/ctx:job/ctx:process/ctx:resources/ctx:filter") do |fil|
- @filters << justtext(mod)
+ @filters << justtext(fil)
+ end
+ commands = Hash.new
+ REXML::XPath.each(@xmldata.root,"/ctx:job/ctx:preprocess/ctx:processors/ctx:processor") do |pre|
+ begin
+ commands[pre.attributes['name']] = pre
+ rescue
+ end
+ end
+ suffix = @@suffix
+ begin
+ suffix = REXML::XPath.match(@xmldata.root,"/ctx:job/ctx:preprocess/@suffix").to_s
+ rescue
+ suffix = @@suffix
+ else
+ if suffix && suffix.empty? then suffix = @@suffix end
end
REXML::XPath.each(@xmldata.root,"/ctx:job/ctx:preprocess/ctx:files") do |files|
REXML::XPath.each(files,"ctx:file") do |pattern|
preprocessor = pattern.attributes['processor']
if preprocessor and not preprocessor.empty? then
+ begin
+ variables['old'] = @jobname
+ variables['new'] = ""
+ REXML::XPath.each(pattern,"ctx:value") do |value|
+ if name = value.attributes['name'] then
+ substititute(value,variables[name.to_s])
+ end
+ end
+ rescue
+ report('unable to resolve file pattern')
+ return
+ end
pattern = justtext(pattern)
Dir.glob(pattern).each do |oldfile|
- newfile = "#{oldfile}.prep"
+ newfile = "#{oldfile}.#{suffix}"
if File.needsupdate(oldfile,newfile) then
+ report("#{oldfile} needs preprocessing")
begin
File.delete(newfile)
rescue
@@ -118,11 +173,17 @@ class CtxRunner
end
# there can be a sequence of processors
preprocessor.split(',').each do |pp|
- if command = REXML::XPath.first(@xmldata.root,"/ctx:job/ctx:preprocess/ctx:processors/ctx:processor[@name='#{pp}']") then
+ if command = commands[pp] then
# a lie: no <?xml ...?>
command = REXML::Document.new(command.to_s) # don't infect original
command = command.elements["ctx:processor"]
- report("preprocessing #{oldfile} using #{pp}")
+ begin
+ if suf = command.attributes['suffix'] then
+ newfile = "#{oldfile}.#{suf}"
+ end
+ rescue
+ end
+ report("preprocessing #{oldfile} into #{newfile} using #{pp}")
REXML::XPath.each(command,"ctx:old") do |value| replace(value,oldfile) end
REXML::XPath.each(command,"ctx:new") do |value| replace(value,newfile) end
variables['old'] = oldfile
@@ -172,7 +233,8 @@ class CtxRunner
log << "<?xml version='1.0' standalone='yes'?>\n\n"
log << "<ctx:preplist>\n"
@prepfiles.keys.sort.each do |prep|
- log << "\t<ctx:prepfile done='#{yes_or_no(@prepfiles[prep])}'>#{File.basename(prep)}</ctx:prepfile>\n"
+ # log << "\t<ctx:prepfile done='#{yes_or_no(@prepfiles[prep])}'>#{File.basename(prep)}</ctx:prepfile>\n"
+ log << "\t<ctx:prepfile done='#{yes_or_no(@prepfiles[prep])}'>#{prep}</ctx:prepfile>\n"
end
log << "</ctx:preplist>\n"
log.close
@@ -196,17 +258,27 @@ class CtxRunner
name = e.attributes.get_attribute(attribute).to_s
name = e.text.to_s if name.empty?
name.strip! if name
- if name and not name.empty? and FileTest.file?(name) then
- if f = File.open(name,'r') and i = REXML::Document.new(f) then
- report("including ctx file #{name}")
- REXML::XPath.each(i.root,"*") do |ii|
- xmldata.root.insert_after(e,ii)
- more = true
+ done = false
+ if name and not name.empty? then
+ ['.',File.dirname(@ctxname),'..','../..'].each do |path|
+ begin
+ fullname = if path == '.' then name else File.join(path,name) end
+ if FileTest.file?(fullname) then
+ if f = File.open(fullname,'r') and i = REXML::Document.new(f) then
+ report("including ctx file #{name}")
+ REXML::XPath.each(i.root,"*") do |ii|
+ xmldata.root.insert_after(e,ii)
+ more = true
+ end
+ end
+ done = true
+ end
+ rescue
end
end
- else
- report("no valid ctx inclusion file #{name}")
+ break if done
end
+ report("no valid ctx inclusion file #{name}") unless done
rescue Exception
# skip this file
ensure
@@ -278,7 +350,10 @@ class CtxRunner
when 'suffix' then if str =~ /^.*\.(.*?)$/o then $1 else '' end
when 'nosuffix' then if str =~ /^(.*)\..*?$/o then $1 else str end
when 'nopath' then if str =~ /^.*[\\\/](.*?)$/o then $1 else str end
- else str
+ when 'full' then str
+ when 'complete' then str
+ when 'expand' then File.expand_path(str).gsub(/\\/,"/")
+ else str
end
end