Skip to content

Commit

Permalink
Update code for Ruff code style compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmayer committed May 27, 2024
1 parent 72bb7f3 commit 5b90b4a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
28 changes: 11 additions & 17 deletions pelican/plugins/pdf/pdf.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""
PDF Generator
-------
"""PDF Generator plugin for Pelican.
The pdf plugin generates PDF files from reStructuredText and Markdown sources.
This plugin generates PDF files from reStructuredText and Markdown source files.
"""

from itertools import chain
import logging
import os
import re
import time
from typing import ClassVar

from rst2pdf.createpdf import RstToPdf

Expand All @@ -21,9 +20,9 @@


class PdfGenerator(Generator):
"Generate PDFs on the output dir, for all articles and pages"
"""Generate PDFs on the output dir, for all articles and pages."""

supported_md_fields = ["date"]
supported_md_fields: ClassVar[list[str]] = ["date"]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -33,10 +32,7 @@ def __init__(self, *args, **kwargs):
else:
pdf_style_path = []

if "PDF_STYLE" in self.settings:
pdf_style = [self.settings["PDF_STYLE"]]
else:
pdf_style = []
pdf_style = [self.settings["PDF_STYLE"]] if "PDF_STYLE" in self.settings else []

self.pdfcreator = RstToPdf(
breakside=0, stylesheets=pdf_style, style_path=pdf_style_path, raw_html=True
Expand Down Expand Up @@ -95,7 +91,7 @@ def _create_pdf(self, obj, output_path):
hrefs = self._get_intrasite_link_regex()
text = hrefs.sub(lambda m: obj._link_replacer(obj.get_siteurl(), m), text)

logger.info(" [ok] writing %s" % output_pdf)
logger.info(f" [ok] writing {output_pdf}")

self.pdfcreator.createPdf(text=(header + text), output=output_pdf)

Expand All @@ -104,11 +100,9 @@ def _create_pdf(self, obj, output_path):

def _get_intrasite_link_regex(self):
intrasite_link_regex = self.settings["INTRASITE_LINK_REGEX"]
regex = r"""
(?P<markup>)(?P<quote>)(?P<path>(\:?){}(?P<value>.*?)(?=[>\n]))
""".format(
intrasite_link_regex
)
regex = rf"""
(?P<markup>)(?P<quote>)(?P<path>(\:?){intrasite_link_regex}(?P<value>.*?)(?=[>\n]))
"""
return re.compile(regex, re.X)

def _set_file_utime(self, path, datetime):
Expand All @@ -128,7 +122,7 @@ def generate_output(self, writer=None):
try:
os.mkdir(pdf_path)
except OSError:
logger.error("Couldn't create the pdf output folder in " + pdf_path)
logger.exception("Couldn't create the pdf output folder in " + pdf_path)

for obj in chain(self.context["articles"], self.context["pages"]):
self._create_pdf(obj, pdf_path)
Expand Down
5 changes: 3 additions & 2 deletions pelican/plugins/pdf/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
from tempfile import mkdtemp
import unittest

import pdf
from pelican import Pelican
from pelican.readers import MarkdownReader
from pelican.settings import read_settings

import pdf

CUR_DIR = os.path.dirname(__file__)


class TestPdfGeneration(unittest.TestCase):
"""Test class for PDF generation."""

def setUp(self, override=None):
self.temp_path = mkdtemp(prefix="pelicantests.")
settings = {
Expand Down

0 comments on commit 5b90b4a

Please sign in to comment.