def to_html text
if Object.const_defined? :Encoding then
html = ''.encode text.encoding
encoded = RDoc::Text::TO_HTML_CHARACTERS[text.encoding]
else
html = ''
encoded = {
:close_dquote => '”',
:close_squote => '’',
:copyright => '©',
:ellipsis => '…',
:em_dash => '—',
:en_dash => '–',
:open_dquote => '“',
:open_squote => '‘',
:trademark => '®',
}
end
s = StringScanner.new text
insquotes = false
indquotes = false
after_word = nil
until s.eos? do
case
when s.scan(/<(tt|code)>.*?<\/\1>/) then
html << s.matched.gsub('\\\\', '\\')
when s.scan(/<(tt|code)>.*?/) then
warn "mismatched <#{s[1]}> tag"
html << s.matched
when s.scan(/<[^>]+\/?s*>/) then
html << s.matched
when s.scan(/\\(\S)/) then
html << s[1]
after_word = nil
when s.scan(/\.\.\.(\.?)/) then
html << s[1] << encoded[:ellipsis]
after_word = nil
when s.scan(/\(c\)/) then
html << encoded[:copyright]
after_word = nil
when s.scan(/\(r\)/) then
html << encoded[:trademark]
after_word = nil
when s.scan(/---/) then
html << encoded[:em_dash]
after_word = nil
when s.scan(/--/) then
html << encoded[:en_dash]
after_word = nil
when s.scan(/"|"/) then
html << encoded[indquotes ? :close_dquote : :open_dquote]
indquotes = !indquotes
after_word = nil
when s.scan(/``/) then
html << encoded[:open_dquote]
after_word = nil
when s.scan(/''/) then
html << encoded[:close_dquote]
after_word = nil
when s.scan(/'/) then
if insquotes
html << encoded[:close_squote]
insquotes = false
elsif after_word
html << encoded[:close_squote]
else
html << encoded[:open_squote]
insquotes = true
end
after_word = nil
else
match = s.scan(/.+?(?=[<\\.("'`&-])/)
if match then
html << match
after_word = match =~ /\w$/
else
html << s.rest
break
end
end
end
html
end