|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +from ._bot import BaseBot |
| 8 | + |
| 9 | +__all__ = [ |
| 10 | + 'LatexBot', |
| 11 | +] |
| 12 | + |
| 13 | + |
| 14 | +class LatexBot(BaseBot): |
| 15 | + """LatexBot. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, config_file=None, sender=None) -> None: |
| 19 | + self.__name__ = "emailbot" |
| 20 | + |
| 21 | + def _clone_overleaf(self, document_code): |
| 22 | + """Temporarily clone the overleaf project on the local machine. |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + document_code : str |
| 27 | + code of the overleaf project. you can find in the address bar in your |
| 28 | + broweser when you open the overleaf project. |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + TemporaryDirctory object |
| 33 | + the temporary directory where the project has been cloned. This can |
| 34 | + be used later to be cleaned up using `temp_dir.cleanup()`. |
| 35 | +
|
| 36 | + Raises |
| 37 | + ------ |
| 38 | + Exception |
| 39 | + if this exception is raised, the cloning process has been aborted. |
| 40 | + Check if the `document_code` is correct and that you have `git` |
| 41 | + installed on your machine. |
| 42 | + """ |
| 43 | + temp_dir = tempfile.TemporaryDirectory() |
| 44 | + out = subprocess.run(["git", "clone", f"https://git.overleaf.com/{document_code}"], cwd=temp_dir.name) |
| 45 | + if out.returncode == 0: |
| 46 | + print(f"project temporary saved in {temp_dir.name}") |
| 47 | + else: |
| 48 | + raise Exception |
| 49 | + # temp_dir.cleanup() |
| 50 | + return temp_dir |
| 51 | + |
| 52 | + def convert_tex_to_docx(self, input_path, output_path=None): |
| 53 | + """convert all the .tex files in a folder into .docx files. If no `output_path` |
| 54 | + is provided, the .docx files will be saved in the same directory as the .tex |
| 55 | + files. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + input_path : str |
| 60 | + path to the folder containing the .tex file(s) |
| 61 | + output_path : str, optional |
| 62 | + path to the output .docx file(s), by default None |
| 63 | + """ |
| 64 | + pathlist = Path(input_path).rglob('*.tex') |
| 65 | + for file in pathlist: |
| 66 | + output = str(file).split('.tex')[ |
| 67 | + 0]+'.docx' if not output_path else Path().joinpath(output_path, str(file.name).split('.tex')[0]+'.docx') |
| 68 | + out = subprocess.run(["pandoc", "-o", output, "-t", "docx", file]) |
| 69 | + print("The exit code was: %d" % out.returncode) |
| 70 | + |
| 71 | + def convert_overleaf_to_docx(self, document_code, output_path): |
| 72 | + """convert the overleaf project into a .docx file. Any .tex file in the |
| 73 | + overleaf repository will be converted into a .docx file with the same name. |
| 74 | +
|
| 75 | + Parameters |
| 76 | + ---------- |
| 77 | + document_code : str |
| 78 | + code of the overleaf project. you can find in the address bar in your |
| 79 | + broweser when you open the overleaf project. |
| 80 | + output_path : str |
| 81 | + path to the output .docx file |
| 82 | + """ |
| 83 | + temp_dir = self._clone_overleaf(document_code) |
| 84 | + self.convert_tex_to_docx(Path().joinpath(temp_dir.name, document_code), output_path) |
| 85 | + print(f"project saved in {output_path}") |
| 86 | + temp_dir.cleanup() |
| 87 | + print("temporary clone removed") |
0 commit comments