Skip to content
Open
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
170 changes: 170 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Test Input/Output
*.in
*.out

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

*.pdf
.vscode/
.devcontainer/
94 changes: 0 additions & 94 deletions build_my_notebook.py

This file was deleted.

11 changes: 11 additions & 0 deletions diary_generator/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sys

from diary_generator.example_diary import create_example_diary


def main():
create_example_diary(sys.argv[1])


if __name__ == "__main__":
main()
84 changes: 84 additions & 0 deletions diary_generator/classes/doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from typing import Optional

import fitz

from diary_generator.classes.page import Page
from diary_generator.classes.table_of_content_entry import TableOfContentEntry


class Doc:
# Represents the output document
# This class exists primarily to collect the invidual pages, in the correct order.
# As the intra pdf linking scheme relies on page number, all pages must be known
# before links are created.
#
# Requires the path to a tempate pdf file when created:
# the first page of this file will be used as the default
# template for each page created, if the page itself does
# not have a dedicated template.
pages: list[Page]
fitz_doc: fitz.Document
toc: list[TableOfContentEntry]

def __init__(self, base_pdf_name: str):
self.pages = []
self.fitz_doc = fitz.open()
self.base_pdf_name = base_pdf_name
self.toc = []

def add_page(
self,
base_pdf_name: Optional[str] = None,
title="",
title_x=20,
title_y=250,
title_size=50,
toc_level=0,
):
base_pdf = self._get_base_pdf_for_page(base_pdf_name)
page = Page(
base_pdf,
title=title,
title_x=title_x,
title_y=title_y,
title_size=title_size,
toc_level=toc_level,
)
self.addPages(page)
return page

def _get_base_pdf_for_page(self, base_pdf_name: Optional[str]) -> fitz.Document:
if base_pdf_name is None:
return fitz.open(self.base_pdf_name)
else:
return fitz.open(base_pdf_name)

# Add one or more pages into the document.
# This method will create fitz pages for each doc, however rendering of content
# is done as a separate pass
def addPages(self, *pages: Page):
for page in pages:
self.pages.append(page)
page.page_number = len(self.pages) - 1
# copy tempate into new doc
self.fitz_doc.insert_pdf(
page.base_pdf,
from_page=0,
to_page=0,
start_at=-1,
rotate=-1,
links=True,
annots=True,
show_progress=0,
final=1,
)
if page.toc_level != 0:
self.toc.append((page.toc_level, page.title, page.page_number))

# Ask each page to render their own conent- this is done once all pages are added
# After rendering the document is saved.
def render(self, output_file_name: str):
for page in self.pages:
page.render(self.fitz_doc)
self.fitz_doc.set_toc(self.toc, collapse=1) # type: ignore
self.fitz_doc.save(output_file_name)
Loading