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

Setup pandoc workflow #1

Merged
merged 6 commits into from
Jun 17, 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
29 changes: 29 additions & 0 deletions .github/workflows/md2pdf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Pandoc - convert markdown to pdf
run-name: python converter
on:
workflow_dispatch:
inputs:
markdown-source-dir:
description: markdown input directory
required: true
type: string

jobs:
md-to-pdf:
name: Pandoc - MD to PDF
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Pandoc core image
uses: docker://pandoc/core:2.9
with:
args: "--help"

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Python virtual environments
venv/
.venv/

# Output PDF path directory
_output/

9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# python-converter
# python-converter

### Usage

Yen virtualenv:
```shell
yen create -p 3.12 venv
```
54 changes: 54 additions & 0 deletions converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
from pathlib import Path
# import pypandoc


if len(sys.argv) < 2:
print("Usage: python converter.py <path-to-markdown>")
sys.exit(1)

SOURCE_PATH_TO_MD = Path(sys.argv[1])
DESTINATION_PATH_TO_PDF = Path(SOURCE_PATH_TO_MD) / "_output"

if not SOURCE_PATH_TO_MD.exists() or not SOURCE_PATH_TO_MD.is_dir():
print("Invalid path to markdown directory!")
sys.exit(1)

if not DESTINATION_PATH_TO_PDF.exists():
DESTINATION_PATH_TO_PDF.mkdir()

markdown_files = list(SOURCE_PATH_TO_MD.rglob("*.md"))

if not markdown_files:
print(f"No markdown files found in {SOURCE_PATH_TO_MD}")

source_md_files = []
output_pdf_files = []

print("Convert MD to PDF:\n")
for markdown_file in markdown_files:
relative_path = markdown_file.relative_to(SOURCE_PATH_TO_MD)
pdf_output_path = DESTINATION_PATH_TO_PDF / relative_path.with_suffix(".pdf")

try:
# pypandoc.convert_file(
# str(markdown_file),
# 'pdf',
# outputfile=str(pdf_file),
# extra_args=['--pdf-engine=pdflatex']
# )
pdf_output_path.parent.mkdir(parents=True, exist_ok=True)
pdf_output_path.touch(exist_ok=True)

source_md_files.append(markdown_file)
output_pdf_files.append(pdf_output_path)
except Exception as e:
print(f"Error converting {markdown_file}: {e}")

print("Source markdown files:")
for input_md in source_md_files:
print(input_md)

print("\nOutput PDF files:")
for output_pdf in output_pdf_files:
print(output_pdf)
1 change: 1 addition & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test Python Pandoc
1 change: 1 addition & 0 deletions test/second/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test Python Pandoc