Update for Gleam 0.33, split codebase

This commit is contained in:
Mikko Ahlroth 2023-12-27 22:54:45 +02:00
parent 2046e4105d
commit aea7e60858
10 changed files with 3899 additions and 4077 deletions

View file

@ -1,3 +1,3 @@
gleam 0.30.2
erlang 25.2.2
gleam 0.33.0
erlang 26.1.2
nodejs 18.14.0

View file

@ -1,3 +1,10 @@
6.0.0
-----
* Update for Gleam 0.32+.
* Split encoders and the decoder into separate files so that they can be separately imported.
This helps keep the file size down.
5.0.0
-----

View file

@ -20,6 +20,10 @@ glentities.decode("&#x3C;&#x2F;html&#x3E;") // "</html>"
glentities.decode("&lt;&sol;html&gt;") // "</html>"
```
### Code size
If you import `glentities`, it will pull in all the encoders and the entire named decoder. Since there are so many different entity names, this will result in a hefty JS payload. If you need to minimise the payload size, prefer importing `glentities/decoder` or one of the `glentities/*_encoder` modules directly.
## Development
```sh

View file

@ -1,5 +1,5 @@
name = "glentities"
version = "5.0.0"
version = "6.0.0"
description = "HTML entity encoder/decoder for Gleam"
# Fill out these fields if you intend to generate HTML documentation or publish
@ -10,9 +10,10 @@ repository = { type = "gitlab", user = "Nicd", repo = "glentities" }
links = []
internal_modules = ["glentities/internal/*"]
gleam = ">= 0.32.0"
[dependencies]
gleam_stdlib = "~> 0.29"
gleam_stdlib = "~> 0.32"
[dev-dependencies]
gleeunit = "~> 0.10"
gleeunit = "~> 1.0"

View file

@ -2,10 +2,10 @@
# You typically do not need to edit this file
packages = [
{ name = "gleam_stdlib", version = "0.30.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "03710B3DA047A3683117591707FCA19D32B980229DD8CE8B0603EB5B5144F6C3" },
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "4E75DCF846D653848094169304743DFFB386E3AECCCF611F99ADB735FF4D4DD9" },
]
[requirements]
gleam_stdlib = { version = "~> 0.29" }
gleeunit = { version = "~> 0.10" }
gleam_stdlib = { version = "~> 0.32" }
gleeunit = { version = "~> 1.0" }

File diff suppressed because it is too large Load diff

2279
src/glentities/decoder.gleam Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,24 @@
import gleam/list
import gleam/string
import gleam/int
import glentities/internal/string_utils
/// Encode text using HTML hex entities, except a-z, A-Z, 0-9, newline, tab, carriage return, and space.
pub fn encode(text: String) {
text
|> string_utils.normalise()
|> do_encode()
}
fn do_encode(text: String) {
text
|> string.to_utf_codepoints()
|> list.map(fn(codepoint) {
case string.utf_codepoint_to_int(codepoint) {
c if c == 9 || c == 10 || c == 13 || c == 32 || c >= 48 && c <= 57 || c >= 65 && c <= 90 || c >= 97 && c <= 122 ->
string.from_utf_codepoints([codepoint])
encodable -> "&#x" <> int.to_base16(encodable) <> ";"
}
})
|> string.join("")
}

View file

@ -0,0 +1,33 @@
import gleam/string
import gleam/string_builder.{type StringBuilder}
import glentities/internal/string_utils
/// Encode text to be safe in the HTML body, inside element or attribute content.
///
/// `&`, `<`, `>`, `'`, and `"` are encoded.
///
/// Note! Not suitable for outputting inside `<style>`, `<script>` elements.
pub fn encode(text: String) -> String {
text
|> string_utils.normalise()
|> do_encode(string_builder.new())
}
fn do_encode(text: String, acc: StringBuilder) {
case text {
"" -> string_builder.to_string(acc)
"&" <> rest -> do_encode(rest, string_builder.append(acc, "&amp;"))
"<" <> rest -> do_encode(rest, string_builder.append(acc, "&lt;"))
">" <> rest -> do_encode(rest, string_builder.append(acc, "&gt;"))
"\"" <> rest -> do_encode(rest, string_builder.append(acc, "&quot;"))
"'" <> rest -> do_encode(rest, string_builder.append(acc, "&#39;"))
other -> {
let maybe_grapheme = string.pop_grapheme(other)
case maybe_grapheme {
Ok(#(grapheme, rest)) ->
do_encode(rest, string_builder.append(acc, grapheme))
Error(Nil) -> string_builder.to_string(acc)
}
}
}
}

File diff suppressed because it is too large Load diff