diff --git a/Note/FormatNoteViewController.swift b/Note/FormatNoteViewController.swift index 78bdc80..dfa2168 100644 --- a/Note/FormatNoteViewController.swift +++ b/Note/FormatNoteViewController.swift @@ -121,14 +121,99 @@ class FormatNoteViewController: UIViewController { } @objc private func didTapiCloudButton() { + textView.text = formatForCloud(textView.text) rightBarButtonMode = .reset updateRightBarButtonItem() } @objc private func didTapNoteButton() { + textView.text = formatForNote(textView.text) rightBarButtonMode = .reset updateRightBarButtonItem() } + + private func formatForCloud(_ text: String) -> String { + var content = text + content = content.replacingOccurrences(of: " ", with: "") + content = content.replacingOccurrences(of: " ", with: "") + content = content.replacingOccurrences(of: "\u{00A0}", with: "") + content = content.replacingOccurrences(of: "# ", with: "") + content = content.replacingOccurrences(of: "#", with: "") + content = content.replacingOccurrences(of: "!", with: "。") + content = content.replacingOccurrences(of: "!", with: "。") + content = content.replacingOccurrences(of: ";", with: "。") + content = content.replacingOccurrences(of: ";", with: "。") + content = content.replacingOccurrences(of: ".", with: "。") + content = content.replacingOccurrences(of: "(", with: "(") + content = content.replacingOccurrences(of: ")", with: ")") + content = content.replacingOccurrences(of: "?", with: "?") + content = content.replacingOccurrences(of: ",", with: ",") + content = content.replacingOccurrences(of: ":", with: ":") + + let numberReplacements: [(String, String)] = [ + ("20、", "二十、"), ("19、", "十九、"), ("18、", "十八、"), ("17、", "十七、"), + ("16、", "十六、"), ("15、", "十五、"), ("14、", "十四、"), ("13、", "十三、"), + ("12、", "十二、"), ("11、", "十一、"), ("10、", "十、"), + ("9、", "九、"), ("8、", "八、"), ("7、", "七、"), ("6、", "六、"), + ("5、", "五、"), ("4、", "四、"), ("3、", "三、"), ("2、", "二、"), ("1、", "一、") + ] + for (old, new) in numberReplacements { + content = content.replacingOccurrences(of: old, with: new) + } + + content = content.replacingOccurrences(of: "。", with: "。\n") + content = content.replacingOccurrences(of: "?", with: "?\n") + content = removeEmptyLines(content) + content = content.replacingOccurrences(of: ":\\s*", with: ":\n\n", options: .regularExpression) + content = content.replacingOccurrences(of: "。", with: "。\n") + content = content.replacingOccurrences(of: "?", with: "?\n") + content = content.replacingOccurrences(of: ":\n\n", with: ":") + content = content.replacingOccurrences(of: ":一、", with: ":\n\n一、") + content = content.replacingOccurrences(of: " ", with: "") + content = content.replacingOccurrences(of: "---", with: "---\n") + content = content.replacingOccurrences(of: "日星期", with: "日 星期") + content = "# " + content + content = insertEmptyLineAfterFirstLine(content) + return content + } + + private func formatForNote(_ text: String) -> String { + var content = text + content = content.replacingOccurrences(of: " ", with: "") + content = content.replacingOccurrences(of: "# ", with: "") + content = content.replacingOccurrences(of: "#", with: "") + content = content.replacingOccurrences(of: "!", with: "。") + content = content.replacingOccurrences(of: "!", with: "。") + content = content.replacingOccurrences(of: ";", with: "。") + content = content.replacingOccurrences(of: ";", with: "。") + content = content.replacingOccurrences(of: ".", with: "。") + content = content.replacingOccurrences(of: "(", with: "(") + content = content.replacingOccurrences(of: ")", with: ")") + content = content.replacingOccurrences(of: "?", with: "?") + content = content.replacingOccurrences(of: ",", with: ",") + content = content.replacingOccurrences(of: ":", with: ":") + content = content.replacingOccurrences(of: "。", with: "。\n") + content = content.replacingOccurrences(of: "?", with: "?\n") + content = removeEmptyLines(content) + content = content.replacingOccurrences(of: "---", with: "") + content = content.replacingOccurrences(of: "日星期", with: "日 星期") + content = insertEmptyLineAfterFirstLine(content) + return content + } + + private func removeEmptyLines(_ text: String) -> String { + let lines = text.components(separatedBy: .newlines) + let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty } + return nonEmptyLines.joined(separator: "\n") + } + + private func insertEmptyLineAfterFirstLine(_ text: String) -> String { + var lines = text.components(separatedBy: .newlines) + if !lines.isEmpty { + lines.insert("", at: 1) + } + return lines.joined(separator: "\n") + } } extension FormatNoteViewController: UITextViewDelegate {