mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Commented helpers package
This commit is contained in:
parent
7436829b82
commit
14bce119b6
4 changed files with 23 additions and 6 deletions
|
@ -11,7 +11,8 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// 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
|
||||
|
||||
import (
|
||||
|
@ -30,7 +31,7 @@ import (
|
|||
// Length of the summary that Hugo extracts from a content.
|
||||
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-->")
|
||||
|
||||
//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 {
|
||||
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 {
|
||||
m := make(map[string]int)
|
||||
for _, f := range strings.Fields(s) {
|
||||
|
@ -187,10 +190,12 @@ func WordCount(s string) map[string]int {
|
|||
return m
|
||||
}
|
||||
|
||||
//RemoveSummaryDivider removes summary-divider <!--more--> from content.
|
||||
func RemoveSummaryDivider(content []byte) []byte {
|
||||
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 {
|
||||
words := strings.Fields(s)
|
||||
if max > len(words) {
|
||||
|
@ -200,6 +205,7 @@ func TruncateWords(s string, max int) string {
|
|||
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 {
|
||||
words := strings.Fields(s)
|
||||
if max > len(words) {
|
||||
|
|
|
@ -64,22 +64,26 @@ func GuessType(in string) string {
|
|||
return "unknown"
|
||||
}
|
||||
|
||||
//ReaderToBytes takes an io.Reader argument, reads from it and returns bytes.
|
||||
func ReaderToBytes(lines io.Reader) []byte {
|
||||
b := new(bytes.Buffer)
|
||||
b.ReadFrom(lines)
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
//ReaderToString is the same as ReaderToBytes, but returns a string.
|
||||
func ReaderToString(lines io.Reader) string {
|
||||
b := new(bytes.Buffer)
|
||||
b.ReadFrom(lines)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
//StringToReader does the opposite of ReaderToString.
|
||||
func StringToReader(in string) io.Reader {
|
||||
return strings.NewReader(in)
|
||||
}
|
||||
|
||||
//BytesToReader does the opposite of ReaderToBytes.
|
||||
func BytesToReader(in []byte) io.Reader {
|
||||
return bytes.NewReader(in)
|
||||
}
|
||||
|
@ -98,6 +102,7 @@ func SliceToLower(s []string) []string {
|
|||
return l
|
||||
}
|
||||
|
||||
//Md5String takes a string and returns a MD5 Hash of it.
|
||||
func Md5String(f string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(f))
|
||||
|
|
|
@ -65,6 +65,7 @@ func UnicodeSanitize(s string) string {
|
|||
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 {
|
||||
f, _ := FileAndExt(path)
|
||||
return f + "." + newExt
|
||||
|
@ -257,9 +258,10 @@ func PathPrep(ugly bool, in string) string {
|
|||
}
|
||||
}
|
||||
|
||||
// /section/name.html -> /section/name/index.html
|
||||
// /section/name/ -> /section/name/index.html
|
||||
// /section/name/index.html -> /section/name/index.html
|
||||
// Same as PrettifyUrlPath() but for paths.
|
||||
// /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 PrettifyPath(in string) string {
|
||||
if filepath.Ext(in) == "" {
|
||||
// /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) {
|
||||
serverFile, err := filepath.Abs(os.Args[0])
|
||||
|
||||
|
@ -303,6 +305,7 @@ func FindCWD() (string, error) {
|
|||
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) {
|
||||
dir, _ := filepath.Split(inpath)
|
||||
ospath := filepath.FromSlash(dir)
|
||||
|
@ -332,6 +335,7 @@ func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
// Writes content to disk.
|
||||
func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
|
||||
dir, _ := filepath.Split(inpath)
|
||||
ospath := filepath.FromSlash(dir)
|
||||
|
|
|
@ -25,6 +25,7 @@ import (
|
|||
|
||||
const pygmentsBin = "pygmentize"
|
||||
|
||||
//HasPygments checks to see if Pygments is installed and available on the system.
|
||||
func HasPygments() bool {
|
||||
if _, err := exec.LookPath(pygmentsBin); err != nil {
|
||||
return false
|
||||
|
@ -32,6 +33,7 @@ func HasPygments() bool {
|
|||
return true
|
||||
}
|
||||
|
||||
//Highlight takes some code and returns highlighted code.
|
||||
func Highlight(code string, lexer string) string {
|
||||
|
||||
if !HasPygments() {
|
||||
|
|
Loading…
Reference in a new issue