<commit_before>#!/usr/bin/env ruby
#
# State Based Session Management	
#	Copyright (C) 2004 <NAME>
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#	ywesee - intellectual capital connected, Winterthurerstrasse 52, CH-8006 Zrich, Switzerland
#	<EMAIL>
#
# Lookandfeel -- sbsm -- <EMAIL>

require 'sbsm/time'
require 'cgi'

module SBSM
  class Lookandfeel
    attr_reader :language, :flavor, :session
    DICTIONARIES = {}
		ENABLED = []
		HTML_ATTRIBUTES = {}
		RESOURCES = {}
		RESOURCE_BASE = "/resources"
		TXT_RESOURCES = "/resources"
		class << self
			def txt_file(filename)
				Proc.new {
					path = File.expand_path(filename, self::TXT_RESOURCES)
					File.read(path).strip.gsub(/(\r\n)|(\n)|(\r)/, '<br>')
				}
			end
		end
		def initialize(session)
      @session = session
      @flavor = @session.flavor
      @language = @session.language
      set_dictionary(@language)
		end
		def attributes(key)
			self::class::HTML_ATTRIBUTES.fetch(key, {}).dup
		end
		def base_url
			[@session.http_protocol + ':/', @session.server_name, @language, @flavor].compact.join("/")
		end
		def direct_event
			@session.direct_event
		end
		def enabled?(event, default=true)
			default || self::class::ENABLED.include?(event)
		end
		def event_url(event=direct_event, args={})
			_event_url(event, args) { |args|
				unless(@session.is_crawler? || args.include?('state_id'))
					args.unshift(:state_id, @session.state.object_id)
				end
			}
		end
		def _event_url(event=direct_event, args={}, &block)
			args = args.collect { |*pair| pair }.flatten
			args = args.collect { |value| CGI.escape(value.to_s) }
			if(block_given?)
				yield(args)
			end
			[base_url(), event, args].compact.join('/')
		end
		def languages
			@languages ||= self::class::DICTIONARIES.keys.sort
		end
		def language_url(language)
			[@session.http_protocol + ':/', @session.server_name, language, @flavor].compact.join("/")
		end
		def lookup(key, *args)
			key = key.intern if key.is_a? String
			if(args.size > 0)
				result = ""
				args.each_with_index { |text, index|
					result << (lookup(key.to_s + index.to_s) || ' ')
					result << text.to_s
				}
				tail = lookup(key.to_s + args.size.to_s)
				result << tail if(tail)
			else
				result = @dictionary[key] if @dictionary
				if(result.is_a? Proc)
					result.call
				elsif(result.nil?)
					def_lan = @session.default_language
					if(dict = self::class::DICTIONARIES[def_lan])
						dict[key]
					end
				else
					result
				end
			end
		end
		def format_date(date)
			date.strftime(lookup(:date_format))
		end
		def format_price(price, currency=nil)
			if(price.to_i > 0)
				[currency, sprintf('%.2f', price.to_f/100.0)].compact.join(' ')
			end
		end
		def navigation(filter=false)
			@session.navigation
		end
		def resource(rname, rstr=nil)
			collect_resource([self::class::RESOURCE_BASE, @session.flavor], 
				rname, rstr)
		end
		def resource_external(rname)
			self::class::RESOURCES[rname]
		end
		def resource_global(rname, rstr=nil)
			collect_resource([self::class::RESOURCE_BASE], rname, rstr)
		end
		def resource_localized(rname, rstr=nil, lang=@language)
			result = resource([rname, lang].join('_').intern, rstr)
			if(result.nil? && (lang != @session.default_language))
				result = resource_localized(rname, rstr, @session.default_language)
			end
			result
		end
		def zone_navigation
			@session.zone_navigation
		end
		def zones(filter=false)
			@session.zones
		end
		private
		def collect_resource(base, rname, rstr=nil)
			varpart = self::class::RESOURCES[rname]
			if(varpart.is_a?(Array))
				varpart.collect { |part|
					[base, part, rstr].flatten.compact.join('/')
				}
			elsif(!varpart.nil?)
				[base, varpart, rstr].flatten.compact.join('/')
			end
		end
    def set_dictionary(language)
      @dictionary = self::class::DICTIONARIES[language] || {}
    end
  end
end<commit_msg>new argument 'anchor' for event_url and _event_url, adds an url-style #anchor to the url<commit_after>#!/usr/bin/env ruby
#
# State Based Session Management	
#	Copyright (C) 2004 <NAME>
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#	ywesee - intellectual capital connected, Winterthurerstrasse 52, CH-8006 Zrich, Switzerland
#	<EMAIL>
#
# Lookandfeel -- sbsm -- <EMAIL>

