Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻ Refactor document_issue_md_to_pdf function #71

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
- pip:
- polyfactory
- -e packages/document-issue
- -e packages/document-issue-io[quarto]
- -e packages/document-issue-io
- -e packages/document-issue-io[dev]
- -e packages/document-issue-api
- -e packages/document-issue-ui
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2023-present J Gunstone <[email protected]>
#
# SPDX-License-Identifier: MIT
from document_issue_io.markdown_document_issue import document_issue_md_to_pdf
from document_issue_io.markdown_document_issue import generate_document_issue_pdf
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ def to_file(self, fpth: pathlib.Path):
f.close()


def check_markdown_file_paths(fpth_md: pathlib.Path, fpth_md_output: pathlib.Path):
"""Check if input and output markdown files are the same."""
if fpth_md == fpth_md_output:
raise ValueError(
f"Input and output markdown file paths must be different. Please change file name of input markdown."
)


def check_quarto_version() -> ty.Union[None, str]:
"""Check if quarto version is at least 0.5.0."""
completed_process = subprocess.run(["quarto", "--version"], capture_output=True)
Expand Down Expand Up @@ -145,26 +137,25 @@ def run_quarto(
return subprocess.run(cmd)


def document_issue_md_to_pdf(
document_issue: DocumentIssue,
fpth_pdf: pathlib.Path,
fpth_md: ty.Union[pathlib.Path, None] = None,
):
"""Convert markdown document issue to pdf using quarto.
# generate_document_issue_docx(document_issue, fpth_docx, *, md_content="")
# generate_document_issue_md(document_issue, fpth_md, *, md_content="")
# ^ TODO: add in future if this functionality is needed


def generate_document_issue_pdf(
document_issue: DocumentIssue, fpth_pdf: pathlib.Path, *, md_content: str = ""
) -> subprocess.CompletedProcess:
"""Convert document issue to pdf using quarto.
Extra markdown content can be added to the document using `md_content`.
The files will be built in the parent directory of fpth_pdf."""
fpth_md_output = fpth_pdf.parent / (fpth_pdf.stem + ".md")
if fpth_md is not None:
check_markdown_file_paths(fpth_md, fpth_md_output)
md_content = fpth_md.read_text()
else:
md_content = ""
with change_dir(fpth_pdf.parent):
shutil.copy(
FPTH_FOOTER_LOGO, FPTH_FOOTER_LOGO.name
src=FPTH_FOOTER_LOGO, dst=FPTH_FOOTER_LOGO.name
) # Copy footer logo to markdown document issue directory
build_schedule_title_page_template_pdf(document_issue=document_issue)
install_or_update_document_issue_quarto_extension()
markdown = MarkdownDocumentIssue(document_issue).md_docissue + md_content
fpth_md_output.write_text(markdown)
completed_process = run_quarto(fpth_md_output, fpth_pdf)
return fpth_pdf
return completed_process
33 changes: 9 additions & 24 deletions packages/document-issue-io/tests/test_markdown_document_issue.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
import shutil
import datetime
from polyfactory.factories.pydantic_factory import ModelFactory
Expand All @@ -7,7 +6,7 @@
from document_issue.issue import StatusRevisionEnum
from document_issue_io.markdown_document_issue import (
MarkdownDocumentIssue,
document_issue_md_to_pdf,
generate_document_issue_pdf,
)

from tests.constants import FDIR_TEST_OUTPUT
Expand Down Expand Up @@ -69,7 +68,7 @@ def test_to_file(self):

- convert this notebook to markdown
- read `docissue.json`
- convert using `document_issue_md_to_pdf`
- convert using `generate_document_issue_pdf`

## My Project

Expand All @@ -82,7 +81,7 @@ def test_to_file(self):


class TestDocumentIssueMdToPdf:
"""Test the function `document_issue_md_to_pdf`."""
"""Test the function `generate_document_issue_pdf`."""

def test_to_pdf(self):
FDIR_RENDER = FDIR_TEST_OUTPUT / "test_to_pdf"
Expand All @@ -92,7 +91,7 @@ def test_to_pdf(self):
document_issue.format_configuration.output_author = False
document_issue.format_configuration.output_checked_by = False
fpth_pdf = FDIR_RENDER / f"{document_issue.document_code}.pdf"
document_issue_md_to_pdf(
generate_document_issue_pdf(
document_issue=document_issue,
fpth_pdf=fpth_pdf,
)
Expand All @@ -106,14 +105,12 @@ def test_to_pdf_with_markdown_content(self):
FDIR_RENDER = FDIR_TEST_OUTPUT / "test_to_pdf_with_markdown_content"
shutil.rmtree(FDIR_RENDER, ignore_errors=True)
FDIR_RENDER.mkdir(parents=True, exist_ok=True)
fpth_md = FDIR_RENDER / "test.md"
fpth_md.write_text(MD)
document_issue = create_test_document_issue()
document_issue.format_configuration.output_author = False
document_issue.format_configuration.output_checked_by = False
fpth_pdf = FDIR_RENDER / f"{document_issue.document_code}.pdf"
document_issue_md_to_pdf(
document_issue=document_issue, fpth_md=fpth_md, fpth_pdf=fpth_pdf
generate_document_issue_pdf(
document_issue=document_issue, fpth_pdf=fpth_pdf, md_content=MD
)
assert fpth_pdf.is_file()
# Check that the markdown file created contains the correct content
Expand All @@ -127,7 +124,7 @@ def test_to_pdf_with_author(self):
document_issue.format_configuration.output_author = True
document_issue.format_configuration.output_checked_by = False
fpth_pdf = FDIR_RENDER / f"{document_issue.document_code}.pdf"
document_issue_md_to_pdf(
generate_document_issue_pdf(
document_issue=document_issue,
fpth_pdf=fpth_pdf,
)
Expand All @@ -145,7 +142,7 @@ def test_to_pdf_with_author_and_checked_by(self):
document_issue.format_configuration.output_author = True
document_issue.format_configuration.output_checked_by = True
fpth_pdf = FDIR_RENDER / f"{document_issue.document_code}.pdf"
document_issue_md_to_pdf(
generate_document_issue_pdf(
document_issue=document_issue,
fpth_pdf=fpth_pdf,
)
Expand Down Expand Up @@ -220,7 +217,7 @@ def test_to_pdf_loads_of_notes_and_issues(self):
document_issue.format_configuration.output_author = True
document_issue.format_configuration.output_checked_by = True
fpth_pdf = FDIR_RENDER / f"{document_issue.document_code}.pdf"
document_issue_md_to_pdf(
generate_document_issue_pdf(
document_issue=document_issue,
fpth_pdf=fpth_pdf,
)
Expand All @@ -230,15 +227,3 @@ def test_to_pdf_loads_of_notes_and_issues(self):
fpth_pdf.with_suffix(".log")
).is_file() # log file should be deleted if Quarto PDF compilation is successful

def test_to_pdf_error(self):
"""If the markdown file has the same file path as the output markdown file, raise an error."""
FDIR_RENDER = FDIR_TEST_OUTPUT / "test_to_pdf_with_markdown_content"
fpth_md = FDIR_RENDER / "test.md"
fpth_pdf = FDIR_RENDER / "test.pdf"
document_issue = create_test_document_issue()
document_issue.format_configuration.output_author = False
document_issue.format_configuration.output_checked_by = False
with pytest.raises(ValueError):
document_issue_md_to_pdf(
document_issue=document_issue, fpth_md=fpth_md, fpth_pdf=fpth_pdf
)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 6 additions & 6 deletions packages/document-issue-io/tests/testoutput/test_to_file.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
project_name: A Max Fordham Project
project_name: A Max Fordham Project \\& Co
project_number : 4321
director_in_charge: OH
document_description: A description of a Max Fordham Project
Expand Down Expand Up @@ -48,11 +48,11 @@ Fordham LLP\

### Contributions

+----------------+-------------------+
| **Initials** | **Role** |
+================+===================+
| OH | RoleEnum.director |
+----------------+-------------------+
+----------------+--------------------+
| **Initials** | **Role** |
+================+====================+
| OH | Director in Charge |
+----------------+--------------------+

: {tbl-colwidths="[30,70]"}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
project_name: A Max Fordham Project
project_name: A Max Fordham Project \\& Co
project_number : 4321
director_in_charge: OH
document_description: A description of a Max Fordham Project
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
project_name: A Max Fordham Project
project_name: A Max Fordham Project \\& Co
project_number : 4321
director_in_charge: OH
document_description: A description of a Max Fordham Project
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
project_name: A Max Fordham Project
project_name: A Max Fordham Project \\& Co
project_number : 4321
director_in_charge: OH
document_description: A description of a Max Fordham Project
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
project_name: A Max Fordham Project
project_name: A Max Fordham Project \\& Co
project_number : 4321
director_in_charge: OH
document_description: A description of a Max Fordham Project
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
project_name: A Max Fordham Project
project_name: A Max Fordham Project \\& Co
project_number : 4321
director_in_charge: OH
document_description: A description of a Max Fordham Project
Expand Down Expand Up @@ -96,7 +96,7 @@ Fordham LLP\

- convert this notebook to markdown
- read `docissue.json`
- convert using `document_issue_md_to_pdf`
- convert using `generate_document_issue_pdf`

## My Project

Expand Down
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file modified packages/document-issue-io/tests/testoutput/title-block-a3.pdf
Binary file not shown.
Binary file not shown.
Binary file modified packages/document-issue-io/tests/testoutput/title-block.pdf
Binary file not shown.
Binary file modified packages/document-issue-io/tests/testoutput/title-page.pdf
Binary file not shown.
15 changes: 0 additions & 15 deletions packages/document-issue-xl/.vscode/launch.json

This file was deleted.

Loading