Kouhei Sutou
kous****@users*****
Tue May 23 17:40:32 JST 2006
Index: kazehakase/data/ext/ruby/kz/actions/Makefile.am diff -u kazehakase/data/ext/ruby/kz/actions/Makefile.am:1.4 kazehakase/data/ext/ruby/kz/actions/Makefile.am:1.5 --- kazehakase/data/ext/ruby/kz/actions/Makefile.am:1.4 Fri May 19 18:07:48 2006 +++ kazehakase/data/ext/ruby/kz/actions/Makefile.am Tue May 23 17:40:32 2006 @@ -8,6 +8,7 @@ reload-without-fragment.rb \ ruby-dialog.rb \ view-pdf-as-image.rb \ + view-pdf-as-image.erb \ text-link.rb EXTRA_DIST = \ Index: kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.erb diff -u /dev/null kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.erb:1.1 --- /dev/null Tue May 23 17:40:32 2006 +++ kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.erb Tue May 23 17:40:32 2006 @@ -0,0 +1,105 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html + PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +<meta http-equiv="Content-Style-Type" content="text/css" /> +<style type="text/css"> +div.navi +{ + text-align: center; +} + +div.image, +div.thumbnails +{ + margin-top: 1em; + margin-bottom: 1em; + margin-left: auto; + margin-right: auto; +} + +div.image +{ + text-align: center; +} + +div.thumbnails +{ + width: 90%; +} + +div.image img +{ + border-style: none; +} +</style> +<script type="text/javascript"> +function handle_key(e) +{ + var key_code = e.which || e.keyCode; + var handled = true; + switch (key_code) { + +<% unless first_image? -%> +<% MOVE_TO_FIRST_KEYS.each do |key| -%> + case <%= key %>: +<% end -%> + location = "<%= first_href %>"; + break; + +<% MOVE_TO_PREVIOUS_KEYS.each do |key| -%> + case <%= key %>: +<% end -%> + location = "<%= previous_href %>"; + break; +<% end -%> + +<% unless last_image? -%> +<% MOVE_TO_NEXT_KEYS.each do |key| -%> + case <%= key %>: +<% end -%> + location = "<%= next_href %>"; + break; + +<% MOVE_TO_LAST_KEYS.each do |key| -%> + case <%= key %>: +<% end -%> + location = "<%= last_href %>"; + break; +<% end -%> + +<% TOGGLE_INDEX_MODE_KEYS.each do |key| -%> + case <%= key %>: +<% end -%> + location = "<%= toggle_mode_href %>"; + break; + + default: + handled = false; + break; + } + + return !handled; +} +</script> +<title><%=h title %></title> +</head> +<body onkeypress="handle_key(event);"> +<%= toggle_mode_navi %> +<%= navi %> +<%= content %> +<%= navi %> +<%= toggle_mode_navi %> +<hr /> +<div class="footer"> +<p> +Generated by +<a href="<%= Kz::VERSION %>"><%=h _("Kazehakase")%></a> +version <%= Kz::VERSION %> +</p> +</div> +</body> +</html> Index: kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb diff -u kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb:1.3 kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb:1.4 --- kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb:1.3 Tue May 23 13:00:36 2006 +++ kazehakase/data/ext/ruby/kz/actions/view-pdf-as-image.rb Tue May 23 17:40:32 2006 @@ -6,26 +6,34 @@ rescue LoadError end -def h(str) - ERB::Util.h(str) -end +module Util + def image_type + "png" + end -def image_type - "png" -end + def image_suffix + "png" + end -def image_suffix - "png" -end + def image_name(i) + "#{i}.#{image_suffix}" + end -def image_name(i) - "#{i}.#{image_suffix}" -end + def thumbnail_name(i) + "#{i}-thumb.#{image_suffix}" + end -def thumbnail_name(i) - "#{i}-thumb.#{image_suffix}" + def html_name(i) + "#{i}.html" + end + + def index_html + "index.html" + end end +extend Util + def ruby_poppler_is_unavailable(action, kz) statusbar_timeout = 3 * 1000 statusbar_id = kz.statusbar.get_context_id(action.name) @@ -86,6 +94,257 @@ end end +class HTMLGenerator + include Util + include ERB::Util + public :h + + MOVE_TO_NEXT_KEYS = [ + Gdk::Keyval::GDK_n, + Gdk::Keyval::GDK_f, + Gdk::Keyval::GDK_j, + Gdk::Keyval::GDK_l, + Gdk::Keyval::GDK_Page_Down, + Gdk::Keyval::GDK_Tab, + Gdk::Keyval::GDK_Return, + Gdk::Keyval::GDK_rightarrow, + Gdk::Keyval::GDK_downarrow, + Gdk::Keyval::GDK_space, + Gdk::Keyval::GDK_plus, + Gdk::Keyval::GDK_KP_Add, + Gdk::Keyval::GDK_KP_Page_Down, + Gdk::Keyval::GDK_KP_Enter, + Gdk::Keyval::GDK_KP_Tab, + ] + + MOVE_TO_PREVIOUS_KEYS = [ + Gdk::Keyval::GDK_p, + Gdk::Keyval::GDK_b, + Gdk::Keyval::GDK_h, + Gdk::Keyval::GDK_k, + Gdk::Keyval::GDK_Page_Up, + Gdk::Keyval::GDK_leftarrow, + Gdk::Keyval::GDK_uparrow, + Gdk::Keyval::GDK_BackSpace, + Gdk::Keyval::GDK_Delete, + Gdk::Keyval::GDK_minus, + Gdk::Keyval::GDK_KP_Subtract, + Gdk::Keyval::GDK_KP_Page_Up, + Gdk::Keyval::GDK_KP_Delete, + ] + + MOVE_TO_FIRST_KEYS = [ + Gdk::Keyval::GDK_a, + Gdk::Keyval::GDK_Home, + Gdk::Keyval::GDK_KP_Home, + Gdk::Keyval::GDK_less, + ] + + MOVE_TO_LAST_KEYS = [ + Gdk::Keyval::GDK_e, + Gdk::Keyval::GDK_End, + Gdk::Keyval::GDK_KP_End, + Gdk::Keyval::GDK_greater, + Gdk::Keyval::GDK_dollar, + ] + + TOGGLE_INDEX_MODE_KEYS = [ + Gdk::Keyval::GDK_i, + ] + + template_path = Kz::Actions.search_file("view-pdf-as-image.erb") + erb = File.open(template_path) {|f| ERB.new(f.read, nil, "-")} + erb.def_method(self, "to_html", template_path) + + def initialize(doc, base_dir) + @doc = doc + @base_dir = base_dir + @max =****@doc*****_pages + end + + def save + index_html_file = File.join(@base_dir, index_html) + @image_number = 0 + File.open(index_html_file, "w") do |index| + @index_mode = true + index << to_html + end + + @index_mode = false + @doc.n_pages.times do |i| + path = File.join(@base_dir, html_name(i)) + @image_number = i + File.open(path, "w") do |image| + image << to_html + end + end + end + + private + def _(msgid) + Kz.gettext(msgid) + end + + def title + @doc.title + end + + def content + if @index_mode + result = "<div class=\"thumbnails\">\n" + @doc.n_pages.times do |i| + result << "<a href=\"#{h html_name(i)}\">\n" + result << "<img src=\"#{h thumbnail_name(i)}\">\n" + result << "</a>\n" + end + result << "</div>" + result + else + "<div class=\"image\">\n#{image}\n</div>" + end + end + + def image_title(image_number=nil) + image_number ||= @image_number + "#{h title} (#{image_number}/#{@max - 1})" + end + + def image_src(image_number=nil) + image_number ||= @image_number + u(image_name(image_number)) + end + + def image(image_number=nil) + image_number ||= @image_number + src = image_src(image_number) + img = "<img title=\"#{image_title(image_number)}\" src=\"#{src}\" />" + if last_image?(image_number) + img + else + href = next_href(image_number) + "<a href=\"#{href}\">\n#{img}\n</a>" + end + end + + def index_mode? + @index_mode + end + + def first_image?(image_number=nil) + image_number ||= @image_number + image_number.zero? + end + + def last_image?(image_number=nil) + image_number ||= @image_number + @max.zero? or image_number == @max - 1 + end + + def first_index(image_number=nil) + 0 + end + + def previous_index(image_number=nil) + image_number ||= @image_number + image_number - 1 + end + + def next_index(image_number=nil) + image_number ||= @image_number + image_number + 1 + end + + def last_index(image_number=nil) + @max - 1 + end + + def first_href(image_number=nil) + href(first_index(image_number)) + end + + def previous_href(image_number=nil) + href(previous_index(image_number)) + end + + def next_href(image_number=nil) + href(next_index(image_number)) + end + + def last_href(image_number=nil) + href(last_index(image_number)) + end + + def toggle_mode_href + if index_mode? + first_href + else + index_html + end + end + + def href(image_number) + image_number ||= @image_number + u(html_name(image_number)) + end + + def a_link(_href, label, label_only) + if label_only + result = label + else + result = "<a href=\"#{_href}\">#{label}</a>" + end + "[#{result}]" + end + + def first_link(image_number=nil) + a_link(first_href(image_number), h("<<"), first_image?(image_number)) + end + + def previous_link(image_number=nil) + a_link(previous_href(image_number), h("<"), first_image?(image_number)) + end + + def next_link(image_number=nil) + a_link(next_href(image_number), h(">"), last_image?(image_number)) + end + + def last_link(image_number=nil) + a_link(last_href(image_number), h(">>"), last_image?(image_number)) + end + + def toggle_mode_href + if index_mode? + first_href + else + index_html + end + end + + def toggle_mode_navi + @index_mode = !@index_mode + if index_mode? + result = a_link(index_html, h(_("Thumbnails")), false) + else + result = a_link(first_href, h(_("Image")), false) + end + "<div class=\"toggle-mode\">\n#{result}\n</div>" + ensure + @index_mode = !@index_mode + end + + def navi(image_number=nil) + return '' if index_mode? + result = '' + result << "<div class=\"navi\">\n" + result << first_link(image_number) + result << previous_link(image_number) + result << next_link(image_number) + result << last_link(image_number) + result << "\n</div>" + result + end +end + def completed(kz, tmp_dir, filename) doc = Poppler::Document.new("file://#{filename}") pages = [] @@ -93,16 +352,8 @@ pages.push([page, i]) end - html_file = File.join(tmp_dir, "index.html") - File.open(html_file, "w") do |f| - f << "<html><head><title>#{h doc.title}</title></head><body>\n" - doc.n_pages.times do |i| - f << "<a href=\"#{h image_name(i)}\">\n" - f << "<img src=\"#{h thumbnail_name(i)}\">\n" - f << "</a>\n" - end - f << "\n</body></html>" - end + HTMLGenerator.new(doc, tmp_dir).save + html_file = File.join(tmp_dir, index_html) index_uri = "file://#{html_file}" tab = kz.open_new_tab(index_uri) make_images(kz, pages, tmp_dir, tab, index_uri)