|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from typing import List |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import argparse |
| 6 | + |
| 7 | + |
| 8 | +class LineBreaker: |
| 9 | + |
| 10 | + def file_to_file_compare(self, file_path1: str, file_path2: str): |
| 11 | + content1 = None |
| 12 | + content2 = None |
| 13 | + with open(file_path1) as f1: |
| 14 | + content1 = f1.readlines() |
| 15 | + with open(file_path2) as f2: |
| 16 | + content2 = f2.readlines() |
| 17 | + |
| 18 | + content1 = "\n".join(content1) |
| 19 | + content2 = "\n".join(content2) |
| 20 | + |
| 21 | + serial1 = [] |
| 22 | + serial2 = [] |
| 23 | + for ch in content1: |
| 24 | + if not ch.isspace(): |
| 25 | + serial1.append(ch) |
| 26 | + for ch in content2: |
| 27 | + if not ch.isspace(): |
| 28 | + serial2.append(ch) |
| 29 | + print(str(len(serial1)), str(len(serial2))) |
| 30 | + |
| 31 | + def __init__(self, file_path): |
| 32 | + |
| 33 | + if os.path.exists(file_path): |
| 34 | + self.original_path = file_path |
| 35 | + filename = os.path.splitext(file_path)[0] |
| 36 | + self.backup_path = filename+".backup"+".tex" |
| 37 | + self.new_contents: List[str] = [] |
| 38 | + if os.path.exists(self.backup_path): |
| 39 | + print("ERROR: Must delete backup file before proceeding.") |
| 40 | + exit(-1) |
| 41 | + shutil.copyfile(self.original_path, self.backup_path) |
| 42 | + with open(file_path, "r") as reader: |
| 43 | + self.contents: List[str] = reader.readlines() |
| 44 | + if len(self.contents) < 0: |
| 45 | + raise ValueError("File has no content") |
| 46 | + |
| 47 | + self.__process_lines() |
| 48 | + with open(file_path, "w") as writer: |
| 49 | + writer.writelines(self.new_contents) |
| 50 | + |
| 51 | + self.file_to_file_compare(self.backup_path, self.original_path) |
| 52 | + else: |
| 53 | + raise FileNotFoundError |
| 54 | + |
| 55 | + def __period_handling(self, line: str) -> str: |
| 56 | + # range is shortened, so that we can skip the last period |
| 57 | + current_line = "" |
| 58 | + for i in range(0, len(line)): |
| 59 | + current_line += line[i] |
| 60 | + if line[i] == ".": |
| 61 | + # check if . has alphanumeric at left, and space at right |
| 62 | + if i+1 != len(line): |
| 63 | + if line[i+1].isspace(): |
| 64 | + # introduce linebreak at line[i+1 position] |
| 65 | + current_line += "\n" |
| 66 | + proper_stripped_lines = current_line.splitlines() |
| 67 | + current_line = "" |
| 68 | + for line in proper_stripped_lines: |
| 69 | + current_line += line.lstrip() + "\n" |
| 70 | + return current_line.lstrip() |
| 71 | + |
| 72 | + def __is_comment(self, line: str) -> bool: |
| 73 | + return line.startswith("%") |
| 74 | + |
| 75 | + def __copy_whitespace(self, old_line: str, processed_line: str): |
| 76 | + prefix_whitespace = "" |
| 77 | + for character in old_line: |
| 78 | + if character.isspace(): |
| 79 | + prefix_whitespace += character |
| 80 | + else: |
| 81 | + break |
| 82 | + return prefix_whitespace + processed_line |
| 83 | + # try: |
| 84 | + # prefix_whitespace + processed_line |
| 85 | + # except TypeError: |
| 86 | + # print(prefix_whitespace, processed_line) |
| 87 | + |
| 88 | + def __process_lines(self): |
| 89 | + for line in self.contents: |
| 90 | + # check length, if contains too few characters, copy, continue |
| 91 | + if len(line) < 1: |
| 92 | + self.new_contents.append(line) |
| 93 | + continue |
| 94 | + # if comment, copy it as it is |
| 95 | + if self.__is_comment(line): |
| 96 | + self.new_contents.append(line) |
| 97 | + continue |
| 98 | + # strip line |
| 99 | + stripped_line = line.strip() |
| 100 | + processed_line = self.__period_handling(stripped_line) |
| 101 | + processed_line = self.__copy_whitespace(line, processed_line) |
| 102 | + self.new_contents.append(processed_line) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + parser = argparse.ArgumentParser("Process tex file to insert new line \ |
| 107 | + after period, selectively.") |
| 108 | + parser.add_argument("path") |
| 109 | + args = parser.parse_args() |
| 110 | + LineBreaker(args.path) |
| 111 | + |
0 commit comments