mirror of
https://github.com/gohugoio/hugo.git
synced 2024-07-05 00:32:03 +00:00
Commented helpers package
This commit is contained in:
parent
7436829b82
commit
14bce119b6
|
@ -11,7 +11,8 @@
|
||||||
// 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 implements general utility functions that work with and on content. The helper functions defined here
|
||||||
|
//lay down the foundation of how Hugo works with files, filepaths and does string operations on content.
|
||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -30,7 +31,7 @@ import (
|
||||||
// Length of the summary that Hugo extracts from a content.
|
// 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.
|
// 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.
|
//StripHTML accepts a string, strips out all HTML tags and returns it.
|
||||||
|
@ -174,10 +175,12 @@ func RenderBytes(content []byte, pagefmt string, documentId string) []byte {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TotalWords returns an int of the total number of words in a given content.
|
||||||
func TotalWords(s string) int {
|
func TotalWords(s string) int {
|
||||||
return len(strings.Fields(s))
|
return len(strings.Fields(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//WordCount takes content and returns a map of words and count of each word.
|
||||||
func WordCount(s string) map[string]int {
|
func WordCount(s string) map[string]int {
|
||||||
m := make(map[string]int)
|
m := make(map[string]int)
|
||||||
for _, f := range strings.Fields(s) {
|
for _, f := range strings.Fields(s) {
|
||||||
|
@ -187,10 +190,12 @@ func WordCount(s string) map[string]int {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//RemoveSummaryDivider removes summary-divider <!--more--> from content.
|
||||||
func RemoveSummaryDivider(content []byte) []byte {
|
func RemoveSummaryDivider(content []byte) []byte {
|
||||||
return bytes.Replace(content, SummaryDivider, []byte(""), -1)
|
return bytes.Replace(content, SummaryDivider, []byte(""), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TruncateWords takes content and na int and shortens down the number of words in the content down to the number of int.
|
||||||
func TruncateWords(s string, max int) string {
|
func TruncateWords(s string, max int) string {
|
||||||
words := strings.Fields(s)
|
words := strings.Fields(s)
|
||||||
if max > len(words) {
|
if max > len(words) {
|
||||||
|
@ -200,6 +205,7 @@ func TruncateWords(s string, max int) string {
|
||||||
return strings.Join(words[:max], " ")
|
return strings.Join(words[:max], " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TruncateWordsToWholeSentence takes content and an int and returns entire sentences from content, delimited by the int.
|
||||||
func TruncateWordsToWholeSentence(s string, max int) string {
|
func TruncateWordsToWholeSentence(s string, max int) string {
|
||||||
words := strings.Fields(s)
|
words := strings.Fields(s)
|
||||||
if max > len(words) {
|
if max > len(words) {
|
||||||
|
|
|
@ -64,22 +64,26 @@ func GuessType(in string) string {
|
||||||
return "unknown"
|
return "unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ReaderToBytes takes an io.Reader argument, reads from it and returns bytes.
|
||||||
func ReaderToBytes(lines io.Reader) []byte {
|
func ReaderToBytes(lines io.Reader) []byte {
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
b.ReadFrom(lines)
|
b.ReadFrom(lines)
|
||||||
return b.Bytes()
|
return b.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ReaderToString is the same as ReaderToBytes, but returns a string.
|
||||||
func ReaderToString(lines io.Reader) string {
|
func ReaderToString(lines io.Reader) string {
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
b.ReadFrom(lines)
|
b.ReadFrom(lines)
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//StringToReader does the opposite of ReaderToString.
|
||||||
func StringToReader(in string) io.Reader {
|
func StringToReader(in string) io.Reader {
|
||||||
return strings.NewReader(in)
|
return strings.NewReader(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BytesToReader does the opposite of ReaderToBytes.
|
||||||
func BytesToReader(in []byte) io.Reader {
|
func BytesToReader(in []byte) io.Reader {
|
||||||
return bytes.NewReader(in)
|
return bytes.NewReader(in)
|
||||||
}
|
}
|
||||||
|
@ -98,6 +102,7 @@ func SliceToLower(s []string) []string {
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Md5String takes a string and returns a MD5 Hash of it.
|
||||||
func Md5String(f string) string {
|
func Md5String(f string) string {
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
h.Write([]byte(f))
|
h.Write([]byte(f))
|
||||||
|
|
|
@ -65,6 +65,7 @@ func UnicodeSanitize(s string) string {
|
||||||
return string(target)
|
return string(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ReplaceExtension takes a path and an extension, strips the old extension and returns the path with the new extension.
|
||||||
func ReplaceExtension(path string, newExt string) string {
|
func ReplaceExtension(path string, newExt string) string {
|
||||||
f, _ := FileAndExt(path)
|
f, _ := FileAndExt(path)
|
||||||
return f + "." + newExt
|
return f + "." + newExt
|
||||||
|
@ -257,9 +258,10 @@ func PathPrep(ugly bool, in string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /section/name.html -> /section/name/index.html
|
// Same as PrettifyUrlPath() but for paths.
|
||||||
// /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 PrettifyPath(in string) string {
|
func PrettifyPath(in string) string {
|
||||||
if filepath.Ext(in) == "" {
|
if filepath.Ext(in) == "" {
|
||||||
// /section/name/ -> /section/name/index.html
|
// /section/name/ -> /section/name/index.html
|
||||||
|
@ -279,7 +281,7 @@ func PrettifyPath(in string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//FindCWD returns the current working directory from where the Hugo executable is run from.
|
//FindCWD returns the current working directory from where the Hugo executable is run.
|
||||||
func FindCWD() (string, error) {
|
func FindCWD() (string, error) {
|
||||||
serverFile, err := filepath.Abs(os.Args[0])
|
serverFile, err := filepath.Abs(os.Args[0])
|
||||||
|
|
||||||
|
@ -303,6 +305,7 @@ func FindCWD() (string, error) {
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Same as WriteToDisk but checks to see if file/directory already exists.
|
||||||
func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
|
func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
|
||||||
dir, _ := filepath.Split(inpath)
|
dir, _ := filepath.Split(inpath)
|
||||||
ospath := filepath.FromSlash(dir)
|
ospath := filepath.FromSlash(dir)
|
||||||
|
@ -332,6 +335,7 @@ func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Writes content to disk.
|
||||||
func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
|
func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
|
||||||
dir, _ := filepath.Split(inpath)
|
dir, _ := filepath.Split(inpath)
|
||||||
ospath := filepath.FromSlash(dir)
|
ospath := filepath.FromSlash(dir)
|
||||||
|
|
|
@ -25,6 +25,7 @@ import (
|
||||||
|
|
||||||
const pygmentsBin = "pygmentize"
|
const pygmentsBin = "pygmentize"
|
||||||
|
|
||||||
|
//HasPygments checks to see if Pygments is installed and available on the system.
|
||||||
func HasPygments() bool {
|
func HasPygments() bool {
|
||||||
if _, err := exec.LookPath(pygmentsBin); err != nil {
|
if _, err := exec.LookPath(pygmentsBin); err != nil {
|
||||||
return false
|
return false
|
||||||
|
@ -32,6 +33,7 @@ func HasPygments() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Highlight takes some code and returns highlighted code.
|
||||||
func Highlight(code string, lexer string) string {
|
func Highlight(code string, lexer string) string {
|
||||||
|
|
||||||
if !HasPygments() {
|
if !HasPygments() {
|
||||||
|
|
Loading…
Reference in a new issue