ludic/packages/ludic.photo/slug.ludic

32 lines
989 B
Text

# slug.ludic - a frame's tags as a file name: lower case letters and digits, words joined by dashes,
# at most 24 characters, so a folder of photographs browses well
const PHT_ALPHA: string = "0123456789abcdefghijklmnopqrstuvwxyz"
@alloc_ok("on a photograph taken: its file name")
export function photo_slug(tags: string) -> string {
var out = ""
var n = 0
var i = 0
var last_dash = true
while i < len(tags) and n < 24 {
var c = tags[i]
if c >= 65 and c <= 90 { c = c + 32 }
var k = -1
if c >= 48 and c <= 57 { k = c - 48 }
if c >= 97 and c <= 122 { k = c - 97 + 10 }
if k >= 0 {
let ch: string = PHT_ALPHA[k..k + 1]
out = out + ch
last_dash = false
n += 1
} else if (c == 32 or c == 44) and not last_dash and n > 0 {
out = out + "-"
last_dash = true
n += 1
}
i += 1
}
while Text.ends_with(out, "-") { out = Text.slice(out, 0, len(out) - 1) }
if len(out) == 0 { return "photo" }
return out
}