Skip to content

Commit

Permalink
helpers: Add smarter check for asciidoctor
Browse files Browse the repository at this point in the history
  • Loading branch information
miltador committed Jul 18, 2017
1 parent 51fda88 commit 6ceb0b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
37 changes: 26 additions & 11 deletions helpers/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,37 +544,52 @@ func truncateWordsToWholeSentenceOld(content string, max int) (string, bool) {
}

func getAsciidocExecPath() string {
path, err := exec.LookPath("asciidoctor")
path, err := exec.LookPath("asciidoc")
if err != nil {
path, err = exec.LookPath("asciidoc")
if err != nil {
return ""
}
return ""
}
return path
}

// HasAsciidoc returns whether Asciidoctor or Asciidoc is installed on this computer.
// HasAsciidoc returns whether Asciidoc is installed on this computer.
func HasAsciidoc() bool {
return getAsciidocExecPath() != ""
}

func getAsciidoctorExecPath() string {
path, err := exec.LookPath("asciidoctor")
if err != nil {
return ""
}
return path
}

// HasAsciidoctor returns whether Asciidoctor is installed on this computer.
func HasAsciidoctor() bool {
return getAsciidoctorExecPath() != ""
}

// getAsciidocContent calls asciidoctor or asciidoc as an external helper
// to convert AsciiDoc content to HTML.
func getAsciidocContent(ctx *RenderingContext) []byte {
content := ctx.Content
cleanContent := bytes.Replace(content, SummaryDivider, []byte(""), 1)

var isAsciidoctor bool
path := getAsciidocExecPath()
if path == "" {
jww.ERROR.Println("asciidoctor / asciidoc not found in $PATH: Please install.\n",
" Leaving AsciiDoc content unrendered.")
return content
path := getAsciidoctorExecPath()
if path == "" {
jww.ERROR.Println("asciidoctor / asciidoc not found in $PATH: Please install.\n",
" Leaving AsciiDoc content unrendered.")
return content
}
isAsciidoctor = true
}

jww.INFO.Println("Rendering", ctx.DocumentName, "with", path, "...")
cmd := exec.Command(path, "--no-header-footer", "--safe")
if strings.HasSuffix(path, "asciidoctor") {
if isAsciidoctor {
// asciidoctor-specific arg to show stack traces on errors
cmd.Args = append(cmd.Args, "--trace")
}
Expand Down Expand Up @@ -677,7 +692,7 @@ func getRstContent(ctx *RenderingContext) []byte {
}
}

return result[bodyStart+7 : bodyEnd]
return result[bodyStart+7: bodyEnd]
}

func orgRender(ctx *RenderingContext, c ContentSpec) []byte {
Expand Down
2 changes: 1 addition & 1 deletion hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ func testAllMarkdownEnginesForPages(t *testing.T,
}{
{"md", func() bool { return true }},
{"mmark", func() bool { return true }},
{"ad", func() bool { return helpers.HasAsciidoc() }},
{"ad", func() bool { return helpers.HasAsciidoc() || helpers.HasAsciidoctor() }},
// TODO(bep) figure a way to include this without too much work.{"html", func() bool { return true }},
{"rst", func() bool { return helpers.HasRst() }},
}
Expand Down
2 changes: 1 addition & 1 deletion hugolib/shortcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ tags:
th := testHelper{s.Cfg, s.Fs, t}

for _, test := range tests {
if strings.HasSuffix(test.contentPath, ".ad") && !helpers.HasAsciidoc() {
if strings.HasSuffix(test.contentPath, ".ad") && !helpers.HasAsciidoc() && !helpers.HasAsciidoctor() {
fmt.Println("Skip Asciidoc test case as no Asciidoc present.")
continue
} else if strings.HasSuffix(test.contentPath, ".rst") && !helpers.HasRst() {
Expand Down

0 comments on commit 6ceb0b4

Please sign in to comment.