Exemplo n.º 1
0
def _download_existing_file(ctx: Context):
    if not ctx.out_file.exists():
        ctx.info(f'trying to download {ctx.out_file.name}')
        url = BASE_URL + urllib.parse.quote(ctx.out_file.name)
        if elib.downloader.download(url, ctx.out_file):
            ctx.info('download successful')
        else:
            ctx.info('download failed')
Exemplo n.º 2
0
def skip_file(ctx: Context) -> bool:
    """
    Checks if a file should be skipped

    Tests for:
        - EDLM version
        - index.md
        - template.tex
        - media folders content

    Args:
        ctx: Context

    Returns: True if file should be skipped

    """
    if ctx.out_file.exists():
        pdf = pdfrw.PdfReader(str(ctx.out_file.absolute()))
        creator = pdfstring.PdfString.to_unicode(pdf.Info.Creator)
        if creator != 'EDLM ' + __version__:
            ctx.info(
                'document generated with an older version of EDLM, regenerating'
            )
            return False
        producer = pdfstring.PdfString.to_unicode(pdf.Info.Producer)
        if producer != 'EDLM ' + _get_document_hash(ctx):
            ctx.info('document updated, regenerating')
            return False
        ctx.info('this document has not been modified, skipping it')
        if ctx.regen:
            ctx.info('forcing re-generation of all documents anyway')
            return False

        return True

    return False
Exemplo n.º 3
0
def _build_folder(ctx: Context):
    ctx.info(f'making PDF')

    with TempDir(ctx):

        get_media_folders(ctx)

        get_template(ctx)

        get_index_file(ctx)

        get_settings(ctx)

        get_includes(ctx)

        ctx.template_file = Path(ctx.temp_dir, 'template.tex').absolute()

        title = ctx.source_folder.name
        ctx.title = title

        out_folder = elib.path.ensure_dir('.', must_exist=False, create=True)
        ctx.out_folder = out_folder

        for paper_size in ctx.settings.papersize:
            ctx.paper_size = paper_size
            _set_max_image_width(ctx)

            if paper_size.lower() == 'a4' or len(ctx.settings.papersize) == 1:
                ctx.out_file = Path(out_folder, f'{title}.PDF').absolute()
            else:
                ctx.out_file = Path(out_folder, f'{title}_{paper_size}.PDF').absolute()

            _download_existing_file(ctx)

            if skip_file(ctx):
                continue

            process_markdown(ctx)

            check_for_unused_images(ctx)

            process_latex(ctx)

            ctx.source_file = Path(ctx.temp_dir, 'source.md').absolute()
            ctx.source_file.write_text(ctx.markdown_text, encoding='utf8')

            ctx.info(f'building format: {paper_size}')

            ctx.debug(f'context:\n{elib.pretty_format(ctx.__repr__())}')

            # noinspection SpellCheckingInspection
            pandoc_cmd = [
                '-s',
                '--toc',
                f'--template "{ctx.template_file}"',
                f'--listings "{ctx.source_file}"',
                f'-o "{ctx.out_file}"',
                '-V geometry:margin=1.5cm',
                '-V test',
                '-V geometry:headheight=17pt',
                '-V geometry:includehead',
                '-V geometry:includefoot',
                '-V geometry:heightrounded',
                '-V lot',
                '-V lof',
                '--pdf-engine=xelatex',
                f'-V papersize:{ctx.paper_size}',
                '-N',
            ]

            PANDOC(' '.join(pandoc_cmd))

            add_metadata_to_pdf(ctx)