package dedent

  1. Overview
  2. Docs
A library for improving redability of multi-line string constants in code.

Install

Dune Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=0e424e351271ffb7aca7ecd6d6c839ac56c22119cc5e14a7c9dcc2fe28708b96

Description

A library for improving redability of multi-line string constants in code.

Published: 26 May 2024

README

README.mdx

"Dedent"
========

`Dedent` is a library for improving readability of multi-line string
constants in code:

  - you can put string delimiters on their own lines
  - you can indent lines to the level of surrounding code
  - you can start each line with a line prefix, `> `

More precisely, on an input string, `Dedent` does the following:

  1. breaks the string into lines
  2. strips trailing whitespace from all lines
  3. drops the first line and the last line, if they are empty
  4. finds the line with the least indendation, and drops that indentation from all lines
  5. drops the line prefix from all lines, if it is present on all lines

For example:

```ocaml
# let string =
    {|
    > a
    >  b
    >   c
    |} |> Dedent.string
val string : string = "a\n b\n  c"
```

Use `Dedent.lines` to dedent and return a list of lines rather than a
string.  For the above example, `Dedent.lines` yields:

```ocaml
# let lines =
    {|
    > a
    >  b
    >   c
    |} |> Dedent.lines
val lines : string list = ["a"; " b"; "  c"]
```

Each of the aspects of `Dedent`'s input handling is optional.  You
don't have to use a line prefix.  This yields the same string:

```ocaml
# let string =
    {|
    a
     b
      c
    |} |> Dedent.string
val string : string = "a\n b\n  c"
```

You don't have to indent.  This yields the same string:

```ocaml
# let string =
    {|
  a
   b
    c
  |} |> Dedent.string
val string : string = "a\n b\n  c"
```

You don't have to put closing delimiters on their own lines.  This yields the
same string:

```ocaml
# let string =
    {|
  a
   b
    c|} |> Dedent.string
val string : string = "a\n b\n  c"
```

Any text on the same line as the opening delimiter has its leading whitespace
stripped, and does not affect the indentation of the rest of the lines:

```ocaml
# let string =
    {|        a
  b
   c
    d|} |> Dedent.string
val string : string = "a\nb\n c\n  d"
```

There is also an extension syntax,
[`[%string_dedent]`](../../ppx/ppx_string_dedent/README.md), that is a
variant of `[%string]` that applies `[Dedent.string]` to its input
before substitution.  For example:

```ocaml
# let string =
  let bar = "BAR" in
    [%string_dedent
      {|
      > foo
      >   %{bar}
      |}]
val string : string = "foo\n  BAR"
```

Dependencies (5)

  1. dune >= "3.11.0"
  2. stdio >= "v0.17" & < "v0.18"
  3. ppx_jane >= "v0.17" & < "v0.18"
  4. base >= "v0.17" & < "v0.18"
  5. ocaml >= "5.1.0"

Dev Dependencies

None

Used by (2)

  1. grace
  2. ppx_demo >= "v0.17.0"

Conflicts

None

OCaml

Innovation. Community. Security.