paths

package
v0.123.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FilePathSeparator = string(filepath.Separator)
)

FilePathSeparator as defined by os.Separator.

Variables

This section is empty.

Functions

func AbsPathify

func AbsPathify(workingDir, inPath string) string

AbsPathify creates an absolute path if given a working dir and a relative path. If already absolute, the path is just cleaned.

func AddContextRoot

func AddContextRoot(baseURL, relativePath string) string

AddContextRoot adds the context root to an URL if it's not already set. For relative URL entries on sites with a base url with a context root set (i.e. http://example.com/mysite), relative URLs must not include the context root if canonifyURLs is enabled. But if it's disabled, it must be set.

func AddLeadingAndTrailingSlash

func AddLeadingAndTrailingSlash(path string) string

AddTrailingAndLeadingSlash adds a leading and trailing Unix styled slash (/) if not already there.

func AddLeadingSlash

func AddLeadingSlash(path string) string

AddLeadingSlash adds a leading Unix styled slash (/) if not already there.

func AddTrailingSlash

func AddTrailingSlash(path string) string

AddTrailingSlash adds a trailing Unix styled slash (/) if not already there.

func CommonDir

func CommonDir(path1, path2 string) string

CommonDir returns the common directory of the given paths.

func Dir

func Dir(s string) string

Dir behaves like path.Dir without the path.Clean step.

The returned path ends in a slash only if it is the root "/".

func Ext

func Ext(in string) string

Ext takes a path and returns the extension, including the delimiter, i.e. ".md".

func ExtNoDelimiter

func ExtNoDelimiter(in string) string

ExtNoDelimiter takes a path and returns the extension, excluding the delimiter, i.e. "md".

func FieldsSlash

func FieldsSlash(s string) []string

FieldsSlash cuts s into fields separated with '/'.

func FileAndExt

func FileAndExt(in string) (string, string)

FileAndExt takes a path and returns the file and extension separated, the extension including the delimiter, i.e. ".md".

func FileAndExtNoDelimiter

func FileAndExtNoDelimiter(in string) (string, string)

FileAndExtNoDelimiter takes a path and returns the file and extension separated, the extension excluding the delimiter, e.g "md".

func Filename

func Filename(in string) (name string)

Filename takes a file path, strips out the extension, and returns the name of the file.

func GetRelativePath

func GetRelativePath(path, base string) (final string, err error)

GetRelativePath returns the relative path of a given path.

func HasExt

func HasExt(p string) bool

HasExt returns true if the Unix styled path has an extension.

func MakePermalink(host, plink string) *url.URL

MakePermalink combines base URL with content path to create full URL paths. Example

base:   http://spf13.com/
path:   post/how-i-blog
result: http://spf13.com/post/how-i-blog

func MakeTitle

func MakeTitle(inpath string) string

MakeTitle converts the path given to a suitable title, trimming whitespace and replacing hyphens with whitespace.

func ModifyPathBundleTypeResource

func ModifyPathBundleTypeResource(p *Path)

func NormalizePathStringBasic

func NormalizePathStringBasic(s string) string

NormalizePathString returns a normalized path string using the very basic Hugo rules.

func PathAndExt

func PathAndExt(in string) (string, string)

PathAndExt is the same as FileAndExt, but it uses the path package.

func PathEscape

func PathEscape(pth string) string

PathEscape escapes unicode letters in pth. Use URLEscape to escape full URLs including scheme, query etc. This is slightly faster for the common case. Note, there is a url.PathEscape function, but that also escapes /.

func PrettifyURL

func PrettifyURL(in string) string

PrettifyURL takes a URL string and returns a semantic, clean URL.

func PrettifyURLPath

func PrettifyURLPath(in string) string

PrettifyURLPath takes a URL path to a content and converts it to enable pretty URLs.

/section/name.html       becomes /section/name/index.html
/section/name/           becomes /section/name/index.html
/section/name/index.html becomes /section/name/index.html

func ReplaceExtension

func ReplaceExtension(path string, newExt string) string

ReplaceExtension takes a path and an extension, strips the old extension and returns the path with the new extension.

func Sanitize

func Sanitize(s string) string

Sanitize sanitizes string to be used in Hugo's file paths and URLs, allowing only a predefined set of special Unicode characters.

Spaces will be replaced with a single hyphen.

This function is the core function used to normalize paths in Hugo.

Note that this is the first common step for URL/path sanitation, the final URL/path may end up looking differently if the user has stricter rules defined (e.g. removePathAccents=true).

func ToSlashPreserveLeading

func ToSlashPreserveLeading(s string) string

ToSlashPreserveLeading converts the path given to a forward slash separated path and preserves the leading slash if present trimming any trailing slash.

func ToSlashTrimLeading

func ToSlashTrimLeading(s string) string

ToSlashTrimLeading is just a filepath.ToSlash with an added / prefix trimmer.

func ToSlashTrimTrailing

func ToSlashTrimTrailing(s string) string

ToSlashTrimTrailing is just a filepath.ToSlash with an added / suffix trimmer.

func URLEscape

func URLEscape(uri string) string

URLEscape escapes unicode letters.

func Uglify

func Uglify(in string) string

Uglify does the opposite of PrettifyURLPath().

/section/name/index.html becomes /section/name.html
/section/name/           becomes /section/name.html
/section/name.html       becomes /section/name.html

func UrlToFilename

func UrlToFilename(s string) (string, bool)

