require 'cgi' module Numerals class Format class HtmlNotation < Notation def assemble(output, text_parts) # 1.23456 ×109 # Or alternative: use classes # 1.23456 ×109 # .numerals-rep { text-decoration: overline; } # .numerals-sup { vertical-align: super; } # TODO: padding if text_parts.special? output << escape(text_parts.special) else output << escape(text_parts.sign) if format.symbols.base_prefix output << format.symbols.base_prefix end output << escape(text_parts.integer) # or decide here if empty integer part is show as 0? if text_parts.show_point?(format) output << escape(format.symbols.point) end output << escape(text_parts.fractional) if text_parts.repeat output << %(#{escape(text_parts.repeat)}) end if format.symbols.base_suffix || format.base != 10 if format.symbols.base_prefix output << format.symbols.base_suffix else # show base suffix as a subscript subscript = format.symbols.base_suffix || base.to_s output << "#{subscript}" end end if text_parts.exponent_value != 0 || format.mode.mode == :scientific output << "×" output << escape(text_parts.exponent_base) output << "#{escape(text_parts.exponent)}" end end end private def escape(text) CGI.escapeHTML(text) end def unescape(text) CGI.unescapeHTML(text) end end define_notation :html, HtmlNotation end end