Beispiel #1
0
// serveFile serves a file from disk, converting any markdown to HTML.
func serveFile(w http.ResponseWriter, r *http.Request, relPath, absPath string) {
	if !strings.HasSuffix(absPath, ".html") && !strings.HasSuffix(absPath, ".md") {
		http.ServeFile(w, r, absPath)
		return
	}

	data, err := ioutil.ReadFile(absPath)
	if err != nil {
		serveError(w, r, absPath, err)
		return
	}

	var markdownRenderer = blackfriday.HtmlRenderer(markdownHTMLFlags, "", "")
	data = blackfriday.MarkdownOptions(data, markdownRenderer, blackfriday.Options{Extensions: markdownExtensions})

	title := ""
	if m := h1TitlePattern.FindSubmatch(data); len(m) > 1 {
		title = string(m[1])
	}

	servePage(w, pageParams{
		title:   title,
		content: data,
	})
}
Beispiel #2
0
func pageRenderMarkdown(content string) (res string) {
	renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
	unsafe := blackfriday.MarkdownOptions([]byte(content), renderer, blackfriday.Options{
		Extensions: markdownExtensions})
	res = string(unsafe)
	return
}
Beispiel #3
0
func parseMarkdown(input string) string {
	if input == "" {
		return ""
	}

	ret := blackfriday.MarkdownOptions([]byte(template.HTMLEscapeString(input)), mdRenderer, mdOptions)

	return string(ret)
}
Beispiel #4
0
func (instance *Renderer) renderMarkdownWithContext(markup string, context Describeable, headerTypeStart int, headerIDPrefix string) (template.HTML, error) {
	var err error

	markup = headerPrefixPattern.ReplaceAllString(markup, "$1"+strings.Repeat("#", headerTypeStart))
	markup = refPropertyPattern.ReplaceAllStringFunc(markup, func(inline string) string {
		match := refPropertyPattern.FindStringSubmatch(inline)
		ref := match[1]
		idType := instance.resolveRef(ref, context)
		element, pErr := instance.PickedDefinitions.GetSourceElementBy(idType)
		if pErr != nil {
			err = pErr
			return inline
		}
		if element != nil {
			targetType := instance.getDisplayIDOf(element)
			display := strings.TrimSpace(match[2])
			if len(display) <= 0 {
				display = targetType
			}
			return "[``" + display + "``](#configuration.dataType." + targetType + ")"
		}
		err = errors.New("Unknonwn reference: %v", ref)
		return markup
	})
	if err != nil {
		return "", err
	}
	prefix := ""
	if len(headerIDPrefix) > 0 {
		prefix = headerIDPrefix + "."
	}
	renderer := blackfriday.HtmlRendererWithParameters(blackfriday.HTML_USE_XHTML|
		blackfriday.HTML_USE_SMARTYPANTS|
		blackfriday.HTML_SMARTYPANTS_FRACTIONS|
		blackfriday.HTML_SMARTYPANTS_DASHES|
		blackfriday.HTML_SMARTYPANTS_LATEX_DASHES,
		"",
		"",
		blackfriday.HtmlRendererParameters{
			HeaderIDPrefix: prefix,
		},
	)
	html := blackfriday.MarkdownOptions([]byte(markup), renderer, blackfriday.Options{
		Extensions: blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
			blackfriday.EXTENSION_TABLES |
			blackfriday.EXTENSION_FENCED_CODE |
			blackfriday.EXTENSION_AUTOLINK |
			blackfriday.EXTENSION_STRIKETHROUGH |
			blackfriday.EXTENSION_SPACE_HEADERS |
			blackfriday.EXTENSION_HEADER_IDS |
			blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
			blackfriday.EXTENSION_DEFINITION_LISTS |
			blackfriday.EXTENSION_AUTO_HEADER_IDS,
	})
	return template.HTML(strings.TrimSpace(string(html))), nil
}
func templateBlackfriday(mkd string) template.HTML {
	//Set up the HTML renderer
	renderer := blackfriday.HtmlRenderer(flags, "", "").(*blackfriday.Html)
	wrappedRenderer := &MarkdownRenderer{
		Html: renderer,
	}

	res := blackfriday.MarkdownOptions([]byte(mkd), wrappedRenderer, blackfriday.Options{
		Extensions: extensions,
	})

	return template.HTML(res)
}
Beispiel #6
0
// toHTML reads a markdown file and returns a HTML string.
func toHTML(filename string) (string, error) {
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		return "", err
	}
	// Use standard HTML rendering.
	renderer := blackfriday.HtmlRenderer(blackfriday.HTML_USE_XHTML, "", "")
	// Parse markdown where all id's are created from the values inside
	// the element tag.
	buf = blackfriday.MarkdownOptions(buf, renderer, blackfriday.Options{
		Extensions: blackfriday.EXTENSION_AUTO_HEADER_IDS | blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE,
	})
	return string(buf), nil
}
Beispiel #7
0
func htmlFromMarkdown(md string) string {
	if len(md) == 0 {
		return md
	}

	// Render markdown to HTML
	//
	htmlFlags := 0 |
		blackfriday.HTML_USE_XHTML |
		blackfriday.HTML_USE_SMARTYPANTS |
		blackfriday.HTML_SMARTYPANTS_FRACTIONS |
		blackfriday.HTML_SMARTYPANTS_LATEX_DASHES

	markdownExtensions := 0 |
		blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
		blackfriday.EXTENSION_FENCED_CODE |
		blackfriday.EXTENSION_STRIKETHROUGH |
		blackfriday.EXTENSION_SPACE_HEADERS

	// Leading whitespace can trigger Markdown features like
	// code blocks so let's avoid that by trimming the input
	//
	ret := string(blackfriday.MarkdownOptions(
		[]byte(strings.TrimSpace(md)),
		blackfriday.HtmlRenderer(htmlFlags, "", ""),
		blackfriday.Options{Extensions: markdownExtensions}))

	// Remove wrapping paragraph tags
	//
	ret = strings.TrimSpace(ret)
	if strings.HasPrefix(ret, "<p>") && strings.HasSuffix(ret, "</p>") {
		ret = ret[3 : len(ret)-4]
	}

	// Reinstate possible leading/trailing whitespace
	// (the markdown compiler will have removed it)
	//
	if leadingSpaces := util.LeadingWhitespace(md); 0 < len(leadingSpaces) {
		ret = leadingSpaces + ret
	}
	if trailingSpaces := util.TrailingWhitespace(md); 0 < len(trailingSpaces) {
		ret = ret + trailingSpaces
	}

	return ret
}
Beispiel #8
0
func markdown(in io.Reader, out io.Writer) error {
	buf, err := ioutil.ReadAll(in)
	if err != nil {
		return err
	}
	flg := commonHTMLFlags
	if *toc {
		flg |= md.HTML_TOC
	}
	render := md.HtmlRenderer(flg, "", css)
	body := md.MarkdownOptions(buf, render, md.Options{
		Extensions: commonExtensions,
	})
	m := map[string]interface{}{
		"css":  css,
		"body": string(body),
	}
	return template.Must(template.New("markdown").Parse(tpl)).Execute(out, m)
}
Beispiel #9
0
// compileMarkdown will replace tags like <ac:rich-tech-body> with escaped
// equivalent, because blackfriday markdown parser replaces that tags with
// <a href="ac:rich-text-body">ac:rich-text-body</a> for whatever reason.
func compileMarkdown(markdown []byte) []byte {
	colon := regexp.MustCompile(`---BLACKFRIDAY-COLON---`)

	tags := regexp.MustCompile(`<(/?\S+):(\S+)>`)

	markdown = tags.ReplaceAll(
		markdown,
		[]byte(`<$1`+colon.String()+`$2>`),
	)

	renderer := ConfluenceRenderer{
		blackfriday.HtmlRenderer(
			blackfriday.HTML_USE_XHTML|
				blackfriday.HTML_USE_SMARTYPANTS|
				blackfriday.HTML_SMARTYPANTS_FRACTIONS|
				blackfriday.HTML_SMARTYPANTS_DASHES|
				blackfriday.HTML_SMARTYPANTS_LATEX_DASHES,
			"", "",
		),
	}

	html := blackfriday.MarkdownOptions(
		markdown,
		renderer,
		blackfriday.Options{
			Extensions: blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
				blackfriday.EXTENSION_TABLES |
				blackfriday.EXTENSION_FENCED_CODE |
				blackfriday.EXTENSION_AUTOLINK |
				blackfriday.EXTENSION_STRIKETHROUGH |
				blackfriday.EXTENSION_SPACE_HEADERS |
				blackfriday.EXTENSION_HEADER_IDS |
				blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
				blackfriday.EXTENSION_DEFINITION_LISTS,
		},
	)

	html = colon.ReplaceAll(html, []byte(`:`))

	return html
}
Beispiel #10
0
func main() {
	baseLocation = getBlogRoot()
	fmt.Printf("blog root: %s\n", baseLocation)

	blogEntryLocation := filepath.Join(baseLocation, "blog_entries")
	blogEntryLocationMD := filepath.Join(baseLocation, "blog_entries_md")

	var indexEntries []indexEntry

	indexEntries = generateEntries(blogEntryLocation, indexEntries, func(b bytes.Buffer) bytes.Buffer {
		return b
	})

	indexEntries = generateEntries(blogEntryLocationMD, indexEntries, func(b bytes.Buffer) bytes.Buffer {
		commonHtmlFlags := 0
		commonExtensions := 0 |
			blackfriday.EXTENSION_TABLES |
			blackfriday.EXTENSION_FENCED_CODE |
			blackfriday.EXTENSION_AUTOLINK |
			blackfriday.EXTENSION_STRIKETHROUGH |
			blackfriday.EXTENSION_DEFINITION_LISTS
		renderer := blackfriday.HtmlRenderer(commonHtmlFlags, "", "")
		out := blackfriday.MarkdownOptions(b.Bytes(), renderer, blackfriday.Options{
			Extensions: commonExtensions,
		})
		return *bytes.NewBuffer(out)
	})

	sort.Sort(ByTimeDesc(indexEntries))

	targetFile := filepath.Join(baseLocation, "index.html")
	target, err := os.Create(targetFile)
	fmt.Printf("generating: %s\n", targetFile)
	exitOnErr(err)
	exitOnErr(html.Render(target, createIndexHTML(indexEntries)))
	target.Close()
}
func main() {
	// Parse command line flags and handle default values (if necessary)
	pathPtr := flag.String("input", "", "Filepath for the markdown file you would like to convert")
	outputPtr := flag.String("output", "", "Path for your html output")
	flag.Parse()

	// Fatal error if no input has been specified
	if *pathPtr == "" {
		log.Fatal(errors.New("Error: You must provide a path to your markdown file"))
	}
	// Check the input path and sanitize the filepath
	path := cleanPath(pathPtr)

	// If no output path was provided, override the default pointer and create a new file at the same
	// location as the input file
	outputPath := outputFilePath(outputPtr, path)
	// Read the markdown file into a []Byte
	rawMarkdown := readInput(path)
	// Create a new HmtlRender struct with the htmlFlags const specified above
	renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
	// Create an Option struct with the extensions const specified above
	outputOpts := blackfriday.Options{
		Extensions: extensions,
	}
	// Render the markdown file into HTML and return a new []Byte
	htmlBody := blackfriday.MarkdownOptions(rawMarkdown,
		renderer,
		outputOpts,
	)
	// Print in-progress message to stdOut
	fmt.Printf("Converting %s. Output is located at %s\n",
		filepath.Base(path),
		outputPath)
	// Write the htmlBody []Byte out to disk with -rw-r--r-- permissions.
	// 0644 is the standard permission config on for html files on Apache. Seemed like a safe bet.
	ioutil.WriteFile(outputPath, htmlBody, 0644)
}
Beispiel #12
0
func main() {

	// read README.md file
	md, err := io.ReadFile("README.md")
	if err != nil {
		io.PfRed("cannot read README.md\n")
		return
	}

	// process markdown
	//html := string(blackfriday.MarkdownCommon(md))

	flags := 0 |
		blackfriday.HTML_USE_XHTML |
		blackfriday.HTML_USE_SMARTYPANTS |
		blackfriday.HTML_SMARTYPANTS_LATEX_DASHES

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

	renderer := blackfriday.HtmlRenderer(flags, "", "")
	html := string(blackfriday.MarkdownOptions(md, renderer, blackfriday.Options{Extensions: extensions}))

	// environment variable => figures path
	path := os.ExpandEnv("${GOPATH}/src/github.com/cpmech/gofem/")
	io.Pforan("path = %v\n", path)

	// set path of figures
	html = strings.Replace(html, "img src=\"", io.Sf("img src=\"%s/examples/", path), -1)

	// set header and footer
	html = `<!DOCTYPE HTML>
<html>
<head>
<title>Gofem &ndash; Examples</title>
<meta charset="utf-8" />

<style>
h1 {color:#0064cb; font-family:verdana; font-size:200%;}
h2 {color:#0064cb}
h3 {color:#0064cb}
a:hover {background-color:#5397dc;}
#container {
	width:500px;
	text-align:center;
}
#container img {
	max-width:100%;
	height:auto;
}
</style>

</head>
<body>
` + html + `
</body>
</html>`

	// write file
	io.WriteFileVD("/tmp", "gofem-README.html", bytes.NewBuffer([]byte(html)))
	//io.WriteFileVD("/tmp", "gofem-README.html", bytes.NewBuffer(html))
}
Beispiel #13
0
func (syntax *markdownSyntax) BodyToHtml(body string) template.HTML {
	renderer, options := syntax.markdownParams()
	unsafeHtml := string(blackfriday.MarkdownOptions([]byte(body), renderer, options))
	return template.HTML(sanitizePolicy().Sanitize(unsafeHtml))
}
Beispiel #14
0
func markdownRender(input []byte) []byte {
	// set up the HTML renderer
	renderer := blackfriday.HtmlRenderer(commonHtmlFlags, "", "")
	return blackfriday.MarkdownOptions(input, renderer, blackfriday.Options{
		Extensions: commonExtensions | blackfriday.EXTENSION_HARD_LINE_BREAK})
}