<commit_before>#
#
# Author:  Michael 'entropie' Trommer <<EMAIL>>
#

module OY

  class WikiSpecial < Wiki

    attr_accessor :dir

    def self.pages
      @pages ||= Pages.new
    end

    def pages
      dir.pages
    end

    def wiki_page
      @ws ||= WikiSpecial.pages[@page].new(self)
    end

    def data_for_page
      ws = wiki_page
      ws.body
    rescue
      raise OY::NotFound, "'#{page}' not found in WikiSpecial.pages"
    end

    def initialize(page, path, extension = :textile)
      @path = path
      @page = page
      @data = data_for_page
      @extension = extension
      @commit ||= OpenStruct.new
      @commit.author = OY::Actor
    end

    def title
      File.basename(@path).split(".").first.capitalize
    end

    def data
      @markup ||= with_markup
    end

    def extension
      @extension.to_s
    end

    def commit
      @commit
    end

    def ident
      "#{path}/#{wiki_page.ident}"
    end

    def with_markup(force_extension = nil)
      ret = @data
      ["*", (force_extension || extension), "xml"].inject(ret){|memo, mup|
        markup = Markup.choose_for(mup)
        Markup::measure_to_stdout(markup, self) do
          markup.new(memo, self).to_html
        end
      }
    end


    class Pages < Array
      def [](obj)
        ret = select{|page| page.to_s.split("::").last == "#{obj.to_s.capitalize}Page"}.first
      end
    end

    class Page

      include OY

      def pages
        @pages ||= repos.directory(@wiki.path).pages
      end

      def self.inherited(o)
        return if WikiSpecial.pages.include?(o)
        WikiSpecial.pages << o
      end

      def initialize(wikispecial)
        @wiki = wikispecial
      end
    end

    class IndexPage < Page

      def ident
        "index"
      end

      def body
        str = "h1. Index Page\n\nThis Page is autogenerated.\n\n"
        pages.each do |page|
          str << %Q'* <a class="oy-link x" href="#{page.link}">#{page.title}</a> <span class="oy-msg">(#{page.commit.message})</span> <span class="oy-ref">(#{page.ref})</span>\n'
        end
        str << "\n"
        str
      end
    end
  end
end

=begin
Local Variables:
  mode:ruby
  fill-column:70
  indent-tabs-mode:nil
  ruby-indent-level:2
End:
=end<commit_msg>sort pages on autogenerated index pages<commit_after>#
#
# Author:  Michael 'entropie' Trommer <<EMAIL>>
#

module OY

  class WikiSpecial < Wiki

    attr_accessor :dir

    def self.pages
      @pages ||= Pages.new
    end

    def pages
      dir.pages
    end

    def wiki_page
      @ws ||= WikiSpecial.pages[@page].new(self)
    end

    def data_for_page
      ws = wiki_page
      ws.body
    rescue
      raise OY::NotFound, "'#{page}' not found in WikiSpecial.pages"
    end

    def initialize(page, path, extension = :textile)
      @path = path
      @page = page
      @data = data_for_page
      @extension = extension
      @commit ||= OpenStruct.new
      @commit.author = OY::Actor
    end

    def title
      File.basename(@path).split(".").first.capitalize
    end

    def data
      @markup ||= with_markup
    end

    def extension
      @extension.to_s
    end

    def commit
      @commit
    end

    def ident
      "#{path}/#{wiki_page.ident}"
    end

    def with_markup(force_extension = nil)
      ret = @data
      ["*", (force_extension || extension), "xml"].inject(ret){|memo, mup|
        markup = Markup.choose_for(mup)
        Markup::measure_to_stdout(markup, self) do
          markup.new(memo, self).to_html
        end
      }
    end


    class Pages < Array
      def [](obj)
        ret = select{|page| page.to_s.split("::").last == "#{obj.to_s.capitalize}Page"}.first
      end
    end

    class Page

      include OY

      def pages
        @pages ||= repos.directory(@wiki.path).pages
      end

      def self.inherited(o)
        return if WikiSpecial.pages.include?(o)
        WikiSpecial.pages << o
      end

      def initialize(wikispecial)
        @wiki = wikispecial
      end
    end

    class IndexPage < Page

      def ident
        "index"
      end

      def body
        str = "h1. Index Page\n\nThis Page is autogenerated.\n\n"
        pages.sort_by{|page| page.title }.each do |page|
          str << %Q'* <a class="oy-link x" href="#{page.link}">#{page.title}</a> <span class="oy-msg">(#{page.commit.message})</span> <span class="oy-ref">(#{page.ref})</span>\n'
        end
        str << "\n"
        str
      end
    end
  end
end

=begin
Local Variables:
  mode:ruby
  fill-column:70
  indent-tabs-mode:nil
  ruby-indent-level:2
End:
=end
{'fraction_non_alphanumeric': 0.1256544502617801, 'fraction_numerical': 0.0017452006980802793, 'mean_word_length': 2.0164473684210527, 'pattern_counts': {'":': 2, '<': 37, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5794294', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1530, 'n_words': 362}