<commit_before>require 'fileutils'

module Softcover
  module Builders
    class Html < Builder
      include Softcover::Utils

      def setup
        Dir.mkdir "html" unless File.directory?("html")
        html_styles = path('html/stylesheets')
        unless File.directory?(html_styles)
          Dir.mkdir html_styles
        end
        template_dir = path("#{File.dirname(__FILE__)}/../template")
        custom_css = path("#{template_dir}/html/stylesheets/custom.css")
        target = path("#{html_styles}/custom.css")
        FileUtils.cp(custom_css, target) unless File.exist?(target)
        clean!
      end

      def build(options = {})
        if Softcover::profiling?
          require 'ruby-prof'
          RubyProf.start
        end

        write_master_latex_file(manifest)

        if manifest.markdown?
          unless options[:'find-overfull']
            remove_unneeded_polytex_files
          end
          manifest.chapters.each do |chapter|
            write_latex_files(chapter, options)
          end

          # Reset the manifest to use PolyTeX.
          self.manifest = Softcover::BookManifest.new(source: :polytex,
                                                      verify_paths: false,
                                                      origin: :markdown)
        end

        if manifest.polytex?
          basename = File.basename(manifest.filename, '.tex')
          @html  = converted_html(basename)
          @title = basename
          @mathjax = Softcover::Mathjax::config(chapter_number: false)
          @src     = Softcover::Mathjax::AMS_SVG
          erb_file = File.read(File.join(File.dirname(__FILE__),
                                         '..', 'server', 'views',
                                         'book.html.erb'))
          file_content = ERB.new(erb_file).result(binding)
          write_full_html_file(basename, file_content)
          write_chapter_html_files(Nokogiri::HTML(file_content), erb_file)
        end

        if Softcover::profiling?
          result = RubyProf.stop
          printer = RubyProf::GraphPrinter.new(result)
          printer.print(STDOUT, {})
        end

        true
      end

      # Removes any PolyTeX files not corresponding to current MD chapters.
      def remove_unneeded_polytex_files
        files_to_keep = manifest.chapters.map do |chapter|
                          path("#{manifest.polytex_dir}/#{chapter.slug}.tex")
                        end
        all_files = Dir.glob(path("#{manifest.polytex_dir}/*.tex"))
        files_to_remove = all_files - files_to_keep
        FileUtils.rm(files_to_remove)<commit_msg>Write a fill-contents file using the filename<commit_after>require 'fileutils'

module Softcover
  module Builders
    class Html < Builder
      include Softcover::Utils

      def setup
        Dir.mkdir "html" unless File.directory?("html")
        html_styles = path('html/stylesheets')
        unless File.directory?(html_styles)
          Dir.mkdir html_styles
        end
        template_dir = path("#{File.dirname(__FILE__)}/../template")
        custom_css = path("#{template_dir}/html/stylesheets/custom.css")
        target = path("#{html_styles}/custom.css")
        FileUtils.cp(custom_css, target) unless File.exist?(target)
        clean!
      end

      def build(options = {})
        if Softcover::profiling?
          require 'ruby-prof'
          RubyProf.start
        end

        write_master_latex_file(manifest)

        if manifest.markdown?
          unless options[:'find-overfull']
            remove_unneeded_polytex_files
          end
          manifest.chapters.each do |chapter|
            write_latex_files(chapter, options)
          end

          # Reset the manifest to use PolyTeX.
          self.manifest = Softcover::BookManifest.new(source: :polytex,
                                                      verify_paths: false,
                                                      origin: :markdown)
        end

        if manifest.polytex?
          basename = File.basename(manifest.filename, '.tex')
          @html  = converted_html(basename)
          @title = basename
          @mathjax = Softcover::Mathjax::config(chapter_number: false)
          @src     = Softcover::Mathjax::AMS_SVG
          erb_file = File.read(File.join(File.dirname(__FILE__),
                                         '..', 'server', 'views',
                                         'book.html.erb'))
          file_content = ERB.new(erb_file).result(binding)
          write_full_html_file(manifest.slug, file_content)
          write_chapter_html_files(Nokogiri::HTML(file_content), erb_file)
        end

        if Softcover::profiling?
          result = RubyProf.stop
          printer = RubyProf::GraphPrinter.new(result)
          printer.print(STDOUT, {})
        end

        true
      end

      # Removes any PolyTeX files not corresponding to current MD chapters.
      def remove_unneeded_polytex_files
        files_to_keep = manifest.chapters.map do |chapter|
                          path("#{manifest.polytex_dir}/#{chapter.slug}.tex")
                        end
        all_files = Dir.glob(path("#{manifest.polytex_dir}/*.tex"))
        files_to_remove = all_files - files_to_keep
        FileUtils.rm(files_to_remove)
{'fraction_non_alphanumeric': 0.09541768810107486, 'fraction_numerical': 0.0, 'mean_word_length': 1.831820608649226, 'pattern_counts': {'":': 0, '<': 5, '<?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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4307834', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1411, 'n_words': 267}