commit 5283a7e4813de74b9dae8da1d17817fd89d0132e Author: Théo Marchal Date: Sat Apr 6 18:30:42 2024 +0200 Squashed 'custom/convert-furigana/' content from commit f21e49b git-subtree-dir: custom/convert-furigana git-subtree-split: f21e49bb9b19794b85ea4cf778872c0524d84573 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0665524 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 keb + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ed02258 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# convert-furigana + +## What is it? + +There are a few extensions that add [furigana](https://en.wikipedia.org/wiki/Furigana) support to Markdown. I use [Obsidian](https://obsidian.md/), and therefore its furigana extension: [obsidian-markdown-furigana](https://github.com/steven-kraft/obsidian-markdown-furigana). It works well, but by nature is limited by being used within Obsidian. + +For that purpose, this python script converts this specific syntax to common, all-platforms HTML ruby tags. + +## Process + +This specific extension syntax is as following: +``` +{漢字|かんじ} +{漢字|かん|じ} +``` + +When processed by this script, the result is: +``` +漢字かんじ +かん +``` + +Which leads visually from, by default markdown standards: + +> {漢字|かんじ} +> +> {漢字|かん|じ} + +to common HTML ruby syntax: + +> 漢字かんじ +> +> かん + +## Limitations + +This has been made for my personal use, which is only kanjis with kanas as furigana. The regexp used do not recognize other inputs. But you can modify them easily for your use if needed. + +## Usage + +Usage: `python3 convert-furigana.py [root folder]` + +Note: Please be careful, this software applies modifications recursively! \ No newline at end of file diff --git a/examples/furigana1.md b/examples/furigana1.md new file mode 100644 index 0000000..9fe47a3 --- /dev/null +++ b/examples/furigana1.md @@ -0,0 +1,6 @@ +{僕|ボク}の{筆記|ひっ|き} + +### ~によるN + +* テレビによる{報道|ほうどう} +* この{地震|じ|しん}による{津波|つ|なみ}の{心配|しんぱい}はありません。 \ No newline at end of file diff --git a/examples/subfolder/furigana2.md b/examples/subfolder/furigana2.md new file mode 100644 index 0000000..3401cd6 --- /dev/null +++ b/examples/subfolder/furigana2.md @@ -0,0 +1,7 @@ +## ふりがなテスト + +{漢字|かんじ} + +{漢字|かん|じ} + +{漢字|カン|ジ} \ No newline at end of file diff --git a/program/convert-furigana.py b/program/convert-furigana.py new file mode 100644 index 0000000..732717a --- /dev/null +++ b/program/convert-furigana.py @@ -0,0 +1,71 @@ +import sys +import os +from glob import glob +import re + +### Patterns ### + +# https://www.reddit.com/r/learnpython/comments/h0tsaa/regex_expression_for_japanese_extracting/ +re_brace = '{(.+?)}' +re_kanji = '([㐀-䶵一-鿋豈-頻]+)' +re_kana = '([ぁ-ゟ゠-ヿ]+)' + +### Open file ### + +def open_file(filename): + with open(filename, 'r', encoding='utf-8-sig') as file: + filedata = file.read() + return filedata + +### Replace ### + +def write_ruby_simple(kanji, kana): + return f"{kanji}{kana}" + +def write_ruby_multiple(kanji, kanas): + result = f"" + for idx, kana in enumerate(kanas): + result += f"{kanji[idx]}" + result += f"{kana}" + result += f"" + return result + +def convert_to_ruby(content): + kanji = re.search(re_kanji, content.group()) + kana = re.findall(re_kana, content.group()) + + if (len(kana) == 1): + return write_ruby_simple(kanji.group(), kana[0]) + else: + return write_ruby_multiple(kanji.group(), kana) + +def convert_pattern(text): + res_text = re.sub(re_brace, convert_to_ruby, text) + return res_text + +### Write to file ### + +def write_file(filename, text): + with open(filename, 'w', encoding='utf-8-sig') as file: + file.write(text) + + +### Execution ### + +file_list = [] + +if len(sys.argv) > 1: + path = sys.argv[1] + for filename in glob(path + "/**/*.md", recursive=True): + if (os.path.isfile(filename)): + file_list.append(filename) +else: + print(f"Usage: python3 {sys.argv[0]} [root folder]") + print(f"Note: Please be careful, this software applies modifications recursively!") + +for filename in file_list: + print(f"Found file {filename}, processing...", end='') + data = open_file(filename) + text = convert_pattern(data) + write_file(filename, text) + print(f" done!") \ No newline at end of file