Re: SVG --> HTML/TXT for searching and accessibility

Dean Jackson wrote:

> Alternatively, you could write a program that
> ignores everything within angle brackets (bonus points to
> anyone who can do this in a program of 25 characters or
> less!)
> 
> If you have suggestions for improvement or if you do actually
> write a program to do the same thing, let me know and I'll
> link it from the page. I'll also include the smallest perl/python/ruby/etc
> script anyone comes up with to extract text from an SVG 
> file.


Although I agree with Dave's points, here's one more overly simplistic 
program:

#!/usr/local/bin/ruby

svg='<svg>
   <title>granny</title>
   <desc id="foo">my granny on the balkony</desc>
   <g>
     <text>2002</text>
   </g>
   <rect/>
</svg>'

ary =
  svg.gsub(/>\s+</,'><').
  scan(/<([^\s>]+)[^>]*?>([^<]+?)</)

ary.each do |element_name,text|
  puts element_name+': '+text
end

# prints:
=begin
title: granny
desc: my granny on the balkony
text: 2002
=end

Tobi


-- 
http://www.pinkjuice.com/

Received on Wednesday, 12 June 2002 04:39:56 UTC