diff --git a/.github/workflows/md2pdf.yml b/.github/workflows/md2pdf.yml new file mode 100644 index 0000000..41fac8b --- /dev/null +++ b/.github/workflows/md2pdf.yml @@ -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" + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53100f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Python virtual environments +venv/ +.venv/ + +# Output PDF path directory +_output/ + diff --git a/README.md b/README.md index a4a2ea7..3c3e70f 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -# python-converter \ No newline at end of file +# python-converter + +### Usage + +Yen virtualenv: +```shell +yen create -p 3.12 venv +``` diff --git a/converter.py b/converter.py new file mode 100644 index 0000000..582f020 --- /dev/null +++ b/converter.py @@ -0,0 +1,54 @@ +import sys +from pathlib import Path +# import pypandoc + + +if len(sys.argv) < 2: + print("Usage: python converter.py ") + 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) diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..cc103d9 --- /dev/null +++ b/test/README.md @@ -0,0 +1 @@ +# Test Python Pandoc diff --git a/test/second/README.md b/test/second/README.md new file mode 100644 index 0000000..cc103d9 --- /dev/null +++ b/test/second/README.md @@ -0,0 +1 @@ +# Test Python Pandoc