class ImagesToScrapbox::Converter

Your code goes here…

Attributes

image_name[R]
image_path[R]
image_url[R]
page_title[R]

Public Class Methods

add(globn) click to toggle source
# File lib/images_to_scrapbox.rb, line 35
def Converter.add(globn)
  Dir.glob(globn).each do |g|
    next unless g =~ /\.(gif|jpe?g|png)$/i
    @@converters.push Converter.new(g)
  end
end
new(path) click to toggle source
# File lib/images_to_scrapbox.rb, line 95
def initialize(path)
  @image_name=path
  @image_path=File.expand_path(path)
end
perform(do_convert=true, options) click to toggle source
# File lib/images_to_scrapbox.rb, line 42
def Converter.perform(do_convert=true, options)

  if options[:unique]
    @@converters.uniq!{ |e| e.image_name }
  end

  case options[:sort]
  when "none"
  when "names"
    @@converters.sort_by!{ |e| e.image_name }
  when "numbers"
    @@converters.sort_by!{ |e|
      File.basename(e.image_name, File.extname(e.image_name)).
        match(%r!(\d+)\Z!).to_a[1].to_i }
  else
    raise Thor::Error, "Unknown sort type: #{options[:sort]}"
  end

  unless options[:ascending]
    @@converters.reverse!
  end

  unless do_convert
    @@converters.each do |c|
      puts c.image_name
    end
    return
  end

  @@converted_on="Converted on #{Time.now.to_s}".gsub!(%r!\s+!,'_')

  pages=[]
  @@converters.map do |converter|
    pages.concat converter.start(options)
  end

  if options[:toc]
    pages.concat Converter.toc_page
  end

  if options[:whole]
    pages.concat Converter.whole_page(options)
  end

  result={
    "pages": pages
  }

  puts result.to_json

end
toc_page() click to toggle source
# File lib/images_to_scrapbox.rb, line 100
def Converter.toc_page()
  tocpage=SbPage.new()
  tocpage.set_page_title(@@converted_on)
  @@converters.map do |c|
    tocpage.push_text("["+c.page_title+"]")
  end
  tocpage.get_page
end
whole_page(options) click to toggle source
# File lib/images_to_scrapbox.rb, line 109
def Converter.whole_page(options)
  wholepage=SbPage.new()
  wholepage.set_page_title("Whole_images_"+@@converted_on)
  @@converters.map do |c|
    wholepage.push_text("[[["+c.page_title+"]]]")
    unless c.image_url.empty?
      wholepage.push_text( options[:larger] ? "[["+c.image_url+"]]" : "["+c.image_url+"]")
      wholepage.push_empty_text()
    end
    wholepage.push_empty_text()
  end
  wholepage.get_page
end

Public Instance Methods

start(options) click to toggle source
# File lib/images_to_scrapbox.rb, line 123
def start(options)

  timestamp=""
  case options[:timestamp]
  when "atime"
    timestamp=File.atime(@image_path).to_s
  when "ctime"
    timestamp=File.ctime(@image_path).to_s
  when "mtime"
    timestamp=File.mtime(@image_path).to_s
  else
    raise Thor::Error, "Unknown timestamp type: #{timestamp_kind}"
  end
  @page_title=(@image_name + " " + timestamp).gsub!(%r!\s+!, '_')
  @page_title.sub!(%r!\A/!, '_/')

  imagepage=SbPage.new()
  imagepage.set_page_title(@page_title)
  imagepage.push_text(@image_name)
  imagepage.push_empty_text()

  @image_url=""
  if options[:image]
    r=register_image(@image_path)
    @image_url=r["url"]
    imagepage.push_text( options[:larger] ? "[["+@image_url+"]]" : "["+@image_url+"]")
  end

  imagepage.push_text("["+@@converted_on+"]")
  imagepage.get_page
end