Skip to content

Commit 36b3bdd

Browse files
committed
support multiline description
1 parent 1de5871 commit 36b3bdd

6 files changed

Lines changed: 45 additions & 49 deletions

File tree

src/somesy/cff/writer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from cffconvert.cli.create_citation import create_citation
88
from ruamel.yaml import YAML
9+
from ruamel.yaml.scalarstring import LiteralScalarString
910

1011
from somesy.core.models import Entity, Person, ProjectMetadata
1112
from somesy.core.writer import FieldKeyMapping, IgnoreKey, ProjectMetadataWriter
@@ -71,6 +72,14 @@ def _validate(self) -> None:
7172
def save(self, path: Optional[Path] = None) -> None:
7273
"""Save the CFF object to a file."""
7374
path = path or self.path
75+
76+
# if description have new line characters, it should be saved as multiline string
77+
if "abstract" in self._data:
78+
if "\n" in self._data["abstract"]:
79+
self._data["abstract"] = LiteralScalarString(self._data["abstract"])
80+
else:
81+
self._data["abstract"] = str(self.description)
82+
7483
self._yaml.dump(self._data, path)
7584

7685
def _sync_authors(self, metadata: ProjectMetadata) -> None:

src/somesy/fortran/writer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ def _validate(self) -> None:
9191
def save(self, path: Optional[Path] = None) -> None:
9292
"""Save the fpm file."""
9393
path = path or self.path
94+
if "description" in self._data:
95+
if "\n" in self._data["description"]:
96+
self._data["description"] = tomlkit.string(
97+
self._data["description"], multiline=True
98+
)
9499
with open(path, "w") as f:
95100
tomlkit.dump(self._data, f)
96101

src/somesy/julia/writer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def _validate(self) -> None:
5555
def save(self, path: Optional[Path] = None) -> None:
5656
"""Save the julia file."""
5757
path = path or self.path
58+
if "description" in self._data:
59+
if "\n" in self._data["description"]:
60+
self._data["description"] = tomlkit.string(
61+
self._data["description"], multiline=True
62+
)
5863
with open(path, "w") as f:
5964
tomlkit.dump(self._data, f)
6065

src/somesy/mkdocs/writer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from rich.pretty import pretty_repr
88
from ruamel.yaml import YAML
9+
from ruamel.yaml.scalarstring import LiteralScalarString
910

1011
from somesy.core.models import Entity, Person, ProjectMetadata
1112
from somesy.core.writer import FieldKeyMapping, IgnoreKey, ProjectMetadataWriter
@@ -67,6 +68,16 @@ def _validate(self) -> None:
6768
def save(self, path: Optional[Path] = None) -> None:
6869
"""Save the MkDocs object to a file."""
6970
path = path or self.path
71+
72+
# if description have new line characters, it should be saved as multiline string
73+
if self._data is not None and "site_description" in self._data:
74+
if "\n" in self._data["site_description"]:
75+
self._data["site_description"] = LiteralScalarString(
76+
self._data["site_description"]
77+
)
78+
else:
79+
self._data["site_description"] = str(self.description)
80+
7081
self._yaml.dump(self._data, path)
7182

7283
@property

src/somesy/pyproject/writer.py

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,8 @@ def save(self, path: Optional[Path] = None) -> None:
7070
tomlkit.dump(self._data, f)
7171
return
7272

73-
with open(path, "r") as f:
74-
# tomlkit formatting sometimes creates empty lines, dont change if context is not changed
75-
existing_data = f.read()
76-
77-
# remove empty lines
78-
existing_data = existing_data.replace("\n", "")
79-
80-
new_data = tomlkit.dumps(self._data)
81-
new_data = new_data.replace("\n", "")
82-
83-
if existing_data != new_data:
84-
with open(path, "w") as f:
85-
tomlkit.dump(self._data, f)
86-
else:
87-
logger.debug("No changes to pyproject.toml file")
73+
with open(path, "w") as f:
74+
tomlkit.dump(self._data, f)
8875

8976
def _get_property(
9077
self, key: Union[str, List[str]], *, remove: bool = False, **kwargs
@@ -235,39 +222,11 @@ def sync(self, metadata: ProjectMetadata) -> None:
235222

236223
# For Poetry v2, convert authors and maintainers from array of tables to inline tables
237224
if self._poetry_version == 2:
238-
# if license field has text, or file, make it inline table of tomlkit
239-
if self._get_property(["license"]) is not None:
240-
license_value = self._get_property(["license"])
241-
# Create and populate inline table
242-
inline_table = tomlkit.inline_table()
243-
if isinstance(license_value, str):
244-
inline_table["text"] = license_value
245-
elif isinstance(license_value, dict):
246-
if "text" in license_value:
247-
inline_table["text"] = license_value["text"]
248-
elif "file" in license_value:
249-
inline_table["file"] = license_value["file"]
250-
elif hasattr(license_value, "text"):
251-
inline_table["text"] = license_value.text
252-
elif hasattr(license_value, "file"):
253-
inline_table["file"] = license_value.file
254-
255-
# Create a new table with the same structure
256-
table = tomlkit.table()
257-
table.add("license", inline_table)
258-
if "license" in self._data["project"]:
259-
# Copy the whitespace/formatting from the existing table
260-
table.trivia.indent = self._data["project"]["license"].trivia.indent
261-
table.trivia.comment_ws = self._data["project"][
262-
"license"
263-
].trivia.comment_ws
264-
table.trivia.comment = self._data["project"][
265-
"license"
266-
].trivia.comment
267-
table.trivia.trail = self._data["project"]["license"].trivia.trail
268-
269-
self._data["project"]["license"] = table["license"]
270-
225+
if "description" in self._data["project"]:
226+
if "\n" in self._data["project"]["description"]:
227+
self._data["project"]["description"] = tomlkit.string(
228+
self._data["project"]["description"], multiline=True
229+
)
271230
# Move urls section to the end if it exists
272231
if "urls" in self._data["project"]:
273232
urls = self._data["project"].pop("urls")

src/somesy/rust/writer.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Any, List, Optional, Union
66

77
from rich.pretty import pretty_repr
8-
from tomlkit import dump, load, table
8+
from tomlkit import dump, load, string, table
99

1010
from somesy.core.models import Entity, Person, ProjectMetadata
1111
from somesy.core.writer import FieldKeyMapping, IgnoreKey, ProjectMetadataWriter
@@ -60,6 +60,13 @@ def _validate(self) -> None:
6060
def save(self, path: Optional[Path] = None) -> None:
6161
"""Save the Cargo.toml file."""
6262
path = path or self.path
63+
64+
if "description" in self._data["package"]:
65+
if "\n" in self._data["package"]["description"]:
66+
self._data["package"]["description"] = string(
67+
self._data["package"]["description"], multiline=True
68+
)
69+
6370
with open(path, "w") as f:
6471
dump(self._data, f)
6572

0 commit comments

Comments
 (0)