Exemplo n.º 1
0
func toMarkdown(input string) string {
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_USE_XHTML
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES

	renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")

	extensions := 0
	extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
	extensions |= blackfriday.EXTENSION_TABLES
	extensions |= blackfriday.EXTENSION_FENCED_CODE
	extensions |= blackfriday.EXTENSION_AUTOLINK
	extensions |= blackfriday.EXTENSION_STRIKETHROUGH
	extensions |= blackfriday.EXTENSION_SPACE_HEADERS
	extensions |= blackfriday.EXTENSION_HEADER_IDS

	b := blackfriday.Markdown([]byte(input), renderer, extensions)
	return string(b)

}
Exemplo n.º 2
0
func (md *MDDoc) convertToHTML(filename string) {
	f, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatal(err)
	}

	// first, let's read the part at the top of the page to get the page title
	pos := bytes.Index(f, []byte{'\n', '-', '-', '-'})
	lines := strings.Split(string(f[:pos]), "\n")
	kv := make(map[string]string)
	for _, line := range lines {
		tmp := key_value_regexp.FindAllStringSubmatch(line, -1)
		if len(tmp) == 1 {
			kv[tmp[0][1]] = tmp[0][2]
		}
	}
	funcMap := template.FuncMap{
		"rootDoc":   md.rootDoc,
		"filelist":  md.filelist,
		"img":       md.image,
		"parentdir": md.parentdir,
		"thumbnail": md.thumbnail,
	}

	// Now we can parse the template
	x := template.Must(template.New("name").Funcs(funcMap).Parse(string(f[pos+4:])))
	x.ParseGlob(filepath.Join(md.basedir, "templatesmd", "*.md"))

	dataMd := mdTemplateData{
		Changelog:      md.changelog,
		Sourcefilename: filename,
		KeyValue:       kv,
		IsEn:           isEn(filename),
	}

	bb := new(bytes.Buffer)
	err = x.Execute(bb, dataMd)
	if err != nil {
		log.Fatal(err)
	}

	out := blackfriday.Markdown(bb.Bytes(), md.renderer, blackfriday.EXTENSION_FENCED_CODE|blackfriday.EXTENSION_TABLES)

	extension := filepath.Ext(filename)
	basename := strings.TrimLeft(filename[0:len(filename)-len(extension)], md.root)
	outfilename := filepath.Join(md.dest, basename+".html")

	err = os.MkdirAll(filepath.Dir(outfilename), 0755)
	if err != nil {
		log.Fatal(err)
	}

	outfile, err := os.OpenFile(outfilename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
	if err != nil {
		log.Fatal(err)
	}

	data := htmlTemplateData{
		Title:          kv["title"],
		Contents:       string(out),
		Layout:         kv["layout"],
		Sourcefilename: outfilename,
		IsEn:           isEn(filename),
	}
	err = templates.ExecuteTemplate(outfile, "main.html", data)
	if err != nil {
		log.Fatal(err)
	}
}