aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-06-23 11:15:08 +0000
committerFuwn <[email protected]>2024-06-23 11:15:08 +0000
commit5dc108b33570e4b785f28aeb4609b51d59e3ac5c (patch)
tree25766cd5f81289e4cc1ee98b951bb4e4a05cf95b /src
parentfix(gopher): common line endings (diff)
downloadmomoka-5dc108b33570e4b785f28aeb4609b51d59e3ac5c.tar.xz
momoka-5dc108b33570e4b785f28aeb4609b51d59e3ac5c.zip
feat(gopher): escape text lines
Diffstat (limited to 'src')
-rw-r--r--src/gemini.gleam2
-rw-r--r--src/gopher.gleam21
2 files changed, 18 insertions, 5 deletions
diff --git a/src/gemini.gleam b/src/gemini.gleam
index 02a6a75..ad04079 100644
--- a/src/gemini.gleam
+++ b/src/gemini.gleam
@@ -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",
)
}
diff --git a/src/gopher.gleam b/src/gopher.gleam
index 5ff02aa..162ac22 100644
--- a/src/gopher.gleam
+++ b/src/gopher.gleam
@@ -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
@@ -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 {
@@ -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
_ -> ""
}
}