require 'sbsm/time'
require 'cgi'

module SBSM
  class Lookandfeel
    attr_reader :language, :flavor, :session
    DICTIONARIES = {}
		ENABLED = []
		HTML_ATTRIBUTES = {}
		RESOURCES = {}
		RESOURCE_BASE = "/resources"
		TXT_RESOURCES = "/resources"
		class << self
			def txt_file(filename)
				Proc.new {
					path = File.expand_path(filename, self::TXT_RESOURCES)
					File.read(path).strip.gsub(/(\r\n)|(\n)|(\r)/, '<br>')
				}
			end
		end
		def initialize(session)
      @session = session
      @flavor = @session.flavor
      @language = @session.language
      set_dictionary(@language)
		end
		def attributes(key)
			self::class::HTML_ATTRIBUTES.fetch(key, {}).dup
		end
		def base_url
			[@session.http_protocol + ':/', @session.server_name, @language, @flavor].compact.join("/")
		end
		def direct_event
			@session.direct_event
		end
		def enabled?(event, default=true)
			default || self::class::ENABLED.include?(event)
		end
		def event_url(event=direct_event, args={}, anchor=nil)
			_event_url(event, args, anchor=nil) { |args|
				unless(@session.is_crawler? || args.include?('state_id'))
					args.unshift(:state_id, @session.state.object_id)
				end
			}
		end
		def _event_url(event=direct_event, args={}, anchor=nil, &block)
			args = args.collect { |*pair| pair }.flatten
			args = args.collect { |value| CGI.escape(value.to_s) }
			if(block_given?)
				yield(args)
			end
			url = [base_url(), event, args].compact.join('/')
			if(anchor)
				url << "#" << anchor.to_s
			end
			url
		end
		def languages
			@languages ||= self::class::DICTIONARIES.keys.sort
		end
		def language_url(language)
			[@session.http_protocol + ':/', @session.server_name, language, @flavor].compact.join("/")
		end
		def lookup(key, *args)
			key = key.intern if key.is_a? String
			if(args.size > 0)
				result = ""
				args.each_with_index { |text, index|
					result << (lookup(key.to_s + index.to_s) || ' ')
					result << text.to_s
				}
				tail = lookup(key.to_s + args.size.to_s)
				result << tail if(tail)
			else
				result = @dictionary[key] if @dictionary
				if(result.is_a? Proc)
					result.call
				elsif(result.nil?)
					def_lan = @session.default_language
					if(dict = self::class::DICTIONARIES[def_lan])
						dict[key]
					end
				else
					result
				end
			end
		end
		def format_date(date)
			date.strftime(lookup(:date_format))
		end
		def format_price(price, currency=nil)
			if(price.to_i > 0)
				[currency, sprintf('%.2f', price.to_f/100.0)].compact.join(' ')
			end
		end
		def navigation(filter=false)
			@session.navigation
		end
		def resource(rname, rstr=nil)
			collect_resource([self::class::RESOURCE_BASE, @session.flavor], 
				rname, rstr)
		end
		def resource_external(rname)
			self::class::RESOURCES[rname]
		end
		def resource_global(rname, rstr=nil)
			collect_resource([self::class::RESOURCE_BASE], rname, rstr)
		end
		def resource_localized(rname, rstr=nil, lang=@language)
			result = resource([rname, lang].join('_').intern, rstr)
			if(result.nil? && (lang != @session.default_language))
				result = resource_localized(rname, rstr, @session.default_language)
			end
			result
		end
		def zone_navigation(filter=false)
			@session.zone_navigation
		end
		def zones(filter=false)
			@session.zones
		end
		private
		def collect_resource(base, rname, rstr=nil)
			varpart = self::class::RESOURCES[rname]
			if(varpart.is_a?(Array))
				varpart.collect { |part|
					[base, part, rstr].flatten.compact.join('/')
				}
			elsif(!varpart.nil?)
				[base, varpart, rstr].flatten.compact.join('/')
			end
		end
    def set_dictionary(language)
      @dictionary = self::class::DICTIONARIES[language] || {}
    end
  end
end
{'fraction_non_alphanumeric': 0.12938383511384483, 'fraction_numerical': 0.00712204596956944, 'mean_word_length': 3.6432865731462925, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6924212', 'n_tokens_mistral': 3729, 'n_tokens_neox': 3147, 'n_words': 783}