Blame src/file.hpp

Packit Service 7770af
#ifndef SASS_FILE_H
Packit Service 7770af
#define SASS_FILE_H
Packit Service 7770af
Packit Service 7770af
#include <string>
Packit Service 7770af
#include <vector>
Packit Service 7770af
Packit Service 7770af
#include "sass/context.h"
Packit Service 7770af
#include "ast_fwd_decl.hpp"
Packit Service 7770af
Packit Service 7770af
namespace Sass {
Packit Service 7770af
Packit Service 7770af
  namespace File {
Packit Service 7770af
Packit Service 7770af
    // return the current directory
Packit Service 7770af
    // always with forward slashes
Packit Service 7770af
    std::string get_cwd();
Packit Service 7770af
Packit Service 7770af
    // test if path exists and is a file
Packit Service 7770af
    bool file_exists(const std::string& file);
Packit Service 7770af
Packit Service 7770af
    // return if given path is absolute
Packit Service 7770af
    // works with *nix and windows paths
Packit Service 7770af
    bool is_absolute_path(const std::string& path);
Packit Service 7770af
Packit Service 7770af
    // return only the directory part of path
Packit Service 7770af
    std::string dir_name(const std::string& path);
Packit Service 7770af
Packit Service 7770af
    // return only the filename part of path
Packit Service 7770af
    std::string base_name(const std::string&);
Packit Service 7770af
Packit Service 7770af
    // do a locigal clean up of the path
Packit Service 7770af
    // no physical check on the filesystem
Packit Service 7770af
    std::string make_canonical_path (std::string path);
Packit Service 7770af
Packit Service 7770af
    // join two path segments cleanly together
Packit Service 7770af
    // but only if right side is not absolute yet
Packit Service 7770af
    std::string join_paths(std::string root, std::string name);
Packit Service 7770af
Packit Service 7770af
    // if the relative path is outside of the cwd we want want to
Packit Service 7770af
    // show the absolute path in console messages
Packit Service 7770af
    std::string path_for_console(const std::string& rel_path, const std::string& abs_path, const std::string& orig_path);
Packit Service 7770af
Packit Service 7770af
    // create an absolute path by resolving relative paths with cwd
Packit Service 7770af
    std::string rel2abs(const std::string& path, const std::string& base = ".", const std::string& cwd = get_cwd());
Packit Service 7770af
Packit Service 7770af
    // create a path that is relative to the given base directory
Packit Service 7770af
    // path and base will first be resolved against cwd to make them absolute
Packit Service 7770af
    std::string abs2rel(const std::string& path, const std::string& base = ".", const std::string& cwd = get_cwd());
Packit Service 7770af
Packit Service 7770af
    // helper function to resolve a filename
Packit Service 7770af
    // searching without variations in all paths
Packit Service 7770af
    std::string find_file(const std::string& file, struct Sass_Compiler* options);
Packit Service 7770af
    std::string find_file(const std::string& file, const std::vector<std::string> paths);
Packit Service 7770af
Packit Service 7770af
    // helper function to resolve a include filename
Packit Service 7770af
    // this has the original resolve logic for sass include
Packit Service 7770af
    std::string find_include(const std::string& file, const std::vector<std::string> paths);
Packit Service 7770af
Packit Service 7770af
    // split a path string delimited by semicolons or colons (OS dependent)
Packit Service 7770af
    std::vector<std::string> split_path_list(const char* paths);
Packit Service 7770af
Packit Service 7770af
    // try to load the given filename
Packit Service 7770af
    // returned memory must be freed
Packit Service 7770af
    // will auto convert .sass files
Packit Service 7770af
    char* read_file(const std::string& file);
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
  // requested import
Packit Service 7770af
  class Importer {
Packit Service 7770af
    public:
Packit Service 7770af
      // requested import path
Packit Service 7770af
      std::string imp_path;
Packit Service 7770af
      // parent context path
Packit Service 7770af
      std::string ctx_path;
Packit Service 7770af
      // base derived from context path
Packit Service 7770af
      // this really just acts as a cache
Packit Service 7770af
      std::string base_path;
Packit Service 7770af
    public:
Packit Service 7770af
      Importer(std::string imp_path, std::string ctx_path)
Packit Service 7770af
      : imp_path(File::make_canonical_path(imp_path)),
Packit Service 7770af
        ctx_path(File::make_canonical_path(ctx_path)),
Packit Service 7770af
        base_path(File::dir_name(ctx_path))
Packit Service 7770af
      { }
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  // a resolved include (final import)
Packit Service 7770af
  class Include : public Importer {
Packit Service 7770af
    public:
Packit Service 7770af
      // resolved absolute path
Packit Service 7770af
      std::string abs_path;
Packit Service 7770af
    public:
Packit Service 7770af
      Include(const Importer& imp, std::string abs_path)
Packit Service 7770af
      : Importer(imp), abs_path(abs_path)
Packit Service 7770af
      { }
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  // a loaded resource
Packit Service 7770af
  class Resource {
Packit Service 7770af
    public:
Packit Service 7770af
      // the file contents
Packit Service 7770af
      char* contents;
Packit Service 7770af
      // conected sourcemap
Packit Service 7770af
      char* srcmap;
Packit Service 7770af
    public:
Packit Service 7770af
      Resource(char* contents, char* srcmap)
Packit Service 7770af
      : contents(contents), srcmap(srcmap)
Packit Service 7770af
      { }
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  // parsed stylesheet from loaded resource
Packit Service 7770af
  class StyleSheet : public Resource {
Packit Service 7770af
    public:
Packit Service 7770af
      // parsed root block
Packit Service 7770af
      Block_Obj root;
Packit Service 7770af
    public:
Packit Service 7770af
      StyleSheet(const Resource& res, Block_Obj root)
Packit Service 7770af
      : Resource(res), root(root)
Packit Service 7770af
      { }
Packit Service 7770af
  };
Packit Service 7770af
Packit Service 7770af
  namespace File {
Packit Service 7770af
Packit Service 7770af
    static std::vector<std::string> defaultExtensions = { ".scss", ".sass", ".css" };
Packit Service 7770af
Packit Service 7770af
    std::vector<Include> resolve_includes(const std::string& root, const std::string& file,
Packit Service 7770af
      const std::vector<std::string>& exts = defaultExtensions);
Packit Service 7770af
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
#endif