mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Move PDF generation to a plugin. Fixes #1009
This commit is contained in:
parent
1b904ae767
commit
dfc3a7ce2f
2 changed files with 2 additions and 50 deletions
|
|
@ -14,8 +14,8 @@ import collections
|
||||||
from pelican import signals
|
from pelican import signals
|
||||||
|
|
||||||
from pelican.generators import (ArticlesGenerator, PagesGenerator,
|
from pelican.generators import (ArticlesGenerator, PagesGenerator,
|
||||||
StaticGenerator, PdfGenerator,
|
StaticGenerator, SourceFileGenerator,
|
||||||
SourceFileGenerator, TemplatePagesGenerator)
|
TemplatePagesGenerator)
|
||||||
from pelican.log import init
|
from pelican.log import init
|
||||||
from pelican.settings import read_settings
|
from pelican.settings import read_settings
|
||||||
from pelican.utils import clean_output_dir, folder_watcher, file_watcher
|
from pelican.utils import clean_output_dir, folder_watcher, file_watcher
|
||||||
|
|
@ -199,8 +199,6 @@ class Pelican(object):
|
||||||
|
|
||||||
if self.settings['TEMPLATE_PAGES']:
|
if self.settings['TEMPLATE_PAGES']:
|
||||||
generators.append(TemplatePagesGenerator)
|
generators.append(TemplatePagesGenerator)
|
||||||
if self.settings['PDF_GENERATOR']:
|
|
||||||
generators.append(PdfGenerator)
|
|
||||||
if self.settings['OUTPUT_SOURCES']:
|
if self.settings['OUTPUT_SOURCES']:
|
||||||
generators.append(SourceFileGenerator)
|
generators.append(SourceFileGenerator)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -581,52 +581,6 @@ class StaticGenerator(Generator):
|
||||||
logger.info('copying {} to {}'.format(sc.source_path, sc.save_as))
|
logger.info('copying {} to {}'.format(sc.source_path, sc.save_as))
|
||||||
|
|
||||||
|
|
||||||
class PdfGenerator(Generator):
|
|
||||||
"""Generate PDFs on the output dir, for all articles and pages coming from
|
|
||||||
rst"""
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super(PdfGenerator, self).__init__(*args, **kwargs)
|
|
||||||
try:
|
|
||||||
from rst2pdf.createpdf import RstToPdf
|
|
||||||
pdf_style_path = os.path.join(self.settings['PDF_STYLE_PATH'])
|
|
||||||
pdf_style = self.settings['PDF_STYLE']
|
|
||||||
self.pdfcreator = RstToPdf(breakside=0,
|
|
||||||
stylesheets=[pdf_style],
|
|
||||||
style_path=[pdf_style_path])
|
|
||||||
except ImportError:
|
|
||||||
raise Exception("unable to find rst2pdf")
|
|
||||||
|
|
||||||
def _create_pdf(self, obj, output_path):
|
|
||||||
if obj.source_path.endswith('.rst'):
|
|
||||||
filename = obj.slug + ".pdf"
|
|
||||||
output_pdf = os.path.join(output_path, filename)
|
|
||||||
# print('Generating pdf for', obj.source_path, 'in', output_pdf)
|
|
||||||
with open(obj.source_path) as f:
|
|
||||||
self.pdfcreator.createPdf(text=f.read(), output=output_pdf)
|
|
||||||
logger.info(' [ok] writing %s' % output_pdf)
|
|
||||||
|
|
||||||
def generate_context(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def generate_output(self, writer=None):
|
|
||||||
# we don't use the writer passed as argument here
|
|
||||||
# since we write our own files
|
|
||||||
logger.info(' Generating PDF files...')
|
|
||||||
pdf_path = os.path.join(self.output_path, 'pdf')
|
|
||||||
if not os.path.exists(pdf_path):
|
|
||||||
try:
|
|
||||||
os.mkdir(pdf_path)
|
|
||||||
except OSError:
|
|
||||||
logger.error("Couldn't create the pdf output folder in " +
|
|
||||||
pdf_path)
|
|
||||||
|
|
||||||
for article in self.context['articles']:
|
|
||||||
self._create_pdf(article, pdf_path)
|
|
||||||
|
|
||||||
for page in self.context['pages']:
|
|
||||||
self._create_pdf(page, pdf_path)
|
|
||||||
|
|
||||||
|
|
||||||
class SourceFileGenerator(Generator):
|
class SourceFileGenerator(Generator):
|
||||||
def generate_context(self):
|
def generate_context(self):
|
||||||
self.output_extension = self.settings['OUTPUT_SOURCES_EXTENSION']
|
self.output_extension = self.settings['OUTPUT_SOURCES_EXTENSION']
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue