<commit_before>require 'pathname'
require 'json'

module Vitrine
  
  # We need to override the Sass importer
  # so that it gives us URLs relative to the server root for sourcemaps
  class Imp < Sass::Importers::Filesystem
    def public_url(of_filesystem_path)
      scss_uri = '/' + Pathname.new(of_filesystem_path).relative_path_from(Pathname.new(root)).to_s
    end
  end
  
  # Compile a SASS/SCSS file to CSS
  def self.compile_sass_and_sourcemap(scss_path, public_folder_path)
    # Compute the paths relative to the webserver public root
    scss_uri = '/' + Pathname.new(scss_path).relative_path_from(Pathname.new(public_folder_path)).to_s
    css_uri = scss_uri.gsub(/\.scss$/, '.css')
    sourcemap_uri= css_uri + '.map'
    
    engine_opts = {importer: Imp.new(public_folder_path), sourcemap: true, cache: false}
    map_options = {css_path: css_uri, sourcemap_path: sourcemap_uri }
    
    engine = Sass::Engine.for_file(scss_path, engine_opts)
    
    # Determine the sourcemap URL for the SASS file
    rendered, mapping = engine.render_with_sourcemap(sourcemap_uri)
    
    # Serialize the sourcemap
    # We need to pass css_uri: so that the generated sourcemap refers to the
    # file that can be pulled of the server as opposed to a file on the filesystem
    sourcemap_body = mapping.to_json(map_options)
    
    # We are using a pre-release version of SASS which still had old sourcemap reference
    # syntax, so we have to fix it by hand
    chunk = Regexp.escape('/*@ sourceMappingURL')
    replacement = '/*# sourceMappingURL'
    re = /^#{chunk}/
    [rendered.gsub(re,replacement), sourcemap_body]
  end
  
  # Compile SASS and return the source map only
  def self.compile_sass_source_map(scss_path, public_folder_path)
    css, map = compile_sass_and_sourcemap(scss_path, public_folder_path)
    map
  end
  
  # Compiles SASS and it's sourcemap and returns the CSS only<commit_msg>Formatting<commit_after>require 'pathname'
require 'json'

module Vitrine
  
  # We need to override the Sass importer
  # so that it gives us URLs relative to the server root for sourcemaps
  class Imp < Sass::Importers::Filesystem
    def public_url(of_filesystem_path)
      scss_uri = '/' + Pathname.new(of_filesystem_path).relative_path_from(Pathname.new(root)).to_s
    end
  end
  
  # Compile a SASS/SCSS file to CSS
  def self.compile_sass_and_sourcemap(scss_path, public_folder_path)
    # Compute the paths relative to the webserver public root
    scss_uri = '/' + Pathname.new(scss_path).relative_path_from(Pathname.new(public_folder_path)).to_s
    css_uri = scss_uri.gsub(/\.scss$/, '.css')
    sourcemap_uri = css_uri + '.map'
    
    engine_opts = {importer: Imp.new(public_folder_path), sourcemap: true, cache: false}
    map_options = {css_path: css_uri, sourcemap_path: sourcemap_uri }
    
    engine = Sass::Engine.for_file(scss_path, engine_opts)
    
    # Determine the sourcemap URL for the SASS file
    rendered, mapping = engine.render_with_sourcemap(sourcemap_uri)
    
    # Serialize the sourcemap
    # We need to pass css_uri: so that the generated sourcemap refers to the
    # file that can be pulled of the server as opposed to a file on the filesystem
    sourcemap_body = mapping.to_json(map_options)
    
    # We are using a pre-release version of SASS which still had old sourcemap reference
    # syntax, so we have to fix it by hand
    chunk = Regexp.escape('/*@ sourceMappingURL')
    replacement = '/*# sourceMappingURL'
    re = /^#{chunk}/
    [rendered.gsub(re,replacement), sourcemap_body]
  end
  
  # Compile SASS and return the source map only
  def self.compile_sass_source_map(scss_path, public_folder_path)
    css, map = compile_sass_and_sourcemap(scss_path, public_folder_path)
    map
  end
  
  # Compiles SASS and it's sourcemap and returns the CSS only
{'fraction_non_alphanumeric': 0.08033385498174231, 'fraction_numerical': 0.0, 'mean_word_length': 3.7699004975124377, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4847084', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1251, 'n_words': 361}