Skip to content

Commit

Permalink
webls 1.4 spec compliant RSS + standard builder patterns for all
Browse files Browse the repository at this point in the history
  • Loading branch information
versecafe committed Aug 12, 2024
1 parent 3c09095 commit 8ad1905
Show file tree
Hide file tree
Showing 9 changed files with 678 additions and 213 deletions.
66 changes: 50 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,53 @@ gleam add webls
```

```gleam
import gleam/option.{None}
import webls/sitemap.{Sitemap}
import webls/sitemap
import webls/rss
import webls/robots
pub fn sitemap() -> String {
let sitemap =
Sitemap(url: "https://gleam.run/sitemap.xml", last_modified: None, items: [
sitemap.item("https://gleam.run")
|> sitemap.with_frequency(sitemap.Monthly)
|> sitemap.with_priority(1.0),
sitemap.item("https://gleam.run/blog")
|> sitemap.with_frequency(sitemap.Weekly),
sitemap.item("https://gleam.run/blog/gleam-1.0"),
sitemap.item("https://gleam.run/blog/gleam-1.1"),
])
sitemap |> sitemap.to_string()
sitemap.sitemap("https://gleam.run/sitemap.xml")
|> sitemap.with_sitemap_last_modified(birl.now())
|> sitemap.with_sitemap_items([
sitemap.item("https://gleam.run")
|> sitemap.with_item_frequency(sitemap.Monthly)
|> sitemap.with_item_priority(1.0),
sitemap.item("https://gleam.run/blog")
|> sitemap.with_item_frequency(sitemap.Weekly),
sitemap.item("https://gleam.run/blog/gleam-1.0"),
sitemap.item("https://gleam.run/blog/gleam-1.1"),
]) |> sitemap.to_string()
}
pub fn rss() -> String {
[
rss.channel("Gleam RSS", "A test RSS feed", "https://gleam.run")
|> rss.with_channel_category("Releases")
|> rss.with_channel_language("en")
|> rss.with_channel_items([
rss.item("Gleam 1.0", "Gleam 1.0 is here!")
|> rss.with_item_link("https://gleam.run/blog/gleam-1.0")
|> rss.with_item_pub_date(birl.now())
|> rss.with_item_guid(#("gleam 1.0", Some(False))),
rss.item("Gleam 0.10", "Gleam 0.10 is here!")
|> rss.with_item_link("https://gleam.run/blog/gleam-0.10")
|> rss.with_item_author("[email protected]")
|> rss.with_item_guid(#("gleam 0.10", Some(True))),
]),
] |> rss.to_string()
}
pub fn robots() -> String {
robots.config("https://example.com/sitemap.xml")
|> robots.with_config_robots([
robots.robot("googlebot")
|> robots.with_robot_allowed_routes(["/posts/", "/contact/"])
|> robots.with_robot_disallowed_routes(["/admin/", "/private/"]),
robots.robot("bingbot")
|> robots.with_robot_allowed_routes(["/posts/", "/contact/", "/private/"])
|> robots.with_robot_disallowed_routes(["/"]),
])
|> robots.to_string()
}
```

Expand All @@ -37,15 +68,18 @@ Further documentation can be found at <https://hexdocs.pm/webls>.
| Protocol | Version | Status |
| ---------- | -------- | -------- |
| Sitemaps | 0.9 | Complete |
| RSS v2.0 | 2.0.1 | Partial |
| RSS | 2.0.1 | Complete |
| Robots.txt | 1997 IDS | Complete |

> A Note on the RSS 2.0 spec, the PICS field for content ratings is not going
> to be supported as the PICS standard was discontinued more than a decade ago.
## Utility Support

| Type | to_string | Builder Functions | Validators |
| ---------- | --------- | ----------------- | ---------- |
| Sitemap | Complete | Complete | None |
| RSS v2.0 | Complete | None | None |
| RSS v2.0 | Complete | Complete | None |
| Robots.txt | Complete | Complete | None |

## Development
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "webls"
version = "1.3.0"
version = "1.4.0"

description = "A simple web utility library for RSS feeds, Sitemaps, Robots.txt, etc."
licences = ["Apache-2.0"]
Expand Down
42 changes: 38 additions & 4 deletions src/webls/robots.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,50 @@ fn robot_to_string(robot: Robot) -> String {

// Builder Patern -------------------------------------------------------------

/// Creates a robots config with a sitemap url
pub fn config(sitemap_url: String) -> RobotsConfig {
RobotsConfig(sitemap_url: sitemap_url, robots: [])
}

/// Adds a list of robots to the robots config
pub fn with_config_robots(
config: RobotsConfig,
robots: List(Robot),
) -> RobotsConfig {
RobotsConfig(..config, robots: list.concat([config.robots, robots]))
}

/// Adds a robot to the robots config
pub fn with_config_robot(config: RobotsConfig, robot: Robot) -> RobotsConfig {
RobotsConfig(..config, robots: [robot, ..config.robots])
}

/// Creates a robot policy
pub fn robot(user_agent: String) -> Robot {
Robot(user_agent, [], [])
}

pub fn allowed_routes(robot: Robot, routes: List(String)) -> Robot {
Robot(..robot, allowed_routes: routes)
/// Adds a list of allowed routes to the robot policy
pub fn with_robot_allowed_routes(robot: Robot, routes: List(String)) -> Robot {
Robot(..robot, allowed_routes: list.concat([robot.allowed_routes, routes]))
}

/// Adds a allowed route to the robot policy
pub fn with_robot_allowed_route(robot: Robot, route: String) -> Robot {
Robot(..robot, allowed_routes: [route, ..robot.allowed_routes])
}

/// Adds a list of disallowed routes to the robot policy
pub fn with_robot_disallowed_routes(robot: Robot, routes: List(String)) -> Robot {
Robot(
..robot,
disallowed_routes: list.concat([robot.disallowed_routes, routes]),
)
}

pub fn disallowed_routes(robot: Robot, routes: List(String)) -> Robot {
Robot(..robot, disallowed_routes: routes)
/// Adds a disallowed route to the robot policy
pub fn with_robot_disallowed_route(robot: Robot, route: String) -> Robot {
Robot(..robot, disallowed_routes: [route, ..robot.disallowed_routes])
}

// Types ----------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 8ad1905

Please sign in to comment.