|
| 1 | +"""Generate latex files""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import traceback |
| 6 | +import sys |
| 7 | +from jinja2 import Environment, FileSystemLoader |
| 8 | + |
| 9 | +if __name__ == "__main__": |
| 10 | + |
| 11 | + # Change working directory to this directory |
| 12 | + abspath = os.path.abspath(__file__) |
| 13 | + dirname = os.path.dirname(abspath) |
| 14 | + os.chdir(dirname) |
| 15 | + |
| 16 | + # Load printablepaperlib parameters |
| 17 | + try: |
| 18 | + with open('./printablepaperlib.json') as f: |
| 19 | + parameters = json.load(f) |
| 20 | + except FileNotFoundError: |
| 21 | + traceback.print_exc() |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | + # Load paperlib |
| 25 | + TEMPLATE_SUBDIR = str(dirname).find( |
| 26 | + "/", str(dirname).find("templates/")+10)+1 |
| 27 | + PAPERLIB_PATH = str(dirname)[0:TEMPLATE_SUBDIR] + "paperlib.json" |
| 28 | + |
| 29 | + try: |
| 30 | + with open(PAPERLIB_PATH) as f: |
| 31 | + paperlib = json.load(f) |
| 32 | + except FileNotFoundError: |
| 33 | + traceback.print_exc() |
| 34 | + sys.exit(1) |
| 35 | + |
| 36 | + # Load template |
| 37 | + |
| 38 | + env = Environment(loader=FileSystemLoader(dirname)) |
| 39 | + template = env.get_template('template.latex_template') |
| 40 | + |
| 41 | + # printablepaperlib specific code |
| 42 | + |
| 43 | + for page_size in parameters["page_sizes"]: |
| 44 | + page_size_name = paperlib[page_size]["displayname"] |
| 45 | + for orientation in parameters["orientations"]: |
| 46 | + FILENAME = "{}_{}_{}.latex".format( |
| 47 | + parameters["template_name"], |
| 48 | + page_size_name, |
| 49 | + orientation) |
| 50 | + |
| 51 | + # 10mm lines |
| 52 | + |
| 53 | + py_horizonzal_10mm_count = int( |
| 54 | + paperlib[page_size][orientation]["width"])-int( |
| 55 | + paperlib[page_size][orientation]["width"] % 10) |
| 56 | + |
| 57 | + # Remove lines on the page borders |
| 58 | + if py_horizonzal_10mm_count == round(paperlib[page_size][orientation]["width"]): |
| 59 | + py_horizonzal_10mm_count -= 10 |
| 60 | + |
| 61 | + py_vertical_10mm_count = int( |
| 62 | + paperlib[page_size][orientation]["height"])-int( |
| 63 | + paperlib[page_size][orientation]["height"] % 10) |
| 64 | + |
| 65 | + # Remove lines on the page borders |
| 66 | + if py_vertical_10mm_count == round(paperlib[page_size][orientation]["height"]): |
| 67 | + py_vertical_10mm_count -= 10 |
| 68 | + |
| 69 | + # 5mm lines |
| 70 | + |
| 71 | + py_horizonzal_5mm_count = int( |
| 72 | + paperlib[page_size][orientation]["width"])-int( |
| 73 | + paperlib[page_size][orientation]["width"] % 5) |
| 74 | + |
| 75 | + # Remove lines on the page borders |
| 76 | + if py_horizonzal_5mm_count == round(paperlib[page_size][orientation]["width"]): |
| 77 | + py_horizonzal_5mm_count -= 5 |
| 78 | + |
| 79 | + py_vertical_5mm_count = int( |
| 80 | + paperlib[page_size][orientation]["height"])-int( |
| 81 | + paperlib[page_size][orientation]["height"] % 5) |
| 82 | + |
| 83 | + # Remove lines on the page borders |
| 84 | + if py_vertical_5mm_count == round(paperlib[page_size][orientation]["height"]): |
| 85 | + py_vertical_5mm_count -= 5 |
| 86 | + |
| 87 | + with open(FILENAME, "w") as f: |
| 88 | + f.write(template.render( |
| 89 | + paperformat=page_size, |
| 90 | + paperorientation=orientation, |
| 91 | + horizontal_5mm_count=py_horizonzal_5mm_count, |
| 92 | + vertical_5mm_count=py_vertical_5mm_count, |
| 93 | + horizontal_10mm_count=py_horizonzal_10mm_count, |
| 94 | + vertical_10mm_count=py_vertical_10mm_count, |
| 95 | + )) |
0 commit comments