mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
Commenting helpers package
This commit is contained in:
parent
b11838da3f
commit
2c8e9a7931
4 changed files with 29 additions and 10 deletions
|
@ -11,6 +11,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
//Package helpers implements general utility functions that work with and on content.
|
||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -26,9 +27,13 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Length of the summary that Hugo extracts from a content.
|
||||||
var SummaryLength = 70
|
var SummaryLength = 70
|
||||||
|
|
||||||
|
// Custom divider "<!--more-->" let's user define where summarization ends.
|
||||||
var SummaryDivider = []byte("<!--more-->")
|
var SummaryDivider = []byte("<!--more-->")
|
||||||
|
|
||||||
|
//StripHTML accepts a string, strips out all HTML tags and returns it.
|
||||||
func StripHTML(s string) string {
|
func StripHTML(s string) string {
|
||||||
output := ""
|
output := ""
|
||||||
|
|
||||||
|
@ -61,10 +66,12 @@ func StripHTML(s string) string {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StripEmptyNav strips out empty <nav> tags from content.
|
||||||
func StripEmptyNav(in []byte) []byte {
|
func StripEmptyNav(in []byte) []byte {
|
||||||
return bytes.Replace(in, []byte("<nav>\n</nav>\n\n"), []byte(``), -1)
|
return bytes.Replace(in, []byte("<nav>\n</nav>\n\n"), []byte(``), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BytesToHTML converts bytes to type template.HTML.
|
||||||
func BytesToHTML(b []byte) template.HTML {
|
func BytesToHTML(b []byte) template.HTML {
|
||||||
return template.HTML(string(b))
|
return template.HTML(string(b))
|
||||||
}
|
}
|
||||||
|
@ -109,6 +116,7 @@ func MarkdownRenderWithTOC(content []byte, documentId string) []byte {
|
||||||
GetMarkdownExtensions())
|
GetMarkdownExtensions())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ExtractTOC extracts Table of Contents from content.
|
||||||
func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
|
func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
|
||||||
origContent := make([]byte, len(content))
|
origContent := make([]byte, len(content))
|
||||||
copy(origContent, content)
|
copy(origContent, content)
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//Filepath separator defined by os.Separator.
|
||||||
const FilePathSeparator = string(filepath.Separator)
|
const FilePathSeparator = string(filepath.Separator)
|
||||||
|
|
||||||
func FindAvailablePort() (*net.TCPAddr, error) {
|
func FindAvailablePort() (*net.TCPAddr, error) {
|
||||||
|
@ -39,6 +40,7 @@ func FindAvailablePort() (*net.TCPAddr, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InStringArray checks if a string is an element of a slice of strings and returns a boolean value.
|
||||||
func InStringArray(arr []string, el string) bool {
|
func InStringArray(arr []string, el string) bool {
|
||||||
for _, v := range arr {
|
for _, v := range arr {
|
||||||
if v == el {
|
if v == el {
|
||||||
|
@ -48,6 +50,7 @@ func InStringArray(arr []string, el string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GuessType attempts to guess the type of file from a given string.
|
||||||
func GuessType(in string) string {
|
func GuessType(in string) string {
|
||||||
switch strings.ToLower(in) {
|
switch strings.ToLower(in) {
|
||||||
case "md", "markdown", "mdown":
|
case "md", "markdown", "mdown":
|
||||||
|
|
|
@ -70,7 +70,7 @@ func ReplaceExtension(path string, newExt string) string {
|
||||||
return f + "." + newExt
|
return f + "." + newExt
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if Exists && is Directory
|
// DirExists checks if a path exists and is a directory.
|
||||||
func DirExists(path string, fs afero.Fs) (bool, error) {
|
func DirExists(path string, fs afero.Fs) (bool, error) {
|
||||||
fi, err := fs.Stat(path)
|
fi, err := fs.Stat(path)
|
||||||
if err == nil && fi.IsDir() {
|
if err == nil && fi.IsDir() {
|
||||||
|
@ -82,6 +82,7 @@ func DirExists(path string, fs afero.Fs) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IsDir check if a given path is a directory.
|
||||||
func IsDir(path string, fs afero.Fs) (bool, error) {
|
func IsDir(path string, fs afero.Fs) (bool, error) {
|
||||||
fi, err := fs.Stat(path)
|
fi, err := fs.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -90,6 +91,7 @@ func IsDir(path string, fs afero.Fs) (bool, error) {
|
||||||
return fi.IsDir(), nil
|
return fi.IsDir(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IsEmpty checks if a given path is empty.
|
||||||
func IsEmpty(path string, fs afero.Fs) (bool, error) {
|
func IsEmpty(path string, fs afero.Fs) (bool, error) {
|
||||||
if b, _ := Exists(path, fs); !b {
|
if b, _ := Exists(path, fs); !b {
|
||||||
return false, fmt.Errorf("%q path does not exist", path)
|
return false, fmt.Errorf("%q path does not exist", path)
|
||||||
|
@ -114,7 +116,7 @@ func IsEmpty(path string, fs afero.Fs) (bool, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if File / Directory Exists
|
// Check if a file or directory exists.
|
||||||
func Exists(path string, fs afero.Fs) (bool, error) {
|
func Exists(path string, fs afero.Fs) (bool, error) {
|
||||||
_, err := fs.Stat(path)
|
_, err := fs.Stat(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -151,6 +153,7 @@ func MakePathRelative(inPath string, possibleDirectories ...string) (string, err
|
||||||
return inPath, errors.New("Can't extract relative path, unknown prefix")
|
return inPath, errors.New("Can't extract relative path, unknown prefix")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Filename takes a path, strips out the extension and returns the name of the file.
|
||||||
func Filename(in string) (name string) {
|
func Filename(in string) (name string) {
|
||||||
name, _ = FileAndExt(in)
|
name, _ = FileAndExt(in)
|
||||||
return
|
return
|
||||||
|
@ -197,6 +200,7 @@ func FileAndExtSep(in, ext, base, pathSeparator string) (name string) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GetRelativePath returns the relative path of a given path.
|
||||||
func GetRelativePath(path, base string) (final string, err error) {
|
func GetRelativePath(path, base string) (final string, err error) {
|
||||||
if filepath.IsAbs(path) && base == "" {
|
if filepath.IsAbs(path) && base == "" {
|
||||||
return "", errors.New("source: missing base directory")
|
return "", errors.New("source: missing base directory")
|
||||||
|
@ -275,6 +279,7 @@ func PrettifyPath(in string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//FindCWD returns the current working directory from where the Hugo executable is run from.
|
||||||
func FindCWD() (string, error) {
|
func FindCWD() (string, error) {
|
||||||
serverFile, err := filepath.Abs(os.Args[0])
|
serverFile, err := filepath.Abs(os.Args[0])
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//SanitizeUrl sanitizes the input URL string.
|
||||||
func SanitizeUrl(in string) string {
|
func SanitizeUrl(in string) string {
|
||||||
url, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
|
url, err := purell.NormalizeURLString(in, purell.FlagsSafe|purell.FlagRemoveTrailingSlash|purell.FlagRemoveDotSegments|purell.FlagRemoveDuplicateSlashes|purell.FlagRemoveUnnecessaryHostDots|purell.FlagRemoveEmptyPortSeparator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -46,7 +47,7 @@ func Urlize(uri string) string {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combines a base with a path
|
// Combines base URL with content path to create full URL paths.
|
||||||
// Example
|
// Example
|
||||||
// base: http://spf13.com/
|
// base: http://spf13.com/
|
||||||
// path: post/how-i-blog
|
// path: post/how-i-blog
|
||||||
|
@ -95,7 +96,7 @@ func UrlPrep(ugly bool, in string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't Return /index.html portion.
|
// PrettifyUrl takes a URL string and returns a semantic, clean URL.
|
||||||
func PrettifyUrl(in string) string {
|
func PrettifyUrl(in string) string {
|
||||||
x := PrettifyUrlPath(in)
|
x := PrettifyUrlPath(in)
|
||||||
|
|
||||||
|
@ -110,9 +111,10 @@ func PrettifyUrl(in string) string {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
// /section/name.html -> /section/name/index.html
|
//PrettifyUrlPath takes a URL path to a content and converts it to enable pretty URLS.
|
||||||
// /section/name/ -> /section/name/index.html
|
// /section/name.html becomes /section/name/index.html
|
||||||
// /section/name/index.html -> /section/name/index.html
|
// /section/name/ becomes /section/name/index.html
|
||||||
|
// /section/name/index.html becomes /section/name/index.html
|
||||||
func PrettifyUrlPath(in string) string {
|
func PrettifyUrlPath(in string) string {
|
||||||
if path.Ext(in) == "" {
|
if path.Ext(in) == "" {
|
||||||
// /section/name/ -> /section/name/index.html
|
// /section/name/ -> /section/name/index.html
|
||||||
|
@ -132,9 +134,10 @@ func PrettifyUrlPath(in string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /section/name/index.html -> /section/name.html
|
//Uglify does the opposite of PrettifyPath().
|
||||||
// /section/name/ -> /section/name.html
|
// /section/name/index.html becomes /section/name.html
|
||||||
// /section/name.html -> /section/name.html
|
// /section/name/ becomes /section/name.html
|
||||||
|
// /section/name.html becomes /section/name.html
|
||||||
func Uglify(in string) string {
|
func Uglify(in string) string {
|
||||||
if path.Ext(in) == "" {
|
if path.Ext(in) == "" {
|
||||||
if len(in) < 2 {
|
if len(in) < 2 {
|
||||||
|
|
Loading…
Reference in a new issue