Skip to content

Commit

Permalink
Fix faulty styling on paragraph insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinderVosDeWael committed Apr 4, 2024
1 parent c0d1dcd commit 672dbeb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 36 deletions.
11 changes: 2 additions & 9 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ Child Mind Institute values the security of our users, data, and servers. We tak

## Supported Versions

MODIFY:

Note which version(s) will receive security updates. For example:

| Version | Supported |
| ------- | ------------------ |
| 1.0.x | :white_check_mark: |
| 0.9.x | :x: |
Only the most recent version will receive security updates.

## Reporting Vulnerabilities

To report security vulnerabilities, please do NOT use our issues page. Instead, kindly email us at reinder.vosdewael@childmind.org. Please refrain from using other communication channels.
To report security vulnerabilities, please do NOT use our issues page. Instead, kindly email us at dair@childmind.org. Please refrain from using other communication channels.

For non-security-related issues, we welcome your input and feedback on our issues page. Feel free to share your ideas and suggestions to help us improve our services.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cmi_docx"
version = "0.1.1"
version = "0.1.2"
description = ".docx utilities"
authors = ["Reinder Vos de Wael <[email protected]>"]
license = "LGPL-2.1"
Expand Down
14 changes: 9 additions & 5 deletions src/cmi_docx/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def insert_paragraph_by_text(
Returns:
The new paragraph.
"""
new_paragraph = self._insert_empty_paragraph(index)
new_paragraph.add_run(text, style=style)
new_paragraph = self._insert_empty_paragraph(index, style)
new_paragraph.add_run(text)
return new_paragraph

def insert_paragraph_by_object(
Expand All @@ -97,7 +97,7 @@ def insert_paragraph_by_object(
"""
new_paragraph = self._insert_empty_paragraph(index)
for paragraph_run in paragraph.runs:
new_paragraph.add_run(paragraph_run.text, paragraph_run.style)
new_paragraph.add_run(paragraph_run.text)
return new_paragraph

def insert_image(
Expand Down Expand Up @@ -131,11 +131,14 @@ def all_paragraphs(self) -> list[docx_paragraph.Paragraph]:
)
return all_paragraphs

def _insert_empty_paragraph(self, index: int) -> docx_paragraph.Paragraph:
def _insert_empty_paragraph(
self, index: int, style: str | None = None
) -> docx_paragraph.Paragraph:
"""Inserts an empty paragraph at a given index.
Args:
index: The index to insert the paragraph at.
style: The style to apply to the paragraph.
Returns:
The new paragraph.
Expand All @@ -145,10 +148,11 @@ def _insert_empty_paragraph(self, index: int) -> docx_paragraph.Paragraph:
raise ValueError(f"Index {index} is out of range.")

if index == n_paragraphs:
new_paragraph = self.document.add_paragraph()
new_paragraph = self.document.add_paragraph(style=style)
else:
new_paragraph = new_paragraph = self.document.paragraphs[
index
]._insert_paragraph_before()
new_paragraph.style = style

return new_paragraph
20 changes: 16 additions & 4 deletions src/cmi_docx/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from docx.enum import text
from docx.oxml import ns

from cmi_docx import paragraph, utils
from cmi_docx import paragraph


class ExtendCell:
Expand Down Expand Up @@ -60,11 +60,23 @@ def format(
if background_rgb is not None:
shading = oxml.parse_xml(
(
r'<w:shd {} w:fill="'
+ f"{utils.rgb_to_hex(*background_rgb)}"
+ r'"/>'
r'<w:shd {} w:fill="' + f"{rgb_to_hex(*background_rgb)}" + r'"/>'
).format(
ns.nsdecls("w"),
),
)
self.cell._tc.get_or_add_tcPr().append(shading) # noqa: SLF001


def rgb_to_hex(r: int, g: int, b: int) -> str:
"""Converts RGB values to a hexadecimal color code.
Args:
r: The red component of the RGB color.
g: The green component of the RGB color.
b: The blue component of the RGB color.
Returns:
The hexadecimal color code representing the RGB color.
"""
return f"#{r:02x}{g:02x}{b:02x}".upper()
15 changes: 0 additions & 15 deletions src/cmi_docx/utils.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test_utils.py → tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from cmi_docx import utils
from cmi_docx import table


@pytest.mark.parametrize(
Expand Down Expand Up @@ -30,4 +30,4 @@ def test_rgb_to_hex(
hexadecimal: str,
) -> None:
"""Tests converting RGB to hex."""
assert utils.rgb_to_hex(*rgb) == hexadecimal
assert table.rgb_to_hex(*rgb) == hexadecimal

0 comments on commit 672dbeb

Please sign in to comment.