From 727f02f4c51cccc95fe57591507a95e140a59d7a Mon Sep 17 00:00:00 2001 From: Saswata Mukherjee Date: Tue, 10 Aug 2021 16:08:48 +0530 Subject: [PATCH] Add `WithSoftWraps` option (#36) * Add WithHardWraps option Signed-off-by: Saswata Mukherjee * Implement suggestions Signed-off-by: Saswata Mukherjee --- main.go | 4 +++ markdown/renderer.go | 14 ++++++++- markdownfmt/markdownfmt_test.go | 29 +++++++++++++++++++ .../testfiles/example-same-softwrap.md | 11 +++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 markdownfmt/testfiles/example-same-softwrap.md diff --git a/main.go b/main.go index ab6976c..0a85cec 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ var ( write = flag.Bool("w", false, "write result to (source) file instead of stdout") doDiff = flag.Bool("d", false, "display diffs instead of rewriting files") underlineHeadings = flag.Bool("u", false, "write underline headings instead of hashes for levels 1 and 2") + softWraps = flag.Bool("soft-wraps", false, "wrap lines even on soft line breaks") exitCode = 0 ) @@ -62,6 +63,9 @@ func processFile(filename string, in io.Reader, out io.Writer) error { if *underlineHeadings { opts = append(opts, markdown.WithUnderlineHeadings()) } + if *softWraps { + opts = append(opts, markdown.WithSoftWraps()) + } res, err := markdownfmt.Process(filename, src, opts...) if err != nil { return err diff --git a/markdown/renderer.go b/markdown/renderer.go index 592a090..ded9cca 100644 --- a/markdown/renderer.go +++ b/markdown/renderer.go @@ -35,6 +35,7 @@ var _ renderer.Renderer = &Renderer{} // Render is reusable across Renders, it holds configuration only. type Renderer struct { underlineHeadings bool + softWraps bool } func (mr *Renderer) AddOptions(...renderer.Option) { @@ -55,6 +56,13 @@ func WithUnderlineHeadings() Option { } } +// WithSoftWraps allows you to wrap lines even on soft line breaks. +func WithSoftWraps() Option { + return func(r *Renderer) { + r.softWraps = true + } +} + func NewRenderer() *Renderer { return &Renderer{} } @@ -128,7 +136,11 @@ func (r *render) renderNode(node ast.Node, entering bool) (ast.WalkStatus, error } if tnode.SoftLineBreak() { - _, _ = r.w.Write(spaceChar) + char := spaceChar + if r.mr.softWraps { + char = newLineChar + } + _, _ = r.w.Write(char) } if tnode.HardLineBreak() { diff --git a/markdownfmt/markdownfmt_test.go b/markdownfmt/markdownfmt_test.go index 1337625..7a225cc 100644 --- a/markdownfmt/markdownfmt_test.go +++ b/markdownfmt/markdownfmt_test.go @@ -43,6 +43,35 @@ func TestSame(t *testing.T) { } } +func TestWithHardWraps(t *testing.T) { + matches, err := filepath.Glob("testfiles/*same-softwrap.md") + if err != nil { + t.Fatal(err) + } + for _, f := range matches { + t.Run(f, func(t *testing.T) { + reference, err := ioutil.ReadFile(f) + if err != nil { + t.Fatal(err) + } + + output, err := markdownfmt.Process("", reference, markdown.WithSoftWraps()) + if err != nil { + t.Fatal(err) + } + + diff, err := diff(reference, output) + if err != nil { + t.Fatal(err) + } + + if len(diff) != 0 { + t.Errorf("Difference in %s of %d lines:\n%s", f, bytes.Count(diff, []byte("\n")), string(diff)) + } + }) + } +} + func TestSameUnderline(t *testing.T) { matches, err := filepath.Glob("testfiles/*.same-underline.md") if err != nil { diff --git a/markdownfmt/testfiles/example-same-softwrap.md b/markdownfmt/testfiles/example-same-softwrap.md new file mode 100644 index 0000000..4fc0ef0 --- /dev/null +++ b/markdownfmt/testfiles/example-same-softwrap.md @@ -0,0 +1,11 @@ +The `thanos rule` command evaluates Prometheus recording and alerting rules +against chosen query API via repeated `--query` (or FileSD via `--query.sd`). +If more than one query is passed, round robin balancing is performed. + +Rule results are written back to disk in the Prometheus 2.0 storage format. +Rule nodes at the same time participate in the system as source store nodes, +which means that they expose StoreAPI and *upload* their generated TSDB blocks +to an object store. + +You can think of Rule as a simplified Prometheus that does not require +a sidecar and does not scrape and do PromQL evaluation (no QueryAPI).