Created
July 21, 2022 03:36
-
-
Save ShawnMilo/287cc1e1530a7d6e9eb8633385706dc6 to your computer and use it in GitHub Desktop.
Convert .adoc file to PDF using AsciiDoctor
This file contains 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 | |
""" | |
Convert .adoc files to .pdf. | |
Periodically check for updated tags: | |
https://hub.docker.com/r/asciidoctor/docker-asciidoctor/tags | |
""" | |
import sys | |
import os | |
from subprocess import check_output | |
from os.path import splitext, basename, abspath, dirname, join | |
pdf_cmd = 'sudo docker run -it --rm -v {0}:/documents asciidoctor/docker-asciidoctor:1.24 asciidoctor-pdf -a pdf-theme=default-with-fallback-font -- /documents/{1}' | |
html_cmd = 'sudo docker run -it --rm -v {0}:/documents asciidoctor/docker-asciidoctor:1.24 asciidoctor /documents/{1}' | |
def ascii_doctor(cmd, path, source, dest): | |
user = os.getenv('USER') | |
check_output(cmd.format(path, source), shell=True) | |
check_output(f'sudo chown {user} {dest}', shell=True) | |
check_output(f'sudo chgrp {user} {dest}', shell=True) | |
print(f'created {dest}') | |
def to_pdf(f): | |
base = basename(f) | |
path = abspath(dirname(f)) | |
pdf = splitext(base)[0] + '.pdf' | |
html = splitext(base)[0] + '.html' | |
pdf_out = join(path, pdf) | |
html_out = join(path, html) | |
ascii_doctor(pdf_cmd, path, base, pdf_out) | |
ascii_doctor(html_cmd, path, base, html_out) | |
def main(): | |
files = sys.argv[1:] | |
for file in files: | |
if not (file.endswith('.adoc') or file.endswith('.md')): | |
continue | |
to_pdf(file) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment