Style abstract class
An enum type describing a "flavor" of path.
abstract class Style {
/// POSIX-style paths use "/" (forward slash) as separators. Absolute paths
/// start with "/". Used by UNIX, Linux, Mac OS X, and others.
static final posix = new _PosixStyle();
/// Windows paths use "\" (backslash) as separators. Absolute paths start with
/// a drive letter followed by a colon (example, "C:") or two backslashes
/// ("\\") for UNC paths.
// TODO(rnystrom): The UNC root prefix should include the drive name too, not
// just the "\\".
static final windows = new _WindowsStyle();
/// URLs aren't filesystem paths, but they're supported by Pathos to make it
/// easier to manipulate URL paths in the browser.
///
/// URLs use "/" (forward slash) as separators. Absolute paths either start
/// with a protocol and optional hostname (e.g. `http://dartlang.org`,
/// `file://`) or with "/".
static final url = new _UrlStyle();
/// The name of this path style. Will be "posix" or "windows".
String get name;
/// The path separator for this style. On POSIX, this is `/`. On Windows,
/// it's `\`.
String get separator;
/// The [Pattern] that can be used to match a separator for a path in this
/// style. Windows allows both "/" and "\" as path separators even though "\"
/// is the canonical one.
Pattern get separatorPattern;
/// The [Pattern] that matches path components that need a separator after
/// them.
///
/// Windows and POSIX styles just need separators when the previous component
/// doesn't already end in a separator, but the URL always needs to place a
/// separator between the root and the first component, even if the root
/// already ends in a separator character. For example, to join "file://" and
/// "usr", an additional "/" is needed (making "file:///usr").
Pattern get needsSeparatorPattern;
/// The [Pattern] that can be used to match the root prefix of an absolute
/// path in this style.
Pattern get rootPattern;
/// The [Pattern] that can be used to match the root prefix of a root-relative
/// path in this style.
///
/// This can be null to indicate that this style doesn't support root-relative
/// paths.
final Pattern relativeRootPattern = null;
/// Gets the root prefix of [path] if path is absolute. If [path] is relative,
/// returns `null`.
String getRoot(String path) {
var match = rootPattern.firstMatch(path);
if (match != null) return match[0];
return getRelativeRoot(path);
}
/// Gets the root prefix of [path] if it's root-relative.
///
/// If [path] is relative or absolute and not root-relative, returns `null`.
String getRelativeRoot(String path) {
if (relativeRootPattern == null) return null;
var match = relativeRootPattern.firstMatch(path);
if (match == null) return null;
return match[0];
}
/// Returns the path represented by [uri] in this style.
String pathFromUri(Uri uri);
/// Returns the URI that represents [path].
///
/// Pathos will always path an absolute path for [path]. Relative paths are
/// handled automatically by [Builder].
Uri pathToUri(String path);
String toString() => name;
}
Static Properties
final posix #
POSIX-style paths use "/" (forward slash) as separators. Absolute paths start with "/". Used by UNIX, Linux, Mac OS X, and others.
static final posix = new _PosixStyle()
final url #
URLs aren't filesystem paths, but they're supported by Pathos to make it easier to manipulate URL paths in the browser.
URLs use "/" (forward slash) as separators. Absolute paths either start
with a protocol and optional hostname (e.g. http://dartlang.org,
file://) or with "/".
static final url = new _UrlStyle()
final windows #
Windows paths use "\" (backslash) as separators. Absolute paths start with a drive letter followed by a colon (example, "C:") or two backslashes ("\\") for UNC paths.
static final windows = new _WindowsStyle()
Properties
final Pattern needsSeparatorPattern #
The Pattern that matches path components that need a separator after them.
Windows and POSIX styles just need separators when the previous component doesn't already end in a separator, but the URL always needs to place a separator between the root and the first component, even if the root already ends in a separator character. For example, to join "file://" and "usr", an additional "/" is needed (making "file:///usr").
Pattern get needsSeparatorPattern;
final Pattern relativeRootPattern #
The Pattern that can be used to match the root prefix of a root-relative path in this style.
This can be null to indicate that this style doesn't support root-relative paths.
final Pattern relativeRootPattern = null
final Pattern rootPattern #
The Pattern that can be used to match the root prefix of an absolute path in this style.
Pattern get rootPattern;
Methods
String getRelativeRoot(String path) #
Gets the root prefix of path if it's root-relative.
If
path is relative or absolute and not root-relative, returns null.
String getRelativeRoot(String path) {
if (relativeRootPattern == null) return null;
var match = relativeRootPattern.firstMatch(path);
if (match == null) return null;
return match[0];
}
String getRoot(String path) #
Gets the root prefix of
path if path is absolute. If
path is relative,
returns null.
String getRoot(String path) {
var match = rootPattern.firstMatch(path);
if (match != null) return match[0];
return getRelativeRoot(path);
}