365 lines
21 KiB
Ziran
365 lines
21 KiB
Ziran
// Diary's Markdown: photo embeds and their places in the media library,
|
|
// inserted photos, the editor's preview, and the spelling check's masking.
|
|
#import, file "../apps/diary/diary_markdown.zi";
|
|
#import, file "../apps/diary/diary_spelling_mask.zi";
|
|
Text :: #import "kryon/text_props";
|
|
Markdown :: #import "kryon/markdown_preview";
|
|
#import "vec"
|
|
#import "std/text"
|
|
#import "byte_text_linux"
|
|
|
|
HASH :: "9f2c4e1a7b3d5f60718293a4b5c6d7e8f90123456789abcdef0123456789abcd"
|
|
PLACE :: "photos/2026/10/17/9f2c4e1a7b3d5f60718293a4b5c6d7e8f90123456789abcdef0123456789abcd.jpg"
|
|
|
|
preview: Markdown.Preview;
|
|
|
|
Applied :: (text: string, edit: DiaryEmbedEdit) -> string {
|
|
out: Vec(u8)
|
|
BuilderPrint(*out, "%", text[:edit.start])
|
|
BuilderPrint(*out, "%", edit.replacement)
|
|
BuilderPrint(*out, "%", text[edit.end:])
|
|
return BuilderFinish(out)
|
|
}
|
|
|
|
RangeIs :: (range: Text.Range, start: s32, end: s32) -> bool {
|
|
return range.start == start && range.end == end
|
|
}
|
|
|
|
Hidden :: (index: s32, start: s32, end: s32) -> bool {
|
|
return index < preview.hidden_count && RangeIs(preview.hidden[index], start, end)
|
|
}
|
|
|
|
Strong :: (index: s32, start: s32, end: s32) -> bool {
|
|
return index < preview.strong_count && RangeIs(preview.strong[index], start, end)
|
|
}
|
|
|
|
Italic :: (index: s32, start: s32, end: s32) -> bool {
|
|
return index < preview.italic_count && RangeIs(preview.italic[index], start, end)
|
|
}
|
|
|
|
Tone :: (index: s32, start: s32, end: s32, tone: s32) -> bool {
|
|
return index < preview.tone_count && preview.tones[index].start == start &&
|
|
preview.tones[index].end == end && preview.tones[index].tone == tone
|
|
}
|
|
|
|
Scan :: (result: *Markdown.Preview, text: string, cursor: s32, anchor: s32, editing: bool) {
|
|
Markdown.Scan(result, text, cursor, anchor, editing, DiaryResolvedImages(text))
|
|
}
|
|
|
|
Preview :: (text: string) {
|
|
Scan(*preview, text, 0, 0, false)
|
|
}
|
|
|
|
// Every list must satisfy Kryon's sorted, disjoint, non-empty rule.
|
|
Ordered :: (ranges: []Text.Range, count: s32) -> bool {
|
|
previous: s32 = 0
|
|
for index: cast(s32)0..count - 1 {
|
|
if ranges[index].start < previous || ranges[index].end <= ranges[index].start {
|
|
return false
|
|
}
|
|
previous = ranges[index].end
|
|
}
|
|
return true
|
|
}
|
|
|
|
PreviewOrdered :: () -> bool {
|
|
return Ordered(preview.hidden[:], preview.hidden_count) &&
|
|
Ordered(preview.strong[:], preview.strong_count) &&
|
|
Ordered(preview.italic[:], preview.italic_count)
|
|
}
|
|
|
|
CheckEmbeds :: () -> s32 {
|
|
embed := DiaryEmbedAt("", 0)
|
|
if !embed.valid || embed.library { return 1 }
|
|
if DiaryEmbedTarget("", embed) !=
|
|
"photo-20261017081500-1.bin" { return 2 }
|
|
if DiaryEmbedAlt("", embed) != "IMG_2041.jpg" { return 3 }
|
|
spaced := "  \r"
|
|
embed = DiaryEmbedNext(spaced, 0)
|
|
if !embed.valid || !embed.library || DiaryEmbedTarget(spaced, embed) != PLACE ||
|
|
DiaryEmbedDestination(spaced, embed) != PLACE || embed.start != 2 || embed.end != 93 ||
|
|
embed.alt_start != embed.alt_end || !DiaryImageLine(spaced, 0) { return 4 }
|
|
if DiaryEmbedAt("", 0).valid { return 5 }
|
|
if DiaryEmbedAt("", 0).valid { return 6 }
|
|
// An image is a photo anywhere in a line, with text around it.
|
|
if DiaryEmbedAt("look ", 0).valid || DiaryEmbedNext("look ", 0).start != 5 ||
|
|
DiaryImageLine("look ", 0) { return 7 }
|
|
if DiaryEmbedAt("![a[b]](diary:x.bin)", 0).valid { return 8 }
|
|
if DiaryEmbedAt("", 0).valid { return 9 }
|
|
if DiaryEmbedAt(" more", 0).end != 17 { return 10 }
|
|
if DiaryEmbedAt("", 0).valid { return 11 }
|
|
if DiaryEmbedAt("", 0).valid { return 12 }
|
|
// A place needs a real month and day, a known type and nothing more.
|
|
if DiaryEmbedAt("", 0).valid { return 120 }
|
|
if DiaryEmbedAt("", 0).valid { return 121 }
|
|
if DiaryEmbedAt("", 0).valid { return 122 }
|
|
if DiaryEmbedAt("", 0).valid { return 123 }
|
|
if DiaryEmbedAt("", 0).valid { return 124 }
|
|
if DiaryEmbedAt("", 0).valid { return 125 }
|
|
if !DiaryEmbedAt("", 0).valid { return 126 }
|
|
if DiaryEmbedDestination("", DiaryEmbedAt("", 0)) != "diary:x.bin" { return 127 }
|
|
if DiaryLibraryPath("2026-10-17", HASH, "jpg") != PLACE { return 128 }
|
|
if DiaryLibraryPath("2026-10-17", HASH, "bmp") != "" || DiaryLibraryPath("17.10.2026", HASH, "jpg") != "" { return 129 }
|
|
// Brackets that are not the alt's own still make it plain text.
|
|
if !DiaryEmbedAt("", 0).valid { return 13 }
|
|
// CommonMark's titles, angle brackets, spaces and escapes.
|
|
titled := ""
|
|
if DiaryEmbedAt(titled, 0).end != cast(s32)titled.count || DiaryEmbedTarget(titled, DiaryEmbedAt(titled, 0)) != "x.bin" { return 150 }
|
|
if DiaryEmbedAt("", 0).end != 27 { return 151 }
|
|
if DiaryEmbedAt(")", 0).end != 27 { return 152 }
|
|
angled := ""
|
|
if !DiaryEmbedAt(angled, 0).valid || DiaryEmbedDestination(angled, DiaryEmbedAt(angled, 0)) != PLACE { return 153 }
|
|
if DiaryEmbedAt("", 0).end != 21 { return 154 }
|
|
if DiaryEmbedAt("", 0).valid || DiaryEmbedAt("", 0).valid { return 155 }
|
|
if DiaryEmbedAt("", 0).valid || DiaryEmbedAt("))", 0).valid { return 156 }
|
|
if DiaryEmbedNext("\\", 0).valid || !DiaryEmbedAt("\\\\", 2).valid { return 157 }
|
|
if DiaryEmbedAlt("![a\\]b](diary:x.bin)", DiaryEmbedAt("![a\\]b](diary:x.bin)", 0)) != "a\\]b" { return 158 }
|
|
// Markup cut short is text, never a photo.
|
|
cut: [7]string = .[".valid || DiaryImageLine(value, 0) { return 159 }
|
|
}
|
|
// Several photos on one line, back to back or apart.
|
|
pair := ""
|
|
if DiaryEmbedNext(pair, 0).end != 17 || DiaryEmbedNext(pair, 17).start != 17 ||
|
|
DiaryEmbedNext(pair, 18).valid || !DiaryImageLine(pair, 0) { return 160 }
|
|
if !DiaryImageLine(" \t ", 0) ||
|
|
DiaryImageLine(" x", 0) || DiaryImageLine("", 0) { return 161 }
|
|
|
|
text := "Morning.\n\nmiddle\n\n"
|
|
first := DiaryEmbedNext(text, 0)
|
|
if !first.valid || first.start != 9 || first.line_start != 9 || DiaryEmbedTarget(text, first) != "a.bin" { return 14 }
|
|
second := DiaryEmbedNext(text, first.end)
|
|
if !second.valid || !second.library { return 15 }
|
|
if DiaryEmbedNext(text, second.end).valid { return 16 }
|
|
if !DiaryEmbedReferences(text, "a.bin") || DiaryEmbedReferences(text, "b.bin") { return 17 }
|
|
// A library photo never counts as a Diary file of the same name.
|
|
if DiaryEmbedReferences(text, HASH) || DiaryEmbedReferences(text, PLACE) { return 18 }
|
|
if DiaryEmbedNext("", 0).valid { return 19 }
|
|
return 0
|
|
}
|
|
|
|
CheckLines :: () -> s32 {
|
|
if DiaryEmbedLine("diary:x.bin", "Fam[ily]\n🌿") != "" { return 30 }
|
|
if DiaryEmbedLine(PLACE, "") != "" { return 31 }
|
|
long: Vec(u8)
|
|
for index: cast(s32)1..199 {
|
|
BuilderPrint(*long, "a")
|
|
}
|
|
BuilderPrint(*long, "🌿tail")
|
|
line := DiaryEmbedLine("diary:x.bin", BuilderFinish(long))
|
|
embed := DiaryEmbedAt(line, 0)
|
|
// 199 letters fit; the four-byte leaf would pass 200 bytes.
|
|
if !embed.valid || embed.alt_end - embed.alt_start != 199 { return 32 }
|
|
// An invalid byte becomes a space and the line still parses.
|
|
broken: [3]u8 = .[cast(u8)97, cast(u8)200, cast(u8)98]
|
|
if DiaryEmbedLine("diary:x.bin", TextFromBytes(broken[:])) != "" { return 33 }
|
|
|
|
embed_line := ""
|
|
if DiaryEmbedAppend("", embed_line) != embed_line { return 34 }
|
|
if DiaryEmbedAppend("Hello", embed_line) != "Hello\n\n" { return 35 }
|
|
if DiaryEmbedAppend("Hello\n", embed_line) != "Hello\n\n" { return 36 }
|
|
if DiaryEmbedAppend("Hello\n\n", embed_line) != "Hello\n\n" { return 37 }
|
|
if DiaryEmbedAppend("Hello\n\n", embed_line) !=
|
|
"Hello\n\n\n" { return 38 }
|
|
if DiaryEmbedAppend("\n", embed_line) != "\n" { return 39 }
|
|
return 0
|
|
}
|
|
|
|
CheckInsertion :: () -> s32 {
|
|
line := ""
|
|
edit := DiaryEmbedInsertion("", 0, line)
|
|
applied := Applied("", edit)
|
|
if applied.count != line.count + 1 || edit.cursor != cast(s32)applied.count { return 50 }
|
|
// At a caret inside a line the line splits around the photo.
|
|
edit = DiaryEmbedInsertion("Hello world", 5, "")
|
|
if Applied("Hello world", edit) != "Hello\n\n world" || edit.cursor != 24 { return 51 }
|
|
// Before an existing line break no second break is added; the caret
|
|
// lands at the start of the following line.
|
|
edit = DiaryEmbedInsertion("a\nb", 1, "")
|
|
if Applied("a\nb", edit) != "a\n\nb" || edit.cursor != 20 { return 54 }
|
|
// A caret inside a character moves to its start.
|
|
edit = DiaryEmbedInsertion("é", 1, "")
|
|
if edit.start != 0 { return 55 }
|
|
// At the end of a photo's line, as after a click on the photo, the next
|
|
// photo gets its own line.
|
|
edit = DiaryEmbedInsertion("", 17, "")
|
|
if Applied("", edit) != "\n\n" { return 56 }
|
|
return 0
|
|
}
|
|
|
|
CheckPreview :: () -> s32 {
|
|
Preview("**bold** end")
|
|
if preview.hidden_count != 2 || !Hidden(0, 0, 2) || !Hidden(1, 6, 8) || !Strong(0, 2, 6) { return 80 }
|
|
Scan(*preview, "**bold** end", 3, 3, true)
|
|
if preview.hidden_count != 0 || !Strong(0, 2, 6) { return 81 }
|
|
// Only the caret's line shows its markers.
|
|
Scan(*preview, "**a**\n**b**", 1, 1, true)
|
|
if preview.hidden_count != 2 || !Hidden(0, 6, 8) || !Hidden(1, 9, 11) || preview.strong_count != 2 { return 82 }
|
|
Preview("*it* and _it_")
|
|
if preview.italic_count != 2 || !Italic(0, 1, 3) || !Italic(1, 10, 12) || preview.hidden_count != 4 { return 83 }
|
|
Preview("snake_case_word and 2 * 3 * 4")
|
|
if preview.italic_count != 0 || preview.hidden_count != 0 { return 84 }
|
|
Preview("\\*not\\* emphasis")
|
|
if preview.italic_count != 0 { return 85 }
|
|
Preview("***x***")
|
|
if !Strong(0, 3, 4) || !Italic(0, 1, 6) || preview.hidden_count != 4 ||
|
|
!Hidden(0, 0, 1) || !Hidden(1, 1, 3) || !Hidden(2, 4, 6) || !Hidden(3, 6, 7) { return 86 }
|
|
Preview("**a **b** c**")
|
|
if preview.strong_count != 1 || !Strong(0, 2, 11) || preview.hidden_count != 4 { return 87 }
|
|
Preview("# Title")
|
|
if preview.heading_count != 1 || preview.headings[0].level != 1 || preview.headings[0].start != 0 ||
|
|
!Hidden(0, 0, 2) || !Strong(0, 2, 7) { return 88 }
|
|
Preview("## Title ##")
|
|
if preview.headings[0].level != 2 || !Hidden(0, 0, 3) || !Hidden(1, 8, 11) || !Strong(0, 3, 8) { return 89 }
|
|
Preview("#hashtag")
|
|
if preview.heading_count != 0 || preview.hidden_count != 0 { return 90 }
|
|
Scan(*preview, "### Kept", 4, 4, true)
|
|
if preview.heading_count != 1 || preview.headings[0].level != 3 || preview.hidden_count != 0 { return 91 }
|
|
Preview("- item\n12. next\n* star\n-5 degrees")
|
|
if preview.tone_count != 3 || !Tone(0, 0, 1, Markdown.TONE_MUTED) || !Tone(1, 7, 10, Markdown.TONE_MUTED) ||
|
|
!Tone(2, 16, 17, Markdown.TONE_MUTED) || preview.italic_count != 0 { return 92 }
|
|
Preview("> quoted *words*")
|
|
if preview.tone_count != 1 || !Tone(0, 0, 16, Markdown.TONE_MUTED) || !Italic(0, 10, 15) { return 93 }
|
|
link := "see [site](https://x.y/a_b_c) now"
|
|
Preview(link)
|
|
if !Hidden(0, 4, 5) || !Hidden(1, 9, 29) || !Tone(0, 5, 9, Markdown.TONE_LINK) || preview.italic_count != 0 { return 94 }
|
|
Preview("*see [x](y)*")
|
|
if !Italic(0, 1, 11) || !Tone(0, 6, 7, Markdown.TONE_LINK) || preview.hidden_count != 4 { return 95 }
|
|
// Emphasis never pairs across a link's edge.
|
|
Preview("*a [b*](c)")
|
|
if preview.italic_count != 0 { return 96 }
|
|
Preview("[**bold link**](u)")
|
|
if !Strong(0, 3, 12) || !Tone(0, 1, 14, Markdown.TONE_LINK) { return 97 }
|
|
// A photo inside a line hides its Markdown and keeps the line's text.
|
|
Preview("an  image *stays*")
|
|
if preview.image_count != 1 || preview.images[0].cover || !Hidden(0, 3, 25) ||
|
|
!Hidden(1, 32, 33) || !Hidden(2, 38, 39) || !Italic(0, 33, 38) { return 98 }
|
|
if !PreviewOrdered() { return 99 }
|
|
return 0
|
|
}
|
|
|
|
CheckPreviewPhotos :: () -> s32 {
|
|
text := "# Day\n\n**bold**\n"
|
|
Preview(text)
|
|
if preview.image_count != 2 || !preview.images[0].cover || !preview.images[1].cover { return 110 }
|
|
if !Hidden(0, 0, 2) || !Hidden(1, 6, 25) || !Hidden(2, 26, 28) || !Hidden(3, 32, 34) || !Hidden(4, 35, 129) { return 111 }
|
|
if !PreviewOrdered() { return 112 }
|
|
// On the caret's line a photo keeps its source above it.
|
|
Scan(*preview, text, 10, 10, true)
|
|
if preview.image_count != 2 || preview.images[0].cover || !preview.images[1].cover { return 113 }
|
|
if preview.images[0].embed.line_start != 6 || preview.images[0].embed.line_end != 25 { return 114 }
|
|
for index: cast(s32)0..preview.hidden_count - 1 {
|
|
if preview.hidden[index].start == 6 { return 115 }
|
|
}
|
|
// A selection over several lines shows all of them as typed.
|
|
Scan(*preview, text, 0, 30, true)
|
|
if preview.images[0].cover || preview.hidden_count != 1 || !Hidden(0, 35, 129) { return 116 }
|
|
// Not editing, every line is shown styled.
|
|
Scan(*preview, text, 10, 10, false)
|
|
if !preview.images[0].cover { return 117 }
|
|
return 0
|
|
}
|
|
|
|
CheckLimits :: () -> s32 {
|
|
many: Vec(u8)
|
|
for index: cast(s32)1..80 {
|
|
BuilderPrint(*many, "\n", index)
|
|
}
|
|
text := BuilderFinish(many)
|
|
Preview(text)
|
|
if preview.image_count != Markdown.PREVIEW_IMAGES || !preview.truncated || !PreviewOrdered() { return 130 }
|
|
// A line with more emphasis than one line holds is shown as typed.
|
|
busy: Vec(u8)
|
|
for index: cast(s32)1..40 {
|
|
BuilderPrint(*busy, "*a* ")
|
|
}
|
|
BuilderPrint(*busy, "\n**after**")
|
|
Preview(BuilderFinish(busy))
|
|
if preview.italic_count != 0 || preview.strong_count != 1 || !PreviewOrdered() { return 131 }
|
|
return 0
|
|
}
|
|
|
|
// Lines with photos inside them: the text stays, each photo's Markdown
|
|
// hides away from the caret, and the photos follow the line in order.
|
|
CheckInlinePhotos :: () -> s32 {
|
|
Preview("Walk  home")
|
|
if preview.image_count != 1 || preview.images[0].cover || preview.hidden_count != 1 ||
|
|
!Hidden(0, 5, 22) { return 170 }
|
|
// Typed right after a photo, as after a click on it: the photo stays,
|
|
// and on the caret's line its source shows above it.
|
|
after := " and more"
|
|
Scan(*preview, after, 26, 26, true)
|
|
if preview.image_count != 1 || preview.images[0].cover || preview.hidden_count != 0 { return 171 }
|
|
Preview(after)
|
|
if preview.image_count != 1 || preview.images[0].cover || !Hidden(0, 0, 17) { return 172 }
|
|
// Before it too.
|
|
Preview("Look: ")
|
|
if preview.image_count != 1 || !Hidden(0, 6, 23) { return 173 }
|
|
// Back to back on a line of their own: the first in the line's place.
|
|
pair := ""
|
|
Preview(pair)
|
|
if preview.image_count != 2 || !preview.images[0].cover || preview.images[1].cover ||
|
|
preview.images[1].embed.line_start != 0 || preview.hidden_count != 1 || !Hidden(0, 0, 34) { return 174 }
|
|
Scan(*preview, pair, 34, 34, true)
|
|
if preview.image_count != 2 || preview.images[0].cover || preview.hidden_count != 0 { return 175 }
|
|
// In a list item and a quote, with their marks.
|
|
Preview("- ")
|
|
if preview.image_count != 1 || preview.images[0].cover || !Hidden(0, 2, 19) ||
|
|
!Tone(0, 0, 1, Markdown.TONE_MUTED) { return 176 }
|
|
Preview(">  nice")
|
|
if preview.image_count != 1 || !Hidden(0, 2, 19) || !Tone(0, 0, 24, Markdown.TONE_MUTED) { return 177 }
|
|
Preview("# Day ")
|
|
if preview.image_count != 1 || preview.heading_count != 1 || !Hidden(0, 0, 2) || !Hidden(1, 6, 23) { return 178 }
|
|
// Text on the next line without a blank line is its own line.
|
|
Preview("\nHello *you*")
|
|
if preview.image_count != 1 || !preview.images[0].cover || !Hidden(0, 0, 17) ||
|
|
!Italic(0, 25, 28) { return 179 }
|
|
// Emphasis pairs around a photo, never inside its Markdown.
|
|
Preview("*a  b*")
|
|
if preview.image_count != 1 || preview.italic_count != 1 || !Italic(0, 1, 24) || !PreviewOrdered() { return 180 }
|
|
// Titles and angle brackets hide with the rest of the image.
|
|
Preview("x  y")
|
|
if preview.image_count != 1 || !Hidden(0, 2, 29) { return 181 }
|
|
// Markup cut short is plain text and shows as typed.
|
|
Preview("
|
|
if preview.image_count != 0 || preview.hidden_count != 0 { return 182 }
|
|
// Other images are text; a link next to a photo keeps its color.
|
|
Preview(" [site](https://x.y) ")
|
|
if preview.image_count != 1 || !Tone(0, 33, 37, Markdown.TONE_LINK) || !PreviewOrdered() { return 183 }
|
|
return 0
|
|
}
|
|
|
|
// Images never reach the spelling service: each goes as one mark, wherever
|
|
// it stands, and comes back at its mark in the correction.
|
|
CheckSpelling :: () -> s32 {
|
|
entry := "I wnet out  and saw\n  \nIt was god."
|
|
if DiarySpellingMasked(entry) != "I wnet out \xEF\xBF\xBC and saw\n \xEF\xBF\xBC \xEF\xBF\xBC\nIt was god." { return 140 }
|
|
restored := DiarySpellingRestored(entry, "I went out \xEF\xBF\xBC and saw\n \xEF\xBF\xBC \xEF\xBF\xBC\nIt was good.")
|
|
if restored != "I went out  and saw\n  \nIt was good." { return 141 }
|
|
// A correction that lost or added a mark is refused.
|
|
if DiarySpellingRestored(entry, "I went out and saw\n \xEF\xBF\xBC \xEF\xBF\xBC\nIt was good.") != "" { return 142 }
|
|
if DiarySpellingRestored(entry, "\xEF\xBF\xBC I went out \xEF\xBF\xBC and saw \xEF\xBF\xBC \xEF\xBF\xBC") != "" { return 143 }
|
|
// A mark the entry held itself comes back as itself.
|
|
held := "a \xEF\xBF\xBC b "
|
|
if DiarySpellingMasked(held) != "a \xEF\xBF\xBC b \xEF\xBF\xBC" ||
|
|
DiarySpellingRestored(held, "A \xEF\xBF\xBC B \xEF\xBF\xBC") != "A \xEF\xBF\xBC B " { return 144 }
|
|
// Image markup cut short goes as typed; it holds no photo.
|
|
if DiarySpellingMasked("see  != "see  != "" || DiarySpellingRestored("", "") != "" { return 146 }
|
|
if DiarySpellingMasked("trailing\n") != "trailing\n" { return 147 }
|
|
return 0
|
|
}
|
|
|
|
#program_export
|
|
Answer :: () -> s32 {
|
|
checks: [8]s32 = .[CheckEmbeds(), CheckLines(), CheckInsertion(), CheckPreview(),
|
|
CheckPreviewPhotos(), CheckInlinePhotos(), CheckLimits(), CheckSpelling()]
|
|
for result: checks {
|
|
if result != 0 {
|
|
return result
|
|
}
|
|
}
|
|
return 42
|
|
}
|