<commit_before>require 'spiderfw/controller/page_controller'
require 'spiderfw/templates/template'
require 'spiderfw/controller/mixins/visual'
require 'spiderfw/widget/widget_attributes'
require 'spiderfw/controller/mixins/http_mixin'
require 'spiderfw/widget/widget_plugin'

module Spider
    
    class Widget < PageController
        include HTTPMixin
        
        attr_accessor :parent
        attr_accessor :request, :scene, :widgets, :template, :id, :id_path, :containing_template, :is_target_ancestor, :is_target_descendant
        attr_reader :attributes, :widget_attributes, :css_classes, :widgets_runtime_content
        attr_accessor :active
        
        @@common_attributes = {
            :id => {}
        }
        
        class << self
            attr_reader :attributes, :scene_attributes
            cattr_reader :tag_name, :plugins
            
            def inherited(subclass)
                subclass.instance_variable_set(:@attributes, attributes.clone)
                subclass.instance_variable_set(:@scene_attributes, @scene_attributes.clone) if @scene_attributes
                subclass.instance_variable_set(:@plugins, @plugins.clone) if @plugins
                subclass.instance_variable_set(:@default_assets, @default_assets.clone) if @default_assets
                @subclasses ||= []
                @subclasses << subclass
                super
            end
            
            def attribute(name, params={})
                # TODO: implement, this is just a placeholder
                @attributes ||= @@common_attributes.clone
                @attributes[name] = params
            end
            
            def attributes
                @attributes ||= @@common_attributes.clone
            end
            
            def attribute?(name)
                @attributes[name.to_sym]
            end
            
            def i_attribute(name, params={})
                params[:instance_attr] = true
                params[:set_var] = true
                attribute(name, params)
                attr_reader(name)
            end
            
            def is_attribute(name, params={})
                params[:instance_attr] = true
                i_attribute(name, params)
                attr_to_scene(name)
                attr_reader(name)
            end
            
            def s_attribute(name, params={})
                attribute(name, params)
                attr_to_scene(name)
            end
            
            def i_attr_accessor(name, params={})
                params[:instance_attr] = true
                i_attribute(name, params)
                attr_accessor(name)
            end
            
            def is_attr_accessor(name, params={})
                params[:instance_attr] = true
                is_attribute(name, params)
                attr_accessor(name)
            end
            
            def attr_to_scene(*names)
                @scene_attributes ||= []
                names.each{ |name| @scene_attributes << name }
            end
            
            def tag(name)
                self.app.register_tag(name, self)
                @tag_name ||= name
            end
            
            def register_tag(name)
                Spider::Template.register(name, self)
            end
            
            def scene_elements(*list)
                @scene_elements ||= []
                @scene_elements += list
            end
            
            def get_scene_elements
                @scene_elements
            end
            
            def template_path_parent(val=nil)
                # FIXME: damn! find a better way!
                @template_path_parent = val if val
                return @template_path_parent || app.path+'/widgets'
            end
            
            def template_path
                p = template_path_parent+'/'+Inflector.underscore(self.to_s.split('::')[-1])
                return p+'/templates' if (File.exist?(p+'/templates'))
                return p
            end
            
            def default_template
                Spider::Inflector.underscore(self.name).split('/')[-1]
            end
            
            def relative_url
                template_path[template_path_parent.length+1..-1]
            end
            
            def route_url
                Spider::ControllerMixins::HTTPMixin.reverse_proxy_mapping('/'+self.app.route_url+'/w/'+relative_url)
            end
            <commit_msg>Autoset @widget_path; reworked template_path (removed template_path_parent); default_template now can accept an argument<commit_after>require 'spiderfw/controller/page_controller'
require 'spiderfw/templates/template'
require 'spiderfw/controller/mixins/visual'
require 'spiderfw/widget/widget_attributes'
require 'spiderfw/controller/mixins/http_mixin'
require 'spiderfw/widget/widget_plugin'

module Spider
    
    class Widget < PageController
        include HTTPMixin
        
        attr_accessor :parent
        attr_accessor :request, :scene, :widgets, :template, :id, :id_path, :containing_template, :is_target_ancestor, :is_target_descendant
        attr_reader :attributes, :widget_attributes, :css_classes, :widgets_runtime_content
        attr_accessor :active
        
        @@common_attributes = {
            :id => {}
        }
        
        class << self
            attr_reader :attributes, :scene_attributes
            cattr_reader :tag_name, :plugins
            
            def inherited(subclass)
                subclass.instance_variable_set(:@attributes, attributes.clone)
                subclass.instance_variable_set(:@scene_attributes, @scene_attributes.clone) if @scene_attributes
                subclass.instance_variable_set(:@plugins, @plugins.clone) if @plugins
                subclass.instance_variable_set(:@default_assets, @default_assets.clone) if @default_assets
                @subclasses ||= []
                @subclasses << subclass
                super
                # Do some magic to try and infer the widget's path, considering intermediate superclass callers
                cnt = 0
                s = subclass.superclass
                while s != Spider::Widget
                    if caller[cnt].split(':')[-1] =~ /inherited/
                        cnt += 1
                    end
                    s = s.superclass

                end
                file = caller[cnt].split(':')[0]
                subclass.instance_variable_set("@widget_path", File.dirname(file))
            end

            
            def attribute(name, params={})
                # TODO: implement, this is just a placeholder
                @attributes ||= @@common_attributes.clone
                @attributes[name] = params
            end
            
            def attributes
                @attributes ||= @@common_attributes.clone
            end
            
            def attribute?(name)
                @attributes[name.to_sym]
            end
            
            def i_attribute(name, params={})
                params[:instance_attr] = true
                params[:set_var] = true
                attribute(name, params)
                attr_reader(name)
            end
            
            def is_attribute(name, params={})
                params[:instance_attr] = true
                i_attribute(name, params)
                attr_to_scene(name)
                attr_reader(name)
            end
            
            def s_attribute(name, params={})
                attribute(name, params)
                attr_to_scene(name)
            end
            
            def i_attr_accessor(name, params={})
                params[:instance_attr] = true
                i_attribute(name, params)
                attr_accessor(name)
            end
            
            def is_attr_accessor(name, params={})
                params[:instance_attr] = true
                is_attribute(name, params)
                attr_accessor(name)
            end
            
            def attr_to_scene(*names)
                @scene_attributes ||= []
                names.each{ |name| @scene_attributes << name }
            end
            
            def tag(name)
                self.app.register_tag(name, self)
                @tag_name ||= name
            end
            
            def register_tag(name)
                Spider::Template.register(name, self)
            end
            
            def scene_elements(*list)
                @scene_elements ||= []
                @scene_elements += list
            end
            
            def get_scene_elements
                @scene_elements
            end
            
            def template_path
                return @template_path if @template_path
                @template_path = @widget_path
                @template_path += '/templates' if ::File.directory?(@template_path+'/templates')
                @template_path
            end
            
            def default_template(val=nil)
                @default_template = val if val
                @default_template ||= Spider::Inflector.underscore(self.name).split('/')[-1]
                @default_template
            end
            
            def relative_url
                ::File.dirname(@widget_path)
            end
            
            def route_url
                Spider::ControllerMixins::HTTPMixin.reverse_proxy_mapping('/'+self.app.route_url+'/w/'+relative_url)
            end
            
{'fraction_non_alphanumeric': 0.08169694424026042, 'fraction_numerical': 0.0009450803318282054, 'mean_word_length': 1.4191008382016763, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, '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': '900078', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2312, 'n_words': 454}