<commit_before># -*- coding: utf-8 -*-

require 'attachments'

include Typedown2Blog
module Typedown2Blog

  class BlogPost < Base
    attr_accessor :mail_from, :mail_to, :typedown_body, :format

    def initialize options = {}, &block
      @mail_to = nil
      @mail_from = nil
      @typedown_body = ""
      @attachments = []
      
      options.each do |k, v|
        send("#{k}=", v)
      end

      instance_eval &block if block_given?
    end


    def add_attachment v
      @attachments << v
    end


    def post!
      mail_to = self.mail_to
      mail_from = self.mail_from
      mail_subject, mail_body, mail_content_type = self.format_body(self.typedown_body)

      mail_attachments = @attachments

      mail = Mail.new do
        from mail_from
        to mail_to
        subject mail_subject

        body mail_body
        content_type mail_content_type
        mail_attachments.each do |a|
          add_file a
        end
      end

      log.info(mail_subject + " delivered to " + mail_to)
      mail.deliver!
    end

    def import_mail filename_or_hash
      post = nil
      extract = Attachments::Extract.new [ "image/jpeg" ]
      begin
        extract.parse filename_or_hash
        self.mail_from = extract.from

        typedown_root = Typedown::Section.sectionize(extract.text_body, extract.subject)
        self.typedown_body = typedown_root.doc

        extract.files.each do |f|
          self.add_attachment f[:tmpfile] # f[:save_as], f[:mime_type
        end
      ensure
        extract.close
      end
    end

    protected
    def format_body typedown
      doc = Typedown::Section.sectionize(typedown)
      body = "#{doc.body.to_html}\n\n"
      [ doc.title, body, "plain/text" ]
    end

  end
end
<commit_msg>Formatters<commit_after># -*- coding: utf-8 -*-

require 'attachments'

include Typedown2Blog
module Typedown2Blog

  class BlogPost < Base
    attr_accessor :mail_from, :mail_to, :typedown_body, :format
    
    begin
      @formatters = {}
    end


    def initialize options = {}, &block
      @mail_to = nil
      @mail_from = nil
      @typedown_body = ""
      @format = nil
      @attachments = []

      options.each do |k, v|
        send("#{k}=", v)
      end

      instance_eval &block if block_given?
    end

    def self.add_formatter name, formatter
      @formatters[ name ] = formatter
    end


    def self.formatters
      @formatters
    end

    def add_attachment v
      @attachments << v
    end


    def post!
      mail_to = self.mail_to
      mail_from = self.mail_from
      mail_subject, mail_body, mail_content_type = self.format_body(self.typedown_body)

      mail_attachments = @attachments

      mail = Mail.new do
        from mail_from
        to mail_to
        subject mail_subject

        body mail_body
        content_type mail_content_type
        mail_attachments.each do |a|
          add_file a
        end
      end

      log.info(mail_subject + " delivered to " + mail_to)
      mail.deliver!
    end

    def import_mail filename_or_hash
      post = nil
      extract = Attachments::Extract.new [ "image/jpeg" ]
      begin
        extract.parse filename_or_hash
        self.mail_from = extract.from

        typedown_root = Typedown::Section.sectionize(extract.text_body, extract.subject)
        self.typedown_body = typedown_root.doc

        extract.files.each do |f|
          self.add_attachment f[:tmpfile] # f[:save_as], f[:mime_type
        end
      ensure
        extract.close
      end
    end

    protected
    def format_body typedown
      if(format)
        self.class.formatters[format].format typedown
      else
        doc = Typedown::Section.sectionize(typedown)
        body = "#{doc.body.to_html}\n\n"
        [ doc.title, body, "plain/text" ]
      end
    end

  end
end

{'fraction_non_alphanumeric': 0.08471020194072909, 'fraction_numerical': 0.0015735641227380016, 'mean_word_length': 2.10839445802771, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '2861357', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1201, 'n_words': 276}