module CSL class Style < Node types << CSL::Info << CSL::Locale include InheritableNameOptions @default = :apa @root = '/usr/local/share/csl/styles'.freeze @extension = '.csl'.freeze @prefix = '' class << self include Loader attr_accessor :default def load(input = nil) super(input || Style.default) end end attr_defaults :version => Schema.major_version, :xmlns => Schema.namespace show_default_attributes! attr_struct :xmlns, :version, :class, :'default-locale', :'initialize-with-hyphen', :'page-range-format', :'demote-non-dropping-particle', *Schema.attr(:name, :names) attr_children :'style-options', :info, :locale, :macro, :citation, :bibliography attr_reader :macros, :errors alias options style_options alias locales locale alias has_macros? has_macro? def_delegators :info, :self_link, :self_link=, :has_self_link?, :self_link!, :template_link, :template_link=, :has_template_link?, :documentation_link, :documentation_link=, :has_documentation_link?, :independent_parent_link, :independent_parent_link=, :has_independent_parent_link?, :title=, :id=, :has_title?, :has_id?, :published_at, :updated_at, :citation_format, :citation_format=, :update!, :license, :license=, :default_license?, :default_license! def initialize(attributes = {}) super(attributes, &nil) children[:locale], children[:macro], @macros, @errors = [], [], {}, [] yield self if block_given? end # @override def added_child(node) delegate = :"added_#{node.nodename}" send delegate, node if respond_to?(delegate, true) node end # @override def deleted_child(node) delegate = :"deleted_#{node.nodename}" send delegate, node if respond_to?(delegate, true) node end def validate @errors = Schema.validate self end def valid? validate.empty? end remove_method :info # generated by struct_children def info children[:info] ||= Info.new end alias_child :metadata, :info # @return [String] the style's id def id return unless info.has_id? info.id.to_s end # @return [String] the style's title def title return unless info.has_title? info.title.to_s end alias has_template? has_template_link? # @return [Style] the style's template def template return unless has_template? load_related_style_from template_link end alias dependent? has_independent_parent_link? def independent? !dependent? end def independent_parent return unless dependent? load_related_style_from independent_parent_link end def demote_non_dropping_particle attributes[:'demote-non-dropping-particle'] end alias demote_particle demote_non_dropping_particle def demote_non_dropping_particle? attribute?(:'demote-non-dropping-particle') end alias demote_particle? demote_non_dropping_particle? def initialize_without_hyphen? attribute?(:'initialize-with-hyphen') && !attributes[:'initialize-with-hyphen'] end def has_page_range_format? attribute?(:'page-range-format') end def page_range_format attributes[:'page-range-format'] end private def preamble Schema.preamble.dup end def load_related_style_from(uri) # TODO try local first Style.load(uri) end def added_macro(node) unless node.attribute?(:name) raise ValidationError, "failed to register macro #{node.inspect}: name attribute missing" end if macros.key?(node[:name]) raise ValidationError, "failed to register macro #{node.inspect}: duplicate name" end macros[node[:name]] = node end def deleted_macro(node) macros.delete node[:name] end end end