Skip to content

Commit

Permalink
Import
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jul 7, 2017
0 parents commit 030df22
Show file tree
Hide file tree
Showing 12 changed files with 847 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/doc/reference/
/.yardoc/
/pkg/
/Gemfile.lock
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
notifications:
email:
recipients:
- groonga-commit@lists.osdn.me
rvm:
- 2.2
- 2.3.3
- 2.4.1
5 changes: 5 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--output-dir doc/reference/en
--markup markdown
--markup-provider kramdown
-
doc/text/*
27 changes: 27 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- ruby -*-
#
# Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

source "https://rubygems.org/"

gemspec

base_dir = File.dirname(__FILE__)
local_chupa_text_dir = File.join(base_dir, "..", "chupa-text")
if File.exist?(local_chupa_text_dir)
gem "chupa-text", :path => local_chupa_text_dir
end
502 changes: 502 additions & 0 deletions LICENSE.txt

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# README

## Name

chupa-text-decomposer-webkit

## Description

This is a ChupaText decomposer plugin for to extract text and
meta-data from WebKitGTK+.

You can use `webkit` decomposer.

## Install

Install chupa-text-decomposer-webkit gem:

```
% gem install chupa-text-decomposer-webkit
```

Now, you can extract text and meta-data from WebKitGTK+:

```
% chupa-text https://webkitgtk.org/
```

## Author

* Kouhei Sutou `<kou@clear-code.com>`

## License

LGPL 2.1 or later.

(Kouhei Sutou has a right to change the license including contributed
patches.)
48 changes: 48 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- ruby -*-
#
# Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

task :default => :test

require "pathname"

require "rubygems"
require "bundler/gem_helper"
require "packnga"

base_dir = Pathname(__FILE__).dirname

helper = Bundler::GemHelper.new(base_dir.to_s)
def helper.version_tag
version
end

helper.install
spec = helper.gemspec

Packnga::DocumentTask.new(spec) do |task|
task.original_language = "en"
task.translate_language = "ja"
end

Packnga::ReleaseTask.new(spec) do
end

desc "Run tests"
task :test do
ruby("test/run-test.rb")
end
50 changes: 50 additions & 0 deletions chupa-text-decomposer-webkit.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- ruby -*-
#
# Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

clean_white_space = lambda do |entry|
entry.gsub(/(\A\n+|\n+\z)/, '') + "\n"
end

Gem::Specification.new do |spec|
spec.name = "chupa-text-decomposer-webkit"
spec.version = "1.0.0"
spec.homepage = "https://github.com/ranguba/chupa-text-decomposer-webkit"
spec.authors = ["Kouhei Sutou"]
spec.email = ["kou@clear-code.com"]
readme = File.read("README.md", :encoding => "UTF-8")
entries = readme.split(/^\#\#\s(.*)$/)
description = clean_white_space.call(entries[entries.index("Description") + 1])
spec.summary = description.split(/\n\n+/, 2).first
spec.description = description
spec.license = "LGPL-2.1+"
spec.files = ["#{spec.name}.gemspec"]
spec.files += ["README.md", "LICENSE.txt", "Rakefile", "Gemfile"]
spec.files += [".yardopts"]
spec.files += Dir.glob("lib/**/*.rb")
spec.files += Dir.glob("doc/text/*")
spec.files += Dir.glob("test/**/*")

spec.add_runtime_dependency("chupa-text", ">= 1.0.7")
spec.add_runtime_dependency("webkit2-gtk")

spec.add_development_dependency("bundler")
spec.add_development_dependency("rake")
spec.add_development_dependency("test-unit")
spec.add_development_dependency("packnga")
spec.add_development_dependency("kramdown")
end
5 changes: 5 additions & 0 deletions doc/text/news.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# News

## 1.0.0: 2017-07-07

The first release!!!
96 changes: 96 additions & 0 deletions lib/chupa-text/decomposers/webkit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

require "webkit2-gtk"

module ChupaText
module Decomposers
class WebKit < Decomposer
registry.register("webkit", self)

TARGET_EXTENSIONS = ["htm", "html", "xhtml"]
TARGET_MIME_TYPES = [
"text/html",
"application/xhtml+xml",
]
def target?(data)
return false unless data.need_screenshot?
return false if data.screenshot

source = data.source
return false if source.nil?

return true if TARGET_EXTENSIONS.include?(source.extension)
return true if TARGET_MIME_TYPES.include?(source.mime_type)

false
end

def decompose(data)
data.screenshot = create_screenshot(data.source)
yield(data)
end

private
def create_screenshot(data)
screenshot = nil

screenshot_width, screenshot_height = data.expected_screenshot_size

main_context = GLib::MainContext.default
view = WebKit2Gtk::WebView.new
view.load_uri(data.uri.to_s)
finished = false
view.signal_connect("load-changed") do |_, load_event|
case load_event
when WebKit2Gtk::LoadEvent::FINISHED
view.get_snapshot(:full_document, :none) do |_, result|
finished = true
snapshot_surface = view.get_snapshot_finish(result)
screenshot_surface = Cairo::ImageSurface.new(:argb32,
screenshot_width,
screenshot_height)
context = Cairo::Context.new(screenshot_surface)
context.set_source_color(:white)
context.paint
ratio = screenshot_width.to_f / snapshot_surface.width
context.scale(ratio, ratio)
context.set_source(snapshot_surface)
context.paint
png = StringIO.new
screenshot_surface.write_to_png(png)
screenshot = Screenshot.new("image/png",
[png.string].pack("m*"),
"base64")
end
end
end
view.signal_connect("load-failed") do |_, _, failed_uri, error|
finished = true
message = "failed to load URI: #{failed_uri}: "
message << "#{error.class}(#{error.code}): #{error.message}"
puts(message)
true
end
until finished
main_context.iteration(true)
end

screenshot
end
end
end
end
29 changes: 29 additions & 0 deletions test/run-test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
#
# Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

$VERBOSE = true

require "bundler/setup"

require "test-unit"

require "chupa-text"

ChupaText::Decomposers.load

exit(Test::Unit::AutoRunner.run(true))
36 changes: 36 additions & 0 deletions test/test-webkit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2017 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

require "pathname"
require "gdk_pixbuf2"

class TestWebKit < Test::Unit::TestCase
def setup
@options = {}
end

private
def decomposer
ChupaText::Decomposers::WebKit.new(@options)
end

def fixture_path(*components)
base_path = Pathname(__FILE__).dirname + "fixture"
base_path.join(*components)
end

# TODO
end

0 comments on commit 030df22

Please sign in to comment.