// Copyright © 2014 Steve Francia . // // Licensed under the Simple Public License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://opensource.org/licenses/Simple-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package helpers import ( "bytes" "html/template" "os/exec" "github.com/russross/blackfriday" "github.com/spf13/viper" jww "github.com/spf13/jwalterweatherman" "strings" ) var SummaryLength = 70 var SummaryDivider = []byte("") func StripHTML(s string) string { output := "" // Shortcut strings with no tags in them if !strings.ContainsAny(s, "<>") { output = s } else { s = strings.Replace(s, "\n", " ", -1) s = strings.Replace(s, "

", "\n", -1) s = strings.Replace(s, "
", "\n", -1) s = strings.Replace(s, "
", "\n", -1) //
is the xhtml line break tag // Walk through the string removing all tags b := new(bytes.Buffer) inTag := false for _, r := range s { switch r { case '<': inTag = true case '>': inTag = false default: if !inTag { b.WriteRune(r) } } } output = b.String() } return output } func StripEmptyNav(in []byte) []byte { return bytes.Replace(in, []byte("\n\n"), []byte(``), -1) } func BytesToHTML(b []byte) template.HTML { return template.HTML(string(b)) } func GetHtmlRenderer(defaultFlags int, footnoteref string) blackfriday.Renderer { renderParameters := blackfriday.HtmlRendererParameters{ FootnoteAnchorPrefix: viper.GetString("FootnoteAnchorPrefix"), FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"), } if len(footnoteref) != 0 { renderParameters.FootnoteAnchorPrefix = footnoteref + ":" + renderParameters.FootnoteAnchorPrefix } htmlFlags := defaultFlags htmlFlags |= blackfriday.HTML_USE_XHTML htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters) } func GetMarkdownExtensions() int { return 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_FOOTNOTES | blackfriday.EXTENSION_HEADER_IDS } func MarkdownRender(content []byte, footnoteref string) []byte { return blackfriday.Markdown(content, GetHtmlRenderer(0, footnoteref), GetMarkdownExtensions()) } func MarkdownRenderWithTOC(content []byte, footnoteref string) []byte { return blackfriday.Markdown(content, GetHtmlRenderer(blackfriday.HTML_TOC, footnoteref), GetMarkdownExtensions()) } func ExtractTOC(content []byte) (newcontent []byte, toc []byte) { origContent := make([]byte, len(content)) copy(origContent, content) first := []byte(``) replacement := []byte(`