Update for Gleam 0.33, split codebase
This commit is contained in:
parent
2046e4105d
commit
aea7e60858
10 changed files with 3899 additions and 4077 deletions
|
@ -1,3 +1,3 @@
|
||||||
gleam 0.30.2
|
gleam 0.33.0
|
||||||
erlang 25.2.2
|
erlang 26.1.2
|
||||||
nodejs 18.14.0
|
nodejs 18.14.0
|
||||||
|
|
|
@ -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
|
5.0.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,10 @@ glentities.decode("</html>") // "</html>"
|
||||||
glentities.decode("</html>") // "</html>"
|
glentities.decode("</html>") // "</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
|
## Development
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name = "glentities"
|
name = "glentities"
|
||||||
version = "5.0.0"
|
version = "6.0.0"
|
||||||
description = "HTML entity encoder/decoder for Gleam"
|
description = "HTML entity encoder/decoder for Gleam"
|
||||||
|
|
||||||
# Fill out these fields if you intend to generate HTML documentation or publish
|
# 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 = []
|
links = []
|
||||||
|
|
||||||
internal_modules = ["glentities/internal/*"]
|
internal_modules = ["glentities/internal/*"]
|
||||||
|
gleam = ">= 0.32.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
gleam_stdlib = "~> 0.29"
|
gleam_stdlib = "~> 0.32"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
gleeunit = "~> 0.10"
|
gleeunit = "~> 1.0"
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
# You typically do not need to edit this file
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
packages = [
|
packages = [
|
||||||
{ name = "gleam_stdlib", version = "0.30.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "03710B3DA047A3683117591707FCA19D32B980229DD8CE8B0603EB5B5144F6C3" },
|
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
|
||||||
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
|
{ name = "gleeunit", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "4E75DCF846D653848094169304743DFFB386E3AECCCF611F99ADB735FF4D4DD9" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[requirements]
|
[requirements]
|
||||||
gleam_stdlib = { version = "~> 0.29" }
|
gleam_stdlib = { version = "~> 0.32" }
|
||||||
gleeunit = { version = "~> 0.10" }
|
gleeunit = { version = "~> 1.0" }
|
||||||
|
|
4076
src/glentities.gleam
4076
src/glentities.gleam
File diff suppressed because it is too large
Load diff
2279
src/glentities/decoder.gleam
Normal file
2279
src/glentities/decoder.gleam
Normal file
File diff suppressed because it is too large
Load diff
24
src/glentities/hex_encoder.gleam
Normal file
24
src/glentities/hex_encoder.gleam
Normal 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("")
|
||||||
|
}
|
33
src/glentities/html_encoder.gleam
Normal file
33
src/glentities/html_encoder.gleam
Normal 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, "&"))
|
||||||
|
"<" <> rest -> do_encode(rest, string_builder.append(acc, "<"))
|
||||||
|
">" <> rest -> do_encode(rest, string_builder.append(acc, ">"))
|
||||||
|
"\"" <> rest -> do_encode(rest, string_builder.append(acc, """))
|
||||||
|
"'" <> rest -> do_encode(rest, string_builder.append(acc, "'"))
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1534
src/glentities/named_encoder.gleam
Normal file
1534
src/glentities/named_encoder.gleam
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue