# route.bible URL helpers (pure Bend) import Base def default_base() -> String: "https://route.bible" type TsPh is Data: TsBody{} TsStep{is_slash: Bool, h: Char} def trim_slashes_go(fuel: Nat, ph: TsPh, rest: String) -> String: match fuel: case 0n: String.reverse(rest) case 1n+p: match ph: case TsBody{}: match rest: case SNil{}: "" case SCon{+h, +t}: trim_slashes_go(p, TsStep{Char.is_eq(h, '/'), h}, t) case TsStep{is_slash, +h}: match is_slash: case True{}: trim_slashes_go(p, TsBody{}, rest) case False{}: String.reverse(SCon{h, rest}) def trim_trailing_slashes(+s: String) -> String: trim_slashes_go( Nat.add(String.length(s), 2n), TsBody{}, String.reverse(s) ) def normalize_base_go(+base: String, empty: Bool) -> String: match empty: case True{}: default_base() case False{}: trim_trailing_slashes(base) def normalize_base(+base: String) -> String: normalize_base_go(String.trim(base), String.is_empty(String.trim(base))) def route_path(+canonical: String) -> String: String.append("/", String.to_lower(canonical)) def build_route_url_base(+base: String, +canonical: String) -> String: String.append(normalize_base(base), route_path(canonical)) def build_route_url(+canonical: String) -> String: build_route_url_base(default_base(), canonical) def md_link(+visible: String, +url: String) -> String: String.append( String.append(String.append("[", visible), "]("), String.append(url, ")") ) def markdown_route_link(+visible: String, +canonical: String) -> String: md_link(visible, build_route_url(canonical))