github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/lib/helpers/helpers.go (about)

     1  // Package helpers contains view helpers
     2  package helpers
     3  
     4  import (
     5  	"fmt"
     6  	"html/template"
     7  	"time"
     8  
     9  	"github.com/fragmenta/server/config"
    10  	"github.com/fragmenta/view/helpers"
    11  
    12  	"github.com/bitcubate/cryptojournal/src/lib/text"
    13      "crypto/md5"
    14  	"encoding/hex"
    15  	"io"
    16  	"strings"
    17  )
    18  
    19  // This function takes in an email address as a parameter and returns a complete url to the image.
    20  // for example: GetImg("example@example.com") will return this: http://www.gravatar.com/avatar/563dd2c62e66d42a3b4454c410a0b3ab?d=identicon
    21  func GetImg(email string) string {
    22  	email = strings.TrimSpace(email)
    23  	email = strings.ToLower(email)
    24  
    25  	h := md5.New()
    26  	io.WriteString(h, email)
    27  
    28  	finalBytes := h.Sum(nil)
    29  
    30  	finalString := "http://www.gravatar.com/avatar/" + hex.EncodeToString(finalBytes) + "?d=retro&s=256"
    31  
    32  	return finalString
    33  }
    34  
    35  // This function takes in an email address as a parameter and returns just the hash, which you can use to your liking.
    36  func GetHash(email string) string {
    37  	email = strings.TrimSpace(email)
    38  	email = strings.ToLower(email)
    39  
    40  	h := md5.New()
    41  	io.WriteString(h, email)
    42  
    43  	finalBytes := h.Sum(nil)
    44  
    45  	finalString := hex.EncodeToString(finalBytes)
    46  
    47  	return finalString
    48  }
    49  
    50  // RootURL returns the root url
    51  func RootURL() string {
    52  	return config.Get("root_url")
    53  }
    54  
    55  // Markup converts text from stories into sanitized html
    56  func Markup(s string) template.HTML {
    57  
    58  	// Convert bare links and usernames to anchors
    59  	s = text.ConvertLinks(s)
    60  
    61  	// Convert newlimnes to paragraph tags
    62  	s = text.ConvertNewlines(s)
    63  
    64  	// Run sanitize on the resulting string
    65  	// (parses html and removes unwated tags and attributes)
    66  	return helpers.Sanitize(s)
    67  }
    68  
    69  // TimeAgo returns a string for a time in format x seconds ago
    70  func TimeAgo(d time.Time) string {
    71  
    72  	duration := time.Since(d)
    73  	hours := duration / time.Hour
    74  
    75  	switch {
    76  	case duration < time.Minute:
    77  		return fmt.Sprintf("%d seconds ago", duration/time.Second)
    78  	case duration < time.Hour:
    79  		return fmt.Sprintf("%d minutes ago", duration/time.Minute)
    80  	case duration < time.Hour*24:
    81  		unit := "hour"
    82  		if hours > 1 {
    83  			unit = "hours"
    84  		}
    85  		return fmt.Sprintf("%d %s ago", hours, unit)
    86  	default:
    87  		unit := "day"
    88  		if hours > 48 {
    89  			unit = "days"
    90  		}
    91  		return fmt.Sprintf("%d %s ago", hours/24, unit)
    92  	}
    93  
    94  }