482 lines
15 KiB
Ziran
482 lines
15 KiB
Ziran
// A Diary entry is Markdown. This module finds its photo embeds and the
|
|
// CommonMark basics the editor previews, and makes the edits that add a
|
|
// photo. It reads only the entry's text; files and the account's media
|
|
// library belong to the host.
|
|
//
|
|
// An embed is a CommonMark image anywhere in a line whose destination is a
|
|
// photo: `` for a photo of the account's
|
|
// media library, which Photos shows too (the photo's day, its SHA-256 and
|
|
// its type), or `` for a photo an older version attached
|
|
// to the entry as a Diary file. Other images and text stay ordinary text.
|
|
// An inserted photo gets a line of its own.
|
|
#import "std/text"
|
|
#import "vec"
|
|
#import "byte_text_linux"
|
|
Markdown :: #import "kryon/markdown_preview";
|
|
|
|
// Resolve Diary's media syntax for Kryon's shared Markdown editor. Keeping
|
|
// one extra span lets the preview report its own image-capacity truncation.
|
|
diary_images: [Markdown.PREVIEW_IMAGES + 1]Markdown.ImageSpan;
|
|
|
|
DiaryResolvedImages :: (text: string) -> []Markdown.ImageSpan {
|
|
count: s32 = 0
|
|
embed := DiaryEmbedNext(text, 0)
|
|
while embed.valid && count < Markdown.PREVIEW_IMAGES + 1 {
|
|
diary_images[count] = Markdown.ImageSpan.{.valid = true,
|
|
.start = embed.start, .end = embed.end, .line_start = embed.line_start,
|
|
.line_end = embed.line_end, .alt_end = embed.alt_end}
|
|
count += 1
|
|
embed = DiaryEmbedNext(text, embed.end)
|
|
}
|
|
return diary_images[:count]
|
|
}
|
|
|
|
DIARY_EMBED_ALT_MAX :: 200
|
|
DIARY_EMBED_NAME_MAX :: 200
|
|
|
|
DiaryEmbed :: struct {
|
|
valid: bool
|
|
// The image's bytes, from its `!` to its closing parenthesis.
|
|
start: s32
|
|
end: s32
|
|
// Its line, without the line break.
|
|
line_start: s32
|
|
line_end: s32
|
|
alt_start: s32
|
|
alt_end: s32
|
|
// The destination, without angle brackets, ends with its target: the
|
|
// Diary file's name after `diary:`, or the whole library place.
|
|
destination_start: s32
|
|
target_start: s32
|
|
target_end: s32
|
|
// A photo of the account's media library (photos/...) or a Diary file
|
|
// (diary:).
|
|
library: bool
|
|
}
|
|
|
|
// Replaces text[start:end]; the caret goes to cursor afterwards.
|
|
DiaryEmbedEdit :: struct {
|
|
start: s32
|
|
end: s32
|
|
replacement: string
|
|
cursor: s32
|
|
}
|
|
|
|
DiaryLineEnd :: (text: string, start: s32) -> s32 {
|
|
end := start
|
|
while end < cast(s32)text.count && text[end] != cast(u8)10 {
|
|
end += 1
|
|
}
|
|
return end
|
|
}
|
|
|
|
DiaryLineStart :: (text: string, at: s32) -> s32 {
|
|
start := at
|
|
if start > cast(s32)text.count {
|
|
start = cast(s32)text.count
|
|
}
|
|
while start > 0 && text[start - 1] != cast(u8)10 {
|
|
start -= 1
|
|
}
|
|
return start
|
|
}
|
|
|
|
DiaryBlank :: (byte: u8) -> bool {
|
|
return byte == cast(u8)32 || byte == cast(u8)9
|
|
}
|
|
|
|
// ---------------------------------------------------------------- embeds
|
|
|
|
// A Diary file name, as the host stores it.
|
|
DiaryFileNameValid :: (name: string) -> bool {
|
|
if name.count == 0 || name.count > DIARY_EMBED_NAME_MAX || name == "." || name == ".." {
|
|
return false
|
|
}
|
|
for byte: name {
|
|
digit := byte >= cast(u8)48 && byte <= cast(u8)57
|
|
letter := byte >= cast(u8)97 && byte <= cast(u8)122
|
|
if !digit && !letter && byte != cast(u8)45 && byte != cast(u8)46 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// A media library key: the photo's SHA-256 in lowercase hex.
|
|
DiaryMediaKeyValid :: (key: string) -> bool {
|
|
if key.count != 64 {
|
|
return false
|
|
}
|
|
for byte: key {
|
|
digit := byte >= cast(u8)48 && byte <= cast(u8)57
|
|
letter := byte >= cast(u8)97 && byte <= cast(u8)102
|
|
if !digit && !letter {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
DiaryDigits :: (value: string) -> bool {
|
|
for byte: value {
|
|
if byte < cast(u8)48 || byte > cast(u8)57 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// The photo types the library keeps.
|
|
DiaryPhotoTypeValid :: (format: string) -> bool {
|
|
return format == "jpg" || format == "png" || format == "webp" || format == "gif"
|
|
}
|
|
|
|
// A library photo's place: photos/YYYY/MM/DD/<sha256>.<type>, its day as the
|
|
// Photos gallery groups it.
|
|
DiaryLibraryPathValid :: (path: string) -> bool {
|
|
if path.count < 86 || path.count > 87 || path[:7] != "photos/" ||
|
|
path[11] != #char "/" || path[14] != #char "/" || path[17] != #char "/" ||
|
|
path[82] != #char "." {
|
|
return false
|
|
}
|
|
if !DiaryDigits(path[7:11]) || !DiaryDigits(path[12:14]) || !DiaryDigits(path[15:17]) {
|
|
return false
|
|
}
|
|
month := (cast(s32)path[12] - 48) * 10 + cast(s32)path[13] - 48
|
|
day := (cast(s32)path[15] - 48) * 10 + cast(s32)path[16] - 48
|
|
if month < 1 || month > 12 || day < 1 || day > 31 {
|
|
return false
|
|
}
|
|
return DiaryMediaKeyValid(path[18:82]) && DiaryPhotoTypeValid(path[83:])
|
|
}
|
|
|
|
// The place of a library photo taken or added on `day` (YYYY-MM-DD), or ""
|
|
// when one of its parts is not valid.
|
|
DiaryLibraryPath :: (day: string, hash: string, format: string) -> string {
|
|
if day.count != 10 || day[4] != #char "-" || day[7] != #char "-" {
|
|
return ""
|
|
}
|
|
out: Vec(u8)
|
|
BuilderPrint(*out, "photos/%/%/%/%.%", day[:4], day[5:7], day[8:10], hash, format)
|
|
path := BuilderFinish(out)
|
|
if !DiaryLibraryPathValid(path) {
|
|
return ""
|
|
}
|
|
return path
|
|
}
|
|
|
|
// The photo at `at`, where an image's `!` stands, or an invalid embed. An
|
|
// image follows CommonMark within one line: ``, with
|
|
// spaces or tabs around the destination, an optional "title", 'title' or
|
|
// (title) after it, and the destination in <angle brackets> or plain.
|
|
// Escaped brackets may appear in ALT. Only a destination naming a photo, a
|
|
// library place or a Diary file, makes it an embed.
|
|
DiaryEmbedAt :: (text: string, at: s32) -> DiaryEmbed {
|
|
embed: DiaryEmbed
|
|
if at < 0 || at + 1 >= cast(s32)text.count || text[at] != #char "!" ||
|
|
text[at + 1] != #char "[" {
|
|
return embed
|
|
}
|
|
line_start := DiaryLineStart(text, at)
|
|
line_end := DiaryLineEnd(text, at)
|
|
if DiaryEscaped(text, at, line_start) {
|
|
return embed
|
|
}
|
|
close := at + 2
|
|
while close < line_end && text[close] != #char "]" {
|
|
if text[close] == #char "[" {
|
|
return embed
|
|
}
|
|
if text[close] == cast(u8)92 && close + 1 < line_end {
|
|
close += 1
|
|
}
|
|
close += 1
|
|
}
|
|
if close + 1 >= line_end || text[close + 1] != #char "(" ||
|
|
close - (at + 2) > DIARY_EMBED_ALT_MAX {
|
|
return embed
|
|
}
|
|
cursor := DiarySkipBlank(text, close + 2, line_end)
|
|
destination_start := cursor
|
|
destination_end := cursor
|
|
if cursor < line_end && text[cursor] == #char "<" {
|
|
destination_start = cursor + 1
|
|
destination_end = destination_start
|
|
while destination_end < line_end && text[destination_end] != #char ">" &&
|
|
text[destination_end] != #char "<" {
|
|
destination_end += 1
|
|
}
|
|
if destination_end >= line_end || text[destination_end] != #char ">" {
|
|
return embed
|
|
}
|
|
cursor = destination_end + 1
|
|
} else {
|
|
while cursor < line_end && text[cursor] > cast(u8)32 && text[cursor] != #char "(" &&
|
|
text[cursor] != #char ")" {
|
|
cursor += 1
|
|
}
|
|
destination_end = cursor
|
|
}
|
|
after := DiarySkipBlank(text, cursor, line_end)
|
|
if after > cursor && after < line_end && (text[after] == cast(u8)34 ||
|
|
text[after] == cast(u8)39 || text[after] == #char "(") {
|
|
after = DiaryTitleEnd(text, after, line_end)
|
|
if after < 0 {
|
|
return embed
|
|
}
|
|
after = DiarySkipBlank(text, after, line_end)
|
|
}
|
|
if after >= line_end || text[after] != #char ")" {
|
|
return embed
|
|
}
|
|
destination := text[destination_start:destination_end]
|
|
target_start := destination_start
|
|
library := false
|
|
if destination.count > 6 && destination[:6] == "diary:" {
|
|
target_start += 6
|
|
if !DiaryFileNameValid(text[target_start:destination_end]) {
|
|
return embed
|
|
}
|
|
} else if DiaryLibraryPathValid(destination) {
|
|
library = true
|
|
} else {
|
|
return embed
|
|
}
|
|
embed.valid = true
|
|
embed.start = at
|
|
embed.end = after + 1
|
|
embed.line_start = line_start
|
|
embed.line_end = line_end
|
|
embed.alt_start = at + 2
|
|
embed.alt_end = close
|
|
embed.destination_start = destination_start
|
|
embed.target_start = target_start
|
|
embed.target_end = destination_end
|
|
embed.library = library
|
|
return embed
|
|
}
|
|
|
|
DiarySkipBlank :: (text: string, at: s32, end: s32) -> s32 {
|
|
cursor := at
|
|
while cursor < end && DiaryBlank(text[cursor]) {
|
|
cursor += 1
|
|
}
|
|
return cursor
|
|
}
|
|
|
|
// Where an image's title that opens at `at` ends, after its closing quote
|
|
// or parenthesis, or -1 when it does not close on the line.
|
|
DiaryTitleEnd :: (text: string, at: s32, end: s32) -> s32 {
|
|
closing := text[at]
|
|
if closing == #char "(" {
|
|
closing = #char ")"
|
|
}
|
|
cursor := at + 1
|
|
while cursor < end {
|
|
if text[cursor] == cast(u8)92 && cursor + 1 < end {
|
|
cursor += 2
|
|
continue
|
|
}
|
|
if text[cursor] == closing {
|
|
return cursor + 1
|
|
}
|
|
if text[at] == #char "(" && text[cursor] == #char "(" {
|
|
return -1
|
|
}
|
|
cursor += 1
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// The first embed starting in text[from:to].
|
|
DiaryEmbedIn :: (text: string, from: s32, to: s32) -> DiaryEmbed {
|
|
at := from
|
|
if at < 0 {
|
|
at = 0
|
|
}
|
|
while at + 1 < to {
|
|
if text[at] == #char "!" && text[at + 1] == #char "[" {
|
|
embed := DiaryEmbedAt(text, at)
|
|
if embed.valid {
|
|
return embed
|
|
}
|
|
}
|
|
at += 1
|
|
}
|
|
return DiaryEmbed.{}
|
|
}
|
|
|
|
// The first embed starting at or after `from`.
|
|
DiaryEmbedNext :: (text: string, from: s32) -> DiaryEmbed {
|
|
return DiaryEmbedIn(text, from, cast(s32)text.count)
|
|
}
|
|
|
|
// Whether the line starting at line_start holds photos and nothing else but
|
|
// spaces, as an inserted photo's line does.
|
|
DiaryImageLine :: (text: string, line_start: s32) -> bool {
|
|
line_end := DiaryLineEnd(text, line_start)
|
|
found := false
|
|
at := line_start
|
|
while at < line_end {
|
|
if DiaryBlank(text[at]) || text[at] == cast(u8)13 {
|
|
at += 1
|
|
continue
|
|
}
|
|
embed := DiaryEmbedAt(text, at)
|
|
if !embed.valid {
|
|
return false
|
|
}
|
|
found = true
|
|
at = embed.end
|
|
}
|
|
return found
|
|
}
|
|
|
|
// A Diary file's name, or a library photo's whole path.
|
|
DiaryEmbedTarget :: (text: string, embed: DiaryEmbed) -> string {
|
|
return text[embed.target_start:embed.target_end]
|
|
}
|
|
|
|
// What the embed links to: diary:FILE or the library place.
|
|
DiaryEmbedDestination :: (text: string, embed: DiaryEmbed) -> string {
|
|
return text[embed.destination_start:embed.target_end]
|
|
}
|
|
|
|
DiaryEmbedAlt :: (text: string, embed: DiaryEmbed) -> string {
|
|
return text[embed.alt_start:embed.alt_end]
|
|
}
|
|
|
|
// Whether a diary: embed names this file.
|
|
DiaryEmbedReferences :: (text: string, file: string) -> bool {
|
|
embed := DiaryEmbedNext(text, 0)
|
|
while embed.valid {
|
|
if !embed.library && DiaryEmbedTarget(text, embed) == file {
|
|
return true
|
|
}
|
|
embed = DiaryEmbedNext(text, embed.end)
|
|
}
|
|
return false
|
|
}
|
|
|
|
// The bytes of the UTF-8 character at index, or 0 when it is not valid.
|
|
DiaryCharacterSize :: (value: string, index: s32) -> s32 {
|
|
lead := value[index]
|
|
size: s32 = 0
|
|
if lead < cast(u8)128 {
|
|
return 1
|
|
}
|
|
if lead >= cast(u8)194 && lead <= cast(u8)223 {
|
|
size = 2
|
|
} else if lead >= cast(u8)224 && lead <= cast(u8)239 {
|
|
size = 3
|
|
} else if lead >= cast(u8)240 && lead <= cast(u8)244 {
|
|
size = 4
|
|
} else {
|
|
return 0
|
|
}
|
|
if index + size > cast(s32)value.count {
|
|
return 0
|
|
}
|
|
for offset: cast(s32)1..size - 1 {
|
|
if (value[index + offset] & cast(u8)192) != cast(u8)128 {
|
|
return 0
|
|
}
|
|
}
|
|
return size
|
|
}
|
|
|
|
// An embed line linking to `destination`, diary:FILE or a library path. Its
|
|
// name keeps whole characters up to the length limit; brackets, line breaks
|
|
// and invalid bytes become spaces.
|
|
DiaryEmbedLine :: (destination: string, name: string) -> string {
|
|
out: Vec(u8)
|
|
BuilderPrint(*out, "![")
|
|
written: s32 = 0
|
|
index: s32 = 0
|
|
while index < cast(s32)name.count {
|
|
size := DiaryCharacterSize(name, index)
|
|
step := ifx size == 0 then 1 else size
|
|
if written + step > DIARY_EMBED_ALT_MAX {
|
|
break
|
|
}
|
|
byte := name[index]
|
|
if size == 0 || (size == 1 && (byte == #char "[" || byte == #char "]" || byte < cast(u8)32)) {
|
|
BuilderPrint(*out, " ")
|
|
} else {
|
|
BuilderPrint(*out, "%", name[index:index + size])
|
|
}
|
|
written += step
|
|
index += step
|
|
}
|
|
BuilderPrint(*out, "](%)", destination)
|
|
return BuilderFinish(out)
|
|
}
|
|
|
|
// The text with an embed line added at its end. A blank line separates it
|
|
// from text before it; embeds that follow each other share no blank line.
|
|
DiaryEmbedAppend :: (text: string, line: string) -> string {
|
|
out: Vec(u8)
|
|
count := cast(s32)text.count
|
|
BuilderPrint(*out, "%", text)
|
|
if count == 0 {
|
|
BuilderPrint(*out, "%", line)
|
|
return BuilderFinish(out)
|
|
}
|
|
if text[count - 1] == cast(u8)10 {
|
|
previous := DiaryLineStart(text, count - 1)
|
|
after_embed := DiaryImageLine(text, previous)
|
|
blank := count >= 2 && text[count - 2] == cast(u8)10
|
|
if !after_embed && !blank {
|
|
BuilderPrint(*out, "\n")
|
|
}
|
|
} else if DiaryImageLine(text, DiaryLineStart(text, count)) {
|
|
BuilderPrint(*out, "\n")
|
|
} else {
|
|
BuilderPrint(*out, "\n\n")
|
|
}
|
|
BuilderPrint(*out, "%", line)
|
|
return BuilderFinish(out)
|
|
}
|
|
|
|
// The edit that puts an embed line at `at`, on a line of its own, with the
|
|
// caret on the line after it.
|
|
DiaryEmbedInsertion :: (text: string, at: s32, line: string) -> DiaryEmbedEdit {
|
|
edit: DiaryEmbedEdit
|
|
count := cast(s32)text.count
|
|
start := at
|
|
if start < 0 {
|
|
start = 0
|
|
}
|
|
if start > count {
|
|
start = count
|
|
}
|
|
while start > 0 && start < count && (text[start] & cast(u8)192) == cast(u8)128 {
|
|
start -= 1
|
|
}
|
|
out: Vec(u8)
|
|
if start > 0 && text[start - 1] != cast(u8)10 {
|
|
BuilderPrint(*out, "\n")
|
|
}
|
|
BuilderPrint(*out, "%\n", line)
|
|
edit.start = start
|
|
edit.end = start
|
|
// At a line break the edit takes it over, so the caret, which an edit
|
|
// leaves after its replacement, starts the next line.
|
|
if start < count && text[start] == cast(u8)10 {
|
|
edit.end = start + 1
|
|
}
|
|
edit.replacement = BuilderFinish(out)
|
|
edit.cursor = start + cast(s32)edit.replacement.count
|
|
return edit
|
|
}
|
|
|
|
DiaryEscaped :: (text: string, at: s32, from: s32) -> bool {
|
|
slashes: s32 = 0
|
|
cursor := at - 1
|
|
while cursor >= from && text[cursor] == cast(u8)92 {
|
|
slashes += 1
|
|
cursor -= 1
|
|
}
|
|
return slashes % 2 == 1
|
|
}
|