ref: 14bce119b60ee6d78a92bfe9361328156d310e57
parent: 7436829b82449fff6da85e82aac0e9b353b24172
author: Ahsanul Haque <[email protected]>
date: Fri Dec 12 03:57:22 EST 2014
Commented helpers package
--- a/helpers/content.go
+++ b/helpers/content.go
@@ -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 @@
// 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 @@
}
}
+// 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 @@
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 @@
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) {
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -64,6 +64,7 @@
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)
@@ -70,6 +71,7 @@
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)
@@ -76,10 +78,12 @@
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 @@
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))
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -65,6 +65,7 @@
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 @@
}
}
-// /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 @@
}
}
-//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 @@
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 @@
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)
--- a/helpers/pygments.go
+++ b/helpers/pygments.go
@@ -25,6 +25,7 @@
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 @@
return true
}
+//Highlight takes some code and returns highlighted code.
func Highlight(code string, lexer string) string {
if !HasPygments() {