diff options
author | Ahsanul Haque <ahsanul@gmail.com> | 2014-12-12 05:57:22 +0300 |
---|---|---|
committer | spf13 <steve.francia@gmail.com> | 2014-12-19 06:18:36 +0300 |
commit | 14bce119b60ee6d78a92bfe9361328156d310e57 (patch) | |
tree | d62c04e2e85020876e003bb0cbf84b8f410ea361 /helpers | |
parent | 7436829b82449fff6da85e82aac0e9b353b24172 (diff) |
Commented helpers package
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/content.go | 10 | ||||
-rw-r--r-- | helpers/general.go | 5 | ||||
-rw-r--r-- | helpers/path.go | 12 | ||||
-rw-r--r-- | helpers/pygments.go | 2 |
4 files changed, 23 insertions, 6 deletions
diff --git a/helpers/content.go b/helpers/content.go index 1b0aea326..d5e507dae 100644 --- 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 @@ 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) { diff --git a/helpers/general.go b/helpers/general.go index c1a6e034f..e2f826f45 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -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)) diff --git a/helpers/path.go b/helpers/path.go index ec6da75e2..10f18ded5 100644 --- a/helpers/path.go +++ b/helpers/path.go @@ -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) diff --git a/helpers/pygments.go b/helpers/pygments.go index bb7790533..b31c834c6 100644 --- a/helpers/pygments.go +++ b/helpers/pygments.go @@ -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() { |