summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/context/ruby/xmltools.rb
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/scripts/context/ruby/xmltools.rb')
-rw-r--r--Master/texmf-dist/scripts/context/ruby/xmltools.rb120
1 files changed, 118 insertions, 2 deletions
diff --git a/Master/texmf-dist/scripts/context/ruby/xmltools.rb b/Master/texmf-dist/scripts/context/ruby/xmltools.rb
index fe4fbde22ec..5b4a112b8b5 100644
--- a/Master/texmf-dist/scripts/context/ruby/xmltools.rb
+++ b/Master/texmf-dist/scripts/context/ruby/xmltools.rb
@@ -15,7 +15,7 @@
# This script will harbor some handy manipulations on tex
# related files.
-banner = ['XMLTools', 'version 1.2.0', '2002/2006', 'PRAGMA ADE/POD']
+banner = ['XMLTools', 'version 1.2.1', '2002/2006', 'PRAGMA ADE/POD']
$: << File.expand_path(File.dirname($0)) ; $: << File.join($:.last,'lib') ; $:.uniq!
@@ -400,6 +400,119 @@ class Commands
end
end
+ def filter
+
+ require "rexml/document"
+
+ element = @commandline.option('element')
+ files = @commandline.arguments
+ result = "xmltools.xlg"
+
+ if element.empty? then
+ report("provide element using --element")
+ elsif files.length == 0 then
+ report("provide filename(s)")
+ else
+ begin
+ File.open(result,'w') do |f|
+ f << "<?xml version='1.0'?>\n\n"
+ f << "<xlg:document>\n\n"
+ total = 0
+ files.sort.each do |file|
+ begin
+ report("loading: #{file}")
+ data = REXML::Document.new(IO.read(file))
+ rescue
+ report("error: invalid xml")
+ else
+ found = 0
+ report("filtering: #{element}")
+ REXML::XPath.each(data,"//#{element}") do |table|
+ str = table.to_s
+ if str.length > 0 then
+ total += 1
+ found += 1
+ report("found: #{total} / #{found} / #{str.length} bytes")
+ f << "<xlg:file name='#{file}'>\n\n" unless found > 1
+ f << "<xlg:filtered n='#{total}' m='#{found}'>"
+ f << "#{str.gsub(/^\s*/m,'').gsub(/\s*$/m,'')}"
+ f << "</xlg:filtered>\n\n"
+ end
+ end
+ f << "</xlg:file>\n\n" if found > 0
+ end
+ end
+ f << "</xlg:document>\n"
+ end
+ report("result: #{result}")
+ rescue
+ report("error in opening #{result}")
+ end
+ end
+
+ end
+
+ def enhance
+ oldname = @commandline.argument('first')
+ newname = @commandline.argument('second')
+ verbose = @commandline.option('verbose')
+ # todo: options, maybe a config file
+ if ! newname || newname.empty? then
+ newname = oldname + ".prep"
+ end
+ if FileTest.file?(oldname) then
+ report("") if verbose
+ data = IO.read(oldname)
+ elements = Array.new
+ preamble = ""
+ done = false
+ data.sub!(/^(.*?)\s*(<[a-z])/mois) do
+ preamble = $1
+ $2
+ end
+ # hide elements
+ data.gsub!(/<(.*?)>/mois) do
+ elements << $1
+ "<#{elements.length}>"
+ end
+ # abc[-/]def
+ data.gsub!(/([a-z]{3,})([\/\-])([a-z]{3,})/mois) do
+ done = true
+ report("compound: #{$1}#{$2}#{$3}") if verbose
+ "#{$1}<compound token='#{$2}'/>#{$3}"
+ end
+ # (abcd
+ data.gsub!(/(\()([a-z]{4,})/mois) do
+ done = true
+ report("compound: #{$1}#{$2}") if verbose
+ "<compound token='#{$1}'/>#{$2}"
+ end
+ # abcd)
+ data.gsub!(/(\()([a-z]{4,})/mois) do
+ done = true
+ report("compound: #{$1}#{$2}") if verbose
+ "#{$2}<compound token='#{$2}'/>"
+ end
+ # roll back elements
+ data.gsub!(/<(\d+)>/mois) do
+ "<#{elements.shift}>"
+ end
+ File.open(newname,'wb') do |f|
+ f << preamble
+ f << "\n"
+ f << data
+ end
+ if verbose then
+ if done then
+ report("")
+ report(oldname," converted to ",newname)
+ else
+ report(oldname," copied to ",newname)
+ end
+ end
+ end
+ end
+
def cleanup # todo, share loading/saving with previous
file = @commandline.argument('first')
@@ -508,6 +621,8 @@ commandline.registeraction('dir', 'generate directory listing')
commandline.registeraction('mmlpages','generate graphic from mathml')
commandline.registeraction('analyze', 'report entities and elements [--utf --process]')
commandline.registeraction('cleanup', 'cleanup xml file [--force]')
+commandline.registeraction('enhance', 'enhance xml file (partial)')
+commandline.registeraction('filter', 'filter elements from xml file [element=]')
# commandline.registeraction('dir', 'filename --pattern= --output= [--recurse --stripname --longname --url --root]')
# commandline.registeraction('mmlpages','filename [--eps --jpg --png --style= --mode=]')
@@ -520,8 +635,10 @@ commandline.registeraction('version')
commandline.registerflag('stripname')
commandline.registerflag('longname')
commandline.registerflag('recurse')
+commandline.registerflag('verbose')
commandline.registervalue('pattern')
+commandline.registervalue('element')
commandline.registervalue('url')
commandline.registervalue('output')
commandline.registervalue('root')
@@ -533,7 +650,6 @@ commandline.registerflag('utf')
commandline.registerflag('process')
commandline.registervalue('style')
commandline.registervalue('modes')
-commandline.registervalue('verbose')
commandline.expand