# 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)) # Optional ?v=TRANSLATION (uppercase). Empty translation → no query. type VqGate is Data: VqGate{empty: Bool} def append_v_query_go(+url: String, +translation: String, g: VqGate) -> String: match g: case VqGate{empty}: match empty: case True{}: url case False{}: String.append( String.append(url, "?v="), String.to_upper(translation) ) def append_v_query_trim(+url: String, +trimmed: String) -> String: append_v_query_go(url, trimmed, VqGate{String.is_empty(trimmed)}) def append_v_query(+url: String, +translation: String) -> String: append_v_query_trim(url, String.trim(translation)) def build_route_url_qv(+canonical: String, +translation: String) -> String: append_v_query(build_route_url(canonical), translation) def build_route_url_base_qv(+base: String, +canonical: String, +translation: String) -> String: append_v_query(build_route_url_base(base, canonical), translation) def markdown_route_link_qv(+visible: String, +canonical: String, +translation: String) -> String: md_link(visible, build_route_url_qv(canonical, translation)) def markdown_route_link_base_qv( +base: String, +visible: String, +canonical: String, +translation: String ) -> String: md_link(visible, build_route_url_base_qv(base, canonical, translation))