From c71f0eeb1be07c4085b2eb6f0e03388c4bce695e Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 1 Jun 2023 01:01:58 +0800 Subject: extract tex generation procedure --- main.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index b8d6184..2e4810d 100644 --- a/main.py +++ b/main.py @@ -171,21 +171,7 @@ class LatexGenerator: return doc -def main(): - if len(sys.argv) > 2: - print(f'Usage: <{sys.argv[0]}> [path_to_html_file]') - exit(0) - if len(sys.argv) == 2: - file_name = sys.argv[1] - try: - with open(file_name, 'r', encoding='utf-8') as f: - html = f.read() - except FileNotFoundError: - print(f'File does not exist: {file_name}') - exit(1) - else: - # read html from STDIN - html = sys.stdin.read() +def html_to_tex(html) -> str: p = BeautifulSoup(html, "html5lib") lyric = p.select_one('.hiragana') if not lyric: @@ -209,7 +195,25 @@ def main(): gen.artist, gen.title = artist, title # FIXME hardcoded CJK font gen.cjk_font_main = 'Noto Serif CJK JP' - print(gen.generate_lyric(tokens)) + return gen.generate_lyric(tokens) + + +def main(): + if len(sys.argv) > 2: + print(f'Usage: <{sys.argv[0]}> [path_to_html_file]') + exit(0) + if len(sys.argv) == 2: + file_name = sys.argv[1] + try: + with open(file_name, 'r', encoding='utf-8') as f: + html = f.read() + except FileNotFoundError: + print(f'File does not exist: {file_name}') + exit(1) + else: + # read html from STDIN + html = sys.stdin.read() + print(html_to_tex(html)) if __name__ == '__main__': -- cgit v1.2.3