Permalink
Browse files

Support for inline styles, to suit Gmail

  • Loading branch information...
1 parent 0a94579 commit b4ebbaf67f6a3a5c9742a005ca48e098cf0e993f @trejkaz committed Feb 3, 2012
Showing with 161 additions and 17 deletions.
  1. +10 −7 example/irssilog2html.rb
  2. +1 −5 example/template.erb
  3. +26 −5 lib/ircformat/html_convertor.rb
  4. +124 −0 lib/ircformat/inline_styler.rb
View
@@ -5,24 +5,27 @@
# usage: irssilog2html.rb <file>
#
-$LOAD_PATH << '../lib'
+$LOAD_PATH << "#{File.dirname(__FILE__)}/../lib"
require 'erb'
require 'ircformat/html_convertor'
-def convert(file)
- title = file
- styles = File.read('../doc/example.css')
- body = IrcFormat::HtmlConvertor.new(:code_parsers => [ :mirc, :irssi ]).irc_to_html(File.read(file))
+# title - the title for the HTML page
+# log_text - IRC log text
+# options - hash of options:
+# :inline_styles - if true, will use inline CSS instead of styles (less compact)
+def convert(title, log_text, options = {})
+ styles = File.read("#{File.dirname(__FILE__)}/../doc/example.css")
+ body = IrcFormat::HtmlConvertor.new(:code_parsers => [ :mirc, :irssi ]).irc_to_html(log_text, options)
- ERB.new(File.read(File.join(File.dirname(__FILE__), 'template.erb'))).run(binding)
+ ERB.new(File.read("#{File.dirname(__FILE__)}/template.erb")).result(binding)
end
if $0 == __FILE__
file = ARGV[0]
if file
if File.exists?(file)
- convert(file)
+ puts convert(file, File.read(file))
exit(0)
else
$stderr.puts "File not found #{file}"
View
@@ -2,12 +2,8 @@
<head>
<title><%= title %></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <style type='text/css'>
- body { font-family: Monaco, 'Lucida Console', monospace; }
- <%= styles %>
- </style>
</head>
- <body class="log">
+ <body>
<div>
<%= body %>
</div>
@@ -2,6 +2,7 @@
require 'ircformat/parser'
require 'ircformat/mirc_code_parser'
require 'ircformat/irssi_code_parser'
+require 'ircformat/inline_styler'
module IrcFormat
class HtmlConvertor
@@ -39,18 +40,35 @@ def to_css_classes(state)
return classes.empty? ? nil : classes.join(' ')
end
+ def to_css_inline_styles(state)
+ classes = to_css_classes(state)
+ return nil if classes.nil?
+ InlineStyler.style(classes)
+ end
+
def escape_html(string)
- string.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;').gsub('\'', '&apos;').gsub('"', '&quot;')
+ # &apos; is deliberately omitted as not all HTML renderers correctly unescape it.
+ string.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;').gsub('"', '&quot;')
end
- def irc_to_html(string)
+ def irc_to_html(string, options = {})
result = ''
+ if options[:inline_styles]
+ result += "<div style=\"#{InlineStyler.body_style}\">\n"
+ end
string.each_line do |line|
IrcFormat::Parser.new(@code_parsers).parse_formatted_string(line.chomp).each do |fragment|
text = self.escape_html(fragment[1])
- classes = self.to_css_classes(fragment[0])
- if classes
- text = "<span class=\"#{classes}\">#{text}</span>"
+ if options[:inline_styles]
+ inline_styles = self.to_css_inline_styles(fragment[0])
+ if inline_styles
+ text = "<span style=\"#{inline_styles}\">#{text}</span>"
+ end
+ else
+ classes = self.to_css_classes(fragment[0])
+ if classes
+ text = "<span class=\"#{classes}\">#{text}</span>"
+ end
end
result += text
end
@@ -61,6 +79,9 @@ def irc_to_html(string)
result += "\n"
end
end
+ if options[:inline_styles]
+ result += "</div>\n"
+ end
result
end
end
@@ -0,0 +1,124 @@
+
+module IrcFormat
+
+ # See http://ethanschoonover.com/solarized
+ SOLARIZED_COLOR_MAPPINGS = {
+ 'nk' => '#073642',
+ 'nr' => '#D30102',
+ 'ng' => '#859900',
+ 'ny' => '#B58900',
+ 'nb' => '#268BD2',
+ 'nm' => '#D33682',
+ 'nc' => '#2AA198',
+ 'nw' => '#EEE8D5',
+ 'ik' => '#002B36',
+ 'ir' => '#CB4B16',
+ 'ig' => '#586E75',
+ 'iy' => '#657B83',
+ 'ib' => '#839496',
+ 'im' => '#6C71C4',
+ 'ic' => '#93A1A1',
+ 'iw' => '#FDF6E3',
+ }
+
+ XTERM_COLOR_MAPPINGS = {
+ 'nk' => '#000000',
+ 'nr' => '#CD0000',
+ 'ng' => '#00CD00',
+ 'ny' => '#CDCD00',
+ 'nb' => '#0000EE',
+ 'nm' => '#CD00CD',
+ 'nc' => '#00CDCD',
+ 'nw' => '#E5E5E5',
+ 'ik' => '#7F7F7F',
+ 'ir' => '#FF0000',
+ 'ig' => '#00FF00',
+ 'iy' => '#FFFF00',
+ 'ib' => '#5C5CFF',
+ 'im' => '#FF00FF',
+ 'ic' => '#00FFFF',
+ 'iw' => '#FFFFFF',
+ }
+
+ COLOR_MAPPINGS = XTERM_COLOR_MAPPINGS
+
+ DEFAULT_FOREGROUND = COLOR_MAPPINGS['nw']
+ DEFAULT_BACKGROUND = COLOR_MAPPINGS['nk']
+
+#elif [ "$1" = "--palette=solarized-xterm" ]; then
+# # Above mapped onto the xterm 256 color palette
+# P0=262626; P1=AF0000; P2=5F8700; P3=AF8700;
+# P4=0087FF; P5=AF005F; P6=00AFAF; P7=E4E4E4;
+# P8=1C1C1C; P9=D75F00; P10=585858; P11=626262;
+# P12=808080; P13=5F5FAF; P14=8A8A8A; P15=FFFFD7;
+# shift;
+#elif [ "$1" = "--palette=tango" ]; then
+# # Gnome default
+# P0=000000; P1=CC0000; P2=4E9A06; P3=C4A000;
+# P4=3465A4; P5=75507B; P6=06989A; P7=D3D7CF;
+# P8=555753; P9=EF2929; P10=8AE234; P11=FCE94F;
+# P12=729FCF; P13=AD7FA8; P14=34E2E2; P15=EEEEEC;
+# shift;
+#else # linux console
+# P0=000000; P1=AA0000; P2=00AA00; P3=AA5500;
+# P4=0000AA; P5=AA00AA; P6=00AAAA; P7=AAAAAA;
+# P8=555555; P9=FF5555; P10=55FF55; P11=FFFF55;
+# P12=5555FF; P13=FF55FF; P14=55FFFF; P15=FFFFFF;
+# [ "$1" = "--palette=linux" ] && shift
+#fi
+
+ class InlineStyler
+
+ # TODO: Configurable colour schemes
+
+ # Generates a css inline style string for the overall content
+ def self.body_style
+ "padding:1em;color:#{DEFAULT_FOREGROUND};background-color:#{DEFAULT_BACKGROUND}"
+ end
+
+ # Converts a css class list string into an inline style string.
+ def self.style(classes)
+ return nil if !classes
+ classes = classes.split(' ')
+ inline_styles = { 'color' => DEFAULT_FOREGROUND, 'background-color' => DEFAULT_BACKGROUND }
+
+ COLOR_MAPPINGS.each_pair do |suffix, color|
+ if classes.delete("f#{suffix}")
+ inline_styles['color'] = color
+ end
+ if classes.delete("b#{suffix}")
+ inline_styles['background-color'] = color
+ end
+ end
+
+ if classes.delete('v')
+ temp = inline_styles['color']
+ inline_styles['color'] = inline_styles['background-color']
+ inline_styles['background-color'] = temp
+ end
+
+ if classes.delete('b')
+ inline_styles['font-weight'] = 'bold'
+ end
+
+ if classes.delete('u')
+ inline_styles['text-decoration'] = 'underline'
+ end
+
+ if classes.delete('m')
+ inline_styles['font-family'] = 'monospace'
+ end
+
+ if classes.delete('f')
+ # find a way to do blinking, if you want it
+ end
+
+ if !classes.empty?
+ raise "Forgot to implement a style mapping for a class: #{classes.inspect}"
+ end
+
+ inline_styles.map{|k,v| "#{k}:#{v}"}.join(';')
+ end
+ end
+end
+

0 comments on commit b4ebbaf

Please sign in to comment.