diff --git a/helpers/content.go b/helpers/content.go index a327a1e5f..daf7e267d 100644 --- a/helpers/content.go +++ b/helpers/content.go @@ -230,6 +230,8 @@ func RenderBytesWithTOC(ctx *RenderingContext) []byte { return markdownRenderWithTOC(ctx) case "markdown": return markdownRenderWithTOC(ctx) + case "asciidoc": + return []byte(GetAsciidocContent(ctx.Content)) case "rst": return []byte(GetRstContent(ctx.Content)) } @@ -242,6 +244,8 @@ func RenderBytes(ctx *RenderingContext) []byte { return markdownRender(ctx) case "markdown": return markdownRender(ctx) + case "asciidoc": + return []byte(GetAsciidocContent(ctx.Content)) case "rst": return []byte(GetRstContent(ctx.Content)) } @@ -299,6 +303,39 @@ func TruncateWordsToWholeSentence(words []string, max int) (string, bool) { return strings.Join(words[:max], " "), true } +// GetAsciidocContent calls asciidoctor or asciidoc as an external helper +// to convert AsciiDoc content to HTML. +func GetAsciidocContent(content []byte) string { + cleanContent := bytes.Replace(content, SummaryDivider, []byte(""), 1) + + path, err := exec.LookPath("asciidoctor") + if err != nil { + path, err = exec.LookPath("asciidoc") + if err != nil { + jww.ERROR.Println("asciidoctor / asciidoc not found in $PATH: Please install.\n", + " Leaving AsciiDoc content unrendered.") + return (string(content)) + } + } + + jww.INFO.Println("Rendering with", path, "...") + cmd := exec.Command(path, "--safe", "-") + cmd.Stdin = bytes.NewReader(cleanContent) + var out bytes.Buffer + cmd.Stdout = &out + if err := cmd.Run(); err != nil { + jww.ERROR.Println(err) + } + + asciidocLines := strings.Split(out.String(), "\n") + for i, line := range asciidocLines { + if strings.HasPrefix(line, " 0 { + tmpContentWithTokensReplaced, err := replaceShortcodeTokens(tmpContent, shortcodePlaceholderPrefix, true, p.contentShortCodes) + + if err != nil { + jww.FATAL.Printf("Fail to replace short code tokens in %s:\n%s", p.BaseFileName(), err.Error()) + return HandledResult{err: err} + } else { + tmpContent = tmpContentWithTokensReplaced + } + } + + p.Content = helpers.BytesToHTML(tmpContent) + p.TableOfContents = helpers.BytesToHTML(tmpTableOfContents) //err := p.Convert() return HandledResult{page: p, err: nil}