improvements on the ui
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import Foundation
|
||||
|
||||
/// The lorem text that `#lorem` puts in the page: an opening sentence of 16 words, two paragraphs and a contact sample.
|
||||
/// The lorem text that `#lorem` puts in the page: a bookmark, an opening sentence of 16 words, two paragraphs and a contact sample.
|
||||
public struct LoremText: Equatable, Sendable {
|
||||
public static let trigger = "#lorem"
|
||||
/// The first line of the lorem text, so it shows in the bookmark index.
|
||||
public static let bookmark = "=lorem"
|
||||
public static let openingWords = 16
|
||||
public static let paragraphSentences = 3...5
|
||||
public static let resourceBundleName = "Plak_PlakCore.bundle"
|
||||
@@ -17,9 +19,9 @@ public struct LoremText: Equatable, Sendable {
|
||||
self.contacts = contacts
|
||||
}
|
||||
|
||||
/// The sentence, the two paragraphs, the phones, the addresses and the e-mails, with a blank line between each.
|
||||
/// The bookmark, the sentence, the two paragraphs, the phones, the addresses and the e-mails, with a blank line between each.
|
||||
public var text: String {
|
||||
([opening] + paragraphs.map { $0.joined(separator: " ") } + contacts.blocks).joined(separator: "\n\n")
|
||||
([Self.bookmark, opening] + paragraphs.map { $0.joined(separator: " ") } + contacts.blocks).joined(separator: "\n\n")
|
||||
}
|
||||
|
||||
public static func wordCount(_ sentence: String) -> Int {
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import Foundation
|
||||
|
||||
extension PageFile {
|
||||
/// The number of days a block stays in the page after its created date.
|
||||
public static let keptDays = 14
|
||||
|
||||
/// The page without the blocks created more than `keptDays` calendar days before `now`, and without undated blocks.
|
||||
///
|
||||
/// A fenced block is one line, so it goes or stays whole. The blank lines above a removed block go with it.
|
||||
public func pruned(now: Date, timeZone: TimeZone = .current) -> PageFile {
|
||||
var calendar = Calendar(identifier: .gregorian)
|
||||
calendar.timeZone = timeZone
|
||||
guard let cutoff = calendar.date(byAdding: .day, value: -Self.keptDays, to: now) else { return self }
|
||||
let blocks = self.blocks
|
||||
let keeps = blocks.map { block in dates[block.lowerBound].map { $0 >= cutoff } ?? false }
|
||||
guard keeps.contains(false) else { return self }
|
||||
guard let first = blocks.first, let last = blocks.last, keeps.contains(true) else { return PageFile() }
|
||||
|
||||
var keptLines = Array(lines[..<first.lowerBound])
|
||||
var keptDates: [Int: Date] = [:]
|
||||
var hasBlock = false
|
||||
for (index, block) in blocks.enumerated() where keeps[index] {
|
||||
if hasBlock { keptLines += lines[blocks[index - 1].upperBound..<block.lowerBound] }
|
||||
keptDates[keptLines.count] = dates[block.lowerBound]
|
||||
keptLines += lines[block]
|
||||
hasBlock = true
|
||||
}
|
||||
keptLines += lines[last.upperBound...]
|
||||
return PageFile(lines: keptLines, dates: keptDates)
|
||||
}
|
||||
|
||||
/// The paths of every image line, as `images/<name>`.
|
||||
public var imagePaths: Set<String> {
|
||||
Set(lines.compactMap { line in
|
||||
if case let .image(path) = line { return path }
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user