<commit_before>require 'front_matter_ninja/version'

require 'safe_yaml/load'

class FrontMatterNinja
  attr_accessor :data, :content

  def initialize(string)
    match = string.match(/\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)(.*)/m)

    @data = SafeYAML.load(match[0]) || {} if match
    @content = match ? match[4] : string
  end

  def raw_data
    @data.to_yaml
         .sub(/\A---\s*/, '')
         .sub(/\.\.\.\n\z/, '')
  end

  def raw_data=(data)
    @data = SafeYAML.load(data) || {}
  end

  def to_s
    "---\n#{raw_data}---\n\n#{content}"
  end
end<commit_msg>Move regular expressions to constants<commit_after>require 'front_matter_ninja/version'

require 'safe_yaml/load'

class FrontMatterNinja
  attr_accessor :data, :content

  DOCUMENT_MATCHER = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)(.*)/m
  YAML_START_MATCHER = /\A---\s*/
  YAML_END_MATCHER = /\.\.\.\n\z/

  def initialize(string)
    match = string.match DOCUMENT_MATCHER

    @data = SafeYAML.load(match[0]) || {} if match
    @content = match ? match[4] : string
  end

  def raw_data
    strip_yaml @data.to_yaml
  end

  def raw_data=(data)
    @data = SafeYAML.load(data) || {}
  end

  def to_s
    "---\n#{raw_data}---\n\n#{content}"
  end

  private

  def strip_yaml(string)
    string.sub(YAML_START_MATCHER, '').sub(YAML_END_MATCHER, '')
  end
end
{'fraction_non_alphanumeric': 0.2056843679880329, 'fraction_numerical': 0.0029917726252804786, 'mean_word_length': 2.6758241758241756, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6686521', 'n_tokens_mistral': 566, 'n_tokens_neox': 531, 'n_words': 91}