Skip to content

Commit

Permalink
feat(gopher): directory prefix absolute urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuwn committed Jun 24, 2024
1 parent 83c685f commit 3568406
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/gemini.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import gleam/bit_array
import gleam/bytes_builder
import gleam/http/request
import gleam/httpc
import gleam/string
import gopher

pub fn get_gemtext_from_capsule(message) {
Expand All @@ -24,16 +25,20 @@ pub fn get_gemtext_from_capsule(message) {
}
let assert Ok(request) = case bit_array.to_string(message) {
Ok(path) -> {
case path {
"/\r\n" | "\r\n" | "\n" -> request.to(gemini_proxy <> root_capsule)
"/proxy/" <> route ->
request.to(gemini_proxy <> gopher.trim_gopher_line_ending(route))
"/" <> path ->
case string.split(path, "/") {
["/\r\n"] | ["\r\n"] | ["\n"] ->
request.to(gemini_proxy <> root_capsule)
["", "proxy", ..route] ->
request.to(
gemini_proxy
<> gopher.trim_gopher_line_ending(string.join(route, "/")),
)
["", ..path] ->
request.to(
gemini_proxy
<> root_capsule
<> "/"
<> gopher.trim_gopher_line_ending(path),
<> gopher.trim_gopher_line_ending(string.join(path, "/")),
)
_ -> request.to(root_capsule <> gopher.trim_gopher_line_ending(path))
}
Expand Down
11 changes: 9 additions & 2 deletions src/gopher.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import gleam/option.{None, Some}
import gleam/string

pub fn trim_gopher_line_ending(line) {
string.replace(line, "\r", "") |> string.replace("\n", "")
line
|> string.replace("\n", "")
|> string.replace("\r", "")
}

fn terminate_text_line(line) {
Expand Down Expand Up @@ -59,7 +61,12 @@ fn gopher_link_line(to, description) {
"h"
<> description
<> "\tURL:"
<> to
<> {
case string.starts_with(to, "/") {
True -> "/1" <> to
False -> to
}
}
<> "\t"
<> extract_domain_from_url(to)
<> "\t70"
Expand Down

0 comments on commit 3568406

Please sign in to comment.