UrlToFilename converts the URL s to a filename. If ParseRequestURI fails, the input is just converted to OS specific slashes and returned.

Types

type DirFile

type DirFile struct {
	Dir  string
	File string
}

DirFile holds the result from path.Split.

func (DirFile) String

func (df DirFile) String() string

Used in test.

type Path

type Path struct {
	// contains filtered or unexported fields
}

func Parse

func Parse(c, s string) *Path

Parse parses component c with path s into Path using the default path parser.

func (*Path) Base

func (p *Path) Base() string

For content files, Base returns the path without any identifiers (extension, language code etc.). Any 'index' as the last path element is ignored.

For other files (Resources), any extension is kept.

func (*Path) BaseNameNoIdentifier

func (p *Path) BaseNameNoIdentifier() string

BaseNameNoIdentifier returns the logcical base name for a resource without any idenifier (e.g. no extension). For bundles this will be the containing directory's name, e.g. "blog".

func (*Path) BaseNoLeadingSlash

func (p *Path) BaseNoLeadingSlash() string

BaseNoLeadingSlash returns the base path without the leading slash.

func (*Path) BaseRel

func (p *Path) BaseRel(owner *Path) string

BaseRel returns the base path relative to the given owner.

func (*Path) BundleType

func (p *Path) BundleType() PathType

func (*Path) Component

func (p *Path) Component() string

Component returns the component for this path (e.g. "content").

func (*Path) Container

func (p *Path) Container() string

Container returns the base name of the container directory for this path.

func (*Path) ContainerDir

func (p *Path) ContainerDir() string

ContainerDir returns the container directory for this path. For content bundles this will be the parent directory.

func (*Path) Dir

func (p *Path) Dir() (d string)

Dir returns all but the last element of path, typically the path's directory.

func (*Path) Disabled

func (p *Path) Disabled() bool

func (*Path) Ext

func (p *Path) Ext() string

func (Path) ForBundleType

func (p Path) ForBundleType(t PathType) *Path

func (*Path) Identifier

func (p *Path) Identifier(i int) string

func (*Path) IdentifierBase

func (p *Path) IdentifierBase() string

IdentifierBase satifies identity.Identity.

func (*Path) Identifiers

func (p *Path) Identifiers() []string

func (*Path) IsBranchBundle

func (p *Path) IsBranchBundle() bool

func (*Path) IsBundle

func (p *Path) IsBundle() bool

func (*Path) IsContent

func (p *Path) IsContent() bool

IsContent returns true if the path is a content file (e.g. mypost.md). Note that this will also return true for content files in a bundle.

func (*Path) IsHTML

func (p *Path) IsHTML() bool

func (*Path) IsLeafBundle

func (p *Path) IsLeafBundle() bool

func (*Path) Lang

func (p *Path) Lang() string

func (*Path) Name

func (p *Path) Name() string

Name returns the last element of path.

func (*Path) NameNoExt

func (p *Path) NameNoExt() string

Name returns the last element of path withhout any extension.

func (*Path) NameNoIdentifier

func (p *Path) NameNoIdentifier() string

NameNoIdentifier returns the last element of path withhout any identifier (e.g. no extension).

func (*Path) NameNoLang

func (p *Path) NameNoLang() string

Name returns the last element of path withhout any language identifier.

func (*Path) Path

func (p *Path) Path() (d string)

Path returns the full path.

func (*Path) PathNoIdentifier

func (p *Path) PathNoIdentifier() string

PathNoIdentifier returns the Path but with any identifier (ext, lang) removed.

func (*Path) PathNoLang

func (p *Path) PathNoLang() string

PathNoLang returns the Path but with any language identifier removed.

func (*Path) PathRel

func (p *Path) PathRel(owner *Path) string

PathRel returns the path relativeto the given owner.

func (*Path) Section

func (p *Path) Section() string

Section returns the first path element (section).

func (Path) TrimLeadingSlash

func (p Path) TrimLeadingSlash() *Path

TrimLeadingSlash returns a copy of the Path with the leading slash removed.

func (*Path) Unnormalized

func (p *Path) Unnormalized() *Path

Unnormalized returns the Path with the original case preserved.

type PathParser

type PathParser struct {
	// Maps the language code to its index in the languages/sites slice.
	LanguageIndex map[string]int

	// Reports whether the given language is disabled.
	IsLangDisabled func(string) bool
}

PathParser parses a path into a Path.

func (*PathParser) Parse

func (pp *PathParser) Parse(c, s string) *Path

Parse parses component c with path s into Path using Hugo's content path rules.

func (*PathParser) ParseBaseAndBaseNameNoIdentifier

func (pp *PathParser) ParseBaseAndBaseNameNoIdentifier(c, s string) (string, string)

ParseBaseAndBaseNameNoIdentifier parses component c with path s into a base and a base name without any identifier.

func (*PathParser) ParseIdentity

func (pp *PathParser) ParseIdentity(c, s string) identity.StringIdentity

ParseIdentity parses component c with path s into a StringIdentity.

type PathType

type PathType int
const (
	// A generic resource, e.g. a JSON file.
	PathTypeFile PathType = iota

	// All below are content files.
	// A resource of a content type with front matter.
	PathTypeContentResource

	// E.g. /blog/my-post.md
	PathTypeContentSingle

	// Leaf bundles, e.g. /blog/my-post/index.md
	PathTypeLeaf

	// Branch bundles, e.g. /blog/_index.md
	PathTypeBranch
)

func (PathType) String

func (i PathType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL