From 0cf5f7a9b1fcb272cd9d632bc76219edd7077c39 Mon Sep 17 00:00:00 2001 From: clsr Date: Thu, 24 Aug 2017 17:21:03 +0200 Subject: Export WriteIndent --- cnmfmt/cnmfmt.go | 27 ++------------------------- content.go | 22 +++++++++++----------- document.go | 20 ++++++++++++-------- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/cnmfmt/cnmfmt.go b/cnmfmt/cnmfmt.go index cb8dc64..fa2ec65 100644 --- a/cnmfmt/cnmfmt.go +++ b/cnmfmt/cnmfmt.go @@ -199,7 +199,7 @@ func (t Text) WriteIndent(w io.Writer, n int) error { si = cleanupTags(state[:], order, span.Format) format = span.Format } - return writeIndent(w, strings.Join(line, ""), n) + return cnm.WriteIndent(w, strings.Join(line, ""), n) } func tagOrder(state []byte, old, new Format) []byte { @@ -430,7 +430,7 @@ func NewTextFmtBlock(paragraphs []Text) *cnm.TextBlock { func (tf TextFmtContents) WriteIndent(w io.Writer, n int) error { for i, p := range tf.Paragraphs { if i != 0 { - if err := writeIndent(w, "", 0); err != nil { + if err := cnm.WriteIndent(w, "", 0); err != nil { return err } } @@ -469,29 +469,6 @@ func Parse(paragraphs string) []Text { return txt } -func writeIndent(w io.Writer, s string, depth int) error { - const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" - - if s == "" { - _, err := w.Write([]byte{'\n'}) - return err - } - if depth == 0 { - _, err := w.Write([]byte(s + "\n")) - return err - } - - var ind string - if depth <= len(tabs) { - ind = tabs[:depth] - } else { - ind = strings.Repeat("\t", depth) - } - _, err := w.Write([]byte(ind + s + "\n")) - return err - -} - func parseTextFmt(p *cnm.Parser, block *cnm.TokenBlock) (cnm.TextContents, error) { txt := TextFmtContents{} var paragraph []string diff --git a/content.go b/content.go index c2ba347..6a728f5 100644 --- a/content.go +++ b/content.go @@ -41,7 +41,7 @@ type ContentBlock struct { func (cb *ContentBlock) WriteIndent(w io.Writer, n int) error { ss := []string{Escape(cb.name)} ss = append(ss, cb.args...) - if err := writeIndent(w, JoinEscape(ss), n); err != nil { + if err := WriteIndent(w, JoinEscape(ss), n); err != nil { return err } for _, ch := range cb.children { @@ -169,7 +169,7 @@ func (t *TextBlock) WriteIndent(w io.Writer, n int) error { if t.Format != "" { s += " " + Escape(t.Format) } - if err := writeIndent(w, s, n); err != nil { + if err := WriteIndent(w, s, n); err != nil { return err } if err := t.Contents.WriteIndent(w, n+1); err != nil { @@ -237,11 +237,11 @@ type TextPlainContents struct { func (t TextPlainContents) WriteIndent(w io.Writer, n int) error { for i, p := range t.Paragraphs { if i != 0 { - if err := writeIndent(w, "", 0); err != nil { + if err := WriteIndent(w, "", 0); err != nil { return err } } - if err := writeIndent(w, Escape(p), n); err != nil { + if err := WriteIndent(w, Escape(p), n); err != nil { return err } } @@ -299,7 +299,7 @@ type TextPreContents struct { func (t TextPreContents) WriteIndent(w io.Writer, n int) error { ss := strings.Split(t.Text, "\n") for _, s := range ss { - if err := writeIndent(w, EscapeNonspace(s), n); err != nil { + if err := WriteIndent(w, EscapeNonspace(s), n); err != nil { return err } } @@ -335,7 +335,7 @@ func parseTextPre(p *Parser, block *TokenBlock) (TextContents, error) { return TextPreContents{strings.Join(lines, "\n")}, err } -// TextRawContents represents raw (unesacped) contents of a text or raw block. +// TextRawContents represents raw contents of a text or raw block. type TextRawContents struct { // Text is the raw content. Text string @@ -345,7 +345,7 @@ type TextRawContents struct { func (t TextRawContents) WriteIndent(w io.Writer, n int) error { ss := strings.Split(t.Text, "\n") for _, s := range ss { - if err := writeIndent(w, s, n); err != nil { + if err := WriteIndent(w, s, n); err != nil { return err } } @@ -408,7 +408,7 @@ func (r *RawBlock) WriteIndent(w io.Writer, n int) error { if r.Syntax != "" { s += " " + Escape(r.Syntax) } - if err := writeIndent(w, s, n); err != nil { + if err := WriteIndent(w, s, n); err != nil { return err } r.Contents.WriteIndent(w, n+1) @@ -487,7 +487,7 @@ func (t *TableBlock) Args() []string { // WriteIndent writes the table header and contents indented by n tabs. func (t *TableBlock) WriteIndent(w io.Writer, n int) error { - if err := writeIndent(w, t.Name(), n); err != nil { + if err := WriteIndent(w, t.Name(), n); err != nil { return err } for _, row := range t.rows { @@ -620,10 +620,10 @@ func (e *EmbedBlock) WriteIndent(w io.Writer, n int) error { s += Escape(e.Type) } s += " " + Escape(e.URL) - if err := writeIndent(w, s, n); err != nil { + if err := WriteIndent(w, s, n); err != nil { return err } - if err := writeIndent(w, Escape(e.Description), n+1); err != nil { + if err := WriteIndent(w, Escape(e.Description), n+1); err != nil { return err } return nil diff --git a/document.go b/document.go index 5137b80..c01837c 100644 --- a/document.go +++ b/document.go @@ -87,15 +87,15 @@ func (doc *Document) WriteIndent(w io.Writer, n int) error { func (doc *Document) Write(w io.Writer) error { bw := bufio.NewWriter(w) if doc.Title != "" { - if err := writeIndent(bw, "title", 0); err != nil { + if err := WriteIndent(bw, "title", 0); err != nil { return err } - if err := writeIndent(bw, Escape(doc.Title), 1); err != nil { + if err := WriteIndent(bw, Escape(doc.Title), 1); err != nil { return err } } if len(doc.Links) > 0 { - if err := writeIndent(bw, "links", 0); err != nil { + if err := WriteIndent(bw, "links", 0); err != nil { return err } for _, link := range doc.Links { @@ -105,7 +105,7 @@ func (doc *Document) Write(w io.Writer) error { } } if len(doc.Site.Children) > 0 { - if err := writeIndent(bw, "site", 0); err != nil { + if err := WriteIndent(bw, "site", 0); err != nil { return err } for _, site := range doc.Site.Children { @@ -196,11 +196,11 @@ func (link Link) WriteIndent(w io.Writer, n int) error { if link.Name != "" { s += " " + Escape(link.Name) } - if err := writeIndent(w, s, n); err != nil { + if err := WriteIndent(w, s, n); err != nil { return err } if link.Description != "" { - if err := writeIndent(w, Escape(link.Description), n+1); err != nil { + if err := WriteIndent(w, Escape(link.Description), n+1); err != nil { return err } } @@ -225,7 +225,7 @@ func (site Site) WriteIndent(w io.Writer, n int) error { if site.Name != "" { s += " " + Escape(site.Name) } - if err := writeIndent(w, s, n); err != nil { + if err := WriteIndent(w, s, n); err != nil { return err } for _, ch := range site.Children { @@ -276,7 +276,11 @@ func parseUnknown(p *Parser, block *TokenBlock) (err error) { return } -func writeIndent(w io.Writer, s string, depth int) error { +// WriteIndent writes an indented line into a writer. +// +// Writes depth tab characters, the string s and a newline. If s is blank, no +// indentation is used. s should not contain newlines. +func WriteIndent(w io.Writer, s string, depth int) error { const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" if s == "" { -- cgit