Skip to content

Commit

Permalink
Add function to generate sitemap :)
Browse files Browse the repository at this point in the history
  • Loading branch information
andre-dasilva committed May 26, 2024
1 parent ac2b970 commit c80fd9b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/elements.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub type Attribute {

pub type Element {
Element(tag: String, attrs: List(Attribute), children: List(Element))
EscapeText(String)
Text(String)
CData(String)
}
Expand All @@ -28,6 +29,10 @@ pub fn text(value: String) -> Element {
Text(value)
}

pub fn escape_text(value: String) -> Element {
EscapeText(value)
}

pub fn cdata(value: String) -> Element {
CData(value)
}
2 changes: 1 addition & 1 deletion src/html.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn document(
}

pub fn title(title: String) -> Element {
elements.element("title", [], [elements.text(title)])
elements.element("title", [], [elements.escape_text(title)])
}

pub fn meta(attrs: List(Attribute)) -> Element {
Expand Down
5 changes: 4 additions & 1 deletion src/render.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ pub fn element(el: elements.Element) -> String {
}
}
}
elements.Text(value) -> {
elements.EscapeText(value) -> {
escape(value)
}
elements.Text(value) -> {
value
}
elements.CData(value) -> {
"<![CDATA["
|> string.append(value)
Expand Down
19 changes: 19 additions & 0 deletions src/xml.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import elements
import gleam/list

pub type Sitemap {
SitemapUrl(loc: String, lastmod: String)
}

pub fn sitemap(items: List(Sitemap)) -> elements.Element {
elements.element(
"urlset",
[elements.attr("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")],
list.map(items, fn(item) {
elements.element("url", [], [
elements.element("loc", [], [elements.text(item.loc)]),
elements.element("lastmod", [], [elements.text(item.lastmod)]),
])
}),
)
}
26 changes: 21 additions & 5 deletions test/render_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import gleam/list
import gleeunit/should
import html
import render
import xml

fn paragraph(text: String) {
html.p([], [elements.text(text)])
html.p([], [elements.escape_text(text)])
}

fn meta() {
Expand Down Expand Up @@ -68,9 +69,9 @@ pub fn render_nested_html_test() {
html.html([elements.attr("lang", "en")], [
html.head([], [html.title("Nested")]),
html.body([], [
html.h1([], [elements.text("Hello")]),
html.h1([], [elements.escape_text("Hello")]),
html.div([elements.attr("class", "text-2xl")], [
html.p([], [elements.text("What a nice library")]),
html.p([], [elements.escape_text("What a nice library")]),
]),
]),
])
Expand All @@ -85,8 +86,8 @@ pub fn render_nested_html_test() {
pub fn render_simple_xml_test() {
let book_element = fn(id: String, author: String, title: String) {
elements.element("book", [elements.attr("id", id)], [
elements.element("author", [], [elements.text(author)]),
elements.element("title", [], [elements.text(title)]),
elements.element("author", [], [elements.escape_text(author)]),
elements.element("title", [], [elements.escape_text(title)]),
])
}

Expand Down Expand Up @@ -121,3 +122,18 @@ pub fn render_xml_cdata_test() {
render.xml(input)
|> should.equal(expected)
}

pub fn render_sitemap_test() {
let input = [
xml.SitemapUrl(loc: "https://www.example.com/foo", lastmod: "2022-06-04"),
xml.SitemapUrl(loc: "https://www.example.com/bar", lastmod: "2022-06-05"),
xml.SitemapUrl(loc: "https://www.example.com/hello", lastmod: "2022-06-10"),
]
let sitemap = xml.sitemap(input)

let expected =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"><url><loc>https://www.example.com/foo</loc><lastmod>2022-06-04</lastmod></url><url><loc>https://www.example.com/bar</loc><lastmod>2022-06-05</lastmod></url><url><loc>https://www.example.com/hello</loc><lastmod>2022-06-10</lastmod></url></urlset>"

render.xml(sitemap)
|> should.equal(expected)
}

0 comments on commit c80fd9b

Please sign in to comment.