~ubuntu-branches/ubuntu/saucy/tdiary/saucy-proposed

« back to all changes in this revision

Viewing changes to contrib2/plugin/yahoo_kousei.rb

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2011-04-11 21:53:16 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110411215316-ih4gt4q8p29d2wf8
Tags: 3.0.1-1
* New upstream release (Closes: #542801, #594947)
* debian/control:
 - Bumped up Standards-Version to 3.9.1.
 - Updated version dependency.
* debian/tdiary-setup.rb: Followed the upstream changes, incorporating js and
  index.fcgi

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# yahoo_kousei.rb - 
 
3
#  Yahoo!JAPANデベロッパーネットワークの校正支援APIを利用して、
 
4
#  日本語文の校正作業を支援します。文字の入力ミスや言葉の誤用がないか、
 
5
#  わかりにくい表記や不適切な表現が使われていないかなどをチェックします。
 
6
#
 
7
# Copyright (c) 2010, hb <http://www.smallstyle.com/>
 
8
# You can redistribute it and/or modify it under GPL.
 
9
#
 
10
# 設定:
 
11
#
 
12
# @options['yahoo_kousei.appid'] : アプリケーションID(必須)
 
13
# @options['yahoo_kousei.filter_group'] :
 
14
#  指摘グループの番号をコンマで区切って指定します。
 
15
# @options['yahoo_kousei.no_filter'] :
 
16
#  filter_groupで指定した指摘グループから除外する指摘を指定します。
 
17
#
 
18
# 設定値は http://developer.yahoo.co.jp/webapi/jlp/kousei/v1/kousei.html を参照
 
19
#
 
20
 
 
21
require 'timeout'
 
22
require 'rexml/document'
 
23
require 'net/http'
 
24
Net::HTTP.version_1_2
 
25
 
 
26
def kousei_api( sentence )
 
27
        appid = @conf['yahoo_kousei.appid']
 
28
 
 
29
        query = "appid=#{appid}&sentence=#{URI.encode( sentence.gsub( /\n/, '' ) )}"
 
30
        query << "&filter_group=" + @conf['yahoo_kousei.filter_group'] if @conf['yahoo_kousei.filter_group']
 
31
        query << "&no_filter=" + @conf['yahoo_kousei.no_filter'] if @conf['yahoo_kousei.no_filter']
 
32
 
 
33
        px_host, px_port = (@conf['proxy'] || '').split( /:/ )
 
34
        px_port = 80 if px_host and !px_port
 
35
        
 
36
        xml = ''
 
37
        Net::HTTP::Proxy( px_host, px_port ).start( 'jlp.yahooapis.jp' ) do |http|
 
38
                xml = http.post( '/KouseiService/V1/kousei', query ).body
 
39
        end
 
40
        xml
 
41
end
 
42
 
 
43
def kousei_result( result_set )
 
44
        html = <<-HTML
 
45
<h3>文章校正結果</h3>
 
46
<table>
 
47
<tr><th>対象表記</th><th>候補文字</th><th>詳細情報</th></tr>
 
48
HTML
 
49
 
 
50
        ranges = []
 
51
        doc = REXML::Document::new( result_set )
 
52
        REXML::XPath.each( doc, "//Result" ) do |result|
 
53
                ranges << [REXML::XPath.match( result, "StartPos/text()").to_s, REXML::XPath.match( result, "Length/text()" ).to_s ]
 
54
                surface = REXML::XPath.match( result, "Surface/text()" ).to_s
 
55
                shiteki = REXML::XPath.match( result, "ShitekiWord/text()" ).to_s
 
56
                info = REXML::XPath.match( result, "ShitekiInfo/text()" ).to_s
 
57
                html << %Q|<tr class="plugin_yahoo_search_result_raw"><td>#{surface}</td><td>#{shiteki}</td><td>#{info}</td></tr>|
 
58
        end
 
59
        
 
60
        html << "</table>"
 
61
 
 
62
        ranges.map!{|r| "[" + r.join( "," ) + "]" }
 
63
 
 
64
        script = <<-SQRIPT
 
65
<script type="text/javascript">
 
66
$( function() {
 
67
        var ranges = [
 
68
                #{ranges.join( ", " )}
 
69
        ]
 
70
        $( ".plugin_yahoo_search_result_raw" ).each( function( index ) {
 
71
                $(this).click( function() {
 
72
                        var o = $( "textarea[name='body']" ).get( 0 );
 
73
                        o.focus();
 
74
                        if ( jQuery.browser.msie ) {
 
75
                                var range = document.selection.createRange();
 
76
                                range.collapse();
 
77
                                range.moveStart( "character", ranges[index][0] );
 
78
                                range.moveEnd( "character", ranges[index][1] );
 
79
                                range.select();
 
80
                        } else {
 
81
                                o.setSelectionRange( ranges[index][0] , ranges[index][0] + ranges[index][1] );
 
82
                        }
 
83
                } );
 
84
        } );
 
85
} );
 
86
</script>
 
87
SQRIPT
 
88
        html << script
 
89
end
 
90
 
 
91
add_edit_proc do
 
92
        if @mode == 'preview' && @conf['yahoo_kousei.appid'] then
 
93
                xml = kousei_api( @cgi.params['body'][0] )
 
94
                <<-HTML
 
95
<div id="plugin_yahoo_kousei" class="section">
 
96
#{kousei_result( xml )}
 
97
<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet -->
 
98
<span style="margin:15px 15px 15px 15px"><a href="http://developer.yahoo.co.jp/about">Webサービス by Yahoo! JAPAN</a></span>
 
99
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->
 
100
</div>
 
101
                HTML
 
102
        end
 
103
end
 
104