<commit_before># encoding: utf-8

class ElvenHut < Sinatra::Application
  
  def database_clean
    Article.order("created_at DESC").each do |article|
      if !File.exist? File.join(settings.archive_path, article.id.to_s << ".md")
        article.tags.each do |tag|
          tag.quantity -= 1
          tag.save
        end
        article.destroy
      end
    end
  end

  def process_tag tags, article
    tags.split(',').each do |item|
      tag_name = item.strip || item if item
      tag = Tag.filter(:name => tag_name).first
      if tag == nil
        tag = Tag.new :name => tag_name, :quantity => 1
      else
        tag.quantity += 1
      end
      tag.save
      article.add_tag(tag)
    end
  end

  def write_mdfile filename, content
    File.open(File.join(settings.archive_path, filename << '.md'), "w:utf-8") do |write_stream|
      write_stream.write content
    end
  end

  get "/archives/" do
    begin
      @all = Article.reverse_order(:created_at)
      @archive_path = settings.archive_path
      erb :archives_index, :layout => :background
    rescue => e
      database_clean
      redirect "/archives/"
    end
  end

  get "/tag/:name" do
    @all = Tag.filter(:name => params[:name]).first.articles
    @archive_path = settings.archive_path
    erb :archives, :layout => :background
  end

  get %r{/archives/\d+$} do
    @article = Article.filter(:id => params[:captures].first).first
    not_found unless @article
    @contentfilepath = File.join(settings.archive_path, @article.id.to_s << '.md'
    erb :post, :layout=>:background
  end

  get "/new_post" do
    erb :edit_post, :layout => :background, :locals=>{:article=>Article.new}
  end<commit_msg>a little bug I left<commit_after># encoding: utf-8

class ElvenHut < Sinatra::Application
  
  def database_clean
    Article.order("created_at DESC").each do |article|
      if !File.exist? File.join(settings.archive_path, article.id.to_s << ".md")
        article.tags.each do |tag|
          tag.quantity -= 1
          tag.save
        end
        article.destroy
      end
    end
  end

  def process_tag tags, article
    tags.split(',').each do |item|
      tag_name = item.strip || item if item
      tag = Tag.filter(:name => tag_name).first
      if tag == nil
        tag = Tag.new :name => tag_name, :quantity => 1
      else
        tag.quantity += 1
      end
      tag.save
      article.add_tag(tag)
    end
  end

  def write_mdfile filename, content
    File.open(File.join(settings.archive_path, filename << '.md'), "w:utf-8") do |write_stream|
      write_stream.write content
    end
  end

  get "/archives/" do
    begin
      @all = Article.reverse_order(:created_at)
      @archive_path = settings.archive_path
      erb :archives_index, :layout => :background
    rescue => e
      database_clean
      redirect "/archives/"
    end
  end

  get "/tag/:name" do
    @all = Tag.filter(:name => params[:name]).first.articles
    @archive_path = settings.archive_path
    erb :archives, :layout => :background
  end

  get %r{/archives/\d+$} do
    @article = Article.filter(:id => params[:captures].first).first
    not_found unless @article
    @contentfilepath = File.join(settings.archive_path, @article.id.to_s << '.md')
    erb :post, :layout=>:background
  end

  get "/new_post" do
    erb :edit_post, :layout => :background, :locals=>{:article=>Article.new}
  end
{'fraction_non_alphanumeric': 0.12791728212703102, 'fraction_numerical': 0.0029542097488921715, 'mean_word_length': 2.5982996811902233, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 27, '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': '7084429', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1090, 'n_words': 267}