summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2023-06-01 01:01:58 +0800
committerKeuin <[email protected]>2023-06-01 01:01:58 +0800
commitc71f0eeb1be07c4085b2eb6f0e03388c4bce695e (patch)
tree8871d47eeee458239fadf2c9ab87a49e0b45cb93
parent10bafe3fae2e8a06d4814ccc6cc499712cfaad46 (diff)
extract tex generation procedure
-rw-r--r--main.py36
1 files 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__':