Skip to content

Commit

Permalink
feat(gopher): escape text lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuwn committed Jun 23, 2024
1 parent 5bfead7 commit a033237
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/gemini.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ pub fn get_gemtext_from_capsule(message) {
let assert Ok(response) = httpc.send(request)

bytes_builder.from_string(
gopher.gemtext_to_gopher(parse.parse_gemtext_raw(response.body)),
gopher.gemtext_to_gopher(parse.parse_gemtext_raw(response.body)) <> "\r\n",
)
}
21 changes: 17 additions & 4 deletions src/gopher.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ pub fn trim_gopher_line_ending(line) {
string.replace(line, "\r", "") |> string.replace("\n", "")
}

fn terminate_text_line(line) {
line <> "\tnull.host\t1\t70"
}

pub fn gemtext_to_gopher(gemtext: List(Gemtext)) -> String {
{
gemtext
Expand All @@ -29,6 +33,14 @@ pub fn gemtext_to_gopher(gemtext: List(Gemtext)) -> String {
<> "."
}
|> string.replace("\r\n\r\n.", "\r\n.")
|> string.split("\r\n")
|> list.map(fn(line) {
case line {
"" -> "i" |> terminate_text_line
_ -> line
}
})
|> string.join("\r\n")
}

fn extract_domain_from_url(url: String) -> String {
Expand All @@ -55,16 +67,17 @@ fn gopher_link_line(to, description) {

pub fn gemtext_line_to_gopher_line(line: Gemtext) -> String {
case line {
gemtext.Text(text) -> "i" <> text
gemtext.Text(text) -> { "i" <> text } |> terminate_text_line
gemtext.Link(to, description) ->
case description {
Some(description) -> gopher_link_line(to, description)
None -> gopher_link_line(to, to)
}
gemtext.Heading(text, depth) ->
"i" <> list.repeat("#", depth) |> string.join("") <> " " <> text
gemtext.ListLine(text) -> "i* " <> text
gemtext.BlockquoteLine(text) -> "i> " <> text
{ "i" <> list.repeat("#", depth) |> string.join("") <> " " <> text }
|> terminate_text_line
gemtext.ListLine(text) -> { "i* " <> text } |> terminate_text_line
gemtext.BlockquoteLine(text) -> { "i> " <> text } |> terminate_text_line
_ -> ""
}
}

0 comments on commit a033237

Please sign in to comment.