<commit_before>class Course::MdParse

  WEEK_HEADER = "h2"
  COURSE_TITLE_HEADER = "h1"

  attr_reader :course_base_dir, :version, :sha, :temp_dir

  def initialize(repo_dir, version = "master")
    raise "repo does not exist" if !File.exist?(repo_dir)
    @version = version
    @course_base_dir = repo_dir #Pathname
    @temp_dir = Course::Utils::TEMP_DIR
    @sha = SecureRandom.hex
    save_file_in_temp
  end


  def result
    course =<<-THERE
    <course name="#{course_name}">
      #{index_xml}
      #{pages_xml}
    </course>
    THERE
    clear_temp_dir
    course
  end

  private

  def course_name
    @course_name ||= course_index_dom.css("h1")[0].text
  end

  def lesson_names
    @lesson_names ||= course_index_dom.css("lesson").map { |node| node["name"] }
  end

  def course_index_dom
    index_file = sha + "index.xmd"
    course_index_file_path = temp_dir + index_file
    compile(course_index_file_path)
  end

  def clear_temp_dir
    Dir.chdir(temp_dir) do
      list = Dir.glob("#{sha}*")
      FileUtils.rm_rf(list)
    end

  end

  def index_xml
    <<-THERE
    <index>
      #{weeks_xml}
    </index>
    THERE
  end

  def weeks_xml
    weeks = ""
    xml = course_index_dom
    node = xml.child
    nodes_in_week = nil
    while node
      if node.name == WEEK_HEADER
        if nodes_in_week
          week = <<-THERE
            <week>
              #{nodes_in_week}
            </week>
          THERE
          weeks << week
        end
        nodes_in_week = ""
        nodes_in_week << node.to_xhtml
      elsif node.name == "lesson"
        lesson = <<-THERE
        <lesson day="#{node["day"]}"  name="#{node["name"]}">
           <overview>#{node.children.to_xhtml}</overview>
        </lesson>
        THERE
        nodes_in_week << lesson if nodes_in_week
      else
        nodes_in_week << node.to_xhtml if nodes_in_week
      end
      node = node.next_sibling
      if node == nil
        week = <<-THERE
         <week>
           #{nodes_in_week}
         </week>
        THERE
        weeks << week
      end
    end


    <<-THERE
    #{weeks}
    THERE
  end

  def lesson_dom(lesson_name)
    dom = nil
    file = sha + "index.md"
    lesson_dir = sha + lesson_name
    file_path = temp_dir + lesson_dir + file
    if File.exists?(file_path)
    else
      file = sha + "index.xmd"
      file_path = temp_dir + lesson_dir + file
    end
    dom = compile(file_path)
    dom
  end

  def pages_xml
    pages = ""
    lesson_names.each do |lesson_name|
      lesson_xml_dom = lesson_dom(lesson_name)
      page = <<-THERE
      <page name="#{lesson_name}">
      #{lesson_xml_dom.children.to_xhtml}
      </page>
      THERE
      pages << page
    end

    <<-THERE
    #{pages}
    THERE
  end

  def compile(file)
    if File.extname(file) == ".xmd"
      compile_xmd(file)
    else
      compile_md(file)
    end

  end

  def compile_xmd(file)
    str = %x{ xmd #{file} }
    Nokogiri::HTML(str).css("body")[0].child
  end

  def compile_md(file)
    str = %x{ marked -i #{file} }
    Nokogiri::HTML(str).css("body")[0]
  end


  def save_file_in_temp
    save_course_index
    save_lesson_index<commit_msg>fix security hole<commit_after>class Course::MdParse

  WEEK_HEADER = "h2"
  COURSE_TITLE_HEADER = "h1"

  attr_reader :course_base_dir, :version, :sha, :temp_dir

  def initialize(repo_dir, version = "master")
    raise "repo does not exist" if !File.exist?(repo_dir)
    @version = version
    @course_base_dir = repo_dir #Pathname
    @temp_dir = Course::Utils::TEMP_DIR
    @sha = SecureRandom.hex
    save_file_in_temp
  end


  def result
    course =<<-THERE
    <course name="#{course_name}">
      #{index_xml}
      #{pages_xml}
    </course>
    THERE
    clear_temp_dir
    course
  end

  private

  def course_name
    @course_name ||= course_index_dom.css("h1")[0].text
  end

  def lesson_names
    @lesson_names ||= course_index_dom.css("lesson").map { |node| node["name"] }
  end

  def course_index_dom
    index_file = sha + "index.xmd"
    course_index_file_path = temp_dir + index_file
    compile(course_index_file_path)
  end

  def clear_temp_dir
    Dir.chdir(temp_dir) do
      list = Dir.glob("#{sha}*")
      FileUtils.rm_rf(list)
    end

  end

  def index_xml
    <<-THERE
    <index>
      #{weeks_xml}
    </index>
    THERE
  end

  def weeks_xml
    weeks = ""
    xml = course_index_dom
    node = xml.child
    nodes_in_week = nil
    while node
      if node.name == WEEK_HEADER
        if nodes_in_week
          week = <<-THERE
            <week>
              #{nodes_in_week}
            </week>
          THERE
          weeks << week
        end
        nodes_in_week = ""
        nodes_in_week << node.to_xhtml
      elsif node.name == "lesson"
        lesson = <<-THERE
        <lesson day="#{node["day"]}"  name="#{node["name"]}">
           <overview>#{node.children.to_xhtml}</overview>
        </lesson>
        THERE
        nodes_in_week << lesson if nodes_in_week
      else
        nodes_in_week << node.to_xhtml if nodes_in_week
      end
      node = node.next_sibling
      if node == nil
        week = <<-THERE
         <week>
           #{nodes_in_week}
         </week>
        THERE
        weeks << week
      end
    end


    <<-THERE
    #{weeks}
    THERE
  end

  def lesson_dom(lesson_name)
    dom = nil
    file = sha + "index.md"
    lesson_dir = sha + lesson_name
    file_path = temp_dir + lesson_dir + file
    if File.exists?(file_path)
    else
      file = sha + "index.xmd"
      file_path = temp_dir + lesson_dir + file
    end
    dom = compile(file_path)
    dom
  end

  def pages_xml
    pages = ""
    lesson_names.each do |lesson_name|
      lesson_xml_dom = lesson_dom(lesson_name)
      page = <<-THERE
      <page name="#{lesson_name}">
      #{lesson_xml_dom.children.to_xhtml}
      </page>
      THERE
      pages << page
    end

    <<-THERE
    #{pages}
    THERE
  end

  def compile(file)
    if File.extname(file) == ".xmd"
      compile_xmd(file)
    else
      compile_md(file)
    end

  end

  def compile_xmd(file)
    str = IO.popen(["xmd", file])
    Nokogiri::HTML(str).css("body")[0].child
  end

  def compile_md(file)
    str = IO.popen(["marked", "-i", file])
    Nokogiri::HTML(str).css("body")[0]
  end


  def save_file_in_temp
    save_course_index
    save_lesson_index
{'fraction_non_alphanumeric': 0.1132105180286569, 'fraction_numerical': 0.001889466225791214, 'mean_word_length': 2.1744127936031985, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '3075895', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2281, 'n_words': 471}