Skip to content

Commit

Permalink
Add WithSoftWraps option (#36)
Browse files Browse the repository at this point in the history
* Add WithHardWraps option

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>

* Implement suggestions

Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
  • Loading branch information
saswatamcode authored Aug 10, 2021
1 parent e6bf3dc commit 727f02f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion markdown/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{}
}
Expand Down Expand Up @@ -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() {
Expand Down
29 changes: 29 additions & 0 deletions markdownfmt/markdownfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions markdownfmt/testfiles/example-same-softwrap.md
Original file line number Diff line number Diff line change
@@ -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).

0 comments on commit 727f02f

Please sign in to comment.