Repository: sdispater/tomlkit

Issue:
way to avoid attempts to preserve whitespace? tomlkit not properly preserving newlines
If I have a very simple TOML file like this

```
description = "this is a test"

[dir.subdir]
```

and then insert some new key-value pairs and some additional levels of hierarchy, tomlkit squashes everything together when I go to print it, destroying any spaces between sections.

```
description = "this is a test"
foo = "bar"
mykey = "myvalue"
[dir.subdir]
foo = "bar"
[dir2.subdir2]
foo = "bar"
```

This is not very readable or the desired output. How can I get tomlkit to place a blank line between each section, to look like this?

```
description = "this is a test"
foo = "bar"
mykey = "myvalue"

[dir.subdir]
foo = "bar"

[dir2.subdir2]
foo = "bar"
```

Is there an option to have it avoid trying to preserve whitespace (which it seems to be failing to do here in this example) and dump the output in a conventional format (with a blank line in between sections)?

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index db370cd..5efdc24 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
 
 ## [Unreleased]
 
+### Fixed
+
+- Preserve the newlines before super tables when rendering. ([#178](https://github.com/sdispater/tomlkit/issues/178))
+
 ## [0.10.0] - 2022-02-18
 
 ### Fixed
diff --git a/tomlkit/container.py b/tomlkit/container.py
index c20a6ab..2db19e3 100644
--- a/tomlkit/container.py
+++ b/tomlkit/container.py
@@ -505,6 +505,8 @@ class Container(_CustomDict):
                 table.trivia.trail,
                 "\n" if "\n" not in table.trivia.trail and len(table.value) > 0 else "",
             )
+        elif table.trivia.indent == "\n":
+            cur += table.trivia.indent
 
         for k, v in table.value.body:
             if isinstance(v, Table):


Test patch:
diff --git a/tests/test_toml_document.py b/tests/test_toml_document.py
index 8d79ed4..c350984 100644
--- a/tests/test_toml_document.py
+++ b/tests/test_toml_document.py
@@ -12,6 +12,7 @@ import tomlkit
 from tomlkit import parse
 from tomlkit import ws
 from tomlkit._utils import _utc
+from tomlkit.api import document
 from tomlkit.exceptions import NonExistentKey
 
 
@@ -927,3 +928,18 @@ def test_pop_add_whitespace_and_insert_table_work_togheter():
     assert out["d"] == 4
     assert "d" not in out["e"]
     assert text == dedent(expected)
+
+
+def test_add_newline_before_super_table():
+    doc = document()
+    doc["a"] = 1
+    doc["b"] = {"c": {}}
+    doc["d"] = {"e": {}}
+    expected = """\
+    a = 1
+
+    [b.c]
+
+    [d.e]
+    """
+    assert doc.as_string() == dedent(expected)