2017-04-29 01:29:57 +02:00
|
|
|
# dejure.rb
|
2017-04-30 16:14:06 +02:00
|
|
|
# A nanoc filter for the dejure.org legal integration service
|
|
|
|
# See <https://dejure.org/vernetzung.html>
|
2017-04-29 01:29:57 +02:00
|
|
|
#
|
2017-04-30 16:14:06 +02:00
|
|
|
# Adapted from the PHP reference implementation
|
2017-04-29 01:29:57 +02:00
|
|
|
# at <https://dejure.org/vernetzung/vernetzungsfunktion.zip>
|
|
|
|
#
|
|
|
|
# (c) 2017 Thomas Hochstein <thh@inter.net>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# params may be set as follows:
|
2017-05-25 12:43:08 +02:00
|
|
|
# params[:format] -> [weit|schmal] (weit)
|
|
|
|
# params[:buzer] -> fallback to buzer.de? (1)
|
|
|
|
# params[:target] -> target for <a href> ('')
|
|
|
|
# params[:class] -> CSS class for <a href> ('')
|
|
|
|
# params[:cache_days] -> cache validity in days (7)
|
2017-04-29 01:29:57 +02:00
|
|
|
|
|
|
|
require 'net/http'
|
2017-04-29 21:18:24 +02:00
|
|
|
require 'digest'
|
2017-04-29 01:29:57 +02:00
|
|
|
|
|
|
|
module Nanoc::Filters
|
2017-05-25 12:42:31 +02:00
|
|
|
class DejureAutolinker < Nanoc::Filter
|
2017-04-29 01:29:57 +02:00
|
|
|
identifier :dejure
|
|
|
|
type :text
|
|
|
|
|
2017-05-25 14:31:58 +02:00
|
|
|
VERSION = '1.0'
|
2017-04-29 21:18:24 +02:00
|
|
|
CACHEDIR = 'tmp/dejure-org'
|
2017-04-30 16:14:06 +02:00
|
|
|
CACHEDAYS = 7
|
2017-04-29 21:18:24 +02:00
|
|
|
|
|
|
|
def run(input, params={})
|
|
|
|
if !(/§|§|Art\.|\/[0-9][0-9](?![0-9\/])| [0-9][0-9]?[\/\.][0-9][0-9](?![0-9\.])|[0-9][0-9], / =~ input)
|
|
|
|
# nothing to replace
|
|
|
|
return input
|
|
|
|
end
|
2017-05-25 13:52:13 +02:00
|
|
|
# return if input contains '<!-- no-dejure -->'
|
|
|
|
return input if (/<!-- ?no-?dejure ?-->/ =~ input)
|
2017-05-25 12:43:08 +02:00
|
|
|
# set cache validity in days from params or set a default
|
|
|
|
cache_days = params.delete(:cache_days)
|
|
|
|
if cache_days.nil?
|
|
|
|
cache_days = CACHEDAYS
|
|
|
|
end
|
2017-04-29 21:18:24 +02:00
|
|
|
# return output if it's already cached
|
2017-05-25 12:43:08 +02:00
|
|
|
if !(output = cache_read(input.strip,cache_days))
|
2017-04-29 21:18:24 +02:00
|
|
|
# purge cache if a purge is due
|
2017-05-25 12:43:08 +02:00
|
|
|
puts "DejureAutolinker cache purged!\n" if cache_purge(cache_days)
|
2017-04-29 21:18:24 +02:00
|
|
|
# call out to dejure.org
|
|
|
|
output = call_dejure(input.strip, set_params(params))
|
|
|
|
end
|
|
|
|
# do an integrity check
|
|
|
|
return integrity_check(input,output)
|
|
|
|
end
|
|
|
|
|
2017-04-29 01:29:57 +02:00
|
|
|
def set_params (params)
|
|
|
|
# set default params
|
2017-04-29 21:18:24 +02:00
|
|
|
params[:version] = VERSION
|
2017-04-29 01:29:57 +02:00
|
|
|
if !@config[:base_url].nil?
|
|
|
|
params[:Anbieterkennung] = @config[:base_url]
|
|
|
|
else
|
|
|
|
params[:Anbieterkennung] = 'http://unknown.nanoc.installation.invalid'
|
|
|
|
end
|
|
|
|
params[:format] ||= 'weit'
|
|
|
|
params[:buzer] ||= 1
|
|
|
|
return params
|
|
|
|
end
|
|
|
|
|
2017-04-29 21:18:24 +02:00
|
|
|
def call_dejure (input, params={})
|
2017-05-25 13:30:29 +02:00
|
|
|
prot = 'https://'
|
2017-04-29 01:29:57 +02:00
|
|
|
host = 'rechtsnetz.dejure.org'
|
|
|
|
path = '/dienste/vernetzung/vernetzen'
|
|
|
|
uri = URI(prot + host + path)
|
|
|
|
|
2017-05-25 13:30:29 +02:00
|
|
|
http = Net::HTTP.new(uri.host)
|
2017-04-29 01:29:57 +02:00
|
|
|
request = Net::HTTP::Post.new(uri.request_uri)
|
2017-05-25 12:42:31 +02:00
|
|
|
request['User-Agent'] = params[:Anbieterkennung] + ' (DejureAutolinker for nanoc ruby-' + params[:version] + ')'
|
2017-04-29 01:29:57 +02:00
|
|
|
request['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
|
|
|
|
|
|
formdata = params
|
2017-04-29 21:18:24 +02:00
|
|
|
formdata['Originaltext'] = input
|
2017-04-29 01:29:57 +02:00
|
|
|
request.set_form_data(formdata)
|
|
|
|
|
|
|
|
response = http.request(request)
|
|
|
|
|
2017-04-29 21:18:24 +02:00
|
|
|
if (response.code != '200') || response.body.nil? || (input.length > response.body.length)
|
2017-04-29 01:29:57 +02:00
|
|
|
# HTTP error, empty body or response body smaller than original text
|
2017-05-25 12:42:31 +02:00
|
|
|
printf("DejureAutolinker HTTP error: %s\n", response.code)
|
2017-04-29 21:18:24 +02:00
|
|
|
return input
|
2017-04-29 01:29:57 +02:00
|
|
|
else
|
2017-04-29 21:18:24 +02:00
|
|
|
output = response.body.force_encoding('UTF-8').strip
|
|
|
|
# write cache
|
|
|
|
cache_write(input,output)
|
|
|
|
return output
|
2017-04-29 01:29:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-29 21:18:24 +02:00
|
|
|
def cache_filename (input)
|
|
|
|
# filename is created from length and MD5 of input;
|
|
|
|
return input.length.to_s + Digest::MD5.hexdigest(input)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_age (cache_days)
|
|
|
|
return (Time.now.to_i - cache_days*86400)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_write (input,output,cache_dir=CACHEDIR)
|
|
|
|
# create cache_dir, if necessary
|
2017-04-30 20:27:01 +02:00
|
|
|
FileUtils.mkdir_p(cache_dir) if !File.exist?(cache_dir)
|
2017-04-29 21:18:24 +02:00
|
|
|
# write output to cache file
|
|
|
|
cache_file = cache_dir + '/' + cache_filename(input)
|
|
|
|
if File.directory?(cache_dir)
|
|
|
|
File.open(cache_file, 'w') do |f|
|
2017-05-25 13:44:03 +02:00
|
|
|
# if input is unchanged, just save '<!-- idem -->'
|
|
|
|
# instead of the whole (unchanged) text to save space
|
|
|
|
if input == output
|
|
|
|
f.write('<!-- idem -->')
|
|
|
|
else
|
|
|
|
f.write(output)
|
|
|
|
end
|
2017-04-29 21:18:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_read (input,cache_days=CACHEDAYS,cache_dir=CACHEDIR)
|
|
|
|
cache_file = cache_dir + '/' + cache_filename(input)
|
|
|
|
# file exists and is younger than cache_days?
|
|
|
|
if File.exist?(cache_file) && File.mtime(cache_file).to_i > cache_age(cache_days)
|
2017-05-25 13:44:03 +02:00
|
|
|
output = File.read(cache_file)
|
|
|
|
# if just '<!-- idem -->' is cached, return input unchanged
|
|
|
|
if output == '<!-- idem -->'
|
|
|
|
return input
|
|
|
|
else
|
|
|
|
return output
|
|
|
|
end
|
2017-04-29 21:18:24 +02:00
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_purge (cache_days=CACHEDAYS,cache_dir=CACHEDIR)
|
|
|
|
# cache_dir is not a directory?
|
|
|
|
return false if !File.directory?(cache_dir)
|
|
|
|
lastpurge = File.read(cache_dir + '/lastpurge') if File.exist?(cache_dir + '/lastpurge')
|
|
|
|
# already purged in the last cache_days days?
|
|
|
|
return false if lastpurge && lastpurge.to_i > cache_age(cache_days)
|
|
|
|
# delete all files in cache_dir older than cache_days
|
|
|
|
Pathname.new(cache_dir).children.each do |f|
|
|
|
|
f.unlink if File.mtime(f).to_i < cache_age(cache_days)
|
|
|
|
end
|
|
|
|
# save the time of the purge
|
|
|
|
File.open(cache_dir + '/lastpurge', 'w') do |f|
|
|
|
|
f.write(Time.now.to_i)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def integrity_check (input,output)
|
|
|
|
# compare input and output text after removing all added links - texts should match!
|
|
|
|
regexp = /<a href="http:\/\/dejure.org\/[^>]*>([^<]*)<\/a>/i
|
|
|
|
if input.strip.gsub(regexp, '\\1') == output.strip.gsub(regexp, '\\1')
|
|
|
|
return output
|
|
|
|
else
|
|
|
|
# texts don't match
|
2017-05-25 12:42:31 +02:00
|
|
|
puts "DejureAutolinker integrity error!\n"
|
2017-04-29 21:18:24 +02:00
|
|
|
return input
|
|
|
|
end
|
2017-04-29 01:29:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2017-04-30 16:14:06 +02:00
|
|
|
end
|