From 32bd623563e16ca2c35c656f84045c3ed266e311 Mon Sep 17 00:00:00 2001 From: Thomas Hochstein Date: Sat, 29 Apr 2017 01:29:57 +0200 Subject: [PATCH] Add DeJureIntegrator filter. Signed-off-by: Thomas Hochstein --- README.md | 13 +++++- lib/filters/dejure.rb | 104 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 lib/filters/dejure.rb diff --git a/README.md b/README.md index c33e249..44c3263 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ *[nanoc](http://nanoc.ws/)* is a popular *static site generator*. -I'll add some little extensions for *nanoc* here. +I'll add some little extensions for *nanoc* (v3) here. ## filters @@ -34,3 +34,14 @@ from being rendered and compiled, i.e. like this: route %r{/_} do nil end + +### dejure.rb + +*nanoc* filter implementation of the *dejure.org* +[legal integration service](https://dejure.org/vernetzung.html). + + filter :dejure, + format: 'weit', + buzer: 1, + target: '_blank', + class: 'dejure' diff --git a/lib/filters/dejure.rb b/lib/filters/dejure.rb new file mode 100644 index 0000000..940132c --- /dev/null +++ b/lib/filters/dejure.rb @@ -0,0 +1,104 @@ +# dejure.rb +# a nanoc filter for dejure.org legal integration service +# see +# +# adapted from PHP reference implementation +# at +# +# (c) 2017 Thomas Hochstein +# +# 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: +# params[:format] -> [weit|schmal] (weit) +# params[:target] -> target for ('') +# params[:class] -> CSS class for ('') +# params[:buzer] -> fallback to buzer.de? (1) + +require 'net/http' + +module Nanoc::Filters + class DeJureIntegrator < Nanoc::Filter + identifier :dejure + type :text + + def set_params (params) + # set default params + params[:version] = '0.1' + 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 + + def run(content, params={}) + if !(/ยง|§|Art\.|\/[0-9][0-9](?![0-9\/])| [0-9][0-9]?[\/\.][0-9][0-9](?![0-9\.])|[0-9][0-9], / =~ content) + # nothing to replace + return content + else + params = set_params(params) + return DeJureIntegrator(content.strip, params) + end + end + + def DeJureIntegrator (text, params={}) + prot = 'http://' + host = 'rechtsnetz.dejure.org' + path = '/dienste/vernetzung/vernetzen' + uri = URI(prot + host + path) + + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Post.new(uri.request_uri) + request['User-Agent'] = params[:Anbieterkennung] + ' (DeJureIntegrator for nanoc ruby-' + params[:version] + ')' + request['Content-Type'] = 'application/x-www-form-urlencoded' + + formdata = params + formdata['Originaltext'] = text + request.set_form_data(formdata) + + response = http.request(request) + + if (response.code != '200') || response.body.nil? || (text.length > response.body.length) + # HTTP error, empty body or response body smaller than original text + printf("DeJureIntegrator HTTP error: %s\n", response.code) + return text + else + return IntegrityCheck(text,response.body.force_encoding('UTF-8')) + end + end + + def IntegrityCheck (input,output) + # compare input and output text after removing all added links - texts should match! + regexp = /