Example #1
0
// Digest computes the digest from provided slices of open and
// closed pull requests.
func Digest(c *Context, open, closed []*PullRequest) error {
	sortedOpen := PullRequests(open)
	sortedClosed := PullRequests(closed)
	sort.Sort(sortedOpen)
	sort.Sort(sortedClosed)

	// Open file for digest HTML.
	now := time.Now()
	content := struct {
		Open   []*PullRequest
		Closed []*PullRequest
	}{
		Open:   sortedOpen,
		Closed: sortedClosed,
	}
	htmlTemplate, err := ioutil.ReadFile(c.Template)
	if err != nil {
		return fmt.Errorf("failed to read template file %q: %s", c.Template, err)
	}
	tmpl := template.Must(template.New("digest").Funcs(template.FuncMap{"markDown": markDowner}).Parse(string(htmlTemplate)))

	buf := new(bytes.Buffer)
	if err := tmpl.Execute(buf, content); err != nil {
		return err
	}

	contents := buf.String()

	if c.InlineStyles {
		options := premailer.NewOptions()
		options.CssToAttributes = true
		prem := premailer.NewPremailerFromString(buf.String(), options)
		contents, err = prem.Transform()
		if err != nil {
			return err
		}
	}

	f, err := createFile(c.OutDir, fmt.Sprintf("digest-%s.html", now.Format("01-02-2006")))
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.WriteString(contents)
	if err != nil {
		return err
	}

	fmt.Fprintf(os.Stdout, "digest: %s\n", f.Name())
	return nil
}
Example #2
0
func setHtmlBody(message *sendgrid.SGMail, inputString string) {
	// This fixes a bug in andybalholm/cascadia in dealing with :: in css for somethings.
	regex := regexp.MustCompile("(?m)^.*::.*$")
	inputString = regex.ReplaceAllLiteralString(inputString, "")

	// This turns stylesheets into inline styles so email clients respect the css.
	prem := premailer.NewPremailerFromString(inputString, premailer.NewOptions())
	htmlString, err := prem.Transform()
	if err != nil {
		log.Fatal(err)
	}

	message.SetHTML(htmlString)
	log.Println("This is HTML")
}
Example #3
0
func main() {
	var (
		host string
		port int
	)

	flag.StringVar(&host, "h", "127.0.0.1", "Host to listen on")
	flag.IntVar(&port, "p", 8080, "Port number to listen on")
	flag.Parse()

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, os.Kill, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR2)
	app := r2router.NewSeeforRouter()

	r := render.New()

	app.Get("/", func(w http.ResponseWriter, req *http.Request, _ r2router.Params) {
		r.JSON(w, http.StatusOK, r2router.M{"usage": "POST / html=HTML&cssToAttributes=boolean&removeClasses=boolean"})
	})
	app.Post("/", func(w http.ResponseWriter, req *http.Request, _ r2router.Params) {
		req.ParseForm()
		html := req.Form.Get("html")
		cssToAttributes := req.Form.Get("cssToAttributes")
		removeClasses := req.Form.Get("removeClasses")
		var result string
		if html != "" {
			options := premailer.NewOptions()
			if removeClasses == "true" {
				options.RemoveClasses = true
			}
			if cssToAttributes == "false" {
				options.CssToAttributes = false
			}
			pre := premailer.NewPremailerFromString(html, options)
			result, _ = pre.Transform()
		} else {
			result = ""
		}
		r.JSON(w, http.StatusOK, r2router.M{"result": result})
	})

	log.Printf("listening to address %s:%d", host, port)
	go http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), app)
	sig := <-sigc
	log.Printf("Got signal: %s", sig)

}
Example #4
0
func main() {
	var (
		inputFile           string
		outputFile          string
		removeClasses       bool
		skipCssToAttributes bool
	)
	flag.StringVar(&inputFile, "i", "", "Input file")
	flag.StringVar(&outputFile, "o", "", "Output file")
	flag.BoolVar(&removeClasses, "remove-classes", false, "Remove class attribute")
	flag.BoolVar(&skipCssToAttributes, "skip-css-to-attributes", false, "No copy of css property to html attribute")
	flag.Parse()
	if inputFile == "" {
		flag.Usage()
		return
	}
	start := time.Now()
	options := premailer.NewOptions()
	options.RemoveClasses = removeClasses
	options.CssToAttributes = !skipCssToAttributes
	prem := premailer.NewPremailerFromFile(inputFile, options)
	html, err := prem.Transform()
	log.Printf("took: %v", time.Now().Sub(start))
	if err != nil {
		log.Fatal(err)
	}
	if outputFile != "" {
		fd, err := os.Create(outputFile)
		if err != nil {
			log.Fatal(err)
		}
		defer fd.Close()
		fd.WriteString(html)
	} else {
		fmt.Println(html)
	}
}
Example #5
0
func main() {
	from, username, password := loadEnvironment()

	to := flag.String("to", "", "Recipients in To")
	cc := flag.String("cc", "", "Recipients in CC")
	bcc := flag.String("bcc", "", "Recipients in BCC")
	subject := flag.String("s", "", "Subject")
	isHtml := flag.Bool("html", false, "Send as HTML")
	help := flag.Bool("h", false, "Display this help message")

	flag.Parse()

	if *help {
		flag.Usage()
		os.Exit(0)
	}

	if *to == "" && *cc == "" && *bcc == "" {
		log.Println("Need atleast one of to,cc,bcc")
		flag.Usage()
		os.Exit(2)
	}

	if *subject == "" {
		log.Println("Subject cannot be empty")
		flag.Usage()
		os.Exit(3)
	}

	sg := sendgrid.NewSendGridClient(username, password)
	message := sendgrid.NewMail()

	message.SetFrom(from)
	message.AddRecipients(parseRecipients(to))
	message.AddCcRecipients(parseRecipients(cc))
	message.AddBccRecipients(parseRecipients(bcc))
	message.SetSubject(*subject)

	b, err := ioutil.ReadAll(os.Stdin)

	if err != nil {
		log.Panic("Could not read stdin")
	}

	inputString := string(b)

	if *isHtml {
		// This fixes a bug in andybalholm/cascadia in dealing with :: in css for somethings.
		regex := regexp.MustCompile("(?m)^.*::.*$")
		inputString = regex.ReplaceAllLiteralString(inputString, "")

		// This turns stylesheets into inline styles so email clients respect the css.
		prem := premailer.NewPremailerFromString(inputString, premailer.NewOptions())
		htmlString, err := prem.Transform()
		if err != nil {
			log.Fatal(err)
		}

		message.SetHTML(htmlString)
		log.Println("This is HTML")
	} else {
		message.SetText(inputString)
		log.Println("This is Text")
	}

	//	fmt.Println ("SG",sg)
	if r := sg.Send(message); r == nil {
		fmt.Println("Email sent!")
	} else {
		fmt.Println(r)
	}
}