diff --git a/org/document.go b/org/document.go index d5da477..eb630e5 100644 --- a/org/document.go +++ b/org/document.go @@ -4,7 +4,9 @@ import ( "bufio" "fmt" "io" + "io/ioutil" "log" + "os" "strings" ) @@ -19,7 +21,7 @@ type Document struct { BufferSettings map[string]string DefaultSettings map[string]string Error error - Debug bool + Log *log.Logger } type Writer interface { @@ -80,6 +82,7 @@ func NewDocument() *Document { DefaultSettings: map[string]string{ "TODO": "TODO | DONE", }, + Log: log.New(os.Stderr, "go-org: ", 0), } } @@ -118,6 +121,12 @@ func (dIn *Document) Parse(input io.Reader) (d *Document) { func (d *Document) SetPath(path string) *Document { d.Path = path + d.Log.SetPrefix(fmt.Sprintf("%s(%s): ", d.Log.Prefix(), path)) + return d +} + +func (d *Document) Silent() *Document { + d.Log = log.New(ioutil.Discard, "", 0) return d } @@ -196,9 +205,7 @@ func (d *Document) parseOne(i int, stop stopFn) (consumed int, node Node) { if consumed != 0 { return consumed, node } - if d.Debug { - log.Printf("Could not parse token %#v: Falling back to treating it as plain text.", d.tokens[i]) - } + d.Log.Printf("Could not parse token %#v: Falling back to treating it as plain text.", d.tokens[i]) m := plainTextRegexp.FindStringSubmatch(d.tokens[i].matches[0]) d.tokens[i] = token{"text", len(m[1]), m[2], m} return d.parseOne(i, stop) diff --git a/org/html.go b/org/html.go index 3c8edd9..5eda58e 100644 --- a/org/html.go +++ b/org/html.go @@ -16,7 +16,7 @@ type HTMLWriter struct { HighlightCodeBlock func(source, lang string) string FootnotesHeadingTitle string htmlEscape bool - debug bool + log *log.Logger } var emphasisTags = map[string][]string{ @@ -65,7 +65,7 @@ func (w *HTMLWriter) nodesAsString(nodes ...Node) string { } func (w *HTMLWriter) before(d *Document) { - w.debug = w.debug || d.Debug + w.log = d.Log } func (w *HTMLWriter) after(d *Document) { @@ -362,7 +362,7 @@ func (w *HTMLWriter) writeNodeWithMeta(n NodeWithMeta) { } } for _, attributes := range n.Meta.HTMLAttributes { - out = withHTMLAttributes(w.debug, out, attributes...) + "\n" + out = w.withHTMLAttributes(out, attributes...) + "\n" } if len(n.Meta.Caption) != 0 { caption := "" @@ -414,19 +414,15 @@ func (w *HTMLWriter) writeTableColumns(columns []Column, tag string) { w.WriteString("\n") } -func withHTMLAttributes(debug bool, input string, kvs ...string) string { +func (w *HTMLWriter) withHTMLAttributes(input string, kvs ...string) string { if len(kvs)%2 != 0 { - if debug { - log.Printf("withHTMLAttributes: Len of kvs must be even: %#v", kvs) - } + w.log.Printf("withHTMLAttributes: Len of kvs must be even: %#v", kvs) return input } context := &h.Node{Type: h.ElementNode, Data: "body", DataAtom: atom.Body} nodes, err := h.ParseFragment(strings.NewReader(strings.TrimSpace(input)), context) if err != nil || len(nodes) != 1 { - if debug { - log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, nodes, err) - } + w.log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, nodes, err) return input } out, node := strings.Builder{}, nodes[0] @@ -435,9 +431,7 @@ func withHTMLAttributes(debug bool, input string, kvs ...string) string { } err = h.Render(&out, nodes[0]) if err != nil { - if debug { - log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, node, err) - } + w.log.Printf("withHTMLAttributes: Could not extend attributes of %s: %v (%s)", input, node, err) return input } return out.String() diff --git a/org/html_test.go b/org/html_test.go index 6450c44..298b206 100644 --- a/org/html_test.go +++ b/org/html_test.go @@ -9,7 +9,7 @@ func TestHTMLWriter(t *testing.T) { for _, path := range orgTestFiles() { expected := fileString(path[:len(path)-len(".org")] + ".html") reader, writer := strings.NewReader(fileString(path)), NewHTMLWriter() - actual, err := NewDocument().SetPath(path).Parse(reader).Write(writer) + actual, err := NewDocument().Silent().SetPath(path).Parse(reader).Write(writer) if err != nil { t.Errorf("%s\n got error: %s", path, err) continue diff --git a/org/keyword.go b/org/keyword.go index 1580ab9..01b7c95 100644 --- a/org/keyword.go +++ b/org/keyword.go @@ -2,9 +2,7 @@ package org import ( "bytes" - "fmt" "io/ioutil" - "log" "path/filepath" "regexp" "strings" @@ -119,7 +117,10 @@ func parseKeyword(t token) Keyword { } func (d *Document) newInclude(k Keyword) (int, Node) { - resolve := func() Node { panic(fmt.Sprintf("bad include: '#+INCLUDE: %s'", k.Value)) } + resolve := func() Node { + d.Log.Printf("Bad include %#v", k) + return k + } if m := includeFileRegexp.FindStringSubmatch(k.Value); m != nil { path, kind, lang := m[1], m[2], m[3] if !filepath.IsAbs(path) { @@ -128,7 +129,8 @@ func (d *Document) newInclude(k Keyword) (int, Node) { resolve = func() Node { bs, err := ioutil.ReadFile(path) if err != nil { - panic(fmt.Sprintf("bad include '#+INCLUDE: %s': %s", k.Value, err)) + d.Log.Printf("Bad include %#v: %s", k, err) + return k } return Block{strings.ToUpper(kind), []string{lang}, d.parseRawInline(string(bs))} } @@ -143,16 +145,12 @@ func (d *Document) loadSetupFile(k Keyword) (int, Node) { } bs, err := ioutil.ReadFile(path) if err != nil { - if d.Debug { - log.Printf("Bad setup file: %#v: %s", k, err) - } + d.Log.Printf("Bad setup file: %#v: %s", k, err) return 1, k } setupDocument := NewDocument().Parse(bytes.NewReader(bs)) if err := setupDocument.Error; err != nil { - if d.Debug { - log.Printf("Bad setup file: %#v: %s", k, err) - } + d.Log.Printf("Bad setup file: %#v: %s", k, err) return 1, k } for k, v := range setupDocument.BufferSettings { diff --git a/org/org_test.go b/org/org_test.go index 64f7c41..c6bb8e6 100644 --- a/org/org_test.go +++ b/org/org_test.go @@ -14,7 +14,7 @@ func TestOrgWriter(t *testing.T) { for _, path := range orgTestFiles() { expected := fileString(path[:len(path)-len(".org")] + ".pretty_org") reader, writer := strings.NewReader(fileString(path)), NewOrgWriter() - actual, err := NewDocument().SetPath(path).Parse(reader).Write(writer) + actual, err := NewDocument().Silent().SetPath(path).Parse(reader).Write(writer) if err != nil { t.Errorf("%s\n got error: %s", path, err) continue