Add page context to error logging in rendering
authorchoeppler <choeppler@users.noreply.github.com>
Wed, 19 Oct 2016 13:22:40 +0000 (15:22 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 19 Oct 2016 13:22:40 +0000 (15:22 +0200)
Add logging of the errors/warnings which rst2html outputs to its stderr
stream when rendering rst documents. Note that rst2html outputs warnings
and errors to stderr but it also adds them to the generated html. ->
hugo logs everything in stderr as error.

Add / complete adding page context (path to file being rendered) to
anything logged by getRstContent and getAsciidocContent.

See #2570

helpers/content.go

index aa1327d7422e96425a23ee42bab1c433f23e76c0..5c9533330230f6d20795b44a4857496ed601a56f 100644 (file)
@@ -395,7 +395,7 @@ func RenderBytes(ctx *RenderingContext) []byte {
        case "mmark":
                return mmarkRender(ctx)
        case "rst":
-               return getRstContent(ctx.Content)
+               return getRstContent(ctx)
        }
 }
 
@@ -552,7 +552,7 @@ func getAsciidocContent(ctx *RenderingContext) []byte {
                return content
        }
 
-       jww.INFO.Println("Rendering with", path, "...")
+       jww.INFO.Println("Rendering", ctx.DocumentName, "with", path, "...")
        cmd := exec.Command(path, "--no-header-footer", "--safe", "-")
        cmd.Stdin = bytes.NewReader(cleanContent)
        var out, cmderr bytes.Buffer
@@ -568,7 +568,7 @@ func getAsciidocContent(ctx *RenderingContext) []byte {
                }
        }
        if err != nil {
-               jww.ERROR.Println(err)
+               jww.ERROR.Printf("%s rendering %s: %v", path, ctx.DocumentName, err)
        }
 
        return out.Bytes()
@@ -592,7 +592,8 @@ func getRstExecPath() string {
 
 // getRstContent calls the Python script rst2html as an external helper
 // to convert reStructuredText content to HTML.
-func getRstContent(content []byte) []byte {
+func getRstContent(ctx *RenderingContext) []byte {
+       content := ctx.Content
        cleanContent := bytes.Replace(content, SummaryDivider, []byte(""), 1)
 
        path := getRstExecPath()
@@ -604,12 +605,23 @@ func getRstContent(content []byte) []byte {
 
        }
 
+       jww.INFO.Println("Rendering", ctx.DocumentName, "with", path, "...")
        cmd := exec.Command(path, "--leave-comments")
        cmd.Stdin = bytes.NewReader(cleanContent)
-       var out bytes.Buffer
+       var out, cmderr bytes.Buffer
        cmd.Stdout = &out
-       if err := cmd.Run(); err != nil {
-               jww.ERROR.Println(err)
+       cmd.Stderr = &cmderr
+       err := cmd.Run()
+       // By default rst2html exits w/ non-zero exit code only if severe, i.e.
+       // halting errors occurred. -> log stderr output regardless of state of err
+       for _, item := range strings.Split(string(cmderr.Bytes()), "\n") {
+               item := strings.TrimSpace(item)
+               if item != "" {
+                       jww.ERROR.Println(strings.Replace(item, "<stdin>", ctx.DocumentName, 1))
+               }
+       }
+       if err != nil {
+               jww.ERROR.Printf("%s rendering %s: %v", path, ctx.DocumentName, err)
        }
 
        result := out.Bytes()