Created
February 11, 2026 12:23
-
-
Save imakin/73964fd25151f925ff6a98b77912db58 to your computer and use it in GitHub Desktop.
simple launcher to convert markdown to odt (using pandoc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| simple launcher for pandoc to convert markdown to odt, with support for math and citation | |
| ## Ubuntu | |
| ```bash | |
| sudo apt update | |
| sudo apt install pandoc | |
| pip install pypandoc | |
| ``` | |
| ## Windows11 | |
| - install chocolatey | |
| ```powershell | |
| choco install pandoc | |
| pip install pypandoc | |
| ``` | |
| """ | |
| import argparse | |
| import os | |
| import sys | |
| import pypandoc | |
| def convert_md_to_odt(input_path, output_path, bib_path=None): | |
| # 1. Validasi Input | |
| if not os.path.exists(input_path): | |
| print(f"Error: File input '{input_path}' tidak ditemukan.") | |
| sys.exit(1) | |
| # 2. Konfigurasi Argumen Pandoc | |
| # --standalone: Wajib untuk membuat dokumen ODT lengkap (bukan fragment) | |
| extra_args = ['--standalone'] | |
| # Penanganan Sitasi | |
| if bib_path: | |
| if os.path.exists(bib_path): | |
| print(f"Info: Menggunakan referensi dari '{bib_path}'") | |
| extra_args.append('--citeproc') # Processor sitasi | |
| extra_args.append(f'--bibliography={bib_path}') | |
| else: | |
| print(f"Warning: File bibliografi '{bib_path}' tidak ditemukan. Sitasi mungkin gagal.") | |
| # Deteksi apakah perlu MathML (Pandoc biasanya otomatis, tapi kita pastikan) | |
| # Tidak perlu argumen khusus, Pandoc otomatis convert $latex$ ke ODF MathML. | |
| try: | |
| print(f"Sedang mengonversi '{input_path}' ke '{output_path}'...") | |
| # 3. Eksekusi Konversi | |
| # Format: markdown dengan ekstensi tex_math_dollars untuk LaTeX math | |
| pypandoc.convert_file( | |
| input_path, | |
| 'odt', | |
| format='markdown+tex_math_dollars', | |
| outputfile=output_path, | |
| extra_args=extra_args | |
| ) | |
| print(f"Sukses! Dokumen tersimpan di: {os.path.abspath(output_path)}") | |
| except OSError as e: | |
| print("\nError: Pandoc tidak ditemukan di sistem.") | |
| print("Pastikan Pandoc terinstall (apt install pandoc / brew install pandoc / choco install pandoc)") | |
| except Exception as e: | |
| print(f"\nTerjadi kesalahan: {e}") | |
| if __name__ == "__main__": | |
| # Setup Argument Parser | |
| parser = argparse.ArgumentParser( | |
| description="Toolchain Konversi Markdown ke ODT (Support: Math, Image, Citation)" | |
| ) | |
| # Argumen Posisi (Wajib) | |
| parser.add_argument("input_file", help="Path ke file Markdown (.md)") | |
| parser.add_argument("output_file", help="Nama file output (.odt)") | |
| # Argumen Opsional | |
| parser.add_argument( | |
| "--bib", | |
| help="Path ke file bibliografi (.bib) untuk sitasi", | |
| default=None | |
| ) | |
| args = parser.parse_args() | |
| convert_md_to_odt(args.input_file, args.output_file, args.bib) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment