Skip to content

Commit

Permalink
Merge pull request #187 from barseghyanartur/dev
Browse files Browse the repository at this point in the history
Docs/fixes (#186)
  • Loading branch information
barseghyanartur authored Sep 26, 2024
2 parents 404c173 + 741b323 commit a8946e0
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 37 deletions.
28 changes: 28 additions & 0 deletions docs/creating_archives.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,31 @@ and `doc_isbn.txt`.
},
}
)
----

Using text templates:

.. code-block:: python
:name: test_text_templates
from fake import FAKER, StringTemplate
template = """
{date(start_date='-7d')}
{name}
{sentence(nb_words=2, suffix='')} {pyint(min_value=1, max_value=99)}
{randomise_string(value='#### ??', digits='123456789')} {city}
Dear friend,
{text(nb_chars=1000, allow_overflow=True)}
Sincerely yours,
{name}
{email}
{domain_name}
"""
# EML file
eml_file = FAKER.eml_file(content=StringTemplate(FAKER, template))
61 changes: 29 additions & 32 deletions docs/creating_docx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,32 @@ With ``nb_pages`` tweak:

Using text templates:

.. container:: jsphinx-toggle-emphasis

.. code-block:: python
:name: test_text_templates
:emphasize-lines: 1-
from fake import FAKER, StringTemplate
template = """
{date(start_date='-7d')}
{name}
{sentence(nb_words=2, suffix='')} {pyint(min_value=1, max_value=99)}
{randomise_string(value='#### ??', digits='123456789')} {city}
Dear friend,
{text(nb_chars=1000, allow_overflow=True)}
Sincerely yours,
{name}
{email}
{domain_name}
"""
# DOCX file of 1 page
docx_file = FAKER.docx_file(
texts=[StringTemplate(FAKER, template)],
)
# ODT file of 10 pages
odt_file = FAKER.odt_file(
texts=[StringTemplate(FAKER, template) for _ in range(10)],
)
.. code-block:: python
:name: test_text_templates
from fake import FAKER, StringTemplate
template = """
{date(start_date='-7d')}
{name}
{sentence(nb_words=2, suffix='')} {pyint(min_value=1, max_value=99)}
{randomise_string(value='#### ??', digits='123456789')} {city}
Dear friend,
{text(nb_chars=1000, allow_overflow=True)}
Sincerely yours,
{name}
{email}
{domain_name}
"""
# DOCX file of 1 page
docx_file_1 = FAKER.docx_file(
texts=[StringTemplate(FAKER, template)],
)
# DOCX file of 10 pages
docx_file_10 = FAKER.odt_file(
texts=[StringTemplate(FAKER, template) for _ in range(10)],
)
34 changes: 34 additions & 0 deletions docs/creating_odt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,37 @@ With ``nb_pages`` tweak:

*See the full example*
:download:`here <_static/examples/creating_odt/odt_file_3.py>`

----

Using text templates:

.. code-block:: python
:name: test_text_templates
from fake import FAKER, StringTemplate
template = """
{date(start_date='-7d')}
{name}
{sentence(nb_words=2, suffix='')} {pyint(min_value=1, max_value=99)}
{randomise_string(value='#### ??', digits='123456789')} {city}
Dear friend,
{text(nb_chars=1000, allow_overflow=True)}
Sincerely yours,
{name}
{email}
{domain_name}
"""
# ODT file of 1 page
odt_file_1 = FAKER.odt_file(
texts=[StringTemplate(FAKER, template)],
)
# ODT file of 10 pages
odt_file_10 = FAKER.odt_file(
texts=[StringTemplate(FAKER, template) for _ in range(10)],
)
51 changes: 46 additions & 5 deletions fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,14 +1229,55 @@ class OdtGenerator:
def __init__(self, faker: "Faker") -> None:
self.faker = faker

def _create_page(self, text: str) -> str:
def _escape_xml(self, text: str) -> str:
"""Escapes XML special characters in the given text.
:param text: The text to escape.
:return: Escaped text.
:rtype: str
"""
return (
f'<text:p text:style-name="P1">{text}</text:p>'
f'<text:p style:name="P1" style:family="paragraph" '
f'style:parent-style-name="Standard">'
f"<text:line-break/></text:p>"
text.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace('"', "&quot;")
.replace("'", "&apos;")
)

def _create_page(self, text: str) -> str:
"""Creates a paragraph with line breaks based on '\n' in the text.
:param text: The text content with '\n' indicating where line breaks
should be.
:return: A string representing the XML of the paragraph with line
breaks.
:rtype: str
"""
# Split the text by '\n' to determine where to insert line breaks
lines = text.split("\n")

# Initialize the paragraph XML with the desired style
paragraph_xml = '<text:p text:style-name="P1">'

# Iterate over each line and insert <text:line-break/> where needed
for i, line in enumerate(lines):
# Escape XML special characters in the line
escaped_line = self._escape_xml(line)
paragraph_xml += escaped_line
if i < len(lines) - 1:
paragraph_xml += "<text:line-break/>"

# Close the paragraph tag
paragraph_xml += "</text:p>"

return paragraph_xml
# return (
# f'<text:p text:style-name="P1">{text}</text:p>'
# f'<text:p style:name="P1" style:family="paragraph" '
# f'style:parent-style-name="Standard">'
# f"<text:line-break/></text:p>"
# )

def create(
self,
nb_pages: Optional[int] = None,
Expand Down

0 comments on commit a8946e0

Please sign in to comment.