133 lines
5.9 KiB
Swift
133 lines
5.9 KiB
Swift
//
|
||
// TextFormatter.swift
|
||
// Note
|
||
//
|
||
// Created by fish on 2026/7/13.
|
||
//
|
||
|
||
import Foundation
|
||
|
||
enum TextFormatter {
|
||
|
||
static func formatLog(_ 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
|
||
content = content.replacingOccurrences(of: "一、生活方面", with: "## 一、生活方面")
|
||
content = content.replacingOccurrences(of: "二、交易方面", with: "## 二、交易方面")
|
||
content = content.replacingOccurrences(of: "三、工作方面", with: "## 三、工作方面")
|
||
|
||
return content
|
||
}
|
||
|
||
static func formatForCloud(_ text: String) -> String {
|
||
var content = formatForShortcut(text)
|
||
content = "# " + content
|
||
content = insertEmptyLineAfterFirstLine(content)
|
||
return content
|
||
}
|
||
|
||
static nonisolated func formatForShortcut(_ 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: "日 星期")
|
||
return content
|
||
}
|
||
|
||
static func formatForNote(_ text: String) -> String {
|
||
var content = text
|
||
content = content.replacingOccurrences(of: "# ", with: "")
|
||
content = content.replacingOccurrences(of: "#", with: "")
|
||
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
|
||
|
||
// 第一行固定为标题
|
||
let allLines = removeEmptyLines(content).components(separatedBy: .newlines)
|
||
guard let title = allLines.first else { return content }
|
||
|
||
// 剩余内容按 --- 分段
|
||
let bodyLines = Array(allLines.dropFirst())
|
||
var groups: [[String]] = []
|
||
var currentGroup: [String] = []
|
||
for line in bodyLines {
|
||
if line == "---" {
|
||
if !currentGroup.isEmpty {
|
||
groups.append(currentGroup)
|
||
currentGroup = []
|
||
}
|
||
} else {
|
||
currentGroup.append(line)
|
||
}
|
||
}
|
||
if !currentGroup.isEmpty {
|
||
groups.append(currentGroup)
|
||
}
|
||
|
||
var result = title
|
||
for group in groups {
|
||
result += "\n\n\n" + group.joined(separator: "\n")
|
||
}
|
||
return result.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
}
|
||
|
||
static 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")
|
||
}
|
||
|
||
static func insertEmptyLineAfterFirstLine(_ text: String) -> String {
|
||
var lines = text.components(separatedBy: .newlines)
|
||
if !lines.isEmpty {
|
||
lines.insert("", at: 1)
|
||
}
|
||
return lines.joined(separator: "\n")
|
||
}
|
||
}
|