[code lang="ruby"]
#!/usr/bin/env ruby
module MacronConversions
LATEX_TO_MACRONS_CHARACTERS = {
"\\={a}" => "?",
"\\={e}" => "?",
"\\={\\i}" => "?",
"\\={o}" => "?",
"\\={u}" => "?",
"\\={A}" => "?",
"\\={E}" => "?",
"\\={\\I}" => "?",
"\\={O}" => "?",
"\\={U}" => "?",
}
LATEX_TO_UTF8 ={
"\\={a}" => "\\xc4\\x81",
"\\={e}" => "\\xc4\\x93",
"\\={\\i}" => "\\xc4\\xab",
"\\={o}" => "\\xc5\\x8d",
"\\={u}" => "\\xc5\\xab",
"\\={A}" => "\\xc4\\x80",
"\\={E}" => "\\xc4\\x92",
"\\={\\I}" => "\\xc4\\xaa",
"\\={O}" => "\\xc5\\x8c",
"\\={U}" => "\\xc5\\xaa",
}
LATEX_TO_HTML_ENTITIES = {
"\\={a}" => "ā",
"\\={e}" => "ē",
"\\={\\i}" => "ī",
"\\={o}" => "ō",
"\\={u}" => "ū",
"\\={A}" => "Ā",
"\\={E}" => "Ē",
"\\={\\I}" => "Ī",
"\\={O}" => "Ō",
"\\={U}" => "Ū",
}
class Converter < Object
def initialize(*ray)
@target = ray[0].nil? ? 'html' : ray[0]
@index=Array.new
end
def processString(s)
firstSlash = s =~ /(\\=.*?\})/
return "" if $1.nil?
testChar = $1
if firstSlash == 0
return processChar(testChar) +
processString(s[firstSlash+testChar.length..s.length])
end
return s[0..firstSlash-1] + processChar(testChar) +
processString(s[firstSlash+testChar.length..s.length])
end
def processChar(c)
if @target == 'utf8'
return LATEX_TO_UTF8[c]
end
if @target == 'html'
return LATEX_TO_HTML_ENTITIES[c]
end
if @target == "mc"
return LATEX_TO_MACRONS_CHARACTERS[c]
end
end
end
end
unless STDIN.tty?
content=STDIN.read
else
content=ARGV[0]
end
# You can change this value to 'mc', '', or 'utf8'. Empty, it means 'html'
puts MacronConversions::Converter.new('html').processString(content)
[/code]