<commit_before>package notable

import (
	"bytes"
	"fmt"
	mandrill "github.com/harvesthq/notable/Godeps/_workspace/src/github.com/keighl/mandrill"
	"log"
	"regexp"
	"strings"
	"text/template"
	"time"
)

type Variables struct {
	Today           string
	NotesByCategory []CategoryNotes
}

type CategoryNotes struct {
	Name  string
	Notes []Note
}

func (categoryNotes *CategoryNotes) Title() string {
	count := len(categoryNotes.Notes)
	announcements := pluralize(count, "Announcement")

	return fmt.Sprintf("#%s &mdash; %s", categoryNotes.Name, announcements)
}

func (note *Note) AbbreviatedAuthor() string {
	nameParts := strings.Fields(note.Author)

	if len(nameParts) > 0 {
		firstName := nameParts[0]
		lastName := nameParts[len(nameParts)-1]

		lastNameInitials := make([]string, 0)
		for _, name := range strings.Split(lastName, "-") {
			lastNameInitials = append(lastNameInitials, fmt.Sprintf("%s.", string(name[0])))
		}

		return fmt.Sprintf("%s %s", firstName, strings.Join(lastNameInitials, "-"))
	} else {
		return note.Author
	}
}

func Email() string {
	var html bytes.Buffer

	notesTemplate, err := template.ParseFiles("template.html")
	check(err)

	today := time.Now().Format("Monday January 2, 2006")
	variables := Variables{today, notesByCategory()}
	err = notesTemplate.Execute(&html, variables)
	check(err)

	autolinkRegexp := regexp.MustCompile(`(\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))))`)
	return autolinkRegexp.ReplaceAllString(html.String(), "<a href=\"$1\">$1</a>")
}

func SendEmail(apiKey string) {
	client := mandrill.ClientWithKey(apiKey)
	subject := pluralize(len(Notes()), "Notable Announcement")

	message := &mandrill.Message{}
	message.AddRecipient("<EMAIL>", "Harvest Team", "to")
	message.FromEmail = "<EMAIL>"
	message.FromName = "Harvest Notables"
	message.Subject = subject
	message.HTML = Email()

	_, err := client.MessagesSend(message)
	if err != nil {
		log.Print(err)
	}
}

func notesByCategory() []CategoryNotes {<commit_msg>Don't autolink URLs inside HTML attributes<commit_after>package notable

import (
	"bytes"
	"fmt"
	mandrill "github.com/harvesthq/notable/Godeps/_workspace/src/github.com/keighl/mandrill"
	"log"
	"regexp"
	"strings"
	"text/template"
	"time"
)

type Variables struct {
	Today           string
	NotesByCategory []CategoryNotes
}

type CategoryNotes struct {
	Name  string
	Notes []Note
}

func (categoryNotes *CategoryNotes) Title() string {
	count := len(categoryNotes.Notes)
	announcements := pluralize(count, "Announcement")

	return fmt.Sprintf("#%s &mdash; %s", categoryNotes.Name, announcements)
}

func (note *Note) AbbreviatedAuthor() string {
	nameParts := strings.Fields(note.Author)

	if len(nameParts) > 0 {
		firstName := nameParts[0]
		lastName := nameParts[len(nameParts)-1]

		lastNameInitials := make([]string, 0)
		for _, name := range strings.Split(lastName, "-") {
			lastNameInitials = append(lastNameInitials, fmt.Sprintf("%s.", string(name[0])))
		}

		return fmt.Sprintf("%s %s", firstName, strings.Join(lastNameInitials, "-"))
	} else {
		return note.Author
	}
}

func Email() string {
	var html bytes.Buffer

	notesTemplate, err := template.ParseFiles("template.html")
	check(err)

	today := time.Now().Format("Monday January 2, 2006")
	variables := Variables{today, notesByCategory()}
	err = notesTemplate.Execute(&html, variables)
	check(err)

	autolinkRegexp := regexp.MustCompile(`([^"])(\b([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))`)
	return autolinkRegexp.ReplaceAllString(html.String(), "$1<a href=\"$2\">$2</a>")
}

func SendEmail(apiKey string) {
	client := mandrill.ClientWithKey(apiKey)
	subject := pluralize(len(Notes()), "Notable Announcement")

	message := &mandrill.Message{}
	message.AddRecipient("<EMAIL>", "Harvest Team", "to")
	message.FromEmail = "<EMAIL>"
	message.FromName = "Harvest Notables"
	message.Subject = subject
	message.HTML = Email()

	_, err := client.MessagesSend(message)
	if err != nil {
		log.Print(err)
	}
}

func notesByCategory() []CategoryNotes {
{'fraction_non_alphanumeric': 0.1765435159930573, 'fraction_numerical': 0.0061988594098685845, 'mean_word_length': 5.057057057057057, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5333327', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1391, 'n_words': 277}